Example usage for org.apache.http.message BasicHttpResponse setEntity

List of usage examples for org.apache.http.message BasicHttpResponse setEntity

Introduction

In this page you can find the example usage for org.apache.http.message BasicHttpResponse setEntity.

Prototype

public void setEntity(HttpEntity httpEntity) 

Source Link

Usage

From source file:com.overture.questdroid.utility.ExtHttpClientStack.java

private HttpResponse convertResponseNewToOld(HttpResponse resp) throws IllegalStateException, IOException {

    ProtocolVersion protocolVersion = new ProtocolVersion(resp.getProtocolVersion().getProtocol(),
            resp.getProtocolVersion().getMajor(), resp.getProtocolVersion().getMinor());

    StatusLine responseStatus = new BasicStatusLine(protocolVersion, resp.getStatusLine().getStatusCode(),
            resp.getStatusLine().getReasonPhrase());

    BasicHttpResponse response = new BasicHttpResponse(responseStatus);
    HttpEntity ent = convertEntityNewToOld(resp.getEntity());
    response.setEntity(ent);

    for (Header h : resp.getAllHeaders()) {
        org.apache.http.Header header = convertheaderNewToOld(h);
        response.addHeader(header);//w  w w  .j a  v a  2 s . c o  m
    }

    return response;
}

From source file:com.chen.cy.talkimage.network.xvolley.XHurlStack.java

@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
        throws IOException, AuthFailureError {
    String url = request.getUrl();
    HashMap<String, String> map = new HashMap<String, String>();
    map.putAll(request.getHeaders());/*  w  w  w  . j a v  a 2  s . c o  m*/
    map.putAll(additionalHeaders);
    if (mUrlRewriter != null) {
        String rewritten = mUrlRewriter.rewriteUrl(url);
        if (rewritten == null) {
            throw new IOException("URL blocked by rewriter: " + url);
        }
        url = rewritten;
    }
    URL parsedUrl = new URL(url);
    HttpURLConnection connection = openConnection(parsedUrl, request);
    for (String headerName : map.keySet()) {
        connection.addRequestProperty(headerName, map.get(headerName));
    }
    setConnectionParametersForRequest(connection, request);
    // Initialize HttpResponse with data from the HttpURLConnection.
    ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
    int responseCode = connection.getResponseCode();
    if (responseCode == -1) {
        // -1 is returned by getResponseCode() if the response code could not be retrieved.
        // Signal to the caller that something was wrong with the connection.
        throw new IOException("Could not retrieve response code from HttpUrlConnection.");
    }
    StatusLine responseStatus = new BasicStatusLine(protocolVersion, connection.getResponseCode(),
            connection.getResponseMessage());
    BasicHttpResponse response = new BasicHttpResponse(responseStatus);
    response.setEntity(entityFromConnection(connection));
    for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
        if (header.getKey() != null) {
            //
            // ?????
            for (int i = 0; i < header.getValue().size(); i++) {
                Header h = new BasicHeader(header.getKey(), header.getValue().get(i));
                response.addHeader(h);
            }
        }
    }
    return response;
}

From source file:com.baasbox.android.HttpUrlConnectionClient.java

@Override
public HttpResponse execute(HttpRequest request) throws BaasException {
    try {/*from  w  w w.  j a  va 2 s.  c  o  m*/
        HttpURLConnection connection = openConnection(request.url);

        for (String name : request.headers.keySet()) {
            connection.addRequestProperty(name, request.headers.get(name));
        }
        setupConnectionForRequest(connection, request);
        connection.connect();

        int responseCode = -1;
        try {
            responseCode = connection.getResponseCode();
        } catch (IOException e) {
            responseCode = connection.getResponseCode();
        }
        Logger.info("Connection response received");
        if (responseCode == -1) {
            throw new IOException("Connection failed");
        }
        StatusLine line = new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), responseCode,
                connection.getResponseMessage());
        BasicHttpResponse response = new BasicHttpResponse(line);
        response.setEntity(asEntity(connection));
        for (Map.Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
            if (header.getKey() != null) {
                Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
                response.addHeader(h);
            }
        }
        return response;
    } catch (IOException e) {
        throw new BaasIOException(e);
    }
}

From source file:com.oplay.nohelper.volley.toolbox.HurlStack.java

