use of org.apache.hc.core5.http.MethodNotSupportedException in project httpcomponents-core by apache.
the class ReactiveRandomProcessor method processRequest.
@Override
public void processRequest(final HttpRequest request, final EntityDetails entityDetails, final ResponseChannel responseChannel, final HttpContext context, final Publisher<ByteBuffer> requestBody, final Callback<Publisher<ByteBuffer>> responseBodyCallback) throws HttpException, IOException {
final String method = request.getMethod();
if (!"GET".equalsIgnoreCase(method) && !"HEAD".equalsIgnoreCase(method) && !"POST".equalsIgnoreCase(method) && !"PUT".equalsIgnoreCase(method)) {
throw new MethodNotSupportedException(method + " not supported by " + getClass().getName());
}
final URI uri;
try {
uri = request.getUri();
} catch (final URISyntaxException ex) {
throw new ProtocolException(ex.getMessage(), ex);
}
final String path = uri.getPath();
final int slash = path.lastIndexOf('/');
if (slash != -1) {
final String payload = path.substring(slash + 1);
final long n;
if (!payload.isEmpty()) {
try {
n = Long.parseLong(payload);
} catch (final NumberFormatException ex) {
throw new ProtocolException("Invalid request path: " + path);
}
} else {
// random length, but make sure at least something is sent
n = 1 + (int) (Math.random() * 79.0);
}
if (new BasicHeader(HttpHeaders.EXPECT, HeaderElements.CONTINUE).equals(request.getHeader(HttpHeaders.EXPECT))) {
responseChannel.sendInformation(new BasicHttpResponse(100), context);
}
final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_OK);
final Flowable<ByteBuffer> stream = ReactiveTestUtils.produceStream(n);
final String hash = ReactiveTestUtils.getStreamHash(n);
response.addHeader("response-hash-code", hash);
final BasicEntityDetails basicEntityDetails = new BasicEntityDetails(n, ContentType.APPLICATION_OCTET_STREAM);
responseChannel.sendResponse(response, basicEntityDetails, context);
responseBodyCallback.execute(stream);
} else {
throw new ProtocolException("Invalid request path: " + path);
}
}
use of org.apache.hc.core5.http.MethodNotSupportedException in project httpcomponents-core by apache.
the class TestHttpService method testMethodNotSupported.
@Test
public void testMethodNotSupported() throws Exception {
final HttpCoreContext context = HttpCoreContext.create();
final ClassicHttpRequest request = new BasicClassicHttpRequest("whatever", "/");
Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
Mockito.when(responseFactory.newHttpResponse(200)).thenReturn(response);
Mockito.when(handlerResolver.resolve(request, context)).thenReturn(requestHandler);
Mockito.doThrow(new MethodNotSupportedException("whatever")).when(requestHandler).handle(request, response, context);
httpservice.handleRequest(conn, context);
final ArgumentCaptor<ClassicHttpResponse> responseCaptor = ArgumentCaptor.forClass(ClassicHttpResponse.class);
Mockito.verify(conn).sendResponseHeader(responseCaptor.capture());
final ClassicHttpResponse error = responseCaptor.getValue();
Assertions.assertNotNull(error);
Assertions.assertSame(request, context.getRequest());
Assertions.assertEquals(HttpStatus.SC_NOT_IMPLEMENTED, error.getCode());
Mockito.verify(httprocessor).process(error, error.getEntity(), context);
Mockito.verify(conn).sendResponseHeader(error);
Mockito.verify(conn).sendResponseEntity(error);
Mockito.verify(conn).close();
}
use of org.apache.hc.core5.http.MethodNotSupportedException in project JAuswertung by dennisfabri.
the class DPRequestHandler method handle.
@Override
public void handle(ClassicHttpRequest request, ClassicHttpResponse response, HttpContext context) throws HttpException, IOException {
String method = request.getMethod().toUpperCase(Locale.ENGLISH);
if (!(method.equals("GET") || method.equals("POST"))) {
throw new MethodNotSupportedException(method + " method not supported");
}
String target = request.getRequestUri();
if (dp.knowsFile(target)) {
DataProducer producer = new DataProducer(dp, target);
response.setCode(HttpStatus.SC_OK);
byte[] data = producer.writeTo();
ByteArrayEntity body = new ByteArrayEntity(data, producer.getContentType());
response.setEntity(body);
} else {
response.setCode(HttpStatus.SC_NOT_FOUND);
StringEntity body = new StringEntity("<html><body><h1>Page not found</h1></body></html>", ContentType.TEXT_HTML);
response.setEntity(body);
}
}
Aggregations