Search in sources :

Example 1 with Response

use of com.yahoo.processing.Response in project vespa by vespa-engine.

the class Processing method doProcessAndRender.

@Override
protected ListenableFuture<Boolean> doProcessAndRender(ComponentSpecification chainSpec, Request request, Renderer<Response> renderer, ByteArrayOutputStream stream) throws IOException {
    Execution execution = handler.createExecution(getChain(chainSpec), request);
    Response response = execution.process(request);
    return renderer.render(stream, response, execution, request);
}
Also used : Response(com.yahoo.processing.Response) Execution(com.yahoo.processing.execution.Execution)

Example 2 with Response

use of com.yahoo.processing.Response in project vespa by vespa-engine.

the class Rot13Processor method process.

@SuppressWarnings("unchecked")
@Override
public Response process(Request request, Execution execution) {
    Object fooObj = request.properties().get("title");
    Response response = new Response(request);
    if (fooObj != null) {
        response.data().add(new ProcessorLibrary.StringData(request, rot13(fooObj.toString())));
    }
    return response;
}
Also used : Response(com.yahoo.processing.Response) ProcessorLibrary(com.yahoo.processing.test.ProcessorLibrary)

Example 3 with Response

use of com.yahoo.processing.Response in project vespa by vespa-engine.

the class MockUserDatabaseClientTest method testClientExampleProcessor.

@Test
public void testClientExampleProcessor() {
    Request request = null;
    try {
        Chain<Processor> chain = new Chain<>("default", new MockUserDatabaseClient());
        setupJDisc(Collections.singletonList(chain));
        request = createRequest();
        Response response = Execution.createRoot(chain, 0, Execution.Environment.createEmpty()).process(request);
        MockUserDatabaseClient.User user = (MockUserDatabaseClient.User) response.data().request().properties().get("User");
        assertNotNull(user);
        assertEquals("foo", user.getId());
    } finally {
        release(request);
    }
}
Also used : Response(com.yahoo.processing.Response) Chain(com.yahoo.component.chain.Chain) Processor(com.yahoo.processing.Processor) Request(com.yahoo.processing.Request) HttpRequest(com.yahoo.jdisc.http.HttpRequest) Test(org.junit.Test)

Example 4 with Response

use of com.yahoo.processing.Response in project vespa by vespa-engine.

the class StreamingTestCase method testStreamingData.

/**
 * Tests adding a chain which is called every time new data is added to a data list
 */
@SuppressWarnings("unchecked")
@Test
public void testStreamingData() throws InterruptedException, ExecutionException, TimeoutException {
    // Set up
    StreamProcessor streamProcessor = new StreamProcessor();
    Chain<Processor> streamProcessing = new Chain<Processor>(streamProcessor);
    ProcessorLibrary.FutureDataSource futureDataSource = new ProcessorLibrary.FutureDataSource();
    Chain<Processor> main = new Chain<>(new ProcessorLibrary.DataCounter(), new ProcessorLibrary.StreamProcessingInitiator(streamProcessing), futureDataSource);
    // Execute
    Request request = new Request();
    Response response = Execution.createRoot(main, 0, Execution.Environment.createEmpty()).process(request);
    IncomingData incomingData = futureDataSource.incomingData.get(0);
    // State prior to receiving any additional data
    assertEquals(1, response.data().asList().size());
    assertEquals("Data count: 0", response.data().get(0).toString());
    assertEquals("Add data listener invoked also for DataCounter", 1, streamProcessor.invocationCount);
    assertEquals("Initial data count", 1, response.data().asList().size());
    // add first data - we have no listener so the data is held in the incoming buffer
    incomingData.add(new ProcessorLibrary.StringData(request, "d1"));
    assertEquals("Data add listener not invoked as we are not listening on new data yet", 1, streamProcessor.invocationCount);
    assertEquals("New data is not consumed", 1, response.data().asList().size());
    // start listening on incoming data - this is what a renderer will do
    incomingData.addNewDataListener(new MockNewDataListener(incomingData), MoreExecutors.directExecutor());
    assertEquals("We got a data add event for the data which was already added", 2, streamProcessor.invocationCount);
    assertEquals("New data is consumed", 2, response.data().asList().size());
    incomingData.add(new ProcessorLibrary.StringData(request, "d2"));
    assertEquals("We are now getting data add events each time", 3, streamProcessor.invocationCount);
    assertEquals("New data is consumed", 3, response.data().asList().size());
    incomingData.addLast(new ProcessorLibrary.StringData(request, "d3"));
    assertEquals("We are getting data add events also the last time", 4, streamProcessor.invocationCount);
    assertEquals("New data is consumed", 4, response.data().asList().size());
    // no-op here
    response.data().complete().get(1000, TimeUnit.MILLISECONDS);
    assertEquals("d1", response.data().get(1).toString().toString());
    assertEquals("d2", response.data().get(2).toString().toString());
    assertEquals("d3", response.data().get(3).toString().toString());
}
Also used : Chain(com.yahoo.component.chain.Chain) Processor(com.yahoo.processing.Processor) Request(com.yahoo.processing.Request) ProcessorLibrary(com.yahoo.processing.test.ProcessorLibrary) Response(com.yahoo.processing.Response) IncomingData(com.yahoo.processing.response.IncomingData) Test(org.junit.Test)

Example 5 with Response

use of com.yahoo.processing.Response in project vespa by vespa-engine.

the class Execution method process.

/**
 * Calls process on the next processor in this chain. If there is no next, an empty response is returned.
 */
public Response process(Request request) {
    Processor processor = next();
    if (processor == null)
        return defaultResponse(request);
    Response response = null;
    try {
        nextProcessor();
        onInvoking(request, processor);
        response = processor.process(request, this);
        if (response == null)
            throw new NullPointerException(processor + " returned null, not a Response object");
        return response;
    } finally {
        previousProcessor();
        onReturning(request, processor, response);
    }
}
Also used : Response(com.yahoo.processing.Response) Processor(com.yahoo.processing.Processor)

Aggregations

Response (com.yahoo.processing.Response)20 Request (com.yahoo.processing.Request)12 Processor (com.yahoo.processing.Processor)11 Test (org.junit.Test)11 Chain (com.yahoo.component.chain.Chain)10 Execution (com.yahoo.processing.execution.Execution)4 DataList (com.yahoo.processing.response.DataList)3 FutureResponse (com.yahoo.processing.response.FutureResponse)2 ProcessorLibrary (com.yahoo.processing.test.ProcessorLibrary)2 ArrayList (java.util.ArrayList)2 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 ContentChannelOutputStream (com.yahoo.container.jdisc.ContentChannelOutputStream)1 HttpRequest (com.yahoo.container.jdisc.HttpRequest)1 HttpResponse (com.yahoo.container.jdisc.HttpResponse)1 HttpRequest (com.yahoo.jdisc.http.HttpRequest)1 AsyncExecution (com.yahoo.processing.execution.AsyncExecution)1 ArrayDataList (com.yahoo.processing.response.ArrayDataList)1 IncomingData (com.yahoo.processing.response.IncomingData)1 AsyncDataProcessingInitiator (com.yahoo.processing.test.documentation.AsyncDataProcessingInitiator)1 AsyncDataProducer (com.yahoo.processing.test.documentation.AsyncDataProducer)1