Search in sources :

Example 1 with Response

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

the class MockDispatchDocproc method process.

@Override
public Progress process(Processing processing) {
    for (DocumentOperation op : processing.getDocumentOperations()) {
        PutDocumentMessage message = new PutDocumentMessage((DocumentPut) op);
        ListenableFuture<Response> future = createRequest(message).dispatch();
        try {
            responses.add(future.get());
        } catch (ExecutionException | InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
    return Progress.DONE;
}
Also used : PutDocumentMessage(com.yahoo.documentapi.messagebus.protocol.PutDocumentMessage) Response(com.yahoo.jdisc.Response) DocumentOperation(com.yahoo.document.DocumentOperation) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with Response

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

the class ProcessingHandlerTestCase method testProcessorSetsResponseHeaders.

@Test
public void testProcessorSetsResponseHeaders() throws InterruptedException {
    ProcessingTestDriver.MockResponseHandler responseHandler = null;
    try {
        Map<String, List<String>> responseHeaders = new HashMap<>();
        responseHeaders.put("foo", Collections.singletonList("fooValue"));
        responseHeaders.put("bar", Arrays.asList(new String[] { "barValue", "bazValue" }));
        Map<String, List<String>> otherResponseHeaders = new HashMap<>();
        otherResponseHeaders.put("foo", Collections.singletonList("fooValue2"));
        otherResponseHeaders.put("bax", Collections.singletonList("baxValue"));
        List<Chain<Processor>> chains = new ArrayList<>();
        chains.add(new Chain<Processor>("default", new ResponseHeaderSetter(responseHeaders), new ResponseHeaderSetter(otherResponseHeaders)));
        driver = new ProcessingTestDriver(chains);
        responseHandler = driver.sendRequest("http://localhost/?chain=default").awaitResponse();
        Response response = responseHandler.getResponse();
        assertEquals("[fooValue2, fooValue]", response.headers().get("foo").toString());
        assertEquals("[barValue, bazValue]", response.headers().get("bar").toString());
        assertEquals("[baxValue]", response.headers().get("bax").toString());
        assertEquals("ResponseHeaders are not rendered", "{\"datalist\":[]}", responseHandler.read());
    } finally {
        if (responseHandler != null)
            responseHandler.readAll();
    }
}
Also used : Chain(com.yahoo.component.chain.Chain) Processor(com.yahoo.processing.Processor) Response(com.yahoo.jdisc.Response) Test(org.junit.Test)

Example 3 with Response

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

the class StateHandlerTest method requestAsString.

private String requestAsString(String requestUri) throws Exception {
    final BufferedContentChannel content = new BufferedContentChannel();
    Response response = driver.dispatchRequest(requestUri, new ResponseHandler() {

        @Override
        public ContentChannel handleResponse(Response response) {
            return content;
        }
    }).get(60, TimeUnit.SECONDS);
    assertNotNull(response);
    assertEquals(Response.Status.OK, response.getStatus());
    StringBuilder str = new StringBuilder();
    Reader in = new InputStreamReader(content.toStream(), StandardCharsets.UTF_8);
    for (int c; (c = in.read()) != -1; ) {
        str.append((char) c);
    }
    return str.toString();
}
Also used : Response(com.yahoo.jdisc.Response) ResponseHandler(com.yahoo.jdisc.handler.ResponseHandler) InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedContentChannel(com.yahoo.jdisc.handler.BufferedContentChannel)

Example 4 with Response

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

the class SecurityFilterUtils method sendErrorResponse.

public static void sendErrorResponse(ResponseHandler responseHandler, int statusCode, String message) {
    Response response = new Response(statusCode);
    response.headers().put("Content-Type", "application/json");
    ObjectNode errorMessage = mapper.createObjectNode();
    errorMessage.put("message", message);
    try (FastContentWriter writer = ResponseDispatch.newInstance(response).connectFastWriter(responseHandler)) {
        writer.write(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(errorMessage));
    } catch (JsonProcessingException e) {
        throw new RuntimeException(e);
    }
}
Also used : Response(com.yahoo.jdisc.Response) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) FastContentWriter(com.yahoo.jdisc.handler.FastContentWriter) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 5 with Response

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

the class MbusRequestHandlerTestCase method requireThatHandlerCanRespondInSameThread.

@Test
public void requireThatHandlerCanRespondInSameThread() throws Exception {
    TestDriver driver = newTestDriver(SameThreadReplier.INSTANCE);
    Response response = dispatchMessage(driver, new SimpleMessage("msg")).get(60, TimeUnit.SECONDS);
    assertTrue(response instanceof MbusResponse);
    assertEquals(Response.Status.OK, response.getStatus());
    Reply reply = ((MbusResponse) response).getReply();
    assertTrue(reply instanceof EmptyReply);
    assertFalse(reply.hasErrors());
    assertTrue(driver.close());
}
Also used : Response(com.yahoo.jdisc.Response) SimpleMessage(com.yahoo.messagebus.test.SimpleMessage) Reply(com.yahoo.messagebus.Reply) EmptyReply(com.yahoo.messagebus.EmptyReply) TestDriver(com.yahoo.jdisc.test.TestDriver) EmptyReply(com.yahoo.messagebus.EmptyReply) Test(org.junit.Test)

Aggregations

Response (com.yahoo.jdisc.Response)52 Test (org.junit.Test)39 Request (com.yahoo.jdisc.Request)17 SimpleMessage (com.yahoo.messagebus.test.SimpleMessage)14 ContainerBuilder (com.yahoo.jdisc.application.ContainerBuilder)11 ContentChannel (com.yahoo.jdisc.handler.ContentChannel)11 TestDriver (com.yahoo.jdisc.test.TestDriver)10 ClientTestDriver (com.yahoo.messagebus.jdisc.test.ClientTestDriver)7 ResponseHandler (com.yahoo.jdisc.handler.ResponseHandler)6 SimpleReply (com.yahoo.messagebus.test.SimpleReply)6 ByteBuffer (java.nio.ByteBuffer)6 ServerTestDriver (com.yahoo.messagebus.jdisc.test.ServerTestDriver)5 Chain (com.yahoo.component.chain.Chain)3 HttpRequest (com.yahoo.jdisc.http.HttpRequest)3 HttpResponse (com.yahoo.jdisc.http.HttpResponse)3 NonWorkingRequest (com.yahoo.jdisc.test.NonWorkingRequest)3 Reply (com.yahoo.messagebus.Reply)3 Processor (com.yahoo.processing.Processor)3 Callable (java.util.concurrent.Callable)3 Test (org.testng.annotations.Test)3