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:org.peterbaldwin.vlcremote.sweep.Worker.java

@Override
public void run() {
    // Note: InetAddress#isReachable(int) always returns false for Windows
    // hosts because applications do not have permission to perform ICMP
    // echo requests.
    for (;;) {//from   w  w w.  j  a  v  a  2  s .  c  o m
        byte[] ipAddress = mManager.pollIpAddress();
        if (ipAddress == null) {
            break;
        }
        try {
            InetAddress address = InetAddress.getByAddress(ipAddress);
            String hostAddress = address.getHostAddress();
            URL url = createUrl("http", hostAddress, mPort, mPath);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setConnectTimeout(1000);
            try {
                int responseCode = connection.getResponseCode();
                String responseMessage = connection.getResponseMessage();
                InputStream inputStream = (responseCode == HttpURLConnection.HTTP_OK)
                        ? connection.getInputStream()
                        : connection.getErrorStream();
                if (inputStream != null) {
                    try {
                        int length = connection.getContentLength();
                        InputStreamEntity entity = new InputStreamEntity(inputStream, length);
                        entity.setContentType(connection.getContentType());

                        // Copy the entire response body into memory
                        // before the HTTP connection is closed:
                        String body = EntityUtils.toString(entity);

                        ProtocolVersion version = new ProtocolVersion("HTTP", 1, 1);
                        String hostname = address.getHostName();
                        StatusLine statusLine = new BasicStatusLine(version, responseCode, responseMessage);
                        HttpResponse response = new BasicHttpResponse(statusLine);
                        response.setHeader(HTTP.TARGET_HOST, hostname + ":" + mPort);
                        response.setEntity(new StringEntity(body));
                        mCallback.onReachable(ipAddress, response);
                    } finally {
                        inputStream.close();
                    }
                } else {
                    Log.w(TAG, "InputStream is null");
                }
            } finally {
                connection.disconnect();
            }
        } catch (IOException e) {
            mCallback.onUnreachable(ipAddress, e);
        }
    }
}

From source file:com.google.android.apps.iosched.io.StubHttpClient.java

/**
 * Build a stub {@link HttpResponse}, probably for use with
 * {@link #setResponse(HttpResponse)}./*from  w  w w . jav a2s . co  m*/
 *
 * @param statusCode {@link HttpStatus} code to use.
 * @param assetName Name of asset that should be included as a single
 *            {@link HttpEntity} to be included in the response, loaded
 *            through {@link AssetManager}.
 */
public static HttpResponse buildResponse(int statusCode, String assetName, Context context) throws IOException {
    final StatusLine status = new BasicStatusLine(HttpVersion.HTTP_1_1, statusCode, null);
    final HttpResponse response = new BasicHttpResponse(status);
    if (assetName != null) {
        final InputStream entity = context.getAssets().open(assetName);
        response.setEntity(new InputStreamEntity(entity, entity.available()));
    }
    return response;
}

From source file:org.esigate.servlet.impl.ResponseCapturingWrapper.java

@Override
public void setStatus(int sc) {
    httpClientResponse.setStatusLine(new BasicStatusLine(HttpVersion.HTTP_1_1, sc, ""));
}

From source file:cn.garymb.wechatmoments.common.OkHttpStack.java

@Override
@SuppressWarnings("deprecation")
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
        throws IOException, AuthFailureError {
    String url = request.getUrl();
    HashMap<String, String> map = new HashMap<>();
    map.putAll(request.getHeaders());/*from www .ja  va  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);
    OkUrlFactory okFactory = new OkUrlFactory(mClient);
    HttpURLConnection connection = openOkHttpURLConnection(okFactory, 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:org.esigate.servlet.impl.ResponseCapturingWrapper.java

@Override
public void setStatus(int sc, String sm) {
    httpClientResponse.setStatusLine(new BasicStatusLine(HttpVersion.HTTP_1_1, sc, sm));
}

From source file:org.sentilo.common.test.rest.RESTClientImplTest.java

@Test
public void get() throws Exception {
    final String path = "/data";
    final String responseContent = "Lorem ipsum";
    final StatusLine statusLine = new BasicStatusLine(HttpVersion.HTTP_1_0, HttpStatus.SC_OK, "");

    when(httpClient.execute(notNull(HttpGet.class))).thenReturn(httpResponse);
    when(httpResponse.getStatusLine()).thenReturn(statusLine);
    when(httpResponse.getEntity()).thenReturn(new StringEntity(responseContent));

    final String result = restClient.get(path);
    Assert.assertEquals(responseContent, result);
}

From source file:com.nxt.zyl.data.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>();
    // chenbo add gzip support,new user-agent
    if (request.isShouldGzip()) {
        map.put(HEADER_ACCEPT_ENCODING, ENCODING_GZIP);
    }// ww  w  . j  a va  2 s  .  c o  m
    map.put(USER_AGENT, mUserAgent);
    // end
    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));

    }

    //        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);
        }
    }
    if (ENCODING_GZIP.equalsIgnoreCase(connection.getContentEncoding())) {
        response.setEntity(new InflatingEntity(response.getEntity()));
    }
    return response;
}

From source file:com.hackerati.android.user_sdk.volley.HHurlStack.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());//from w w  w .java  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);
    int responseCode;
    try {
        // Will throw IOException if server responds with 401.
        responseCode = connection.getResponseCode();
    } catch (final IOException e) {
        // Will return 401, because now connection has the correct internal
        // state.
        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) {
            final Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
            response.addHeader(h);
        }
    }
    return response;
}

From source file:org.finra.dm.tools.common.databridge.DataBridgeWebClientTest.java

@Test
public void testGetBusinessObjectData200ValidResponse() throws Exception {
    CloseableHttpResponse httpResponse = new MockCloseableHttpResponse(
            new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "testReasonPhrase"));
    httpResponse.setEntity(new StringEntity(xmlHelper.objectToXml(new BusinessObjectData())));
    String actionDescription = "testActionDescription";
    BusinessObjectData businessObjectData = dataBridgeWebClient.getBusinessObjectData(httpResponse,
            actionDescription);/*from w w  w . ja va 2  s . c om*/
    Assert.assertNotNull("businessObjectData", businessObjectData);
}

From source file:com.nexmo.client.voice.endpoints.CreateCallMethodTest.java

@Test
public void testParseResponse() throws Exception {
    HttpWrapper wrapper = new HttpWrapper();
    CreateCallMethod methodUnderTest = new CreateCallMethod(wrapper);

    HttpResponse stubResponse = new BasicHttpResponse(
            new BasicStatusLine(new ProtocolVersion("1.1", 1, 1), 200, "OK"));

    String json = " {\"uuid\":\"93137ee3-580e-45f7-a61a-e0b5716000ea\",\"status\":\"started\",\"direction\":\"outbound\",\"conversation_uuid\":\"aa17bd11-c895-4225-840d-30dc78c31e50\"}";
    InputStream jsonStream = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8));
    BasicHttpEntity entity = new BasicHttpEntity();
    entity.setContent(jsonStream);//from w  w w  .  j  a  va  2s .c  o m
    stubResponse.setEntity(entity);

    // Execute test call:
    CallEvent callEvent = methodUnderTest.parseResponse(stubResponse);
    assertEquals("93137ee3-580e-45f7-a61a-e0b5716000ea", callEvent.getUuid());
    assertEquals("aa17bd11-c895-4225-840d-30dc78c31e50", callEvent.getConversationUuid());
    assertEquals(CallStatus.STARTED, callEvent.getStatus());
    assertEquals(CallDirection.OUTBOUND, callEvent.getDirection());
}