@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
        throws IOException, AuthFailureError, NetworkError {
    if (request.getHasHttpResponse()) {
        throw new NetworkError("Slow Network leads to more HttpResponse");
    }//from w  w  w  .j a v  a2  s  .  co  m
    String url = request.getUrl();
    request.addMarker(url);
    HashMap<String, String> map = new HashMap<String, String>();
    map.putAll(request.getHeaders());
    map.putAll(additionalHeaders);
    if (mUrlRewriter != null) {
        String rewritten = mUrlRewriter.rewriteUrl(url);
        if (rewritten == null) {
            throw new IOException("URL blocked by rewriter: " + url);
        }
        url = rewritten;
    }
    URL parsedUrl = new URL(url);
    HttpURLConnection connection = openConnection(parsedUrl, request);
    for (String headerName : map.keySet()) {
        connection.addRequestProperty(headerName, map.get(headerName));
    }
    setConnectionParametersForRequest(connection, request);
    // Initialize HttpResponse with data from the HttpURLConnection.
    ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
    int responseCode = connection.getResponseCode();
    if (responseCode == -1) {
        // -1 is returned by getResponseCode() if the response code could not be retrieved.
        // Signal to the caller that something was wrong with the connection.
        throw new IOException("Could not retrieve response code from HttpUrlConnection.");
    }
    StatusLine responseStatus = new BasicStatusLine(protocolVersion, connection.getResponseCode(),
            connection.getResponseMessage());
    BasicHttpResponse response = new BasicHttpResponse(responseStatus);
    response.setEntity(entityFromConnection(connection));
    request.addMarker("network-http-complete");
    for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
        if (header.getKey() != null) {
            Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
            response.addHeader(h);
        }
    }
    return response;
}

From source file:cn.bidaround.ytcore.util.HurlStack.java

@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
        throws IOException, AuthFailureError {
    String url = request.getUrl();
    HashMap<String, String> map = new HashMap<String, String>();
    map.putAll(request.getHeaders());// w w w  .  j  a  v  a  2  s  .c om
    map.putAll(additionalHeaders);
    if (mUrlRewriter != null) {
        String rewritten = mUrlRewriter.rewriteUrl(url);
        if (rewritten == null) {
            throw new IOException("URL blocked by rewriter: " + url);
        }
        url = rewritten;
    }
    URL parsedUrl = new URL(url);
    HttpURLConnection connection = openConnection(parsedUrl, request);
    for (String headerName : map.keySet()) {
        connection.addRequestProperty(headerName, map.get(headerName));
    }
    setConnectionParametersForRequest(connection, request);
    // Initialize HttpResponse with data from the HttpURLConnection.
    ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
    int responseCode = connection.getResponseCode();
    if (responseCode == -1) {
        // -1 is returned by getResponseCode() if the response code could
        // not be retrieved.
        // Signal to the caller that something was wrong with the
        // connection.
        throw new IOException("Could not retrieve response code from HttpUrlConnection.");
    }
    StatusLine responseStatus = new BasicStatusLine(protocolVersion, connection.getResponseCode(),
            connection.getResponseMessage());
    BasicHttpResponse response = new BasicHttpResponse(responseStatus);
    response.setEntity(entityFromConnection(connection));
    for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
        if (header.getKey() != null) {
            Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
            response.addHeader(h);
        }
    }
    return response;
}

From source file:com.miya38.connection.volley.CustomHurlStack.java

@Override
public HttpResponse performRequest(final Request<?> request, final Map<String, String> additionalHeaders)
        throws IOException, AuthFailureError {
    String url = request.getUrl();
    final HashMap<String, String> map = new HashMap<String, String>();
    map.putAll(request.getHeaders());//w ww  .  jav  a2  s  .co m
    map.putAll(additionalHeaders);
    if (mUrlRewriter != null) {
        final String rewritten = mUrlRewriter.rewriteUrl(url);
        if (rewritten == null) {
            throw new IOException("URL blocked by rewriter: " + url);
        }
        url = rewritten;
    }
    final URL parsedUrl = new URL(url);
    final HttpURLConnection connection = openConnection(parsedUrl, request);
    for (final String headerName : map.keySet()) {
        connection.addRequestProperty(headerName, map.get(headerName));
    }
    setConnectionParametersForRequest(connection, request);
    // Initialize HttpResponse with data from the HttpURLConnection.
    final ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
    final int responseCode = connection.getResponseCode();
    if (responseCode == -1) {
        // -1 is returned by getResponseCode() if the response code could not be retrieved.
        // Signal to the caller that something was wrong with the connection.
        throw new IOException("Could not retrieve response code from HttpUrlConnection.");
    }
    final StatusLine responseStatus = new BasicStatusLine(protocolVersion, connection.getResponseCode(),
            connection.getResponseMessage());
    final BasicHttpResponse response = new BasicHttpResponse(responseStatus);
    response.setEntity(entityFromConnection(connection));
    for (final Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
        if (header.getKey() != null) {
            // bug fix y-miyazaki start
            for (final String value : header.getValue()) {
                final Header h = new BasicHeader(header.getKey(), value);
                response.addHeader(h);
            }
            // bug fix y-miyazaki end
        }
    }
    return response;
}

