Search in sources :

Example 41 with HttpResponse

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

the class RestApi method handleInternal.

// protected for testing
protected HttpResponse handleInternal(HttpRequest request) {
    final RestUri restUri;
    try {
        restUri = new RestUri(request.getUri());
    } catch (RestApiException e) {
        return e.getResponse();
    } catch (Exception e2) {
        return Response.createErrorResponse(500, "Exception while parsing URI: " + e2.getMessage(), RestUri.apiErrorCodes.URL_PARSING);
    }
    final Optional<Boolean> create;
    try {
        create = parseBoolean(CREATE_PARAMETER_NAME, request);
    } catch (IllegalArgumentException e) {
        return Response.createErrorResponse(403, "Non valid value for 'create' parameter, must be empty, true, or " + "false: " + request.getProperty(CREATE_PARAMETER_NAME), RestUri.apiErrorCodes.INVALID_CREATE_VALUE);
    }
    String condition = request.getProperty(CONDITION_PARAMETER_NAME);
    Optional<String> route = Optional.ofNullable(request.getProperty(ROUTE_PARAMETER_NAME));
    Optional<ObjectNode> resultJson = Optional.empty();
    try {
        switch(request.getMethod()) {
            case // Vespa Visit/Get
            GET:
                return restUri.getDocId().isEmpty() ? handleVisit(restUri, request) : handleGet(restUri, request);
            case // Vespa Put
            POST:
                operationHandler.put(restUri, createPutOperation(request, restUri.generateFullId(), condition), route);
                break;
            case // Vespa Update
            PUT:
                operationHandler.update(restUri, createUpdateOperation(request, restUri.generateFullId(), condition, create), route);
                break;
            case // Vespa Delete
            DELETE:
                operationHandler.delete(restUri, condition, route);
                break;
            default:
                return new Response(405, Optional.empty(), Optional.of(restUri));
        }
    } catch (RestApiException e) {
        return e.getResponse();
    } catch (Exception e2) {
        // types, but with nice descriptions.
        return Response.createErrorResponse(400, e2.getMessage(), restUri, RestUri.apiErrorCodes.PARSER_ERROR);
    }
    return new Response(200, resultJson, Optional.of(restUri));
}
Also used : Response(com.yahoo.document.restapi.Response) HttpResponse(com.yahoo.container.jdisc.HttpResponse) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) RestUri(com.yahoo.document.restapi.RestUri) RestApiException(com.yahoo.document.restapi.RestApiException) IOException(java.io.IOException) RestApiException(com.yahoo.document.restapi.RestApiException)

Example 42 with HttpResponse

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

the class RestApi method handleGet.

private HttpResponse handleGet(RestUri restUri, HttpRequest request) throws RestApiException {
    final Optional<String> fieldSet = requestProperty("fieldSet", request);
    final Optional<String> getDocument = operationHandler.get(restUri, fieldSet);
    final ObjectNode resultNode = mapper.createObjectNode();
    if (getDocument.isPresent()) {
        final JsonNode parseNode;
        try {
            parseNode = mapper.readTree(getDocument.get());
        } catch (IOException e) {
            throw new RuntimeException("Failed while parsing my own results", e);
        }
        resultNode.putPOJO(FIELDS, parseNode.get(FIELDS));
    }
    resultNode.put(DOC_ID_NAME, restUri.generateFullId());
    resultNode.put(PATH_NAME, restUri.getRawPath());
    return new HttpResponse(getDocument.isPresent() ? 200 : 404) {

        @Override
        public String getContentType() {
            return APPLICATION_JSON;
        }

        @Override
        public void render(OutputStream outputStream) throws IOException {
            outputStream.write(resultNode.toString().getBytes(StandardCharsets.UTF_8.name()));
        }
    };
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) OutputStream(java.io.OutputStream) HttpResponse(com.yahoo.container.jdisc.HttpResponse) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException)

Example 43 with HttpResponse

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

the class FeedTesterV3 method feedManyDocument.

@Test
public void feedManyDocument() throws Exception {
    final FeedHandlerV3 feedHandlerV3 = setupFeederHandler();
    HttpResponse httpResponse = feedHandlerV3.handle(createRequest(100));
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    httpResponse.render(outStream);
    assertThat(httpResponse.getContentType(), is("text/plain"));
    String result = Utf8.toString(outStream.toByteArray());
    assertThat(Splitter.on("\n").splitToList(result).size(), is(101));
}
Also used : FeedHandlerV3(com.yahoo.vespa.http.server.FeedHandlerV3) HttpResponse(com.yahoo.container.jdisc.HttpResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 44 with HttpResponse

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

the class FeedTesterV3 method feedOneDocument.

@Test
public void feedOneDocument() throws Exception {
    final FeedHandlerV3 feedHandlerV3 = setupFeederHandler();
    HttpResponse httpResponse = feedHandlerV3.handle(createRequest(1));
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    httpResponse.render(outStream);
    assertThat(httpResponse.getContentType(), is("text/plain"));
    assertThat(Utf8.toString(outStream.toByteArray()), is("1230 OK message trace\n"));
}
Also used : FeedHandlerV3(com.yahoo.vespa.http.server.FeedHandlerV3) HttpResponse(com.yahoo.container.jdisc.HttpResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 45 with HttpResponse

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

the class RestApiMaxThreadTest method testCallsAreThrottled.

@Test
public void testCallsAreThrottled() throws InterruptedException {
    RestApiMocked restApiMocked = new RestApiMocked();
    // Fire lots of requests.
    for (int x = 0; x < 30; x++) {
        new Thread(() -> restApiMocked.handle(null)).start();
    }
    // Wait for all threads to be used
    while (requestsInFlight.get() != 19) {
        Thread.sleep(1);
    }
    // A new request should be blocked.
    final HttpResponse response = restApiMocked.handle(null);
    assertThat(response.getStatus(), is(429));
    latch.countDown();
}
Also used : HttpResponse(com.yahoo.container.jdisc.HttpResponse) Test(org.junit.Test)

Aggregations

HttpResponse (com.yahoo.container.jdisc.HttpResponse)103 Test (org.junit.Test)75 SessionHandlerTest (com.yahoo.vespa.config.server.http.SessionHandlerTest)33 HandlerTest (com.yahoo.vespa.config.server.http.HandlerTest)24 HttpRequest (com.yahoo.container.jdisc.HttpRequest)16 CompressedApplicationInputStreamTest (com.yahoo.vespa.config.server.http.CompressedApplicationInputStreamTest)6 SessionTest (com.yahoo.vespa.config.server.session.SessionTest)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)6 SimpletypesConfig (com.yahoo.config.SimpletypesConfig)5 InnerCNode (com.yahoo.config.codegen.InnerCNode)5 Slime (com.yahoo.slime.Slime)5 ConfigPayload (com.yahoo.vespa.config.ConfigPayload)5 OutputStream (java.io.OutputStream)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 ApplicationId (com.yahoo.config.provision.ApplicationId)4 IOException (java.io.IOException)4 InputStream (java.io.InputStream)4 HashMap (java.util.HashMap)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3