Example usage for org.apache.http.protocol HTTP CONTENT_LEN

List of usage examples for org.apache.http.protocol HTTP CONTENT_LEN

Introduction

In this page you can find the example usage for org.apache.http.protocol HTTP CONTENT_LEN.

Prototype

String CONTENT_LEN

To view the source code for org.apache.http.protocol HTTP CONTENT_LEN.

Click Source Link

Usage

From source file:com.microsoft.live.unittest.DownloadTest.java

@Override
protected void loadValidResponseBody() throws Exception {
    byte[] bytes = RESPONSE.getBytes();
    this.mockResponse.addHeader(HTTP.CONTENT_LEN, Long.toString(bytes.length));
    this.mockEntity.setInputStream(new ByteArrayInputStream(bytes));
}

From source file:com.apigee.sdk.apm.http.impl.client.cache.CachedHttpResponseGenerator.java

private void addMissingContentLengthHeader(HttpResponse response, HttpEntity entity) {
    if (transferEncodingIsPresent(response))
        return;//from  ww  w  . ja  va 2s .  c  om

    Header contentLength = response.getFirstHeader(HTTP.CONTENT_LEN);
    if (contentLength == null) {
        contentLength = new BasicHeader(HTTP.CONTENT_LEN, Long.toString(entity.getContentLength()));
        response.setHeader(contentLength);
    }
}

From source file:ch.cyberduck.core.onedrive.OneDriveCommonsHttpRequestExecutor.java

@Override
public Response doGet(final URL url, final Set<RequestHeader> headers) throws IOException {
    final HttpGet request = new HttpGet(url.toString());
    for (RequestHeader header : headers) {
        if (header.getKey().equals(HTTP.TRANSFER_ENCODING)) {
            continue;
        }//from   w w  w.j a  v  a  2s  . co m
        if (header.getKey().equals(HTTP.CONTENT_LEN)) {
            continue;
        }
        request.addHeader(new BasicHeader(header.getKey(), header.getValue()));
    }
    final CloseableHttpResponse response = client.execute(request);
    return new CommonsHttpResponse(response);
}

From source file:org.jsnap.http.base.HttpServlet.java

