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(ProtocolVersion protocolVersion, int i, String str) 

Source Link

Usage

From source file:org.apache.synapse.transport.passthru.SourceRequest.java

/**
 * Start processing the request by connecting the pipe if this request has an entity body.
 * @param conn connection//from  w w w .  j  a v  a2 s.  c  o m
 * @throws IOException if an error occurs
 * @throws HttpException if an error occurs
 */
public void start(NHttpServerConnection conn) throws IOException, HttpException {
    if (entityEnclosing) {
        pipe = new Pipe(conn, sourceConfiguration.getBufferFactory().getBuffer(), "source",
                sourceConfiguration);

        SourceContext.get(conn).setReader(pipe);

        // See if the client expects a 100-Continue
        if (((HttpEntityEnclosingRequest) request).expectContinue()) {
            HttpResponse ack = new BasicHttpResponse(version, HttpStatus.SC_CONTINUE, "Continue");
            conn.submitResponse(ack);
        }
    } else {
        // this request is completed, there is nothing more to read
        SourceContext.updateState(conn, ProtocolState.REQUEST_DONE);
        // No httpRequest content expected. Suspend client input
        conn.suspendInput();
    }
}

From source file:org.esigate.http.HttpClientRequestExecutorTest.java

private HttpResponse createMockResponse(String entity) throws Exception {
    HttpResponse response = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), HttpStatus.SC_OK, "OK");
    HttpEntity httpEntity = new StringEntity(entity);
    response.setEntity(httpEntity);/*from www.  j a va  2  s. c o  m*/
    return response;
}

From source file:org.dataconservancy.ui.services.EZIDServiceImplTest.java

/**
 * Tests that update is called succesfully without throwing exception
 * @throws ClientProtocolException//from   w  w w  . j a v a  2s .  com
 * @throws IOException
 */
@Test
public void testSuccessfulUpdate() throws ClientProtocolException, IOException {
    boolean noExceptions = true;
    HttpResponse mockResponse = new BasicHttpResponse(new HttpVersion(1, 1), 200, "ok");
    StringEntity entity = new StringEntity("success: namespace:id");
    mockResponse.setEntity(entity);

    HttpClient mockHttpClient = mock(HttpClient.class);
    when(mockHttpClient.execute(any(HttpPost.class))).thenReturn(mockResponse);

    ezidService.setHttpClient(mockHttpClient);

    try {
        ezidService.saveID("www.test.com/id/namespace:id");
    } catch (EZIDServiceException e) {
        noExceptions = false;
    }

    assertTrue(noExceptions);
}

From source file:com.comcast.cim.rest.client.xhtml.TestXhtmlResponseHandler.java

@Test
public void testPassesUpIOExceptionIfThrown() {
    HttpResponse resp = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
    resp.setHeader("Content-Type", "application/xhtml+xml;charset=utf-8");
    String xhtml = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML " + "1.0 Transitional//EN\" "
            + "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"
            + "<html xmlns=\"http://www.w3.org/1999/xhtml\" " + "xml:lang=\"en\" lang=\"en\"><head/><body/>";
    byte[] bytes = xhtml.getBytes();
    long purportedLength = bytes.length + 6;
    resp.setHeader("Content-Length", "" + purportedLength);

    ByteArrayInputStream buf = new ByteArrayInputStream(bytes);
    InterruptedInputStream iis = new InterruptedInputStream(buf);
    resp.setEntity(new InputStreamEntity(iis, purportedLength));

    try {/*ww  w  .  j  a v  a  2 s  .  c  om*/
        impl.handleResponse(resp);
        Assert.fail("should have thrown IOException");
    } catch (IOException expected) {
    }
}

From source file:org.fishwife.jrugged.httpclient.TestFailureExposingHttpClient.java

@Test
public void doesNotExposeFailureIfAssessorSaysNotTo() throws Exception {
    resp = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_INTERNAL_SERVER_ERROR, "Bork");
    backend.setResponse(resp);//from   ww w  .  j ava 2s  . c o  m
    impl = new FailureExposingHttpClient(backend, new ResponseFailureAssessor() {
        public boolean isFailure(HttpResponse response) {
            return false;
        }
    });
    HttpResponse result = impl.execute(host, req, ctx);
    assertSame(result, resp);
}

From source file:com.autonomy.aci.client.services.impl.DocumentProcessorTest.java

@Test
public void testConvertACIResponseToDOMSAXException() throws AciErrorException, IOException {
    try {//from   w w w  .  ja  va  2 s . c  o  m
        // Setup with a proper XML response file...
        final BasicHttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
        response.setEntity(
                new InputStreamEntity(getClass().getResourceAsStream("/MalformedAciException.xml"), -1));

        // Set the AciResponseInputStream...
        final AciResponseInputStream stream = new AciResponseInputStreamImpl(response);

        // Process...
        processor.process(stream);
        fail("Should have raised an ProcessorException.");
    } catch (final ProcessorException pe) {
        // Check for the correct causes...
        assertThat("Cause not correct.", pe.getCause(), is(instanceOf(SAXException.class)));
    }
}

