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.yaozu.object.volley.toolbox.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());//  www . ja  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);
    if (hasResponseBody(request.getMethod(), responseStatus.getStatusCode())) {
        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.selene.volley.stack.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>();
    //      if (!TextUtils.isEmpty(mUserAgent)) {
    //         map.put(HTTP.USER_AGENT, mUserAgent);
    //      }//from   ww w  . j av a2s . c om
    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);
    if (hasResponseBody(request.getMethod(), responseStatus.getStatusCode())) {
        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.vimc.ahttp.HurlWorker.java

@Override
public HttpResponse doHttpRquest(Request<?> request) throws IOException {
    String url = request.getUrl();

    if (mUrlRewriter != null) {
        String rewritten = mUrlRewriter.rewriteUrl(url);
        if (rewritten == null) {
            throw new IOException("URL blocked by rewriter: " + url);
        }//from  w w  w.j  av a 2  s . co  m
        url = rewritten;
    }
    URL parsedUrl = new URL(url);
    HttpURLConnection connection = openConnection(parsedUrl, request);
    addHeadersIfExists(request, connection);
    setConnectionParametersByRequest(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.android.volley.toolbox.http.HurlStack.java

@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
        throws AuthFailureError, IOException {
    HashMap<String, String> map = new HashMap<String, String>();
    map.putAll(request.getHeaders());/*from   www  . j  a  va2 s  .  co  m*/
    map.putAll(additionalHeaders);
    String url = request.getUrl();
    if (mUrlRewriter != null) {
        url = mUrlRewriter.rewriteUrl(url);
        if (url == null) {
            throw new IOException("URL blocked by rewriter: " + url);
        }
    }
    URL parsedUrl = new URL(url);
    System.err.println(url);
    HttpURLConnection connection = openConnection(parsedUrl, request);

    if (!TextUtils.isEmpty(mUserAgent)) {
        connection.setRequestProperty(HEADER_USER_AGENT, mUserAgent);
    }

    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.comcast.drivethru.client.ClientExecuteTest.java

@Test
public void testExecuteSuccess() throws Exception {
    String body = "this is a response from the internet";
    HttpEntity entity = new ByteArrayEntity(body.getBytes());

    BasicHttpResponse resp = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), 200, "OK");
    resp.setHeader("a", "apple");
    resp.setHeader("b", "bobcat");
    resp.setHeader("Content-Type", "text/plain");
    resp.setEntity(entity);

    RestRequest request = new RestRequest(new URL().setPath("/").addQuery("q", "stuff"), POST);
    request.setContentType("application/json");
    request.setBody("{ \"name\" : \"Clark\" }");
    request.addHeader("x-transaction-id", "0011223344556677");

    Capture<HttpPost> capture = EasyMock.newCapture();
    Capture<HttpPost> capture2 = EasyMock.newCapture();

    HttpClient delegate = createMock(HttpClient.class);
    expect(delegate.execute(capture(capture))).andReturn(resp);

    SecurityProvider securityProvider = createMock(SecurityProvider.class);
    securityProvider.sign(capture(capture2));
    expectLastCall();//from ww w .  j  av a  2  s. c  o  m

    replay(delegate, securityProvider);

    String base = "http://www.google.com";
    DefaultRestClient client = new DefaultRestClient(base, delegate);
    client.addDefaultHeader("Fintan", "The Salmon of Knowledge");
    client.setSecurityProvider(securityProvider);

    RestResponse response = client.execute(request);
    client.close();

    assertEquals(response.getBodyString(), body);
    assertEquals(response.getStatusCode(), 200);
    assertEquals(response.getStatusMessage(), "OK");
    assertEquals(response.getContentType(), "text/plain");
    assertEquals(response.getHeaderValue("a"), "apple");
    assertEquals(response.getHeaderValue("b"), "bobcat");

    /* Verify the constructed request object */
    assertTrue(capture.hasCaptured());
    HttpPost req = capture.getValue();
    assertEquals(req.getLastHeader("Content-Type").getValue(), "application/json");
    assertEquals(req.getLastHeader("x-transaction-id").getValue(), "0011223344556677");
    assertEquals(req.getLastHeader("Fintan").getValue(), "The Salmon of Knowledge");
    assertEquals(req.getURI().toString(), "http://www.google.com/?q=stuff");

    /* Verify the "aborted" status of the request */
    assertEquals(req.isAborted(), true);

    /* Read and verify the contents of the request */
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    IOUtils.copy(req.getEntity().getContent(), baos);
    assertEquals(baos.toString(), "{ \"name\" : \"Clark\" }");

    /* Verify Security was called with same element */
    assertTrue(capture2.hasCaptured());
    assertSame(capture2.getValue(), req);

    verify(delegate, securityProvider);
}

From source file:de.micromata.genome.tpsb.soapui.DelegateToSoapUiTestBuilderHttpClientRequestTransport.java

private HttpResponse execute(SubmitContext submitContext, ExtendedHttpMethod method, HttpContext httpContext,
        Map<String, String> httpRequestParameter) throws Exception {
    boolean passtoremote = false;
    if (passtoremote == true) {
        return HttpClientSupport.execute(method, httpContext);

    }//from   w ww  .  j  a va2  s.co  m
    byte[] reqData = null;
    if (method.getRequestEntity() != null && method.getRequestEntity().getContent() != null) {
        reqData = filterRequestData(IOUtils.toByteArray(method.getRequestEntity().getContent()));
    }
    Header[] soaphaedera = method.getHeaders("SOAPAction");
    String soapAction = "";
    if (soaphaedera != null && soaphaedera.length > 0) {
        soapAction = method.getHeaders("SOAPAction")[0].getValue();
    }
    String uri = method.getURI().toString();
    //    testBuilder.initWithUri(uri);
    testBuilder//
            .createNewPostRequestIntern(submitContext) //
            .initWithUri(uri).setRequestMethod(method.getMethod()).setRequestData(reqData);
    if (StringUtils.isNotBlank(soapAction) == true) {
        testBuilder.addRequestHeader("SOAPAction", soapAction);
    }
    Header[] allHeaders = method.getAllHeaders();
    for (Header h : allHeaders) {
        testBuilder.addRequestHeader(h.getName(), h.getValue());
    }
    httpRequestParameter.forEach((k, v) -> testBuilder.getHttpRequest().addRequestParameter(k, v));
    MockHttpServletResponse httpr = testBuilder.executeServletRequest() //
            .getHttpResponse();

    byte[] respData = filterResponseData(httpr.getOutputBytes());
    //    String outp = httpr.getOutputString();
    BasicStatusLine statusLine = new BasicStatusLine(new ProtocolVersion("http", 1, 1), httpr.getStatus(),
            null);
    BasicHttpResponse httpResponse = new BasicHttpResponse(statusLine);
    httpResponse.setEntity(new ByteArrayEntity(respData));
    httpResponse = filterBasicHttpResponse(httpResponse);
    //        WsdlSinglePartHttpResponse wsdls = new WsdlSinglePartHttpResponse();
    method.setHttpResponse(httpResponse);
    try {
        method.setURI(new URI("http://localhost/dummy"));
    } catch (URISyntaxException ex) {
        throw new RuntimeException(ex);
    }
    return httpResponse;
}

From source file:com.daidiansha.acarry.control.network.core.volley.toolbox.HurlStack.java

@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
        throws AuthFailureError, IOException {
    String url = request.getUrl();
    HashMap<String, String> map = new HashMap<String, String>();
    map.putAll(request.getHeaders());/*from www.ja  v a  2s  . co  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);

    if (!TextUtils.isEmpty(mUserAgent)) {
        connection.setRequestProperty(HEADER_USER_AGENT, mUserAgent);
    }

    for (String headerName : map.keySet()) {
        connection.addRequestProperty(headerName, map.get(headerName));
    }
    if (request instanceof MultiPartRequest) {
        setConnectionParametersForMultipartRequest(connection, (MultiPartRequest) request);
    } else {
        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.iflytek.android.framework.volley.toolbox.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());/*from   ww  w  . j av  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()) {
        VolleyLog.e("======4:" + headerName + ";" + map.get(headerName));
        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);
    if (hasResponseBody(request.getMethod(), responseStatus.getStatusCode())) {
        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.androidex.volley.toolbox.HurlStack.java

@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
        throws AuthFailureError, IOException {
    String url = request.getUrl();
    HashMap<String, String> map = new HashMap<String, String>();
    map.putAll(request.getHeaders());// www  .  j  a va2 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);

    if (!TextUtils.isEmpty(mUserAgent)) {
        connection.setRequestProperty(HEADER_USER_AGENT, mUserAgent);
    }

    for (String headerName : map.keySet()) {
        connection.addRequestProperty(headerName, map.get(headerName));
    }
    if (request instanceof MultiPartRequest) {
        setConnectionParametersForMultipartRequest(connection, request);
    } else {
        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;
}