use of com.linkedin.restli.internal.server.ServerResourceContext in project rest.li by linkedin.
the class BatchGetResponseBuilder method buildResponse.
@Override
@SuppressWarnings("unchecked")
public PartialRestResponse buildResponse(RoutingResult routingResult, RestLiResponseData responseData) {
final Map<Object, BatchResponseEntry> responses = (Map<Object, BatchResponseEntry>) responseData.getBatchResponseEnvelope().getBatchResponseMap();
// Build the EntityResponse for each key from the merged map with mask from routingResult.
Map<Object, EntityResponse<RecordTemplate>> entityBatchResponse = buildEntityResponse(routingResult, responses);
PartialRestResponse.Builder builder = new PartialRestResponse.Builder();
final ProtocolVersion protocolVersion = ((ServerResourceContext) routingResult.getContext()).getRestliProtocolVersion();
@SuppressWarnings("unchecked") final BatchResponse<AnyRecord> response = toBatchResponse(entityBatchResponse, protocolVersion);
builder.entity(response);
return builder.headers(responseData.getHeaders()).cookies(responseData.getCookies()).build();
}
use of com.linkedin.restli.internal.server.ServerResourceContext in project rest.li by linkedin.
the class BatchUpdateResponseBuilder method buildResponse.
@Override
@SuppressWarnings("unchecked")
public PartialRestResponse buildResponse(RoutingResult routingResult, RestLiResponseData responseData) {
Map<Object, UpdateStatus> mergedResults = new HashMap<Object, UpdateStatus>();
final Map<Object, BatchResponseEntry> responses = (Map<Object, BatchResponseEntry>) responseData.getBatchResponseEnvelope().getBatchResponseMap();
generateResultEntityResponse(routingResult, responses, mergedResults);
PartialRestResponse.Builder builder = new PartialRestResponse.Builder();
final ProtocolVersion protocolVersion = ((ServerResourceContext) routingResult.getContext()).getRestliProtocolVersion();
@SuppressWarnings("unchecked") final BatchResponse<AnyRecord> response = toBatchResponse(mergedResults, protocolVersion);
return builder.entity(response).headers(responseData.getHeaders()).cookies(responseData.getCookies()).build();
}
use of com.linkedin.restli.internal.server.ServerResourceContext in project rest.li by linkedin.
the class CreateResponseBuilder method buildRestLiResponseData.
@Override
public RestLiResponseData buildRestLiResponseData(RestRequest request, RoutingResult routingResult, Object result, Map<String, String> headers, List<HttpCookie> cookies) {
CreateResponse createResponse = (CreateResponse) result;
if (createResponse.hasError()) {
RestLiResponseDataImpl responseData = new RestLiResponseDataImpl(createResponse.getError(), headers, cookies);
responseData.setResponseEnvelope(new CreateResponseEnvelope(null, responseData));
return responseData;
}
Object id = null;
if (createResponse.hasId()) {
id = ResponseUtils.translateCanonicalKeyToAlternativeKeyIfNeeded(createResponse.getId(), routingResult);
final ProtocolVersion protocolVersion = ((ServerResourceContext) routingResult.getContext()).getRestliProtocolVersion();
String stringKey = URIParamUtils.encodeKeyForUri(id, UriComponent.Type.PATH_SEGMENT, protocolVersion);
UriBuilder uribuilder = UriBuilder.fromUri(request.getURI());
uribuilder.path(stringKey);
if (routingResult.getContext().hasParameter(RestConstants.ALT_KEY_PARAM)) {
// add altkey param to location URI
uribuilder.queryParam(RestConstants.ALT_KEY_PARAM, routingResult.getContext().getParameter(RestConstants.ALT_KEY_PARAM));
}
headers.put(RestConstants.HEADER_LOCATION, uribuilder.build((Object) null).toString());
headers.put(HeaderUtil.getIdHeaderName(protocolVersion), URIParamUtils.encodeKeyForHeader(id, protocolVersion));
}
//Verify that a null status was not passed into the CreateResponse. If so, this is a developer error.
if (createResponse.getStatus() == null) {
throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. HttpStatus is null inside of a CreateResponse from the resource method: " + routingResult.getResourceMethod());
}
RestLiResponseDataImpl responseData = new RestLiResponseDataImpl(createResponse.getStatus(), headers, cookies);
CreateResponseEnvelope responseEnvelope;
if (createResponse instanceof CreateKVResponse) {
final ResourceContext resourceContext = routingResult.getContext();
DataMap entityData = ((CreateKVResponse) createResponse).getEntity().data();
final DataMap data = RestUtils.projectFields(entityData, resourceContext.getProjectionMode(), resourceContext.getProjectionMask());
responseEnvelope = new CreateResponseEnvelope(new AnyRecord(data), true, responseData);
} else //Instance of idResponse
{
IdResponse<?> idResponse = new IdResponse<Object>(id);
responseEnvelope = new CreateResponseEnvelope(idResponse, responseData);
}
responseData.setResponseEnvelope(responseEnvelope);
return responseData;
}
use of com.linkedin.restli.internal.server.ServerResourceContext in project rest.li by linkedin.
the class TestRestLiResponseHandler method testCollections.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "basicData")
public void testCollections(AcceptTypeData acceptTypeData, ProtocolVersion protocolVersion, String errorResponseHeaderName) throws Exception {
ResourceModel resourceModel = buildResourceModel(StatusCollectionResource.class);
ResourceMethodDescriptor methodDescriptor = resourceModel.findNamedMethod("search");
RestResponse response;
// #1 check datamap/entity structure
ServerResourceContext context = new ResourceContextImpl();
RestUtils.validateRequestHeadersAndUpdateResourceContext(acceptTypeData.acceptHeaders, context);
response = _responseHandler.buildResponse(buildRequest(acceptTypeData.acceptHeaders, protocolVersion), new RoutingResult(context, methodDescriptor), buildStatusList(3));
checkResponse(response, 200, 2, acceptTypeData.responseContentType, CollectionResponse.class.getName(), Status.class.getName(), true, errorResponseHeaderName);
String baseUri = "/test?someParam=foo";
// #1.1 using CollectionResult
response = invokeResponseHandler(baseUri + "&start=0&count=5", methodDescriptor, new BasicCollectionResult<Status>(buildStatusList(5)), acceptTypeData.acceptHeaders, protocolVersion);
checkCollectionResponse(response, 5, 0, 5, 1, null, null, null, acceptTypeData);
// #1.1 using CollectionResult (with total)
response = invokeResponseHandler(baseUri + "&start=0&count=5", methodDescriptor, new BasicCollectionResult<Status>(buildStatusList(5), 10), acceptTypeData.acceptHeaders, protocolVersion);
checkCollectionResponse(response, 5, 0, 5, 1, 10, null, null, acceptTypeData);
// using CollectionResult with metadata RecordTemplate
CollectionMetadata metadata = new CollectionMetadata();
metadata.setCount(42);
response = invokeResponseHandler(baseUri + "&start=0&count=5", methodDescriptor, new CollectionResult<Status, CollectionMetadata>(buildStatusList(5), 10, metadata), acceptTypeData.acceptHeaders, protocolVersion);
checkCollectionResponse(response, 5, 0, 5, 1, 10, null, null, acceptTypeData);
DataMap dataMap = acceptTypeData.dataCodec.readMap(response.getEntity().asInputStream());
CollectionResponse<Status> collectionResponse = new CollectionResponse<Status>(dataMap, Status.class);
assertEquals(new CollectionMetadata(collectionResponse.getMetadataRaw()), metadata);
// #2 pagination: first page, no next
response = invokeResponseHandler(baseUri + "&start=0&count=5", methodDescriptor, buildStatusList(3), acceptTypeData.acceptHeaders, protocolVersion);
checkCollectionResponse(response, 3, 0, 5, 0, null, null, null, acceptTypeData);
// #3 pagination: first page, has next (boundary case)
response = invokeResponseHandler(baseUri + "&start=0&count=5", methodDescriptor, buildStatusList(5), acceptTypeData.acceptHeaders, protocolVersion);
//"/test?count=5&start=5&someParam=foo"
final Map<String, String> queryParamsMap3next = new HashMap<String, String>();
queryParamsMap3next.put("count", "5");
queryParamsMap3next.put("start", "5");
queryParamsMap3next.put("someParam", "foo");
final URIDetails expectedURIDetails3next = new URIDetails(protocolVersion, "/test", null, queryParamsMap3next, null);
checkCollectionResponse(response, 5, 0, 5, 1, null, null, expectedURIDetails3next, acceptTypeData);
// #4 pagination: second page, has prev/ext
response = invokeResponseHandler(baseUri + "&start=5&count=5", methodDescriptor, buildStatusList(5), acceptTypeData.acceptHeaders, protocolVersion);
//"/test?count=5&start=0&someParam=foo", "/test?count=5&start=10&someParam=foo",
final Map<String, String> queryParamsMap4prev = new HashMap<String, String>();
queryParamsMap4prev.put("count", "5");
queryParamsMap4prev.put("start", "0");
queryParamsMap4prev.put("someParam", "foo");
final URIDetails expectedURIDetails4prev = new URIDetails(protocolVersion, "/test", null, queryParamsMap4prev, null);
final Map<String, String> queryParamsMap4next = new HashMap<String, String>();
queryParamsMap4next.put("count", "5");
queryParamsMap4next.put("start", "10");
queryParamsMap4next.put("someParam", "foo");
final URIDetails expectedURIDetails4next = new URIDetails(protocolVersion, "/test", null, queryParamsMap4next, null);
checkCollectionResponse(response, 5, 5, 5, 2, null, expectedURIDetails4prev, expectedURIDetails4next, acceptTypeData);
// #5 pagination:last page, has prev
response = invokeResponseHandler(baseUri + "&start=10&count=5", methodDescriptor, buildStatusList(4), acceptTypeData.acceptHeaders, protocolVersion);
//"/test?count=5&start=5&someParam=foo"
final Map<String, String> queryParamsMap5prev = new HashMap<String, String>();
queryParamsMap5prev.put("count", "5");
queryParamsMap5prev.put("start", "5");
queryParamsMap5prev.put("someParam", "foo");
final URIDetails expectedURIDetails5prev = new URIDetails(protocolVersion, "/test", null, queryParamsMap5prev, null);
checkCollectionResponse(response, 4, 10, 5, 1, null, expectedURIDetails5prev, null, acceptTypeData);
response = invokeResponseHandler(baseUri + "&start=10&count=5", methodDescriptor, new BasicCollectionResult<Status>(buildStatusList(4), 15), acceptTypeData.acceptHeaders, protocolVersion);
//"/test?count=5&start=5&someParam=foo", "/test?count=5&start=14&someParam=foo"
final Map<String, String> queryParamsMap6prev = new HashMap<String, String>();
queryParamsMap6prev.put("count", "5");
queryParamsMap6prev.put("start", "5");
queryParamsMap6prev.put("someParam", "foo");
final URIDetails expectedURIDetails6prev = new URIDetails(protocolVersion, "/test", null, queryParamsMap6prev, null);
final Map<String, String> queryParamsMap6next = new HashMap<String, String>();
queryParamsMap6next.put("count", "5");
queryParamsMap6next.put("start", "14");
queryParamsMap6next.put("someParam", "foo");
final URIDetails expectedURIDetails6next = new URIDetails(protocolVersion, "/test", null, queryParamsMap6next, null);
checkCollectionResponse(response, 4, 10, 5, 2, 15, expectedURIDetails6prev, expectedURIDetails6next, acceptTypeData);
response = invokeResponseHandler(baseUri + "&start=10&count=5", methodDescriptor, new BasicCollectionResult<Status>(buildStatusList(4), 14), acceptTypeData.acceptHeaders, protocolVersion);
//"/test?count=5&start=5&someParam=foo"
final Map<String, String> queryParamsMap7prev = new HashMap<String, String>();
queryParamsMap7prev.put("count", "5");
queryParamsMap7prev.put("start", "5");
queryParamsMap7prev.put("someParam", "foo");
final URIDetails expectedURIDetails7prev = new URIDetails(protocolVersion, "/test", null, queryParamsMap7prev, null);
checkCollectionResponse(response, 4, 10, 5, 1, 14, expectedURIDetails7prev, null, acceptTypeData);
}
use of com.linkedin.restli.internal.server.ServerResourceContext in project rest.li by linkedin.
the class TestRestLiResponseHandler method testGetRequestCookies.
@Test
public void testGetRequestCookies() throws URISyntaxException, RestLiSyntaxException {
List<HttpCookie> cookies = Arrays.asList(new HttpCookie("cook1", "value1"), new HttpCookie("cook2", "value2"));
RestRequest request = new RestRequestBuilder(new URI("http://www.abc.org/")).setMethod("DONT_CARE").setHeaders(new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER)).setCookies(CookieUtil.encodeCookies(cookies)).build();
ServerResourceContext resourceContext = new ResourceContextImpl(new PathKeysImpl(), request, new RequestContext());
Assert.assertEquals(resourceContext.getRequestCookies(), cookies);
}
Aggregations