use of com.linkedin.restli.server.ResourceContext in project rest.li by linkedin.
the class BatchCreateResponseBuilder method buildRestLiResponseData.
@Override
public RestLiResponseData buildRestLiResponseData(RestRequest request, RoutingResult routingResult, Object result, Map<String, String> headers, List<HttpCookie> cookies) {
if (result instanceof BatchCreateKVResult) {
BatchCreateKVResult<?, ?> list = (BatchCreateKVResult<?, ?>) result;
if (list.getResults() == null) {
throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null List inside of a BatchCreateKVResult returned by the resource method: " + routingResult.getResourceMethod());
}
List<BatchCreateResponseEnvelope.CollectionCreateResponseItem> collectionCreateList = new ArrayList<BatchCreateResponseEnvelope.CollectionCreateResponseItem>(list.getResults().size());
for (CreateKVResponse e : list.getResults()) {
if (e == null) {
throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null element inside of List inside of a BatchCreateResult returned by the resource method: " + routingResult.getResourceMethod());
} else {
Object id = ResponseUtils.translateCanonicalKeyToAlternativeKeyIfNeeded(e.getId(), routingResult);
if (e.getError() == null) {
final ResourceContext resourceContext = routingResult.getContext();
DataMap entityData = e.getEntity() != null ? e.getEntity().data() : null;
final DataMap data = RestUtils.projectFields(entityData, resourceContext.getProjectionMode(), resourceContext.getProjectionMask());
CreateIdEntityStatus<Object, RecordTemplate> entry = new CreateIdEntityStatus<Object, RecordTemplate>(e.getStatus().getCode(), id, new AnyRecord(data), null, ProtocolVersionUtil.extractProtocolVersion(headers));
collectionCreateList.add(new BatchCreateResponseEnvelope.CollectionCreateResponseItem(entry));
} else {
collectionCreateList.add(new BatchCreateResponseEnvelope.CollectionCreateResponseItem(e.getError(), id));
}
}
}
RestLiResponseDataImpl responseData = new RestLiResponseDataImpl(HttpStatus.S_200_OK, headers, cookies);
responseData.setResponseEnvelope(new BatchCreateResponseEnvelope(collectionCreateList, true, responseData));
return responseData;
} else {
BatchCreateResult<?, ?> list = (BatchCreateResult<?, ?>) result;
//Verify that a null list was not passed into the BatchCreateResult. If so, this is a developer error.
if (list.getResults() == null) {
throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null List inside of a BatchCreateResult returned by the resource method: " + routingResult.getResourceMethod());
}
List<BatchCreateResponseEnvelope.CollectionCreateResponseItem> collectionCreateList = new ArrayList<BatchCreateResponseEnvelope.CollectionCreateResponseItem>(list.getResults().size());
for (CreateResponse e : list.getResults()) {
//Verify that a null element was not passed into the BatchCreateResult list. If so, this is a developer error.
if (e == null) {
throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null element inside of List inside of a BatchCreateResult returned by the resource method: " + routingResult.getResourceMethod());
} else {
Object id = ResponseUtils.translateCanonicalKeyToAlternativeKeyIfNeeded(e.getId(), routingResult);
if (e.getError() == null) {
CreateIdStatus<Object> entry = new CreateIdStatus<Object>(e.getStatus().getCode(), id, null, ProtocolVersionUtil.extractProtocolVersion(headers));
collectionCreateList.add(new BatchCreateResponseEnvelope.CollectionCreateResponseItem(entry));
} else {
collectionCreateList.add(new BatchCreateResponseEnvelope.CollectionCreateResponseItem(e.getError(), id));
}
}
}
RestLiResponseDataImpl responseData = new RestLiResponseDataImpl(HttpStatus.S_200_OK, headers, cookies);
responseData.setResponseEnvelope(new BatchCreateResponseEnvelope(collectionCreateList, responseData));
return responseData;
}
}
use of com.linkedin.restli.server.ResourceContext in project incubator-gobblin by apache.
the class JobExecutionInfoResource method batchGet.
@Override
public Map<ComplexResourceKey<JobExecutionQuery, EmptyRecord>, JobExecutionQueryResult> batchGet(Set<ComplexResourceKey<JobExecutionQuery, EmptyRecord>> keys) {
Map<ComplexResourceKey<JobExecutionQuery, EmptyRecord>, JobExecutionQueryResult> results = Maps.newHashMap();
for (ComplexResourceKey<JobExecutionQuery, EmptyRecord> key : keys) {
JobExecutionQueryResult result = get(key);
if (result != null) {
results.put(key, get(key));
}
}
ResourceContext rc = this.getContext();
rc.setResponseHeader("Access-Control-Allow-Origin", "*");
this.setContext(rc);
return results;
}
use of com.linkedin.restli.server.ResourceContext in project rest.li by linkedin.
the class CookieResource method setResponseCookie.
/**
* Concatonates request cookies and add it as a single response cookie.
*/
private void setResponseCookie() {
ResourceContext context = getContext();
List<HttpCookie> requestCookies = context.getRequestCookies();
if (requestCookies.size() > 0) {
for (HttpCookie elem : requestCookies) {
context.addResponseCookie(new HttpCookie(elem.getValue(), elem.getName()));
}
} else {
context.addResponseCookie(new HttpCookie("empty_name", "empty_cookie"));
}
}
use of com.linkedin.restli.server.ResourceContext in project rest.li by linkedin.
the class TestArgumentBuilder method testResourceContextParameterType.
@Test
@SuppressWarnings("deprecation")
public void testResourceContextParameterType() {
String testParamKey = "testParam";
ServerResourceContext mockResourceContext = EasyMock.createMock(ServerResourceContext.class);
List<Parameter<?>> parameters = new ArrayList<>();
Parameter<ResourceContext> param1 = new Parameter<>(testParamKey, ResourceContext.class, null, false, null, Parameter.ParamType.RESOURCE_CONTEXT, false, AnnotationSet.EMPTY);
Parameter<ResourceContext> param2 = new Parameter<>(testParamKey, ResourceContext.class, null, false, null, Parameter.ParamType.RESOURCE_CONTEXT_PARAM, false, AnnotationSet.EMPTY);
parameters.add(param1);
parameters.add(param2);
EasyMock.expect(mockResourceContext.getRequestAttachmentReader()).andReturn(null);
Object[] results = ArgumentBuilder.buildArgs(new Object[0], getMockResourceMethod(parameters), mockResourceContext, null, getMockResourceMethodConfig(false));
Assert.assertEquals(results[0], mockResourceContext);
Assert.assertEquals(results[1], mockResourceContext);
}
use of com.linkedin.restli.server.ResourceContext in project rest.li by linkedin.
the class ManualProjectionsResource method get.
@RestMethod.Get
public Greeting get(Long key, @QueryParam("ignoreProjection") @Optional("false") boolean ignoreProjection) {
ResourceContext context = getContext();
Greeting greeting = new Greeting();
context.setProjectionMode(ProjectionMode.MANUAL);
MaskTree mask = context.getProjectionMask();
if (mask != null && ignoreProjection == false) {
if (mask.getOperations().get(Greeting.fields().message()) == MaskOperation.POSITIVE_MASK_OP) {
greeting.setMessage("Projected message!");
}
if (mask.getOperations().get(Greeting.fields().tone()) == MaskOperation.POSITIVE_MASK_OP) {
greeting.setTone(Tone.FRIENDLY);
}
if (mask.getOperations().get(Greeting.fields().id()) == MaskOperation.POSITIVE_MASK_OP) {
greeting.setId(key);
}
} else {
greeting.setMessage("Full greeting.");
greeting.setTone(Tone.FRIENDLY);
greeting.setId(key);
}
return greeting;
}
Aggregations