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

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

Introduction

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

Prototype

public BasicStatusLine(ProtocolVersion protocolVersion, int i, String str) 

Source Link

Usage

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());/*ww  w.ja v a  2s. c  o 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.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);/*w  w  w.ja v a 2 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:com.autonavi.gxdtaojin.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  . java2s  . 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 = null;
    try {
        parsedUrl = new URL(url);
    } catch (Throwable t) {
        t.printStackTrace();
        parsedUrl = new URL("http://www.google.com");
    }
    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.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 a  va 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:edu.wisc.cypress.dao.advrpt.AdvisorReportDaoTest.java

protected HttpResponse setupHttpClient(InputStream benefitStatementsStream, String contentType)
        throws IOException, ClientProtocolException {
    final HttpResponse httpResponse = mock(HttpResponse.class);
    when(httpClient.execute(any(HttpUriRequest.class), any(HttpContext.class))).thenReturn(httpResponse);
    when(httpResponse.getStatusLine()).thenReturn(
            new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase()));
    when(httpResponse.getAllHeaders())//www . j a v  a  2s . co  m
            .thenReturn(new Header[] { new BasicHeader("Content-Type", contentType) });
    when(httpResponse.getEntity()).thenReturn(new InputStreamEntity(benefitStatementsStream, -1));

    return httpResponse;
}

From source file:com.gooddata.http.client.LoginSSTRetrievalStrategyTest.java

@Test
public void shouldLogout() throws Exception {
    statusLine = new BasicStatusLine(new ProtocolVersion("https", 1, 1), HttpStatus.SC_NO_CONTENT,
            "NO CONTENT");
    final HttpResponse response = new BasicHttpResponse(statusLine);
    when(httpClient.execute(isA(HttpHost.class), isA(HttpDelete.class))).thenReturn(response);

    sstStrategy.logout(httpClient, host, "/gdc/account/login/profileid", SST, TT);

    final ArgumentCaptor<HttpHost> hostCaptor = ArgumentCaptor.forClass(HttpHost.class);
    final ArgumentCaptor<HttpDelete> deleteCaptor = ArgumentCaptor.forClass(HttpDelete.class);

    verify(httpClient).execute(hostCaptor.capture(), deleteCaptor.capture());

    assertEquals("server.com", hostCaptor.getValue().getHostName());
    assertEquals(123, hostCaptor.getValue().getPort());

    final HttpDelete delete = deleteCaptor.getValue();
    assertNotNull(delete);//  w  w w. ja v a2  s. c om
    assertEquals("/gdc/account/login/profileid", delete.getURI().getPath());
    assertEquals(SST, delete.getFirstHeader("X-GDC-AuthSST").getValue());
    assertEquals(TT, delete.getFirstHeader("X-GDC-AuthTT").getValue());
}

From source file:org.codegist.crest.io.http.HttpClientHttpChannelTest.java

@Test
public void sendShouldWrapHttpClientResponse() throws IOException {
    HttpResponse clientResponse = mock(HttpResponse.class);
    HttpEntity httpEntity = mock(HttpEntity.class);
    InputStream inputStream = mock(InputStream.class);
    StatusLine statusLine = new BasicStatusLine(mock(ProtocolVersion.class), 123, "reason");
    Header contentType = new BasicHeader("somename", "contentType");
    Header contentEncoding = new BasicHeader("somename", "contentEncoding");

    when(httpEntity.getContent()).thenReturn(inputStream);
    when(client.execute(request)).thenReturn(clientResponse);

    HttpChannel.Response response = toTest.send();
    assertNull(response.getContentType());
    assertNull(response.getContentEncoding());
    assertSame(EmptyInputStream.INSTANCE, response.getEntity());

    when(clientResponse.getStatusLine()).thenReturn(statusLine);
    when(clientResponse.getFirstHeader("Content-Type")).thenReturn(contentType);
    when(clientResponse.getFirstHeader("Content-Encoding")).thenReturn(contentEncoding);
    when(clientResponse.getEntity()).thenReturn(httpEntity);

    assertSame(inputStream, response.getEntity());
    assertEquals("contentType", response.getContentType());
    assertEquals("contentEncoding", response.getContentEncoding());
    assertEquals(123, response.getStatusCode());
    assertEquals("reason", response.getStatusMessage());

    response.close();/*from   w w w. j  ava 2s.c o m*/
    verify(httpEntity).consumeContent();
    verify(request).abort();
}

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   w  ww . j a  v a2s .  co  m
    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.github.avarabyeu.restendpoint.http.DefaultErrorHandlerTest.java

private HttpResponse getHttpResponse(int statusCode, String message) {
    StatusLine statusLine = new BasicStatusLine(HttpVersion.HTTP_1_1, statusCode, message);
    BasicHttpResponse response = new BasicHttpResponse(statusLine, EnglishReasonPhraseCatalog.INSTANCE,
            Locale.US);/*from   w ww.java 2  s  . co m*/
    response.setEntity(new StringEntity("test string response body", Consts.UTF_8));
    return response;
}

From source file:co.cask.cdap.client.rest.RestClientTest.java

@Test
public void testConflictResponseCodeAnalysis() {

    StatusLine statusLine = new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), HttpStatus.SC_CONFLICT,
            "Conflict");
    when(response.getStatusLine()).thenReturn(statusLine);
    TestUtils.verifyResponse(HttpStatus.SC_CONFLICT, response);
    verify(response).getStatusLine();/*from ww  w .  j av a2  s  . c  om*/
}