Search in sources :

Example 46 with HttpResponse

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

the class VespaFeedHandlerTestCase method testRequest.

private Result testRequest(HttpRequest req) throws Exception {
    HttpResponse response = null;
    String feedPrefix = "feed";
    String removePrefix = "remove";
    String feedStatusPrefix = "feedstatus";
    String removeLocationPrefix = "removelocation";
    if (req.getUri().getPath().startsWith(feedPrefix)) {
        response = feedHandler.handle(req);
    }
    if (req.getUri().getPath().startsWith(removePrefix)) {
        response = removeHandler.handle(req);
    }
    if (req.getUri().getPath().startsWith(feedStatusPrefix)) {
        response = statusHandler.handle(req);
    }
    if (req.getUri().getPath().startsWith(removeLocationPrefix)) {
        response = removeLocationHandler.handle(req);
    }
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    response.render(output);
    Result res = new Result();
    res.messages = factory.messages;
    res.output = new String(output.toByteArray());
    if (response instanceof FeedResponse) {
        FeedResponse feedResponse = (FeedResponse) response;
        res.error = feedResponse.getErrorMessageList().isEmpty() ? null : feedResponse.getErrorMessageList().get(0);
        res.errorCount = feedResponse.getErrorMessageList().size();
        assertTrue(feedResponse.isSuccess() == (res.errorCount == 0));
    }
    return res;
}
Also used : HttpResponse(com.yahoo.container.jdisc.HttpResponse)

Example 47 with HttpResponse

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

the class V2ExternalFeedTestCase method testCleaningDoesNotBlowUp.

