Search in sources :

Example 1 with ResourceEntityType

use of com.linkedin.restli.restspec.ResourceEntityType in project rest.li by linkedin.

the class ResponseUtils method buildResponse.

public static RestResponse buildResponse(RoutingResult routingResult, RestLiResponse restLiResponse) {
    RestResponseBuilder builder = new RestResponseBuilder().setHeaders(restLiResponse.getHeaders()).setCookies(CookieUtil.encodeSetCookies(restLiResponse.getCookies())).setStatus(restLiResponse.getStatus().getCode());
    ServerResourceContext context = routingResult.getContext();
    ResourceEntityType resourceEntityType = routingResult.getResourceMethod().getResourceModel().getResourceEntityType();
    if (restLiResponse.hasData() && ResourceEntityType.STRUCTURED_DATA == resourceEntityType) {
        DataMap dataMap = restLiResponse.getDataMap();
        String mimeType = context.getResponseMimeType();
        URI requestUri = context.getRequestURI();
        Map<String, String> requestHeaders = context.getRequestHeaders();
        builder = encodeResult(mimeType, requestUri, requestHeaders, builder, dataMap);
    }
    return builder.build();
}
Also used : ResourceEntityType(com.linkedin.restli.restspec.ResourceEntityType) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) ByteString(com.linkedin.data.ByteString) URI(java.net.URI) DataMap(com.linkedin.data.DataMap)

Example 2 with ResourceEntityType

use of com.linkedin.restli.restspec.ResourceEntityType in project rest.li by linkedin.

the class RestLiResponseHandler method buildRestLiResponseData.

/**
 * Executes {@linkplain RestLiResponseHandler the first step} of building the response.
 */
public RestLiResponseData<?> buildRestLiResponseData(final Request request, final RoutingResult routingResult, final Object responseObject) throws IOException {
    ServerResourceContext context = routingResult.getContext();
    final ProtocolVersion protocolVersion = context.getRestliProtocolVersion();
    Map<String, String> responseHeaders = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
    responseHeaders.putAll(context.getResponseHeaders());
    responseHeaders.put(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, protocolVersion.toString());
    List<HttpCookie> responseCookies = context.getResponseCookies();
    if (responseObject == null) {
        // If we have a null result, we have to assign the correct response status
        if (routingResult.getResourceMethod().getType().equals(ResourceMethod.ACTION)) {
            return new RestLiResponseDataImpl<>(new ActionResponseEnvelope(HttpStatus.S_200_OK, null), responseHeaders, responseCookies);
        } else if (routingResult.getResourceMethod().getType().equals(ResourceMethod.GET)) {
            ResourceEntityType resourceEntityType = routingResult.getResourceMethod().getResourceModel().getResourceEntityType();
            if (ResourceEntityType.UNSTRUCTURED_DATA == resourceEntityType) {
                // TODO: A dummy empty record is used here to avoid NPE where a record is expected for GET, need a better fix.
                return new RestLiResponseDataImpl<>(new GetResponseEnvelope(HttpStatus.S_200_OK, new EmptyRecord()), responseHeaders, responseCookies);
            } else {
                throw new RestLiServiceException(HttpStatus.S_404_NOT_FOUND, "Requested entity not found: " + routingResult.getResourceMethod());
            }
        } else {
            // All other cases do not permit null to be returned
            throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null returned by the resource method: " + routingResult.getResourceMethod());
        }
    }
    if (responseObject instanceof RestLiServiceException) {
        return _errorResponseBuilder.buildRestLiResponseData(routingResult, (RestLiServiceException) responseObject, responseHeaders, responseCookies);
    }
    RestLiResponseBuilder<?> responseBuilder = _methodAdapterProvider.getResponseBuilder(routingResult.getResourceMethod().getType());
    if (responseBuilder == null) {
        // this should not happen if valid return types are specified
        ResourceMethodDescriptor resourceMethod = routingResult.getResourceMethod();
        String fqMethodName = resourceMethod.getResourceModel().getResourceClass().getName() + '#' + routingResult.getResourceMethod().getMethod().getName();
        throw new RestLiInternalException("Invalid return type '" + responseObject.getClass() + " from method '" + fqMethodName + '\'');
    }
    return responseBuilder.buildRestLiResponseData(request, routingResult, responseObject, responseHeaders, responseCookies);
}
Also used : EmptyRecord(com.linkedin.restli.common.EmptyRecord) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ProtocolVersion(com.linkedin.restli.common.ProtocolVersion) TreeMap(java.util.TreeMap) ResourceEntityType(com.linkedin.restli.restspec.ResourceEntityType) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) RestLiInternalException(com.linkedin.restli.internal.server.RestLiInternalException) HttpCookie(java.net.HttpCookie)

Aggregations

ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)2 ResourceEntityType (com.linkedin.restli.restspec.ResourceEntityType)2 ByteString (com.linkedin.data.ByteString)1 DataMap (com.linkedin.data.DataMap)1 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)1 EmptyRecord (com.linkedin.restli.common.EmptyRecord)1 ProtocolVersion (com.linkedin.restli.common.ProtocolVersion)1 RestLiInternalException (com.linkedin.restli.internal.server.RestLiInternalException)1 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)1 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)1 HttpCookie (java.net.HttpCookie)1 URI (java.net.URI)1 TreeMap (java.util.TreeMap)1