Search in sources :

Example 36 with HttpResponse

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

the class FileDistributionStatusTest method require_different_statuses_many_hosts.

@Test
public void require_different_statuses_many_hosts() throws IOException {
    application = createApplication("localhost", "localhost2");
    Map<String, Double> fileReferenceStatuses = new HashMap<>();
    fileReferenceStatuses.put("1234", 0.2);
    fileReferenceStatuses.put("abcd", 1.0);
    HostStatus localhost = statusWithError("localhost", Status.IN_PROGRESS, fileReferenceStatuses, "connection timed out");
    Map<String, Double> fileReferenceStatuses2 = new HashMap<>();
    fileReferenceStatuses2.put("1234", 1.0);
    HostStatus localhost2 = statusFinished("localhost2", Status.FINISHED, fileReferenceStatuses2);
    FileDistributionStatus status = new MockStatus(new HashSet<>(Arrays.asList(localhost, localhost2)));
    application = createApplication("localhost", "localhost2");
    HttpResponse response = getStatus(status, application);
    assertResponse(200, "{" + "\"hosts\":[" + "{\"hostname\":\"localhost\"," + "\"status\":\"IN_PROGRESS\"," + "\"message\":\"connection timed out\"," + "\"fileReferences\":[" + "{\"1234\":0.2},{\"abcd\":1.0}]}," + "{\"hostname\":\"localhost2\"," + "\"status\":\"FINISHED\"," + "\"fileReferences\":[" + "{\"1234\":1.0}]}" + "]," + "\"status\":\"IN_PROGRESS\"}", response);
}
Also used : HashMap(java.util.HashMap) HttpResponse(com.yahoo.container.jdisc.HttpResponse) HostStatus(com.yahoo.vespa.config.server.application.FileDistributionStatus.HostStatus) Test(org.junit.Test) SessionHandlerTest(com.yahoo.vespa.config.server.http.SessionHandlerTest)

Example 37 with HttpResponse

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

the class AccessLogRequestHandler method handle.

@Override
public HttpResponse handle(HttpRequest request) {
    final List<String> uris = circularArrayAccessLogKeeper.getUris();
    return new HttpResponse(200) {

        @Override
        public void render(OutputStream outputStream) throws IOException {
            JsonGenerator generator = jsonFactory.createGenerator(outputStream);
            generator.writeStartObject();
            generator.writeArrayFieldStart("entries");
            for (String uri : uris) {
                generator.writeStartObject();
                generator.writeStringField("url", uri);
                generator.writeEndObject();
            }
            generator.writeEndArray();
            generator.writeEndObject();
            generator.close();
        }
    };
}
Also used : OutputStream(java.io.OutputStream) HttpResponse(com.yahoo.container.jdisc.HttpResponse) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator)

Example 38 with HttpResponse

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

the class AccessLogRequestHandlerTest method testManyLogLines.

@Test
public void testManyLogLines() throws IOException {
    keeper.addUri("foo");
    keeper.addUri("foo");
    HttpResponse response = handler.handle(null);
    response.render(out);
    assertThat(out.toString(), is("{\"entries\":[{\"url\":\"foo\"},{\"url\":\"foo\"}]}"));
}
Also used : HttpResponse(com.yahoo.container.jdisc.HttpResponse) Test(org.junit.Test)

Example 39 with HttpResponse

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

the class MockServiceTest method testNoHandlerFound.

@Test
public void testNoHandlerFound() throws InterruptedException, IOException {
    HttpResponse response = runHandler(com.yahoo.jdisc.http.HttpRequest.Method.DELETE, "/foo/bar");
    assertThat(response.getStatus(), is(404));
    assertResponseContents(response, "DELETE:/foo/bar was not found");
}
Also used : HttpResponse(com.yahoo.container.jdisc.HttpResponse) Test(org.junit.Test)

Example 40 with HttpResponse

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

the class RestApi method handleVisit.

private HttpResponse handleVisit(RestUri restUri, HttpRequest request) throws RestApiException {
    String documentSelection = Optional.ofNullable(request.getProperty(SELECTION)).orElse("");
    if (restUri.getGroup().isPresent() && !restUri.getGroup().get().value.isEmpty()) {
        if (!documentSelection.isEmpty()) {
            // TODO why is this restriction in place? Document selection allows composition of location predicate and other expressions
            return Response.createErrorResponse(400, "Visiting does not support setting value for group/value in combination with expression, try using only expression parameter instead.", restUri, RestUri.apiErrorCodes.GROUP_AND_EXPRESSION_ERROR);
        }
        RestUri.Group group = restUri.getGroup().get();
        if (group.name == 'n') {
            documentSelection = "id.user=" + group.value;
        } else {
            documentSelection = "id.group='" + group.value + "'";
        }
    }
    OperationHandler.VisitOptions options;
    try {
        options = visitOptionsFromRequest(request);
    } catch (BadRequestParameterException e) {
        return createInvalidParameterResponse(e.getParameter(), e.getMessage());
    }
    final OperationHandler.VisitResult visit = operationHandler.visit(restUri, documentSelection, options);
    final ObjectNode resultNode = mapper.createObjectNode();
    visit.token.ifPresent(t -> resultNode.put(CONTINUATION, t));
    resultNode.putArray(DOCUMENTS).addPOJO(visit.documentsAsJsonList);
    resultNode.put(PATH_NAME, restUri.getRawPath());
    HttpResponse httpResponse = new HttpResponse(200) {

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

        @Override
        public void render(OutputStream outputStream) throws IOException {
            try {
                outputStream.write(resultNode.toString().getBytes(StandardCharsets.UTF_8));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    };
    return httpResponse;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) OutputStream(java.io.OutputStream) HttpResponse(com.yahoo.container.jdisc.HttpResponse) RestUri(com.yahoo.document.restapi.RestUri) IOException(java.io.IOException) RestApiException(com.yahoo.document.restapi.RestApiException) OperationHandler(com.yahoo.document.restapi.OperationHandler)

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