@Test
public final void testCleaningDoesNotBlowUp() throws IOException {
    InputStream in = new MetaStream(new byte[] { 1 });
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    HttpRequest nalle = HttpRequest.createTestRequest("http://test4-steinar:19020/reserved-for-internal-use/feedapi", Method.POST, in);
    nalle.getJDiscRequest().headers().add(Headers.VERSION, "2");
    nalle.getJDiscRequest().headers().add(Headers.DRAIN, "false");
    HttpResponse r = handler.handle(nalle);
    r.render(out);
    assertEquals("", Utf8.toString(out.toByteArray()));
    handler.forceRunCleanClients();
}
Also used : HttpRequest(com.yahoo.container.jdisc.HttpRequest) InputStream(java.io.InputStream) HttpResponse(com.yahoo.container.jdisc.HttpResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 48 with HttpResponse

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

the class V2ExternalFeedTestCase method test.

@Test
public final void test() throws IOException, InterruptedException {
    String sessionId;
    {
        InputStream in = new MetaStream(new byte[] { 1 });
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        HttpRequest nalle = HttpRequest.createTestRequest("http://test4-steinar:19020/reserved-for-internal-use/feedapi", Method.POST, in);
        nalle.getJDiscRequest().headers().add(Headers.VERSION, "2");
        nalle.getJDiscRequest().headers().add(Headers.DRAIN, "false");
        HttpResponse r = handler.handle(nalle);
        sessionId = r.headers().getFirst(Headers.SESSION_ID);
        r.render(out);
        assertEquals("", Utf8.toString(out.toByteArray()));
    }
    {
        InputStream in = new MetaStream(new byte[] { 1, 3, 2 });
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        HttpRequest nalle = HttpRequest.createTestRequest("http://test4-steinar:19020/reserved-for-internal-use/feedapi", Method.POST, in);
        nalle.getJDiscRequest().headers().add(Headers.VERSION, "2");
        nalle.getJDiscRequest().headers().add(Headers.TIMEOUT, "1000000000");
        nalle.getJDiscRequest().headers().add(Headers.SESSION_ID, sessionId);
        nalle.getJDiscRequest().headers().add(Headers.PRIORITY, "LOWEST");
        nalle.getJDiscRequest().headers().add(Headers.TRACE_LEVEL, "4");
        nalle.getJDiscRequest().headers().add(Headers.DRAIN, "true");
        HttpResponse r = handler.handle(nalle);
        r.render(out);
        assertEquals("id:banana:banana::doc1 OK Document{20}processed. \n" + "id:banana:banana::doc1 OK Document{20}processed. \n" + "id:banana:banana::doc1 OK Document{20}processed. \n", Utf8.toString(out.toByteArray()));
        assertEquals("text/plain", r.getContentType());
        assertEquals(StandardCharsets.US_ASCII.name(), r.getCharacterEncoding());
        assertEquals(7, logChecker.records.size());
        String actualHandshake = logChecker.records.take().getMessage();
        assertThat(actualHandshake, actualHandshake.matches("Handshake completed for client (-?)(.+?)-#(.*?)\\."), is(true));
        assertEquals("Successfully deserialized document id: id:banana:banana::doc1", logChecker.records.take().getMessage());
        assertEquals("Sent message successfully, document id: id:banana:banana::doc1", logChecker.records.take().getMessage());
    }
    // test session ID without #, i.e. something fishy related to VIPs is going on
    sessionId = "something";
    {
        InputStream in = new MetaStream(new byte[] { 1, 3, 2 });
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        HttpRequest nalle = HttpRequest.createTestRequest("http://test4-steinar:19020/reserved-for-internal-use/feedapi", Method.POST, in);
        nalle.getJDiscRequest().headers().add(Headers.VERSION, "2");
        nalle.getJDiscRequest().headers().add(Headers.TIMEOUT, "1000000000");
        nalle.getJDiscRequest().headers().add(Headers.SESSION_ID, sessionId);
        nalle.getJDiscRequest().headers().add(Headers.DRAIN, "true");
        nalle.getJDiscRequest().headers().add(Headers.PRIORITY, "LOWEST");
        nalle.getJDiscRequest().headers().add(Headers.TRACE_LEVEL, "4");
        nalle.getJDiscRequest().headers().add(Headers.TRACE_LEVEL, "2");
        HttpResponse r = handler.handle(nalle);
        r.render(out);
        String expectedErrorMsg = "Got request from client with id 'something', but found no session for this client.";
        assertThat(Utf8.toString(out.toByteArray()), containsString(expectedErrorMsg));
        assertEquals("text/plain", r.getContentType());
        assertEquals(StandardCharsets.UTF_8.name(), r.getCharacterEncoding());
    }
    // test session ID with trailing # but no hostname
    sessionId = "something#";
    {
        InputStream in = new MetaStream(new byte[] { 1, 3, 2 });
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        HttpRequest nalle = HttpRequest.createTestRequest("http://test4-steinar:19020/reserved-for-internal-use/feedapi", Method.POST, in);
        nalle.getJDiscRequest().headers().add(Headers.VERSION, "2");
        nalle.getJDiscRequest().headers().add(Headers.TIMEOUT, "1000000000");
        nalle.getJDiscRequest().headers().add(Headers.SESSION_ID, sessionId);
        nalle.getJDiscRequest().headers().add(Headers.DRAIN, "true");
        nalle.getJDiscRequest().headers().add(Headers.PRIORITY, "LOWEST");
        nalle.getJDiscRequest().headers().add(Headers.TRACE_LEVEL, "4");
        HttpResponse r = handler.handle(nalle);
        r.render(out);
        String expectedErrorMsg = "Got request from client with id 'something#', but found no session for this client.";
        assertThat(Utf8.toString(out.toByteArray()), containsString(expectedErrorMsg));
        assertEquals("text/plain", r.getContentType());
        assertEquals(StandardCharsets.UTF_8.name(), r.getCharacterEncoding());
    }
    // test session ID with trailing # and some unknown hostname at the end
    sessionId = "something#thisHostnameDoesNotExistAnywhere";
    {
        InputStream in = new MetaStream(new byte[] { 1, 3, 2 });
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        HttpRequest nalle = HttpRequest.createTestRequest("http://test4-steinar:19020/reserved-for-internal-use/feedapi", Method.POST, in);
        nalle.getJDiscRequest().headers().add(Headers.VERSION, "2");
        nalle.getJDiscRequest().headers().add(Headers.TIMEOUT, "1000000000");
        nalle.getJDiscRequest().headers().add(Headers.SESSION_ID, sessionId);
        nalle.getJDiscRequest().headers().add(Headers.DRAIN, "true");
        nalle.getJDiscRequest().headers().add(Headers.PRIORITY, "LOWEST");
        nalle.getJDiscRequest().headers().add(Headers.TRACE_LEVEL, "4");
        HttpResponse r = handler.handle(nalle);
        r.render(out);
        String expectedErrorMsg = "Got request from client with id 'something#thisHostnameDoesNotExistAnywhere', " + "but found no session for this client. Session was originally established " + "towards host thisHostnameDoesNotExistAnywhere, but our hostname is " + "ourHostname.";
        assertThat(Utf8.toString(out.toByteArray()), containsString(expectedErrorMsg));
        assertEquals("text/plain", r.getContentType());
        assertEquals(StandardCharsets.UTF_8.name(), r.getCharacterEncoding());
    }
    // test session ID with trailing # and some unknown hostname at the end
    sessionId = "something#ourHostname";
    {
        InputStream in = new MetaStream(new byte[] { 1, 3, 2 });
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        HttpRequest nalle = HttpRequest.createTestRequest("http://test4-steinar:19020/reserved-for-internal-use/feedapi", Method.POST, in);
        nalle.getJDiscRequest().headers().add(Headers.VERSION, "2");
        nalle.getJDiscRequest().headers().add(Headers.TIMEOUT, "1000000000");
        nalle.getJDiscRequest().headers().add(Headers.SESSION_ID, sessionId);
        nalle.getJDiscRequest().headers().add(Headers.PRIORITY, "LOWEST");
        nalle.getJDiscRequest().headers().add(Headers.TRACE_LEVEL, "4");
        nalle.getJDiscRequest().headers().add(Headers.DRAIN, "true");
        HttpResponse r = handler.handle(nalle);
        r.render(out);
        assertEquals("id:banana:banana::doc1 OK Document{20}processed. \n" + "id:banana:banana::doc1 OK Document{20}processed. \n" + "id:banana:banana::doc1 OK Document{20}processed. \n", Utf8.toString(out.toByteArray()));
        assertEquals("text/plain", r.getContentType());
        assertEquals(StandardCharsets.US_ASCII.name(), r.getCharacterEncoding());
        Thread.sleep(1000);
    }
}
Also used : HttpRequest(com.yahoo.container.jdisc.HttpRequest) InputStream(java.io.InputStream) HttpResponse(com.yahoo.container.jdisc.HttpResponse) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 49 with HttpResponse

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

the class V2ExternalFeedTestCase method testIllegalVersion.

@Test
public final void testIllegalVersion() throws IOException {
    InputStream in = new MetaStream(new byte[] { 1 });
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    HttpRequest nalle = HttpRequest.createTestRequest("http://test4-steinar:19020/reserved-for-internal-use/feedapi", Method.POST, in);
    nalle.getJDiscRequest().headers().add(Headers.VERSION, Integer.toString(Integer.MAX_VALUE));
    HttpResponse r = handler.handle(nalle);
    r.render(out);
    assertEquals(Headers.HTTP_NOT_ACCEPTABLE, r.getStatus());
}
Also used : HttpRequest(com.yahoo.container.jdisc.HttpRequest) InputStream(java.io.InputStream) HttpResponse(com.yahoo.container.jdisc.HttpResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 50 with HttpResponse

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

the class ApplicationApiHandler method authenticatedUser.

private HttpResponse authenticatedUser(HttpRequest request) {
    String userIdString = request.getProperty("userOverride");
    if (userIdString == null)
        userIdString = getUserId(request).map(UserId::id).orElseThrow(() -> new ForbiddenException("You must be authenticated or specify userOverride"));
    UserId userId = new UserId(userIdString);
    List<Tenant> tenants = controller.tenants().asList(userId);
    Slime slime = new Slime();
    Cursor response = slime.setObject();
    response.setString("user", userId.id());
    Cursor tenantsArray = response.setArray("tenants");
    for (Tenant tenant : tenants) tenantInTenantsListToSlime(tenant, request.getUri(), tenantsArray.addObject());
    response.setBool("tenantExists", tenants.stream().map(Tenant::getId).anyMatch(id -> id.isTenantFor(userId)));
    return new SlimeJsonResponse(slime);
}
Also used : AlreadyExistsException(com.yahoo.vespa.hosted.controller.AlreadyExistsException) EndpointStatus(com.yahoo.vespa.hosted.controller.api.application.v4.model.EndpointStatus) Inject(com.google.inject.Inject) URISyntaxException(java.net.URISyntaxException) SlimeJsonResponse(com.yahoo.vespa.hosted.controller.restapi.SlimeJsonResponse) Scanner(java.util.Scanner) DeploymentJobs(com.yahoo.vespa.hosted.controller.application.DeploymentJobs) ConfigServerException(com.yahoo.vespa.hosted.controller.api.integration.configserver.ConfigServerException) RegionName(com.yahoo.config.provision.RegionName) TenantName(com.yahoo.config.provision.TenantName) ResourceResponse(com.yahoo.vespa.hosted.controller.restapi.ResourceResponse) Tenant(com.yahoo.vespa.hosted.controller.api.Tenant) ZoneId(com.yahoo.vespa.hosted.controller.api.integration.zone.ZoneId) ClusterUtilization(com.yahoo.vespa.hosted.controller.application.ClusterUtilization) Duration(java.time.Duration) Map(java.util.Map) LogLevel(com.yahoo.log.LogLevel) Path(com.yahoo.vespa.hosted.controller.restapi.Path) JobStatus(com.yahoo.vespa.hosted.controller.application.JobStatus) GitRevision(com.yahoo.vespa.hosted.controller.api.application.v4.model.GitRevision) ClusterCost(com.yahoo.vespa.hosted.controller.application.ClusterCost) BadRequestException(javax.ws.rs.BadRequestException) DeployOptions(com.yahoo.vespa.hosted.controller.api.application.v4.model.DeployOptions) URI(java.net.URI) DeploymentCost(com.yahoo.vespa.hosted.controller.application.DeploymentCost) ScrewdriverBuildJob(com.yahoo.vespa.hosted.controller.api.application.v4.model.ScrewdriverBuildJob) Exceptions(com.yahoo.yolean.Exceptions) AthenzDomain(com.yahoo.vespa.athenz.api.AthenzDomain) ImmutableSet(com.google.common.collect.ImmutableSet) Inspector(com.yahoo.slime.Inspector) NotExistsException(com.yahoo.vespa.hosted.controller.NotExistsException) ApplicationVersion(com.yahoo.vespa.hosted.controller.application.ApplicationVersion) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) ErrorResponse(com.yahoo.vespa.hosted.controller.restapi.ErrorResponse) RestartAction(com.yahoo.vespa.hosted.controller.api.application.v4.model.configserverbindings.RestartAction) Property(com.yahoo.vespa.hosted.controller.api.identifiers.Property) ApplicationView(com.yahoo.vespa.serviceview.bindings.ApplicationView) Objects(java.util.Objects) ZmsException(com.yahoo.vespa.hosted.controller.api.integration.athenz.ZmsException) List(java.util.List) Principal(java.security.Principal) AthenzPrincipal(com.yahoo.vespa.athenz.api.AthenzPrincipal) NotAuthorizedException(javax.ws.rs.NotAuthorizedException) Optional(java.util.Optional) Deployment(com.yahoo.vespa.hosted.controller.application.Deployment) HttpResponse(com.yahoo.container.jdisc.HttpResponse) Controller(com.yahoo.vespa.hosted.controller.Controller) Joiner(com.google.common.base.Joiner) Log(com.yahoo.vespa.hosted.controller.api.integration.configserver.Log) AthenzClientFactory(com.yahoo.vespa.hosted.controller.api.integration.athenz.AthenzClientFactory) GitRepository(com.yahoo.vespa.hosted.controller.api.identifiers.GitRepository) ApplicationName(com.yahoo.config.provision.ApplicationName) AthenzUser(com.yahoo.vespa.athenz.api.AthenzUser) Version(com.yahoo.component.Version) ApplicationId(com.yahoo.config.provision.ApplicationId) PropertyId(com.yahoo.vespa.hosted.controller.api.identifiers.PropertyId) RefeedAction(com.yahoo.vespa.hosted.controller.api.application.v4.model.configserverbindings.RefeedAction) DeploymentId(com.yahoo.vespa.hosted.controller.api.identifiers.DeploymentId) Slime(com.yahoo.slime.Slime) AthenzIdentity(com.yahoo.vespa.athenz.api.AthenzIdentity) IOUtils(com.yahoo.io.IOUtils) NToken(com.yahoo.vespa.athenz.api.NToken) Level(java.util.logging.Level) DeploymentMetrics(com.yahoo.vespa.hosted.controller.application.DeploymentMetrics) ApplicationResource(com.yahoo.vespa.hosted.controller.api.application.v4.ApplicationResource) SlimeUtils(com.yahoo.vespa.config.SlimeUtils) Change(com.yahoo.vespa.hosted.controller.application.Change) TenantId(com.yahoo.vespa.hosted.controller.api.identifiers.TenantId) GitBranch(com.yahoo.vespa.hosted.controller.api.identifiers.GitBranch) ServiceInfo(com.yahoo.vespa.hosted.controller.api.application.v4.model.configserverbindings.ServiceInfo) SetBouncerPassthruHeaderFilter(com.yahoo.vespa.hosted.controller.restapi.filter.SetBouncerPassthruHeaderFilter) EnvironmentResource(com.yahoo.vespa.hosted.controller.api.application.v4.EnvironmentResource) TenantResource(com.yahoo.vespa.hosted.controller.api.application.v4.TenantResource) Application(com.yahoo.vespa.hosted.controller.Application) ActivateResult(com.yahoo.vespa.hosted.controller.api.ActivateResult) Cursor(com.yahoo.slime.Cursor) StringResponse(com.yahoo.vespa.hosted.controller.restapi.StringResponse) ForbiddenException(javax.ws.rs.ForbiddenException) Hostname(com.yahoo.vespa.hosted.controller.api.identifiers.Hostname) Environment(com.yahoo.config.provision.Environment) GitCommit(com.yahoo.vespa.hosted.controller.api.identifiers.GitCommit) HttpRequest(com.yahoo.container.jdisc.HttpRequest) SourceRevision(com.yahoo.vespa.hosted.controller.application.SourceRevision) IOException(java.io.IOException) MessageResponse(com.yahoo.vespa.hosted.controller.restapi.MessageResponse) ApplicationPackage(com.yahoo.vespa.hosted.controller.application.ApplicationPackage) LoggingRequestHandler(com.yahoo.container.jdisc.LoggingRequestHandler) User(com.yahoo.vespa.hosted.controller.api.integration.organization.User) UserId(com.yahoo.vespa.hosted.controller.api.identifiers.UserId) RotationStatus(com.yahoo.vespa.hosted.controller.api.integration.routing.RotationStatus) DeploymentSpec(com.yahoo.config.application.api.DeploymentSpec) DayOfWeek(java.time.DayOfWeek) ScrewdriverId(com.yahoo.vespa.hosted.controller.api.identifiers.ScrewdriverId) Collections(java.util.Collections) InputStream(java.io.InputStream) ForbiddenException(javax.ws.rs.ForbiddenException) Tenant(com.yahoo.vespa.hosted.controller.api.Tenant) SlimeJsonResponse(com.yahoo.vespa.hosted.controller.restapi.SlimeJsonResponse) UserId(com.yahoo.vespa.hosted.controller.api.identifiers.UserId) Slime(com.yahoo.slime.Slime) Cursor(com.yahoo.slime.Cursor)

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