Search in sources :

Example 1 with ResourceReference

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

the class FormPostRequestHandler method close.

@SuppressWarnings("try")
@Override
public void close(final CompletionHandler completionHandler) {
    try (final ResourceReference ref = requestReference) {
        final byte[] requestContentBytes = accumulatedRequestContent.toByteArray();
        final String content = new String(requestContentBytes, contentCharset);
        completionHandler.completed();
        final Map<String, List<String>> parameterMap = parseFormParameters(content);
        mergeParameters(parameterMap, request.parameters());
        final ContentChannel contentChannel = delegateHandler.handleRequest(request, responseHandler);
        if (contentChannel != null) {
            if (!removeBody) {
                final ByteBuffer byteBuffer = ByteBuffer.wrap(requestContentBytes);
                contentChannel.write(byteBuffer, NOOP_COMPLETION_HANDLER);
            }
            contentChannel.close(NOOP_COMPLETION_HANDLER);
        }
    }
}
Also used : ContentChannel(com.yahoo.jdisc.handler.ContentChannel) LinkedList(java.util.LinkedList) List(java.util.List) ResourceReference(com.yahoo.jdisc.ResourceReference) ByteBuffer(java.nio.ByteBuffer)

Example 2 with ResourceReference

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

the class FilterTestCase method requireThatResponseFilterChainRetainsFilters.

@Test
public void requireThatResponseFilterChainRetainsFilters() {
    final ResponseFilter responseFilter1 = mock(ResponseFilter.class);
    final ResponseFilter responseFilter2 = mock(ResponseFilter.class);
    verify(responseFilter1, never()).refer();
    verify(responseFilter2, never()).refer();
    final ResourceReference reference1 = mock(ResourceReference.class);
    final ResourceReference reference2 = mock(ResourceReference.class);
    when(responseFilter1.refer()).thenReturn(reference1);
    when(responseFilter2.refer()).thenReturn(reference2);
    final ResponseFilter chain = ResponseFilterChain.newInstance(responseFilter1, responseFilter2);
    verify(responseFilter1, times(1)).refer();
    verify(responseFilter2, times(1)).refer();
    verify(reference1, never()).close();
    verify(reference2, never()).close();
    chain.release();
    verify(reference1, times(1)).close();
    verify(reference2, times(1)).close();
}
Also used : ResponseFilter(com.yahoo.jdisc.http.filter.ResponseFilter) ResourceReference(com.yahoo.jdisc.ResourceReference) Test(org.testng.annotations.Test)

Example 3 with ResourceReference

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

the class ReferenceCountingRequestHandler method handleRequest.

@Override
public ContentChannel handleRequest(Request request, ResponseHandler responseHandler) {
    try (final ResourceReference requestReference = request.refer()) {
        ContentChannel contentChannel;
        final ReferenceCountingResponseHandler referenceCountingResponseHandler = new ReferenceCountingResponseHandler(request, new NullContentResponseHandler(responseHandler));
        try {
            contentChannel = delegate.handleRequest(request, referenceCountingResponseHandler);
            Objects.requireNonNull(contentChannel, "contentChannel");
        } catch (Throwable t) {
            try {
                // The response handler might never be invoked, due to the exception thrown from handleRequest().
                referenceCountingResponseHandler.unrefer();
            } catch (Throwable thrownFromUnrefer) {
                log.log(Level.WARNING, "Unexpected problem", thrownFromUnrefer);
            }
            throw t;
        }
        return new ReferenceCountingContentChannel(request, contentChannel);
    }
}
Also used : ContentChannel(com.yahoo.jdisc.handler.ContentChannel) ResourceReference(com.yahoo.jdisc.ResourceReference)

Example 4 with ResourceReference

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

the class ResourcePoolTestCase method requireThatResourcesAreReleasedOnDestroy.

@Test
public void requireThatResourcesAreReleasedOnDestroy() {
    TestDriver driver = TestDriver.newSimpleApplicationInstanceWithoutOsgi();
    ResourcePool pool = new ResourcePool(driver.newContainerBuilder());
    MyResource foo = pool.add(new MyResource());
    MyResource bar = pool.add(new MyResource());
    MyResource baz = pool.add(new MyResource());
    assertEquals(1, pool.retainCount());
    assertEquals(1, foo.retainCount());
    assertEquals(1, bar.retainCount());
    assertEquals(1, baz.retainCount());
    final ResourceReference secondPoolReference = pool.refer();
    assertEquals(2, pool.retainCount());
    assertEquals(1, foo.retainCount());
    assertEquals(1, bar.retainCount());
    assertEquals(1, baz.retainCount());
    secondPoolReference.close();
    assertEquals(1, pool.retainCount());
    assertEquals(1, foo.retainCount());
    assertEquals(1, bar.retainCount());
    assertEquals(1, baz.retainCount());
    pool.release();
    assertEquals(0, pool.retainCount());
    assertEquals(0, foo.retainCount());
    assertEquals(0, bar.retainCount());
    assertEquals(0, baz.retainCount());
    assertTrue(driver.close());
}
Also used : ResourceReference(com.yahoo.jdisc.ResourceReference) TestDriver(com.yahoo.jdisc.test.TestDriver) Test(org.junit.Test)

Example 5 with ResourceReference

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

the class ClientTestDriver method newClientRequest.

public Request newClientRequest(Message msg) {
    msg.setRoute(Route.parse(server.connectionSpec()));
    if (msg.getTrace().getLevel() == 0) {
        msg.getTrace().setLevel(9);
    }
    final Request parent = newServerRequest();
    try (final ResourceReference ref = References.fromResource(parent)) {
        return new MbusRequest(parent, URI.create("mbus://remotehost/"), msg);
    }
}
Also used : MbusRequest(com.yahoo.messagebus.jdisc.MbusRequest) MbusRequest(com.yahoo.messagebus.jdisc.MbusRequest) Request(com.yahoo.jdisc.Request) ResourceReference(com.yahoo.jdisc.ResourceReference)

Aggregations

ResourceReference (com.yahoo.jdisc.ResourceReference)8 ContentChannel (com.yahoo.jdisc.handler.ContentChannel)3 TestDriver (com.yahoo.jdisc.test.TestDriver)2 Test (org.junit.Test)2 Test (org.testng.annotations.Test)2 Request (com.yahoo.jdisc.Request)1 HttpRequest (com.yahoo.jdisc.http.HttpRequest)1 RequestFilter (com.yahoo.jdisc.http.filter.RequestFilter)1 ResponseFilter (com.yahoo.jdisc.http.filter.ResponseFilter)1 MbusRequest (com.yahoo.messagebus.jdisc.MbusRequest)1 ManualClock (com.yahoo.test.ManualClock)1 ByteBuffer (java.nio.ByteBuffer)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 ServletInputStream (javax.servlet.ServletInputStream)1