Search in sources :

Example 26 with HttpRequest

use of com.yahoo.jdisc.http.HttpRequest in project vespa by vespa-engine.

the class FilteringRequestHandler method handleRequest.

@Override
public ContentChannel handleRequest(Request request, ResponseHandler originalResponseHandler) {
    Preconditions.checkArgument(request instanceof HttpRequest, "Expected HttpRequest, got " + request);
    Objects.requireNonNull(originalResponseHandler, "responseHandler");
    RequestFilter requestFilter = requestFilters.resolve(request.getUri());
    ResponseFilter responseFilter = responseFilters.resolve(request.getUri());
    // Not using request.connect() here - it adds logic for error handling that we'd rather leave to the framework.
    RequestHandler resolvedRequestHandler = request.container().resolveHandler(request);
    if (resolvedRequestHandler == null) {
        throw new BindingNotFoundException(request.getUri());
    }
    RequestHandler requestHandler = new ReferenceCountingRequestHandler(resolvedRequestHandler);
    ResponseHandler responseHandler;
    if (responseFilter != null) {
        responseHandler = new FilteringResponseHandler(originalResponseHandler, responseFilter, request);
    } else {
        responseHandler = originalResponseHandler;
    }
    if (requestFilter != null) {
        InterceptingResponseHandler interceptingResponseHandler = new InterceptingResponseHandler(responseHandler);
        requestFilter.filter(HttpRequest.class.cast(request), interceptingResponseHandler);
        if (interceptingResponseHandler.hasProducedResponse()) {
            return COMPLETING_CONTENT_CHANNEL;
        }
    }
    ContentChannel contentChannel = requestHandler.handleRequest(request, responseHandler);
    if (contentChannel == null) {
        throw new RequestDeniedException(request);
    }
    return contentChannel;
}
Also used : HttpRequest(com.yahoo.jdisc.http.HttpRequest) RequestHandler(com.yahoo.jdisc.handler.RequestHandler) AbstractRequestHandler(com.yahoo.jdisc.handler.AbstractRequestHandler) ResponseHandler(com.yahoo.jdisc.handler.ResponseHandler) RequestDeniedException(com.yahoo.jdisc.handler.RequestDeniedException) ContentChannel(com.yahoo.jdisc.handler.ContentChannel) ResponseFilter(com.yahoo.jdisc.http.filter.ResponseFilter) BindingNotFoundException(com.yahoo.jdisc.handler.BindingNotFoundException) RequestFilter(com.yahoo.jdisc.http.filter.RequestFilter)

Example 27 with HttpRequest

use of com.yahoo.jdisc.http.HttpRequest in project vespa by vespa-engine.

the class VipStatusHandlerTestCase method testFileNotFound.

@Test
public final void testFileNotFound() {
    final VipStatusConfig config = new VipStatusConfig(new VipStatusConfig.Builder().accessdisk(true).statusfile("/VipStatusHandlerTestCaseFileThatReallyReallyShouldNotExist").noSearchBackendsImpliesOutOfService(false));
    final VipStatusHandler handler = new VipStatusHandler(Executors.newCachedThreadPool(), config, metric);
    final NotFoundResponseHandler responseHandler = new NotFoundResponseHandler();
    final HttpRequest request = createRequest();
    final BufferedContentChannel requestContent = createChannel();
    handler.handleRequest(request, requestContent, responseHandler);
    final ByteBuffer b = responseHandler.channel.read();
    final byte[] asBytes = new byte[b.remaining()];
    b.get(asBytes);
    assertEquals(VipStatusHandler.StatusResponse.COULD_NOT_FIND_STATUS_FILE, Utf8.toString(asBytes));
}
Also used : HttpRequest(com.yahoo.jdisc.http.HttpRequest) VipStatusConfig(com.yahoo.container.core.VipStatusConfig) BufferedContentChannel(com.yahoo.jdisc.handler.BufferedContentChannel) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 28 with HttpRequest

use of com.yahoo.jdisc.http.HttpRequest in project vespa by vespa-engine.

the class VipStatusHandlerTestCase method testHandleRequest.

