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);
}
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;
}
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);
}
}
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());
}
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);
}
}
Aggregations