From source file:com.ksc.http.KscHttpClientTest.java

@SuppressWarnings({ "unchecked", "deprecation" })
@Test/*  w  ww .j a v a  2  s .  c o m*/
public void testRetryIOExceptionFromHandler() throws Exception {
    final IOException exception = new IOException("BOOM");

    HttpResponseHandler<KscWebServiceResponse<Object>> handler = EasyMock.createMock(HttpResponseHandler.class);

    EasyMock.expect(handler.needsConnectionLeftOpen()).andReturn(false).anyTimes();

    EasyMock.expect(handler.handle(EasyMock.<HttpResponse>anyObject())).andThrow(exception).times(2);

    EasyMock.replay(handler);

    BasicHttpEntity entity = new BasicHttpEntity();
    entity.setContent(new ByteArrayInputStream(new byte[0]));

    BasicHttpResponse response = new BasicHttpResponse(new ProtocolVersion("http", 1, 1), 200, "OK");
    response.setEntity(entity);

    EasyMock.reset(httpClient);

    EasyMock.expect(httpClient.getConnectionManager()).andReturn(null).anyTimes();

    EasyMock.expect(httpClient.execute(EasyMock.<HttpUriRequest>anyObject(), EasyMock.<HttpContext>anyObject()))
            .andReturn(response).times(2);

    EasyMock.replay(httpClient);

    ExecutionContext context = new ExecutionContext();

    Request<?> request = new DefaultRequest<Object>(null, "testsvc");
    request.setEndpoint(java.net.URI.create("http://testsvc.region.amazonaws.com"));
    request.setContent(new java.io.ByteArrayInputStream(new byte[0]));

    try {

        client.execute(request, handler, null, context);
        Assert.fail("No exception when request repeatedly fails!");

    } catch (KscClientException e) {
        Assert.assertSame(exception, e.getCause());
    }

    // Verify that we called execute 4 times.
    EasyMock.verify(httpClient);
}

From source file:com.gistlabs.mechanize.PageRequest.java

public HttpResponse consume(final HttpClient client, final HttpRequestBase request) throws Exception {
    if (!wasExecuted) {
        this.client = client;
        this.request = request;

        if (!request.getMethod().equalsIgnoreCase(httpMethod))
            throw new IllegalArgumentException(
                    String.format("Expected %s, but was %s", httpMethod, request.getMethod()));

        if (request.getURI().toString().equals(uri)) {
            HttpResponse response = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), 200, "OK");
            BasicHttpEntity entity = new BasicHttpEntity();
            if (contentLocation != null)
                response.addHeader(new BasicHeader("Content-Location", contentLocation));
            entity.setContentEncoding(charset);
            entity.setContentType(this.contentType);
            entity.setContent(this.body);
            response.setEntity(new BufferedHttpEntity(entity));

            assertParameters(request);//from   www. j  av  a2  s .  co  m
            assertHeaders(request);

            this.wasExecuted = true;
            return response;
        } else {
            assertEquals("URI of the next PageRequest does not match", uri, request.getURI().toString());
            return null;
        }
    } else
        throw new UnsupportedOperationException("Request already executed");
}

From source file:org.paolomoz.zehnkampf.utils.GenHTTPResponse.java

public HttpResponse setNotFound() throws UnsupportedEncodingException {
    HttpResponse response;//from   ww w  . j av a 2  s  .com
    response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_NOT_FOUND, "Not Found");
    String entityBody = "<html>" + "<head><title>404 - Not Found</title></head>"
            + "<body>Not Found</body></html>";
    HttpEntity entity = new StringEntity(entityBody);
    response.setEntity(entity);
    return response;
}

From source file:business.security.control.OwmClientTest.java

@Test
public void testAPPIDHeaderRequest() throws IOException, JSONException {
    final String appid = UUID.randomUUID().toString();
    HttpClient mockHttpClient = mock(HttpClient.class);
    when(mockHttpClient.execute(any(HttpGet.class))).then(new Answer<HttpResponse>() {
        @Override/*ww w . ja  va2s  . co  m*/
        public HttpResponse answer(InvocationOnMock invocation) throws Throwable {
            HttpGet httpGet = (HttpGet) invocation.getArguments()[0];
            Header[] headers = httpGet.getHeaders("x-api-key");
            assertNotNull(headers);
            assertEquals(1, headers.length);
            assertEquals(appid, headers[0].getValue());

            HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "Ok");
            response.setEntity(new StringEntity(TestData.CURRENT_WEATHER_AROUND_CITY_COORD));
            return response;
        }
    });

    OwmClient owm = new OwmClient(mockHttpClient);
    owm.setAPPID(appid);
    owm.currentWeatherAtCity(55f, 37f, 10);
}