use of org.apache.hc.core5.http.message.BasicClassicHttpResponse in project httpcomponents-core by apache.
the class AbstractAsyncServerAuthFilter method handle.
@Override
public final AsyncDataConsumer handle(final HttpRequest request, final EntityDetails entityDetails, final HttpContext context, final AsyncFilterChain.ResponseTrigger responseTrigger, final AsyncFilterChain chain) throws HttpException, IOException {
final Header h = request.getFirstHeader(HttpHeaders.AUTHORIZATION);
final T challengeResponse = h != null ? parseChallengeResponse(h.getValue(), context) : null;
final URIAuthority authority = request.getAuthority();
final String requestUri = request.getRequestUri();
final boolean authenticated = authenticate(challengeResponse, authority, requestUri, context);
final Header expect = request.getFirstHeader(HttpHeaders.EXPECT);
final boolean expectContinue = expect != null && HeaderElements.CONTINUE.equalsIgnoreCase(expect.getValue());
if (authenticated) {
if (expectContinue) {
responseTrigger.sendInformation(new BasicClassicHttpResponse(HttpStatus.SC_CONTINUE));
}
return chain.proceed(request, entityDetails, context, responseTrigger);
}
final HttpResponse unauthorized = new BasicHttpResponse(HttpStatus.SC_UNAUTHORIZED);
unauthorized.addHeader(HttpHeaders.WWW_AUTHENTICATE, generateChallenge(challengeResponse, authority, requestUri, context));
final AsyncEntityProducer responseContentProducer = generateResponseContent(unauthorized);
if (respondImmediately || expectContinue || entityDetails == null) {
responseTrigger.submitResponse(unauthorized, responseContentProducer);
return null;
}
return new AsyncDataConsumer() {
@Override
public void updateCapacity(final CapacityChannel capacityChannel) throws IOException {
capacityChannel.update(Integer.MAX_VALUE);
}
@Override
public void consume(final ByteBuffer src) throws IOException {
}
@Override
public void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
responseTrigger.submitResponse(unauthorized, responseContentProducer);
}
@Override
public void releaseResources() {
if (responseContentProducer != null) {
responseContentProducer.releaseResources();
}
}
};
}
use of org.apache.hc.core5.http.message.BasicClassicHttpResponse in project httpcomponents-core by apache.
the class TestDefaultBHttpServerConnection method testWriteResponseEntityWithContentLength.
@Test
public void testWriteResponseEntityWithContentLength() throws Exception {
final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
Mockito.when(socket.getOutputStream()).thenReturn(outStream);
conn.bind(socket);
Assertions.assertEquals(0, conn.getEndpointDetails().getResponseCount());
final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
response.addHeader("Server", "test");
response.addHeader("Content-Length", "3");
response.setEntity(new StringEntity("123", ContentType.TEXT_PLAIN));
conn.sendResponseHeader(response);
conn.sendResponseEntity(response);
conn.flush();
Assertions.assertEquals(1, conn.getEndpointDetails().getResponseCount());
final String s = new String(outStream.toByteArray(), StandardCharsets.US_ASCII);
Assertions.assertEquals("HTTP/1.1 200 OK\r\nServer: test\r\nContent-Length: 3\r\n\r\n123", s);
}
use of org.apache.hc.core5.http.message.BasicClassicHttpResponse in project httpcomponents-core by apache.
the class TestDefaultBHttpServerConnection method testWriteResponseEntityIdentity.
@Test
public void testWriteResponseEntityIdentity() throws Exception {
final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
Mockito.when(socket.getOutputStream()).thenReturn(outStream);
conn.bind(socket);
Assertions.assertEquals(0, conn.getEndpointDetails().getResponseCount());
final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
response.addHeader("Server", "test");
response.addHeader("Transfer-Encoding", "identity");
response.setEntity(new StringEntity("123", ContentType.TEXT_PLAIN));
conn.sendResponseHeader(response);
Assertions.assertThrows(NotImplementedException.class, () -> conn.sendResponseEntity(response));
conn.flush();
}
use of org.apache.hc.core5.http.message.BasicClassicHttpResponse in project httpcomponents-core by apache.
the class TestHttpRequestExecutor method testExecutionNoResponseBody.
@Test
public void testExecutionNoResponseBody() throws Exception {
final HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
final HttpClientConnection conn = Mockito.mock(HttpClientConnection.class);
final HttpRequestExecutor executor = new HttpRequestExecutor();
final HttpCoreContext context = HttpCoreContext.create();
final ClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, "/");
executor.preProcess(request, httprocessor, context);
Mockito.verify(httprocessor).process(request, request.getEntity(), context);
Mockito.when(conn.receiveResponseHeader()).thenReturn(new BasicClassicHttpResponse(204, "OK"));
final ClassicHttpResponse response = executor.execute(request, conn, context);
Mockito.verify(conn).sendRequestHeader(request);
Mockito.verify(conn).flush();
Mockito.verify(conn).receiveResponseHeader();
Mockito.verify(conn, Mockito.never()).receiveResponseEntity(response);
executor.postProcess(response, httprocessor, context);
Mockito.verify(httprocessor).process(response, response.getEntity(), context);
}
use of org.apache.hc.core5.http.message.BasicClassicHttpResponse in project httpcomponents-core by apache.
the class TestHttpRequestExecutor method testExecutionSkipIntermediateResponses.
@Test
public void testExecutionSkipIntermediateResponses() throws Exception {
final HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
final HttpClientConnection conn = Mockito.mock(HttpClientConnection.class);
final HttpRequestExecutor executor = new HttpRequestExecutor();
final HttpCoreContext context = HttpCoreContext.create();
final ClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, "/");
executor.preProcess(request, httprocessor, context);
Mockito.verify(httprocessor).process(request, request.getEntity(), context);
Mockito.when(conn.receiveResponseHeader()).thenReturn(new BasicClassicHttpResponse(100, "Continue"), new BasicClassicHttpResponse(110, "Huh?"), new BasicClassicHttpResponse(111, "Huh?"), new BasicClassicHttpResponse(200, "OK"));
final HttpResponseInformationCallback callback = Mockito.mock(HttpResponseInformationCallback.class);
final ClassicHttpResponse response = executor.execute(request, conn, callback, context);
Mockito.verify(conn).sendRequestHeader(request);
Mockito.verify(conn).flush();
Mockito.verify(conn, Mockito.times(4)).receiveResponseHeader();
Mockito.verify(conn, Mockito.times(1)).receiveResponseEntity(response);
final ArgumentCaptor<HttpResponse> responseCaptor = ArgumentCaptor.forClass(HttpResponse.class);
Mockito.verify(callback, Mockito.times(2)).execute(responseCaptor.capture(), ArgumentMatchers.eq(conn), ArgumentMatchers.eq(context));
final List<HttpResponse> infos = responseCaptor.getAllValues();
Assertions.assertNotNull(infos);
Assertions.assertEquals(2, infos.size());
final HttpResponse info1 = infos.get(0);
Assertions.assertNotNull(info1);
Assertions.assertEquals(110, info1.getCode());
final HttpResponse info2 = infos.get(1);
Assertions.assertNotNull(info2);
Assertions.assertEquals(111, info2.getCode());
executor.postProcess(response, httprocessor, context);
Mockito.verify(httprocessor).process(response, response.getEntity(), context);
Assertions.assertSame(request, context.getRequest());
Assertions.assertSame(response, context.getResponse());
}
Aggregations