Search in sources :

Example 1 with RestUri

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

Example 2 with RestUri

use of com.yahoo.document.restapi.RestUri 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)2 HttpResponse (com.yahoo.container.jdisc.HttpResponse)2 RestApiException (com.yahoo.document.restapi.RestApiException)2 RestUri (com.yahoo.document.restapi.RestUri)2 IOException (java.io.IOException)2 OperationHandler (com.yahoo.document.restapi.OperationHandler)1 Response (com.yahoo.document.restapi.Response)1 OutputStream (java.io.OutputStream)1