Search in sources :

Example 1 with MethodNotSupportedException

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);
    }
}
Also used : ProtocolException(org.apache.hc.core5.http.ProtocolException) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) HttpResponse(org.apache.hc.core5.http.HttpResponse) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) ByteBuffer(java.nio.ByteBuffer) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) MethodNotSupportedException(org.apache.hc.core5.http.MethodNotSupportedException) BasicEntityDetails(org.apache.hc.core5.http.impl.BasicEntityDetails) BasicHeader(org.apache.hc.core5.http.message.BasicHeader)

Example 2 with MethodNotSupportedException

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();
}
Also used : ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) ClassicHttpRequest(org.apache.hc.core5.http.ClassicHttpRequest) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) HttpCoreContext(org.apache.hc.core5.http.protocol.HttpCoreContext) MethodNotSupportedException(org.apache.hc.core5.http.MethodNotSupportedException) Test(org.junit.jupiter.api.Test)

Example 3 with MethodNotSupportedException

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);
    }
}
Also used : StringEntity(org.apache.hc.core5.http.io.entity.StringEntity) ByteArrayEntity(org.apache.hc.core5.http.io.entity.ByteArrayEntity) MethodNotSupportedException(org.apache.hc.core5.http.MethodNotSupportedException)

Aggregations

MethodNotSupportedException (org.apache.hc.core5.http.MethodNotSupportedException)3 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 ByteBuffer (java.nio.ByteBuffer)1 ClassicHttpRequest (org.apache.hc.core5.http.ClassicHttpRequest)1 ClassicHttpResponse (org.apache.hc.core5.http.ClassicHttpResponse)1 HttpResponse (org.apache.hc.core5.http.HttpResponse)1 ProtocolException (org.apache.hc.core5.http.ProtocolException)1 BasicEntityDetails (org.apache.hc.core5.http.impl.BasicEntityDetails)1 ByteArrayEntity (org.apache.hc.core5.http.io.entity.ByteArrayEntity)1 StringEntity (org.apache.hc.core5.http.io.entity.StringEntity)1 BasicClassicHttpRequest (org.apache.hc.core5.http.message.BasicClassicHttpRequest)1 BasicClassicHttpResponse (org.apache.hc.core5.http.message.BasicClassicHttpResponse)1 BasicHeader (org.apache.hc.core5.http.message.BasicHeader)1 BasicHttpResponse (org.apache.hc.core5.http.message.BasicHttpResponse)1 HttpCoreContext (org.apache.hc.core5.http.protocol.HttpCoreContext)1 Test (org.junit.jupiter.api.Test)1