use of com.linkedin.restli.internal.server.methods.arguments.RestLiArgumentBuilder in project rest.li by linkedin.
the class MethodAdapterRegistry method buildAdapterRegistry.
private Map<ResourceMethod, RestLiArgumentBuilder> buildAdapterRegistry() {
Map<ResourceMethod, RestLiArgumentBuilder> result = new HashMap<ResourceMethod, RestLiArgumentBuilder>(ResourceMethod.values().length);
result.put(ResourceMethod.GET, new GetArgumentBuilder());
result.put(ResourceMethod.BATCH_GET, new BatchGetArgumentBuilder());
result.put(ResourceMethod.FINDER, new CollectionArgumentBuilder());
result.put(ResourceMethod.CREATE, new CreateArgumentBuilder());
result.put(ResourceMethod.PARTIAL_UPDATE, new PatchArgumentBuilder());
result.put(ResourceMethod.UPDATE, new UpdateArgumentBuilder());
result.put(ResourceMethod.DELETE, new GetArgumentBuilder());
result.put(ResourceMethod.ACTION, new ActionArgumentBuilder());
result.put(ResourceMethod.BATCH_UPDATE, new BatchUpdateArgumentBuilder());
result.put(ResourceMethod.BATCH_PARTIAL_UPDATE, new BatchPatchArgumentBuilder());
result.put(ResourceMethod.BATCH_CREATE, new BatchCreateArgumentBuilder());
result.put(ResourceMethod.BATCH_DELETE, new BatchDeleteArgumentBuilder());
result.put(ResourceMethod.GET_ALL, new CollectionArgumentBuilder());
return Collections.unmodifiableMap(result);
}
use of com.linkedin.restli.internal.server.methods.arguments.RestLiArgumentBuilder in project rest.li by linkedin.
the class TestRestLiMethodInvocation method checkInvocation.
private void checkInvocation(Object resource, ResourceMethodDescriptor resourceMethodDescriptor, String httpMethod, ProtocolVersion version, String uri, String entityBody, MutablePathKeys pathkeys, final RequestExecutionCallback<RestResponse> callback, final boolean isDebugMode, final boolean expectRoutingException, final RestLiAttachmentReader expectedRequestAttachments, final RestLiResponseAttachments expectedResponseAttachments) throws URISyntaxException, RestLiSyntaxException {
assertNotNull(resource);
assertNotNull(resourceMethodDescriptor);
try {
EasyMock.replay(resource);
RestRequestBuilder builder = new RestRequestBuilder(new URI(uri)).setMethod(httpMethod).addHeaderValue("Accept", "application/json").setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, version.toString());
if (entityBody != null) {
builder.setEntity(entityBody.getBytes(Data.UTF_8_CHARSET));
}
RestRequest request = builder.build();
final ResourceContext resourceContext = new ResourceContextImpl(pathkeys, request, new RequestContext(), true, expectedRequestAttachments);
resourceContext.setResponseAttachments(expectedResponseAttachments);
RoutingResult routingResult = new RoutingResult(resourceContext, resourceMethodDescriptor);
FilterRequestContextInternal filterContext = new FilterRequestContextInternalImpl((ServerResourceContext) routingResult.getContext(), resourceMethodDescriptor);
final CountDownLatch latch = new CountDownLatch(1);
final CountDownLatch expectedRoutingExceptionLatch = new CountDownLatch(1);
RestLiArgumentBuilder adapter = _methodAdapterRegistry.getArgumentBuilder(resourceMethodDescriptor.getType());
RestLiRequestData requestData = adapter.extractRequestData(routingResult, request);
filterContext.setRequestData(requestData);
RestLiResponseHandler restLiResponseHandler = new RestLiResponseHandler.Builder().build();
RequestExecutionReportBuilder requestExecutionReportBuilder = null;
if (isDebugMode) {
requestExecutionReportBuilder = new RequestExecutionReportBuilder();
}
RequestExecutionCallback<RestResponse> executionCallback = new RequestExecutionCallback<RestResponse>() {
@Override
public void onError(Throwable e, RequestExecutionReport executionReport, RestLiAttachmentReader requestAttachmentReader, RestLiResponseAttachments responseAttachments) {
if (isDebugMode) {
Assert.assertNotNull(executionReport);
} else {
Assert.assertNull(executionReport);
}
if (e.getCause().getCause() instanceof RoutingException) {
expectedRoutingExceptionLatch.countDown();
}
if (callback != null) {
callback.onError(e, executionReport, null, null);
}
Assert.assertEquals(requestAttachmentReader, expectedRequestAttachments);
Assert.assertEquals(responseAttachments, expectedResponseAttachments);
latch.countDown();
}
@Override
public void onSuccess(final RestResponse result, RequestExecutionReport executionReport, RestLiResponseAttachments responseAttachments) {
if (isDebugMode) {
Assert.assertNotNull(executionReport);
} else {
Assert.assertNull(executionReport);
}
if (callback != null) {
callback.onSuccess(result, executionReport, null);
}
Assert.assertEquals(responseAttachments, expectedResponseAttachments);
latch.countDown();
}
};
FilterChainCallback filterChainCallback = new FilterChainCallbackImpl(routingResult, _invoker, adapter, requestExecutionReportBuilder, expectedRequestAttachments, restLiResponseHandler, executionCallback);
final RestLiCallback<Object> outerCallback = new RestLiCallback<Object>(filterContext, new RestLiFilterResponseContextFactory<Object>(request, routingResult, restLiResponseHandler), new RestLiFilterChain(null, filterChainCallback));
RestUtils.validateRequestHeadersAndUpdateResourceContext(request.getHeaders(), (ServerResourceContext) routingResult.getContext());
filterContext.setRequestData(adapter.extractRequestData(routingResult, request));
_invoker.invoke(filterContext.getRequestData(), routingResult, adapter, outerCallback, requestExecutionReportBuilder);
try {
latch.await();
if (expectRoutingException) {
expectedRoutingExceptionLatch.await();
}
} catch (InterruptedException e) {
// Ignore
}
EasyMock.verify(resource);
Assert.assertEquals(((ServerResourceContext) routingResult.getContext()).getResponseMimeType(), "application/json");
} catch (RestLiSyntaxException e) {
throw new RoutingException("syntax exception", 400);
} finally {
EasyMock.reset(resource);
EasyMock.makeThreadSafe(resource, true);
}
}
use of com.linkedin.restli.internal.server.methods.arguments.RestLiArgumentBuilder 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.methods.arguments.RestLiArgumentBuilder in project rest.li by linkedin.
the class RestLiServer method handleResourceRequest.
private void handleResourceRequest(final RestRequest request, final RequestContext requestContext, final RequestExecutionCallback<RestResponse> callback, final RestLiAttachmentReader attachmentReader, final boolean isDebugMode) {
try {
ensureRequestUsesValidRestliProtocol(request);
} catch (RestLiServiceException e) {
respondWithPreRoutingError(e, request, attachmentReader, callback);
return;
}
final RoutingResult method;
try {
method = _router.process(request, requestContext, attachmentReader);
} catch (Exception e) {
respondWithPreRoutingError(e, request, attachmentReader, callback);
return;
}
final RequestExecutionCallback<RestResponse> wrappedCallback = notifyInvokeAwares(method, callback);
RequestExecutionReportBuilder requestExecutionReportBuilder = null;
if (isDebugMode) {
requestExecutionReportBuilder = new RequestExecutionReportBuilder();
}
final FilterRequestContextInternal filterContext = new FilterRequestContextInternalImpl((ServerResourceContext) method.getContext(), method.getResourceMethod());
RestLiArgumentBuilder adapter;
try {
RestUtils.validateRequestHeadersAndUpdateResourceContext(request.getHeaders(), (ServerResourceContext) method.getContext());
adapter = buildRestLiArgumentBuilder(method, _errorResponseBuilder);
filterContext.setRequestData(adapter.extractRequestData(method, request));
} catch (Exception e) {
// would not trigger response filters because request filters haven't run yet
wrappedCallback.onError(e, requestExecutionReportBuilder == null ? null : requestExecutionReportBuilder.build(), ((ServerResourceContext) method.getContext()).getRequestAttachmentReader(), null);
return;
}
RestLiFilterResponseContextFactory<Object> filterResponseContextFactory = new RestLiFilterResponseContextFactory<Object>(request, method, _responseHandler);
FilterChainCallback filterChainCallback = new FilterChainCallbackImpl(method, _methodInvoker, adapter, requestExecutionReportBuilder, attachmentReader, _responseHandler, wrappedCallback);
RestLiFilterChain filterChain = new RestLiFilterChain(_filters, filterChainCallback);
filterChain.onRequest(filterContext, filterResponseContextFactory);
}
use of com.linkedin.restli.internal.server.methods.arguments.RestLiArgumentBuilder in project rest.li by linkedin.
the class TestCustomMethodAdapterProvider method initClass.
@BeforeClass
public void initClass() throws Exception {
RestLiConfig config = new RestLiConfig();
config.setMethodAdapterProvider(new DefaultMethodAdapterProvider(new ErrorResponseBuilder(ErrorResponseFormat.MESSAGE_AND_SERVICECODE)) {
@Override
public RestLiArgumentBuilder getArgumentBuilder(ResourceMethod resourceMethod) {
// Override the behavior of the CREATE argument builder
if (resourceMethod == ResourceMethod.CREATE) {
return new CreateArgumentBuilder() {
@Override
public RestLiRequestData extractRequestData(RoutingResult routingResult, DataMap dataMap) {
// Always use the FRIENDLY record regardless of the actual data
return new RestLiRequestDataImpl.Builder().entity(FRIENDLY).build();
}
};
} else {
return super.getArgumentBuilder(resourceMethod);
}
}
});
super.init(false, config);
}
Aggregations