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