Search in sources :

Example 1 with Response

use of com.yahoo.document.restapi.Response 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)

Aggregations

ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 HttpResponse (com.yahoo.container.jdisc.HttpResponse)1 Response (com.yahoo.document.restapi.Response)1 RestApiException (com.yahoo.document.restapi.RestApiException)1 RestUri (com.yahoo.document.restapi.RestUri)1 IOException (java.io.IOException)1