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

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

Introduction

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

Prototype

public BasicHttpResponse(StatusLine statusLine) 

Source Link

Usage

From source file:com.ab.network.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());/*ww w  . j  a  va 2  s  . com*/
    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: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);//www  .  j a  v  a 2  s .  c  o m

    for (Header h : resp.getAllHeaders()) {
        org.apache.http.Header header = convertheaderNewToOld(h);
        response.addHeader(header);
    }

    return response;
}

From source file:com.lion328.xenonlauncher.proxy.HttpDataHandler.java

@Override
public boolean process(Socket client, Socket server) throws Exception {
    InputStream clientIn = client.getInputStream();
    clientIn.mark(65536);/*from ww w .java2 s .  c  om*/

    try {
        DefaultBHttpServerConnection httpClient = new DefaultBHttpServerConnection(8192);
        httpClient.bind(client);
        httpClient.setSocketTimeout(timeout);

        DefaultBHttpClientConnection httpServer = new DefaultBHttpClientConnection(8192);
        httpServer.bind(server);

        HttpCoreContext context = HttpCoreContext.create();
        context.setAttribute("client.socket", client);
        context.setAttribute("server.socket", server);

        HttpEntityEnclosingRequest request;

        do {
            HttpRequest rawRequest = httpClient.receiveRequestHeader();

            if (rawRequest instanceof HttpEntityEnclosingRequest) {
                request = (HttpEntityEnclosingRequest) rawRequest;
            } else {
                request = new BasicHttpEntityEnclosingRequest(rawRequest.getRequestLine());
                request.setHeaders(rawRequest.getAllHeaders());
            }

            httpClient.receiveRequestEntity(request);

            HttpResponse response = new BasicHttpResponse(
                    new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"));

            boolean sent = false;

            for (Map.Entry<Integer, HttpRequestHandler> entry : handlers.entrySet()) {
                entry.getValue().handle(request, response, context);

                if (context.getAttribute("response.set") instanceof HttpResponse) {
                    response = (HttpResponse) context.getAttribute("response.set");
                }

                if (context.getAttribute("pipeline.end") == Boolean.TRUE) {
                    break;
                }

                if (context.getAttribute("response.need-original") == Boolean.TRUE && !sent) {
                    httpServer.sendRequestHeader(request);
                    httpServer.sendRequestEntity(request);
                    response = httpServer.receiveResponseHeader();
                    httpServer.receiveResponseEntity(response);

                    entry.getValue().handle(request, response, context);

                    context.removeAttribute("response.need-original");
                    context.setAttribute("request.sent", true);

                    sent = true;
                }
            }

            if (context.getAttribute("response.sent") != Boolean.TRUE) {
                httpClient.sendResponseHeader(response);

                if (response.getEntity() != null) {
                    httpClient.sendResponseEntity(response);
                }
            }
        } while (request.getFirstHeader("Connection").getValue().equals("keep-alive"));

        return true;
    } catch (ProtocolException e) {
        clientIn.reset();
        return false;
    } catch (ConnectionClosedException e) {
        return true;
    }
}

From source file:com.seleritycorp.common.base.http.client.HttpResponseTest.java

private HttpResponse createHttpResponse(int status, byte[] body, Charset charset) throws Exception {
    StatusLine statusLine = new BasicStatusLine(HttpVersion.HTTP_1_1, status, "ReasonFoo");
    org.apache.http.HttpResponse backendResponse = new BasicHttpResponse(statusLine);
    if (body != null) {
        backendResponse.setEntity(new ByteArrayEntity(body, ContentType.create("foo", charset)));
    }//w  w  w.jav a  2 s.  c o  m
    return new HttpResponse(backendResponse);
}

From source file:net.sf.jsog.client.DefaultJsogClientImplTest.java

@Test
public void testGetDefaultContentTypeAndCharset() throws Exception {

    String url = "http://www.example.com";
    JSOG expected = JSOG.object("foo", "bar");

    // Create and prepare the mock
    final HttpClient client = createMock(HttpClient.class);
    Capture<HttpGet> request = new Capture<HttpGet>();

    HttpResponse response = new BasicHttpResponse(createStatusLine(200, "OK"));
    response.setEntity(createStringEntity(expected));
    expect(client.execute(capture(request))).andReturn(response);

    // Create the instance
    DefaultJsogClientImpl instance = new DefaultJsogClientImpl() {
        @Override//from w  w  w.j av a  2s.c  o m
        protected HttpClient getClient() {
            return client;
        }
    };

    // Run the test
    replay(client);

    JSOG actual = instance.getJsog(url);
    assertEquals(expected, actual);

    verify(client);

    Header[] headers = request.getValue().getHeaders("Content-Type");
    assertEquals(0, headers.length);
}

From source file:com.android.volley.toolbox.https.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);
    org.apache.http.HttpEntity ent = convertEntityNewToOld(resp.getEntity());
    response.setEntity(ent);/*w  ww  .  j a v a2s.c om*/

    for (Header h : resp.getAllHeaders()) {
        Header header = convertheaderNewToOld((Header) h);
        response.addHeader(header);
    }

    return response;
}

From source file:org.opencastproject.workflow.handler.HttpNotificationWorkflowOperationHandlerTest.java

@Test
public void testNotificationFailedAfterOneTry() throws Exception {
    client = EasyMock.createNiceMock(TrustedHttpClient.class);
    HttpResponse response = new BasicHttpResponse(
            new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), HttpStatus.SC_NOT_FOUND, ""));
    EasyMock.expect(client.execute((HttpPut) EasyMock.anyObject(), EasyMock.anyInt(), EasyMock.anyInt()))
            .andReturn(response);/*from w  w w. ja v  a2 s  .  c o  m*/
    EasyMock.replay(client);

    // set up service
    operationHandler = new HttpNotificationWorkflowOperationHandler();

    // operation configuration
    Map<String, String> configurations = new HashMap<String, String>();
    configurations.put(HttpNotificationWorkflowOperationHandler.OPT_URL_PATH,
            "http://www.host-does-not-exist.com");
    configurations.put(HttpNotificationWorkflowOperationHandler.OPT_NOTIFICATION_SUBJECT, "test");
    configurations.put(HttpNotificationWorkflowOperationHandler.OPT_MAX_RETRY, "0");
    configurations.put(HttpNotificationWorkflowOperationHandler.OPT_TIMEOUT, Integer.toString(10 * 1000));

    // run the operation handler
    try {
        getWorkflowOperationResult(mp, configurations);
        Assert.fail("Operation handler should have thrown an exception!");
    } catch (WorkflowOperationException e) {
        Assert.assertTrue("Exception thrown as expected by the operation handler", true);
    }

}

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 ww .j a  v a  2  s  .  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);
    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:cn.com.xxutils.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 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);
    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.farru.android.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  w w  w  .  j av a 2s . 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) {
            Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
            response.addHeader(h);
        }
    }
    return response;
}