protected void doService(org.apache.http.HttpRequest request, org.apache.http.HttpResponse response)
        throws HttpException, IOException {
    // Client might keep the executing thread blocked for very long unless this header is added.
    response.addHeader(new Header(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE));
    // Create a wrapped request object.
    String uri, data;/*from  ww  w .  j  av a 2s . c  o  m*/
    String method = request.getRequestLine().getMethod();
    if (method.equals(HttpGet.METHOD_NAME)) {
        BasicHttpRequest get = (BasicHttpRequest) request;
        data = get.getRequestLine().getUri();
        int ix = data.indexOf('?');
        uri = (ix < 0 ? data : data.substring(0, ix));
        data = (ix < 0 ? "" : data.substring(ix + 1));
    } else if (method.equals(HttpPost.METHOD_NAME)) {
        BasicHttpEntityEnclosingRequest post = (BasicHttpEntityEnclosingRequest) request;
        HttpEntity postedEntity = post.getEntity();
        uri = post.getRequestLine().getUri();
        data = EntityUtils.toString(postedEntity);
    } else {
        response.setStatusCode(HttpStatus.SC_NOT_IMPLEMENTED);
        response.setHeader(new Header(HTTP.CONTENT_LEN, "0"));
        return;
    }
    String cookieLine = "";
    if (request.containsHeader(COOKIE)) {
        Header[] cookies = request.getHeaders(COOKIE);
        for (Header cookie : cookies) {
            if (cookieLine.length() > 0)
                cookieLine += "; ";
            cookieLine += cookie.getValue();
        }
    }
    HttpRequest req = new HttpRequest(uri, underlying, data, cookieLine);
    // Create a wrapped response object.
    ByteArrayOutputStream out = new ByteArrayOutputStream(BUFFER_SIZE);
    HttpResponse resp = new HttpResponse(out);
    // Do implementation specific processing.
    doServiceImpl(req, resp);
    out.flush(); // It's good practice to do this.
    // Do the actual writing to the actual response object.
    if (resp.redirectTo != null) {
        // Redirection is requested.
        resp.statusCode = HttpStatus.SC_MOVED_TEMPORARILY;
        response.setStatusCode(resp.statusCode);
        Header redirection = new Header(LOCATION, resp.redirectTo);
        response.setHeader(redirection);
        Logger.getLogger(HttpServlet.class).log(Level.DEBUG,
                "Status Code: " + Integer.toString(resp.statusCode));
        Logger.getLogger(HttpServlet.class).log(Level.DEBUG, redirection.toString());
    } else {
        // There will be a response entity.
        response.setStatusCode(resp.statusCode);
        HttpEntity entity;
        Header contentTypeHeader;
        boolean text = resp.contentType.startsWith(Formatter.TEXT);
        if (text) { // text/* ...
            entity = new StringEntity(out.toString(resp.characterSet), resp.characterSet);
            contentTypeHeader = new Header(HTTP.CONTENT_TYPE,
                    resp.contentType + HTTP.CHARSET_PARAM + resp.characterSet);
        } else { // application/octet-stream, image/* ...
            entity = new ByteArrayEntity(out.toByteArray());
            contentTypeHeader = new Header(HTTP.CONTENT_TYPE, resp.contentType);
        }
        boolean acceptsGzip = clientAcceptsGzip(request);
        long contentLength = entity.getContentLength();
        // If client accepts gzipped content, the implementing object requested that response
        // gets gzipped and size of the response exceeds implementing object's size threshold
        // response entity will be gzipped.
        boolean gzipped = false;
        if (acceptsGzip && resp.zipSize > 0 && contentLength >= resp.zipSize) {
            ByteArrayOutputStream zipped = new ByteArrayOutputStream(BUFFER_SIZE);
            GZIPOutputStream gzos = new GZIPOutputStream(zipped);
            entity.writeTo(gzos);
            gzos.close();
            entity = new ByteArrayEntity(zipped.toByteArray());
            contentLength = zipped.size();
            gzipped = true;
        }
        // This is where true writes are made.
        Header contentLengthHeader = new Header(HTTP.CONTENT_LEN, Long.toString(contentLength));
        Header contentEncodingHeader = null;
        response.setHeader(contentTypeHeader);
        response.setHeader(contentLengthHeader);
        if (gzipped) {
            contentEncodingHeader = new Header(CONTENT_ENCODING, Formatter.GZIP);
            response.setHeader(contentEncodingHeader);
        }
        response.setEntity(entity);
        // Log critical headers.
        Logger.getLogger(HttpServlet.class).log(Level.DEBUG,
                "Status Code: " + Integer.toString(resp.statusCode));
        Logger.getLogger(HttpServlet.class).log(Level.DEBUG, contentTypeHeader.toString());
        if (gzipped)
            Logger.getLogger(HttpServlet.class).log(Level.DEBUG, contentEncodingHeader.toString());
        Logger.getLogger(HttpServlet.class).log(Level.DEBUG, contentLengthHeader.toString());
    }
    // Log cookies.
    for (Cookie cookie : resp.cookies) {
        if (cookie.valid()) {
            Header h = new Header(SET_COOKIE, cookie.toString());
            response.addHeader(h);
            Logger.getLogger(HttpServlet.class).log(Level.DEBUG, h.toString());
        }
    }
}

From source file:edu.ucsb.eucalyptus.transport.http.Axis2HttpWorker.java