From source file:org.esigate.test.http.HttpResponseBuilder.java

/**
 * Build the HTTP response using all data previously set on this builder and/or use defaults.
 * //from w  w w. j  a  v a2 s.c  o m
 * @return The HTTP response
 */
public CloseableHttpResponse build() {
    BasicHttpResponse response = new BasicHttpResponse(this.protocolVersion, this.status, this.reason);

    for (Header h : this.headers) {
        response.addHeader(h.getName(), h.getValue());
    }

    if (this.entity != null) {
        response.setEntity(this.entity);
    }
    return BasicCloseableHttpResponse.adapt(response);
}

From source file:com.amazonaws.http.AmazonHttpClientTest.java

@Test
public void testRetryIOExceptionFromHandler() throws Exception {
    final IOException exception = new IOException("BOOM");

    HttpResponseHandler<AmazonWebServiceResponse<Object>> handler = EasyMock
            .createMock(HttpResponseHandler.class);

    EasyMock.expect(handler.needsConnectionLeftOpen()).andReturn(false).anyTimes();

    EasyMock.expect(handler.handle(EasyMock.<HttpResponse>anyObject())).andThrow(exception).times(4);

    EasyMock.replay(handler);//w ww .jav  a 2s  . c om

    BasicHttpEntity entity = new BasicHttpEntity();
    entity.setContent(new ByteArrayInputStream(new byte[0]));

    BasicHttpResponse response = new BasicHttpResponse(new ProtocolVersion("http", 1, 1), 200, "OK");
    response.setEntity(entity);

    EasyMock.reset(httpClient);

    EasyMock.expect(httpClient.getConnectionManager()).andReturn(null).anyTimes();

    EasyMock.expect(httpClient.execute(EasyMock.<HttpUriRequest>anyObject(), EasyMock.<HttpContext>anyObject()))
            .andReturn(response).times(4);

    EasyMock.replay(httpClient);

    ExecutionContext context = new ExecutionContext();

    Request<?> request = new DefaultRequest<Object>(null, "testsvc");
    request.setEndpoint(java.net.URI.create("http://testsvc.region.amazonaws.com"));
    request.setContent(new java.io.ByteArrayInputStream(new byte[0]));

    try {

        client.execute(request, handler, null, context);
        Assert.fail("No exception when request repeatedly fails!");

    } catch (AmazonClientException e) {
        Assert.assertSame(exception, e.getCause());
    }

    // Verify that we called execute 4 times.
    EasyMock.verify(httpClient);
}

From source file:carleton150.edu.carleton.carleton150.CertificateManagement.ExtHttpClientStack.java

private org.apache.http.HttpResponse convertResponseNewToOld(HttpResponse resp)
        throws IllegalStateException, IOException {

    ProtocolVersion protocolVersion = new ProtocolVersion(resp.getProtocolVersion().getProtocol(),
            resp.getProtocolVersion().getMajor(), resp.getProtocolVersion().getMinor());

    StatusLine responseStatus = new BasicStatusLine(protocolVersion, resp.getStatusLine().getStatusCode(),
            resp.getStatusLine().getReasonPhrase());

    BasicHttpResponse response = new BasicHttpResponse(responseStatus);
    org.apache.http.HttpEntity ent = convertEntityNewToOld(resp.getEntity());
    response.setEntity(ent);

    for (Header h : resp.getAllHeaders()) {
        org.apache.http.Header header = convertheaderNewToOld(h);
        response.addHeader(header);// w  ww . jav  a  2 s  .c  o m
    }

    return response;
}

From source file:com.subgraph.vega.internal.http.requests.HttpResponseBuilder.java

@Override
public synchronized HttpResponse buildResponse() {
    BasicHttpResponse response = new BasicHttpResponse(getProtocolVersion(), statusCode, reasonPhrase);

    setHeadersEntity();/*from w ww  . ja  va2s . co m*/
    IHttpHeaderBuilder[] headers = getHeaders();
    for (IHttpHeaderBuilder h : headers) {
        response.addHeader(h.buildHeader());
    }

    response.setEntity(getEntity());

    return response;
}