use of com.linkedin.restli.internal.server.model.ResourceMethodDescriptor in project rest.li by linkedin.
the class RestLiServer method buildRestLiArgumentBuilder.
/**
* Builder for building a {@link RestLiArgumentBuilder}
*
* @param method the REST method
* @param errorResponseBuilder the {@link ErrorResponseBuilder}
* @return a {@link RestLiArgumentBuilder}
*/
private RestLiArgumentBuilder buildRestLiArgumentBuilder(RoutingResult method, ErrorResponseBuilder errorResponseBuilder) {
ResourceMethodDescriptor resourceMethodDescriptor = method.getResourceMethod();
RestLiArgumentBuilder adapter = new MethodAdapterRegistry(errorResponseBuilder).getArgumentBuilder(resourceMethodDescriptor.getType());
if (adapter == null) {
throw new IllegalArgumentException("Unsupported method type: " + resourceMethodDescriptor.getType());
}
return adapter;
}
use of com.linkedin.restli.internal.server.model.ResourceMethodDescriptor in project rest.li by linkedin.
the class RestLiResponseHandler method buildRestLiResponseData.
/**
* Build a RestLiResponseDataInternal from response object, incoming RestRequest and RoutingResult.
*
* @param request
* {@link RestRequest}
* @param routingResult
* {@link RoutingResult}
* @param responseObject
* response value
* @return {@link RestLiResponseEnvelope}
* @throws IOException
* if cannot build response
*/
public RestLiResponseData buildRestLiResponseData(final RestRequest request, final RoutingResult routingResult, final Object responseObject) throws IOException {
ServerResourceContext context = (ServerResourceContext) routingResult.getContext();
final ProtocolVersion protocolVersion = context.getRestliProtocolVersion();
Map<String, String> responseHeaders = new TreeMap<String, String>(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)) {
RestLiResponseDataImpl responseData = new RestLiResponseDataImpl(HttpStatus.S_200_OK, responseHeaders, responseCookies);
responseData.setResponseEnvelope(new ActionResponseEnvelope(null, responseData));
return responseData;
} else if (routingResult.getResourceMethod().getType().equals(ResourceMethod.GET)) {
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());
}
}
RestLiResponseBuilder responseBuilder = chooseResponseBuilder(responseObject, routingResult);
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);
}
use of com.linkedin.restli.internal.server.model.ResourceMethodDescriptor in project rest.li by linkedin.
the class RestLiArgumentBuilderTestHelper method getMockResourceMethodDescriptor.
public static ResourceMethodDescriptor getMockResourceMethodDescriptor(ResourceModel model, List<Parameter<?>> paramList, String actionName, RecordDataSchema dataSchema) {
ResourceMethodDescriptor descriptor = createMock(ResourceMethodDescriptor.class);
if (model != null) {
expect(descriptor.getResourceModel()).andReturn(model);
}
expect(descriptor.getRequestDataSchema()).andReturn(dataSchema);
if (actionName != null) {
expect(descriptor.getActionName()).andReturn(actionName);
}
if (paramList != null) {
expect(descriptor.getParameters()).andReturn(paramList);
}
replay(descriptor);
return descriptor;
}
use of com.linkedin.restli.internal.server.model.ResourceMethodDescriptor in project rest.li by linkedin.
the class TestActionArgumentBuilder method testBuildArgumentsFailure.
@Test
public void testBuildArgumentsFailure() {
String entity = "{\"param2\":5678}";
RestRequest request = RestLiArgumentBuilderTestHelper.getMockRequest(false, entity, 3);
ResourceMethodDescriptor descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(null, getStringAndIntParams(), null, null);
ResourceContext context = RestLiArgumentBuilderTestHelper.getMockResourceContext(null, null, null, false);
RoutingResult routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, 2, context, 1);
RestLiArgumentBuilder argumentBuilder = new ActionArgumentBuilder();
RestLiRequestData requestData = argumentBuilder.extractRequestData(routingResult, request);
try {
argumentBuilder.buildArguments(requestData, routingResult);
fail("Expected RoutingException");
} catch (RoutingException e) {
assertEquals(e.getMessage(), "Parameter 'param1' is required");
}
verify(request, descriptor, routingResult);
}
use of com.linkedin.restli.internal.server.model.ResourceMethodDescriptor in project rest.li by linkedin.
the class TestRestLiMethodInvocation method testAsyncBatchDelete.
@Test
public void testAsyncBatchDelete() throws Exception {
ResourceModel statusResourceModel = buildResourceModel(AsyncStatusCollectionResource.class);
RestLiCallback<?> callback = getCallback();
ResourceMethodDescriptor methodDescriptor;
AsyncStatusCollectionResource statusResource;
methodDescriptor = statusResourceModel.findMethod(ResourceMethod.BATCH_DELETE);
statusResource = getMockResource(AsyncStatusCollectionResource.class);
@SuppressWarnings("unchecked") BatchDeleteRequest<Long, Status> mockBatchDeleteReq = (BatchDeleteRequest<Long, Status>) EasyMock.anyObject();
statusResource.batchDelete(mockBatchDeleteReq, EasyMock.<Callback<BatchUpdateResult<Long, Status>>>anyObject());
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
@SuppressWarnings("unchecked") Callback<BatchCreateResult<Long, Status>> callback = (Callback<BatchCreateResult<Long, Status>>) EasyMock.getCurrentArguments()[1];
callback.onSuccess(null);
return null;
}
});
EasyMock.replay(statusResource);
checkAsyncInvocation(statusResource, callback, methodDescriptor, "DELETE", version, "/asyncstatuses?ids=List(1,2,3)", buildBatchPathKeys(1L, 2L, 3L));
}
Aggregations