@Test
public final void testHandleRequest() {
    final VipStatusConfig config = new VipStatusConfig(new VipStatusConfig.Builder().accessdisk(false).noSearchBackendsImpliesOutOfService(false));
    final VipStatusHandler handler = new VipStatusHandler(Executors.newCachedThreadPool(), config, metric);
    final MockResponseHandler responseHandler = new MockResponseHandler();
    final HttpRequest request = createRequest();
    final BufferedContentChannel requestContent = createChannel();
    handler.handleRequest(request, requestContent, responseHandler);
    final ByteBuffer b = responseHandler.channel.read();
    final byte[] asBytes = new byte[b.remaining()];
    b.get(asBytes);
    assertEquals(VipStatusHandler.OK_MESSAGE, Utf8.toString(asBytes));
}
Also used : HttpRequest(com.yahoo.jdisc.http.HttpRequest) VipStatusConfig(com.yahoo.container.core.VipStatusConfig) BufferedContentChannel(com.yahoo.jdisc.handler.BufferedContentChannel) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 29 with HttpRequest

use of com.yahoo.jdisc.http.HttpRequest in project vespa by vespa-engine.

the class VipStatusHandlerTestCase method createRequest.

public static HttpRequest createRequest(String uri) {
    HttpRequest request = null;
    try {
        request = HttpRequest.newClientRequest(new com.yahoo.jdisc.Request(new MockCurrentContainer(), new URI(uri)), new URI(uri), HttpRequest.Method.GET, HttpRequest.Version.HTTP_1_1);
        request.setRemoteAddress(new InetSocketAddress(0));
    } catch (URISyntaxException e) {
        fail("Illegal URI string in test?");
    }
    return request;
}
Also used : HttpRequest(com.yahoo.jdisc.http.HttpRequest) InetSocketAddress(java.net.InetSocketAddress) HttpRequest(com.yahoo.jdisc.http.HttpRequest) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 30 with HttpRequest

use of com.yahoo.jdisc.http.HttpRequest in project vespa by vespa-engine.

the class DiscFilterResponseTest method newRequest.

private static HttpRequest newRequest(URI uri, HttpRequest.Method method, HttpRequest.Version version) {
    InetSocketAddress address = new InetSocketAddress("localhost", 69);
    TestDriver driver = TestDriver.newSimpleApplicationInstanceWithoutOsgi();
    driver.activateContainer(driver.newContainerBuilder());
    HttpRequest request = HttpRequest.newServerRequest(driver, uri, method, version, address);
    request.release();
    assertTrue(driver.close());
    return request;
}
Also used : HttpRequest(com.yahoo.jdisc.http.HttpRequest) InetSocketAddress(java.net.InetSocketAddress) TestDriver(com.yahoo.jdisc.test.TestDriver)

Aggregations

HttpRequest (com.yahoo.jdisc.http.HttpRequest)48 Test (org.testng.annotations.Test)36 URI (java.net.URI)27 ResponseHandler (com.yahoo.jdisc.handler.ResponseHandler)7 HttpResponse (com.yahoo.jdisc.http.HttpResponse)7 RequestFilter (com.yahoo.jdisc.http.filter.RequestFilter)5 VipStatusConfig (com.yahoo.container.core.VipStatusConfig)4 BufferedContentChannel (com.yahoo.jdisc.handler.BufferedContentChannel)4 ContentChannel (com.yahoo.jdisc.handler.ContentChannel)4 Cookie (com.yahoo.jdisc.http.Cookie)4 InetSocketAddress (java.net.InetSocketAddress)4 ByteBuffer (java.nio.ByteBuffer)4 Test (org.junit.Test)4 Response (com.yahoo.jdisc.Response)2 TestDriver (com.yahoo.jdisc.test.TestDriver)2 HeaderFields (com.yahoo.jdisc.HeaderFields)1 ResourceReference (com.yahoo.jdisc.ResourceReference)1 AbstractRequestHandler (com.yahoo.jdisc.handler.AbstractRequestHandler)1 BindingNotFoundException (com.yahoo.jdisc.handler.BindingNotFoundException)1 FastContentWriter (com.yahoo.jdisc.handler.FastContentWriter)1