Search in sources :

Example 51 with ResourceContextImpl

use of com.linkedin.restli.internal.server.ResourceContextImpl in project rest.li by linkedin.

the class ExampleRequestResponseGenerator method batchFinder.

public ExampleRequestResponse batchFinder(String name) {
    BatchFinderSchema batchFinderSchema = _resourceSchema.getBatchFinder(name);
    if (batchFinderSchema == null) {
        throw new IllegalArgumentException("No such batch finder for resource: " + name);
    }
    RecordDataSchema metadataSchema = null;
    if (batchFinderSchema.hasMetadata()) {
        metadataSchema = (RecordDataSchema) RestSpecCodec.textToSchema(batchFinderSchema.getMetadata().getType(), _schemaResolver);
    }
    Request<?> request = buildBatchFinderRequest(batchFinderSchema);
    RestRequest restRequest = buildRequest(request);
    try {
        ServerResourceContext context = new ResourceContextImpl(new PathKeysImpl(), restRequest, new RequestContext());
        DataList criteriaParams = (DataList) context.getStructuredParameter(batchFinderSchema.getBatchParam());
        // Since batchFinder has 2 kinds of responses. One is successful CollectionResponse. The other one is ErrorResponse.
        // When BatchFinderResponseBuilder cannot find a search criteria, it will return an ErrorResponse.
        // To include only one criteria in BatchFinderResult will make the response example diverse.
        // guarantee batchFinder request and response has a same criteria
        AnyRecord batchFinderCriteria = new AnyRecord((DataMap) criteriaParams.get(0));
        return buildRequestResponse(request, buildBatchFinderResult(metadataSchema, batchFinderCriteria), buildResourceMethodDescriptorForBatchFinder(name, batchFinderSchema.getBatchParam()));
    } catch (RestLiSyntaxException e) {
        throw new ExampleGenerationException("Internal error during example generation", e);
    }
}
Also used : AnyRecord(com.linkedin.restli.internal.server.methods.AnyRecord) RestLiSyntaxException(com.linkedin.restli.internal.server.util.RestLiSyntaxException) PathKeysImpl(com.linkedin.restli.internal.server.PathKeysImpl) BatchFinderSchema(com.linkedin.restli.restspec.BatchFinderSchema) DataList(com.linkedin.data.DataList) RestRequest(com.linkedin.r2.message.rest.RestRequest) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) RequestContext(com.linkedin.r2.message.RequestContext) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl)

Example 52 with ResourceContextImpl

use of com.linkedin.restli.internal.server.ResourceContextImpl in project rest.li by linkedin.

the class ExampleRequestResponseGenerator method buildResponse.

private RestResponse buildResponse(Object responseEntity, ResourceMethodDescriptor method, RestRequest restRequest) {
    try {
        RequestContext requestContext = new RequestContext();
        ServerResourceContext context = new ResourceContextImpl(new PathKeysImpl(), restRequest, requestContext);
        RestUtils.validateRequestHeadersAndUpdateResourceContext(restRequest.getHeaders(), Collections.emptySet(), context, requestContext);
        method.setResourceModel(_resourceModel);
        final RoutingResult routingResult = new RoutingResult(context, method);
        RestLiResponseData<?> responseData = _responseHandler.buildRestLiResponseData(restRequest, routingResult, responseEntity);
        RestLiResponse restLiResponse = _responseHandler.buildPartialResponse(routingResult, responseData);
        return ResponseUtils.buildResponse(routingResult, restLiResponse);
    } catch (RestLiSyntaxException e) {
        throw new ExampleGenerationException("Internal error during example generation", e);
    } catch (IOException e) {
        throw new ExampleGenerationException("Unable to build example response", e);
    }
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RestLiSyntaxException(com.linkedin.restli.internal.server.util.RestLiSyntaxException) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) PathKeysImpl(com.linkedin.restli.internal.server.PathKeysImpl) RestLiResponse(com.linkedin.restli.internal.server.response.RestLiResponse) RequestContext(com.linkedin.r2.message.RequestContext) IOException(java.io.IOException) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl)

Aggregations

ResourceContextImpl (com.linkedin.restli.internal.server.ResourceContextImpl)52 RequestContext (com.linkedin.r2.message.RequestContext)41 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)40 PathKeysImpl (com.linkedin.restli.internal.server.PathKeysImpl)34 Test (org.testng.annotations.Test)31 RestRequest (com.linkedin.r2.message.rest.RestRequest)29 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)29 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)25 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)20 URI (java.net.URI)15 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)14 RestLiRouter (com.linkedin.restli.internal.server.RestLiRouter)14 RestLiConfig (com.linkedin.restli.server.RestLiConfig)14 ByteString (com.linkedin.data.ByteString)11 RestLiTestHelper.buildResourceModel (com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel)11 RestLiSyntaxException (com.linkedin.restli.internal.server.util.RestLiSyntaxException)10 PathKeys (com.linkedin.restli.server.PathKeys)10 RoutingException (com.linkedin.restli.server.RoutingException)9 DataMap (com.linkedin.data.DataMap)7 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)7