public void service(final AxisHttpRequest request, final AxisHttpResponse response,
        final MessageContext msgContext) throws HttpException, IOException {

    ConfigurationContext configurationContext = msgContext.getConfigurationContext();
    final String servicePath = configurationContext.getServiceContextPath();
    final String contextPath = (servicePath.startsWith("/") ? servicePath : "/" + servicePath) + "/";

    String uri = request.getRequestURI();
    String method = request.getMethod();
    String soapAction = HttpUtils.getSoapAction(request);
    Handler.InvocationResponse pi = null;
    msgContext.setProperty(REAL_HTTP_REQUEST, request);
    msgContext.setProperty(REAL_HTTP_RESPONSE, response);

    Header[] headers = request.getHeaders(HTTP.CONTENT_LEN);
    if (headers != null && headers.length > 0) {
        String contentLength = headers[0].getValue();
        msgContext.setProperty(HTTP.CONTENT_LEN, contentLength);
    }/* w ww  .  j  av a 2 s  .  co  m*/

    if (method.equals(HTTPConstants.HEADER_GET)) {
        if ((uri.startsWith("/latest/") || uri.matches("/\\d\\d\\d\\d-\\d\\d-\\d\\d/.*"))
                && handleMetaData(response, msgContext, uri))
            return;
        if (!uri.startsWith(contextPath)) {
            response.setStatus(HttpStatus.SC_MOVED_PERMANENTLY);
            response.addHeader(new BasicHeader("Location", (contextPath + uri).replaceAll("//", "/")));
            return;
        }
        if (uri.endsWith("services/")) {
            handleServicesList(response, configurationContext);
            return;
        }
        if (uri.endsWith("?wsdl")) {
            handleWSDL(response);
            return;
        }
        pi = handleGet(request, response, msgContext);
    } else if (method.equals(HTTPConstants.HEADER_POST)) {
        String contentType = request.getContentType();
        if (HTTPTransportUtils.isRESTRequest(contentType))
            pi = Axis2HttpWorker.processXMLRequest(msgContext, request.getInputStream(),
                    response.getOutputStream(), contentType);
        else {
            String ip = (String) msgContext.getProperty(MessageContext.TRANSPORT_ADDR);
            if (ip != null)
                uri = ip + uri;
            pi = HTTPTransportUtils.processHTTPPostRequest(msgContext, request.getInputStream(),
                    response.getOutputStream(), contentType, soapAction, uri);
        }
    } else if (method.equals(HTTPConstants.HEADER_PUT)) {
        String contentType = request.getContentType();
        msgContext.setProperty(Constants.Configuration.CONTENT_TYPE, contentType);

        pi = Axis2HttpWorker.processXMLRequest(msgContext, request.getInputStream(), response.getOutputStream(),
                contentType);
    } else if (method.equals(HTTPConstants.HEADER_DELETE))
        pi = Axis2HttpWorker.processURLRequest(msgContext, response.getOutputStream(), null);
    else if (method.equals("HEAD"))
        pi = Axis2HttpWorker.processURLRequest(msgContext, response.getOutputStream(), null);
    else
        throw new MethodNotSupportedException(method + " method not supported");

    Boolean holdResponse = (Boolean) msgContext.getProperty(RequestResponseTransport.HOLD_RESPONSE);
    if (pi.equals(Handler.InvocationResponse.SUSPEND)
            || (holdResponse != null && Boolean.TRUE.equals(holdResponse)))
        try {
            ((RequestResponseTransport) msgContext.getProperty(RequestResponseTransport.TRANSPORT_CONTROL))
                    .awaitResponse();
        } catch (InterruptedException e) {
            throw new IOException("We were interrupted, so this may not function correctly:" + e.getMessage());
        }
    RequestResponseTransport requestResponseTransportControl = (RequestResponseTransport) msgContext
            .getProperty(RequestResponseTransport.TRANSPORT_CONTROL);
    if (TransportUtils.isResponseWritten(msgContext)
            || ((requestResponseTransportControl != null) && requestResponseTransportControl.getStatus()
                    .equals(RequestResponseTransport.RequestResponseTransportStatus.SIGNALLED)))
        ;
    else
        response.setStatus(HttpStatus.SC_ACCEPTED);
    Integer status = (Integer) msgContext.getProperty(HTTP_STATUS);
    if (status != null)
        response.setStatus(status);
}

From source file:ch.cyberduck.core.onedrive.OneDriveCommonsHttpRequestExecutor.java

@Override
public Response doDelete(final URL url, final Set<RequestHeader> headers) throws IOException {
    final HttpDelete request = new HttpDelete(url.toString());
    for (RequestHeader header : headers) {
        if (header.getKey().equals(HTTP.TRANSFER_ENCODING)) {
            continue;
        }/*from  w ww  .  j  a  v a2s.c  o m*/
        if (header.getKey().equals(HTTP.CONTENT_LEN)) {
            continue;
        }
        request.addHeader(new BasicHeader(header.getKey(), header.getValue()));
    }
    final CloseableHttpResponse response = client.execute(request);
    return new CommonsHttpResponse(response);
}

From source file:net.kungfoo.grizzly.proxy.impl.ConnectingHandler.java

private static void requestReadyCleanUpHeaders(final HttpRequest request) {
    request.removeHeaders(HTTP.CONTENT_LEN);
    request.removeHeaders(HTTP.TRANSFER_ENCODING);
    request.removeHeaders(HTTP.CONN_DIRECTIVE);
    request.removeHeaders("Keep-Alive");
    request.removeHeaders("Proxy-Authenticate");
    request.removeHeaders("Proxy-Authorization");
    request.removeHeaders("TE");
    request.removeHeaders("Trailers");
    request.removeHeaders("Upgrade");
    // Remove host header
    request.removeHeaders(HTTP.TARGET_HOST);
}