Search in sources :

Example 26 with ContentChannel

use of com.yahoo.jdisc.handler.ContentChannel in project vespa by vespa-engine.

the class RequestHandlerTestDriver method sendRequest.

public MockResponseHandler sendRequest(String uri, HttpRequest.Method method, ByteBuffer body) {
    responseHandler = new MockResponseHandler();
    Request request = HttpRequest.newServerRequest(driver, URI.create(uri), method);
    // TODO: Add a method for accepting a Request instead
    request.context().put("contextVariable", 37);
    ContentChannel requestContent = request.connect(responseHandler);
    requestContent.write(body, null);
    requestContent.close(null);
    request.release();
    return responseHandler;
}
Also used : ContentChannel(com.yahoo.jdisc.handler.ContentChannel) ReadableContentChannel(com.yahoo.jdisc.handler.ReadableContentChannel) BufferedContentChannel(com.yahoo.jdisc.handler.BufferedContentChannel) HttpRequest(com.yahoo.jdisc.http.HttpRequest) Request(com.yahoo.jdisc.Request)

Example 27 with ContentChannel

use of com.yahoo.jdisc.handler.ContentChannel in project vespa by vespa-engine.

the class AccessControlRequestFilter method filter.

@Override
public void filter(DiscFilterRequest discFilterRequest, ResponseHandler responseHandler) {
    String origin = discFilterRequest.getHeader("Origin");
    if (!discFilterRequest.getMethod().equals(OPTIONS.name()))
        return;
    HttpResponse response = HttpResponse.newInstance(Response.Status.OK);
    if (allowedUrls.contains(origin))
        response.headers().add(ALLOW_ORIGIN_HEADER, origin);
    ACCESS_CONTROL_HEADERS.forEach((name, value) -> response.headers().add(name, value));
    ContentChannel cc = responseHandler.handleResponse(response);
    cc.close(null);
}
Also used : ContentChannel(com.yahoo.jdisc.handler.ContentChannel) HttpResponse(com.yahoo.jdisc.http.HttpResponse)

Example 28 with ContentChannel

use of com.yahoo.jdisc.handler.ContentChannel in project vespa by vespa-engine.

the class ConnectToHandlerTestCase method requireThatConnectToHandlerWorks.

@Test
public void requireThatConnectToHandlerWorks() {
    TestDriver driver = TestDriver.newSimpleApplicationInstanceWithoutOsgi();
    MyRequestHandler requestHandler = new MyRequestHandler(new MyContent());
    ContainerBuilder builder = driver.newContainerBuilder();
    builder.serverBindings().bind("http://host/*", requestHandler);
    driver.activateContainer(builder);
    Request request = new Request(driver, URI.create("http://host/path"));
    MyResponseHandler responseHandler = new MyResponseHandler();
    ContentChannel content = request.connect(responseHandler);
    request.release();
    assertNotNull(content);
    content.close(null);
    assertNotNull(requestHandler.handler);
    assertSame(request, requestHandler.request);
    requestHandler.handler.handleResponse(new Response(Response.Status.OK)).close(null);
    driver.close();
}
Also used : Response(com.yahoo.jdisc.Response) ContainerBuilder(com.yahoo.jdisc.application.ContainerBuilder) ContentChannel(com.yahoo.jdisc.handler.ContentChannel) Request(com.yahoo.jdisc.Request) TestDriver(com.yahoo.jdisc.test.TestDriver) Test(org.junit.Test)

Example 29 with ContentChannel

use of com.yahoo.jdisc.handler.ContentChannel in project vespa by vespa-engine.

the class ServerProviderConformanceTest method testRequestNondeterministicExceptionWithAsyncHandleResponse.

private <T extends ServerProvider, U, V> void testRequestNondeterministicExceptionWithAsyncHandleResponse(final Adapter<T, U, V> adapter, final Module... config) throws Throwable {
    runTest(adapter, Modules.combine(config), RequestType.WITHOUT_CONTENT, new TestRequestHandler() {

        @Override
        public ContentChannel handle(final Request request, final ResponseHandler handler) {
            callInOtherThread(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    try {
                        final ContentChannel out = handler.handleResponse(new Response(Response.Status.OK));
                        closeResponse(out);
                    } catch (Throwable ignored) {
                    }
                    return null;
                }
            });
            throw new ConformanceException();
        }
    });
}
Also used : Response(com.yahoo.jdisc.Response) ResponseHandler(com.yahoo.jdisc.handler.ResponseHandler) ContentChannel(com.yahoo.jdisc.handler.ContentChannel) Request(com.yahoo.jdisc.Request) Callable(java.util.concurrent.Callable)

Example 30 with ContentChannel

use of com.yahoo.jdisc.handler.ContentChannel in project vespa by vespa-engine.

the class ServerProviderConformanceTest method testRequestExceptionBeforeResponseWriteWithAsyncHandleResponse.

private <T extends ServerProvider, U, V> void testRequestExceptionBeforeResponseWriteWithAsyncHandleResponse(final Adapter<T, U, V> adapter, final Module... config) throws Throwable {
    runTest(adapter, Modules.combine(config), RequestType.WITHOUT_CONTENT, new TestRequestHandler() {

        @Override
        public ContentChannel handle(final Request request, final ResponseHandler handler) {
            final Event exceptionHandledByFramework = new Event();
            callInOtherThread(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    exceptionHandledByFramework.await();
                    try {
                        final ContentChannel out = handler.handleResponse(new Response(Response.Status.OK));
                        exceptionHandledByFramework.await();
                        writeResponse(out);
                        closeResponse(out);
                    } catch (Throwable ignored) {
                    }
                    return null;
                }
            });
            throw new ConformanceException(exceptionHandledByFramework);
        }
    });
}
Also used : Response(com.yahoo.jdisc.Response) ResponseHandler(com.yahoo.jdisc.handler.ResponseHandler) ContentChannel(com.yahoo.jdisc.handler.ContentChannel) Request(com.yahoo.jdisc.Request) Callable(java.util.concurrent.Callable)

Aggregations

ContentChannel (com.yahoo.jdisc.handler.ContentChannel)48 Test (org.junit.Test)31 Request (com.yahoo.jdisc.Request)10 Response (com.yahoo.jdisc.Response)10 TestDriver (com.yahoo.jdisc.test.TestDriver)10 ByteBuffer (java.nio.ByteBuffer)7 ResponseHandler (com.yahoo.jdisc.handler.ResponseHandler)6 HttpRequest (com.yahoo.jdisc.http.HttpRequest)4 ResourceReference (com.yahoo.jdisc.ResourceReference)3 RequestHandler (com.yahoo.jdisc.handler.RequestHandler)3 Callable (java.util.concurrent.Callable)3 ContainerBuilder (com.yahoo.jdisc.application.ContainerBuilder)2 BindingNotFoundException (com.yahoo.jdisc.handler.BindingNotFoundException)2 RequestDeniedException (com.yahoo.jdisc.handler.RequestDeniedException)2 NonWorkingRequest (com.yahoo.jdisc.test.NonWorkingRequest)2 AbstractRequestHandler (com.yahoo.jdisc.handler.AbstractRequestHandler)1 BufferedContentChannel (com.yahoo.jdisc.handler.BufferedContentChannel)1 ReadableContentChannel (com.yahoo.jdisc.handler.ReadableContentChannel)1 RequestDispatch (com.yahoo.jdisc.handler.RequestDispatch)1 HttpResponse (com.yahoo.jdisc.http.HttpResponse)1