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