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.callimachusproject.rewrite.RedirectAdviceFactory.java

private StatusLine getStatusLine(Method method) {
    if (method.isAnnotationPresent(alternate.class))
        return new BasicStatusLine(HttpVersion.HTTP_1_1, 302, "Alternate");
    if (method.isAnnotationPresent(describedby.class))
        return new BasicStatusLine(HttpVersion.HTTP_1_1, 303, "Described By");
    if (method.isAnnotationPresent(resides.class))
        return new BasicStatusLine(HttpVersion.HTTP_1_1, 307, "Resides");
    if (method.isAnnotationPresent(moved.class))
        return new BasicStatusLine(HttpVersion.HTTP_1_1, 308, "Moved");
    throw new AssertionError();
}

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());//w w  w  .  j ava  2 s  .c o 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.baasbox.android.HttpUrlConnectionClient.java

@Override
public HttpResponse execute(HttpRequest request) throws BaasException {
    try {/*from w w  w .j a  v  a  2s.  com*/
        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:org.apache.kylin.jdbc.KylinConnectionTest.java

private KylinConnection getConnectionWithMockHttp() throws SQLException, IOException {
    final Properties info = new Properties();
    info.setProperty("user", "ADMIN");
    info.setProperty("password", "KYLIN");

    // hack KylinClient
    doAnswer(new Answer() {
        @Override/*from w w w. ja  v a 2 s.c o m*/
        public Object answer(InvocationOnMock invo) throws Throwable {
            KylinConnectionInfo connInfo = invo.getArgument(0);
            KylinClient client = new KylinClient(connInfo);
            client.setHttpClient(httpClient);
            return client;
        }
    }).when(factory).newRemoteClient(any(KylinConnectionInfo.class));

    // Workaround IRemoteClient.connect()
    HttpResponse response = mock(HttpResponse.class);
    when(httpClient.execute(any(HttpUriRequest.class))).thenReturn(response);
    when(response.getStatusLine()).thenReturn(new BasicStatusLine(HTTP_1_1, 200, "OK"));

    return new KylinConnection(driver, factory, "jdbc:kylin:test_url/test_db", info);
}

From source file:org.esigate.HttpErrorPage.java

public static CloseableHttpResponse generateHttpResponse(int statusCode, String statusText) {
    CloseableHttpResponse result = BasicCloseableHttpResponse
            .adapt(new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, statusCode, statusText)));
    result.setEntity(toMemoryEntity(statusText));
    return result;
}

From source file:org.piraso.client.net.HttpPirasoEntryReaderTest.java

@Test(expected = HttpPirasoException.class)
public void testStopInvalidStatusCode() throws Exception {
    StatusLine line = new BasicStatusLine(new ProtocolVersion("http", 1, 0), HttpStatus.SC_BAD_REQUEST,
            "Bad Request");
    doReturn(line).when(response).getStatusLine();

    reader.stop();/*w ww . j a  v a  2 s . c o  m*/
}

From source file:mobi.jenkinsci.server.core.services.HttpClientURLDownloaderTest.java

@Test(expected = ResourceNotFoundException.class)
public void httpGetRemoteUrlThrowsResourceNotFoundExceptionWhenHttpStatusIs404() throws IOException {
    when(mockHttpClient.execute(any(HttpUriRequest.class))).thenReturn(new BasicHttpResponse(
            new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), HttpStatus.SC_NOT_FOUND, "NOT FOUND")));

    httpClientDownloader.download(mockPluginRequest, A_REMOTE_RESOURCE_URL.toString(), mockAccount,
            mockPluginConfig);/*from   w w  w  . j  av  a2 s  .  com*/
}

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

@Test
public void delete() 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(HttpDelete.class))).thenReturn(httpResponse);
    when(httpResponse.getStatusLine()).thenReturn(statusLine);
    when(httpResponse.getEntity()).thenReturn(new StringEntity(responseContent));

    final String result = restClient.delete(path);

    verify(httpClient).execute(any(HttpDelete.class));
    Assert.assertEquals(responseContent, result);
}

From source file:com.techgrains.service.TGRequestQueue.java

@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
        throws IOException, AuthFailureError {
    try {/*  ww  w  .j  av a2 s  . c  o  m*/
        Thread.sleep(SIMULATED_DELAY_MS);
    } catch (InterruptedException e) {
        // Ignores simulated delay on interruption.
    }

    HttpResponse response = new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "OK"));
    DateFormat dateFormat = new SimpleDateFormat("EEE, dd mmm yyyy HH:mm:ss zzz");
    response.setHeader(new BasicHeader("Date", dateFormat.format(new Date())));
    response.setLocale(Locale.getDefault());
    response.setEntity(createEntity(request));
    return response;
}

From source file:org.sentilo.platform.server.test.parser.OrderParserTest.java

@Test
public void parseSensorOrdersWriteResponse() throws Exception {
    final String[] parts = { "prov1", "sensor1" };
    when(resource.getParts()).thenReturn(parts);

    final SentiloResponse response = SentiloResponse
            .build(new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_0, 200, "")));
    parser.writeResponse(sentiloRequest, response, getSensorOrders());

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ((ByteArrayEntity) response.getHttpResponse().getEntity()).writeTo(baos);
    final String expected = "{\"orders\":[{\"order\":\"stop\",\"timestamp\":\"21/02/2013T17:49:24\",\"sender\":\"sender1\"},{\"order\":\"start\",\"timestamp\":\"21/02/2013T17:49:30\",\"sender\":\"sender1\"}]}";
    assertEquals(expected, baos.toString());
}