List of usage examples for com.google.common.net HttpHeaders CONTENT_LENGTH
String CONTENT_LENGTH
To view the source code for com.google.common.net HttpHeaders CONTENT_LENGTH.
Click Source Link
From source file:org.gitools.utils.HttpUtils.java
public static long getContentLength(URL url) { if (url == null) { return -1; }/*from w w w .java 2 s. c om*/ HttpURLConnection conn = null; try { conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("HEAD"); } catch (IOException e) { return -1; } String contentLengthString = conn.getHeaderField(HttpHeaders.CONTENT_LENGTH); conn.disconnect(); if (contentLengthString == null) { return -1; } else { return Long.parseLong(contentLengthString); } }
From source file:org.haiku.haikudepotserver.support.URLHelper.java
public static long payloadLength(URL url) throws IOException { Preconditions.checkArgument(null != url, "the url must be supplied"); long result = -1; HttpURLConnection connection = null; switch (url.getProtocol()) { case "http": case "https": try {//from ww w.j a va2s .c o m connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(PAYLOAD_LENGTH_CONNECT_TIMEOUT); connection.setReadTimeout(PAYLOAD_LENGTH_READ_TIMEOUT); connection.setRequestMethod(HttpMethod.HEAD.name()); connection.connect(); String contentLengthHeader = connection.getHeaderField(HttpHeaders.CONTENT_LENGTH); if (!Strings.isNullOrEmpty(contentLengthHeader)) { long contentLength; try { contentLength = Long.parseLong(contentLengthHeader); if (contentLength > 0) { result = contentLength; } else { LOGGER.warn("bad content length; {}", contentLength); } } catch (NumberFormatException nfe) { LOGGER.warn("malformed content length; {}", contentLengthHeader); } } else { LOGGER.warn("unable to get the content length header"); } } finally { if (null != connection) { connection.disconnect(); } } break; case "file": File file = new File(url.getPath()); if (file.exists() && file.isFile()) { result = file.length(); } else { LOGGER.warn("unable to find the local file; {}", url.getPath()); } break; } LOGGER.info("did obtain length for url; {} - {}", url, result); return result; }
From source file:com.linkedin.flashback.serializable.RecordedHttpMessage.java
public RecordedHttpMessage(Map<String, String> headers, RecordedHttpBody httpBody) { if (headers != null) { _headers = headers;// w ww . j a v a 2s.c om } _httpBody = httpBody; // Update the Content-Length header if appropriate if (_headers.containsKey(HttpHeaders.CONTENT_LENGTH)) { try { int contentLength = _httpBody.getContent(getCharset()).length; _headers = new HashMap<>(_headers); _headers.put(HttpHeaders.CONTENT_LENGTH, Integer.toString(contentLength)); } catch (IOException e) { logger.error("Caught exception " + e + " while updating Content-Length header"); } } }
From source file:jp.tomorrowkey.irkit4j.http.HttpClient.java
public HttpResponse request(HttpRequest httpRequest) throws IOException { URL url = new URL(httpRequest.getUrl()); HttpRequestMethod method = httpRequest.getHttpRequestMethod(); String encoding = httpRequest.getEncoding(); httpURLConnection = newHttpURLConnection(url, method, encoding); String content = httpRequest.getContent(); if (content == null) { content = HttpParameter.encodeParameters(httpRequest.getParameters(), encoding); }/*from w ww. ja v a 2 s .c o m*/ if (method == HttpRequestMethod.POST) { httpURLConnection.setRequestProperty(HttpHeaders.CONTENT_LENGTH, String.valueOf(content.length())); } return request(content, encoding); }
From source file:com.facebook.nifty.client.HttpClientChannel.java
@Override protected ChannelFuture writeRequest(ChannelBuffer request) { HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, endpointUri); httpRequest.setHeader(HttpHeaders.HOST, hostName); httpRequest.setHeader(HttpHeaders.CONTENT_LENGTH, request.readableBytes()); httpRequest.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-thrift"); httpRequest.setHeader(HttpHeaders.USER_AGENT, "Java/Swift-HttpThriftClientChannel"); if (headerDictionary != null) { for (Map.Entry<String, String> entry : headerDictionary.entrySet()) { httpRequest.setHeader(entry.getKey(), entry.getValue()); }// w w w . ja v a2 s . com } httpRequest.setContent(request); return underlyingNettyChannel.write(httpRequest); }
From source file:org.glassfish.jersey.tests.integration.jersey2176.TraceResponseFilter.java
@Override public void doFilter(final ServletRequest request, ServletResponse response, final FilterChain chain) throws IOException, ServletException { TraceResponseWrapper wrappedResponse = null; if (((HttpServletRequest) request).getHeader(X_NO_FILTER_HEADER) == null) { response = wrappedResponse = new TraceResponseWrapper((HttpServletResponse) response); }/*from w ww. jav a 2 s . c om*/ String status = "n/a"; long startTime = System.nanoTime(); try { chain.doFilter(request, response); status = "OK"; } catch (Throwable th) { status = "FAIL"; } finally { final long duration = System.nanoTime() - startTime; ((HttpServletResponse) response).addHeader(X_SERVER_DURATION_HEADER, String.valueOf(duration)); ((HttpServletResponse) response).addHeader(X_STATUS_HEADER, status); if (wrappedResponse != null) { ((HttpServletResponse) response).setHeader(HttpHeaders.CONTENT_LENGTH, wrappedResponse.getContentLength()); wrappedResponse.writeBodyAndClose(); } } }
From source file:com.heliosapm.tsdblite.handlers.http.HttpSwitch.java
@Override protected void decode(final ChannelHandlerContext ctx, final HttpRequest msg, final List<Object> out) throws Exception { final String uri = msg.uri(); log.info("-----------------------> URI [{}]", uri); if (uri.endsWith("/favicon.ico")) { final DefaultFullHttpResponse resp = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, favicon); resp.headers().set(HttpHeaders.CONTENT_TYPE, "image/x-icon"); resp.headers().setInt(HttpHeaders.CONTENT_LENGTH, favSize); ctx.writeAndFlush(resp);//from ww w.j a v a2s . com return; } ReferenceCountUtil.retain(msg); final ChannelPipeline p = ctx.pipeline(); final int index = uri.indexOf("/api/"); final String endpoint = index == -1 ? "" : uri.substring(5); if (index != -1 && pureJsonEndPoints.contains(endpoint)) { log.info("Switching to PureJSON handler"); p.addLast(eventExecutorGroup, "httpToJson", httpToJson); // p.addLast("jsonLogger", loggingHandler); p.addLast("jsonDecoder", new JsonObjectDecoder(true)); // p.addLast("jsonLogger", loggingHandler); p.addLast("traceHandler", traceHandler); p.remove(this); if (msg instanceof FullHttpMessage) { out.add(msg); } } else { log.info("Switching to Http Request Manager"); out.add(msg); p.addLast(eventExecutorGroup, "requestManager", HttpRequestManager.getInstance()); p.remove(this); } }
From source file:com.ibm.og.http.AuthenticatedHttpRequest.java
@Override public long getContentLength() { if (this.headers().containsKey(HttpHeaders.CONTENT_LENGTH)) { return Long.parseLong(this.headers().get(HttpHeaders.CONTENT_LENGTH)); }/*from www. j a v a 2 s . co m*/ return 0; }
From source file:org.apache.hadoop.hdfs.web.ByteRangeInputStream.java
private static long getStreamLength(HttpURLConnection connection, Map<String, List<String>> headers) throws IOException { String cl = connection.getHeaderField(HttpHeaders.CONTENT_LENGTH); if (cl == null) { // Try to get the content length by parsing the content range // because HftpFileSystem does not return the content length // if the content is partial. if (connection.getResponseCode() == HttpStatus.SC_PARTIAL_CONTENT) { cl = connection.getHeaderField(HttpHeaders.CONTENT_RANGE); return getLengthFromRange(cl); } else {//from w ww . j a va 2 s . com throw new IOException(HttpHeaders.CONTENT_LENGTH + " is missing: " + headers); } } return Long.parseLong(cl); }
From source file:org.sonatype.nexus.repository.http.HttpResponses.java
public static Response rangeNotSatisfiable(final long contentSize) { return new Response.Builder().status(Status.failure(REQUESTED_RANGE_NOT_SATISFIABLE)) .header(HttpHeaders.CONTENT_LENGTH, "0").header(HttpHeaders.CONTENT_RANGE, "bytes */" + contentSize) .build();/*from ww w . java 2 s .c om*/ }