use of com.linkedin.r2.message.rest.RestResponse in project rest.li by linkedin.
the class TestRestClientRequestBuilder method clientGeneratedRestRequest.
@SuppressWarnings({ "unchecked", "rawtypes", "deprecation" })
private <T extends Request> RestRequest clientGeneratedRestRequest(Class<T> requestClass, ResourceMethod method, DataMap entityBody, RestClient.ContentType contentType, List<RestClient.AcceptType> acceptTypes, boolean acceptContentTypePerClient) throws URISyntaxException {
// massive setup...
Client mockClient = EasyMock.createMock(Client.class);
@SuppressWarnings({ "rawtypes" }) Request<?> mockRequest = EasyMock.createMock(requestClass);
RecordTemplate mockRecordTemplate = EasyMock.createMock(RecordTemplate.class);
@SuppressWarnings({ "rawtypes" }) RestResponseDecoder mockResponseDecoder = EasyMock.createMock(RestResponseDecoder.class);
RestliRequestOptions requestOptions = RestliRequestOptions.DEFAULT_OPTIONS;
if (!acceptContentTypePerClient) {
requestOptions = new RestliRequestOptions(ProtocolVersionOption.USE_LATEST_IF_AVAILABLE, null, null, contentType, acceptTypes, false);
}
setCommonExpectations(mockRequest, method, mockResponseDecoder, requestOptions);
EasyMock.expect(mockRequest.getStreamingAttachments()).andReturn(null).times(2);
setResourceMethodExpectations(method, mockRequest, mockRecordTemplate, entityBody);
Capture<RestRequest> restRequestCapture = new Capture<RestRequest>();
EasyMock.expect(mockClient.getMetadata(new URI(HOST + SERVICE_NAME))).andReturn(Collections.<String, Object>emptyMap()).once();
mockClient.restRequest(EasyMock.capture(restRequestCapture), (RequestContext) EasyMock.anyObject(), (Callback<RestResponse>) EasyMock.anyObject());
EasyMock.expectLastCall().once();
EasyMock.replay(mockClient, mockRequest, mockRecordTemplate);
// do work!
RestClient restClient;
if (acceptContentTypePerClient) {
// configuration per client
restClient = new RestClient(mockClient, HOST, contentType, acceptTypes);
} else {
// configuration per request
restClient = new RestClient(mockClient, HOST);
}
restClient.sendRequest(mockRequest);
return restRequestCapture.getValue();
}
use of com.linkedin.r2.message.rest.RestResponse in project rest.li by linkedin.
the class RestLiServer method respondWithPreRoutingError.
private void respondWithPreRoutingError(Throwable th, RestRequest request, RestLiAttachmentReader attachmentReader, RequestExecutionCallback<RestResponse> callback) {
RestLiFilterResponseContextFactory<Object> filterResponseContextFactory = new RestLiFilterResponseContextFactory<Object>(request, null, _responseHandler);
RestLiResponseData responseData = filterResponseContextFactory.fromThrowable(th).getResponseData();
RestException restException = _responseHandler.buildRestException(th, _responseHandler.buildPartialResponse(null, responseData));
callback.onError(restException, createEmptyExecutionReport(), attachmentReader, null);
}
use of com.linkedin.r2.message.rest.RestResponse 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.r2.message.rest.RestResponse in project incubator-gobblin by apache.
the class AvroR2JoinConverter method createHttpClient.
@Override
protected HttpClient<RestRequest, RestResponse> createHttpClient(Config config, SharedResourcesBroker<GobblinScopeTypes> broker) {
String urlTemplate = config.getString(HttpConstants.URL_TEMPLATE);
// By default, use http schema
R2ClientFactory.Schema schema = R2ClientFactory.Schema.HTTP;
if (urlTemplate.startsWith(HttpConstants.SCHEMA_D2)) {
schema = R2ClientFactory.Schema.D2;
}
R2ClientFactory factory = new R2ClientFactory(schema);
Client client = factory.createInstance(config);
return new R2Client(client, config, broker);
}
use of com.linkedin.r2.message.rest.RestResponse in project rest.li by linkedin.
the class RetryClient method restRequest.
@Override
public void restRequest(final RestRequest request, final RequestContext requestContext, final Callback<RestResponse> callback) {
if (_restRetryEnabled) {
RestRequest newRequest = request.builder().setHeader(HttpConstants.HEADER_NUMBER_OF_RETRY_ATTEMPTS, "0").build();
ClientRetryTracker retryTracker = updateRetryTracker(newRequest.getURI(), false);
final Callback<RestResponse> transportCallback = new RestRetryRequestCallback(newRequest, requestContext, callback, retryTracker);
_d2Client.restRequest(newRequest, requestContext, transportCallback);
} else {
_d2Client.restRequest(request, requestContext, callback);
}
}
Aggregations