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