Search in sources :

Example 41 with RestResponse

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();
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) ByteString(com.linkedin.data.ByteString) URI(java.net.URI) Capture(org.easymock.Capture) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponseDecoder(com.linkedin.restli.internal.client.RestResponseDecoder) RecordTemplate(com.linkedin.data.template.RecordTemplate) Client(com.linkedin.r2.transport.common.Client)

Example 42 with RestResponse

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);
}
Also used : RestLiFilterResponseContextFactory(com.linkedin.restli.internal.server.filter.RestLiFilterResponseContextFactory) RestException(com.linkedin.r2.message.rest.RestException)

Example 43 with RestResponse

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);
}
Also used : FilterRequestContextInternalImpl(com.linkedin.restli.internal.server.filter.FilterRequestContextInternalImpl) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestLiArgumentBuilder(com.linkedin.restli.internal.server.methods.arguments.RestLiArgumentBuilder) MultiPartIllegalFormatException(com.linkedin.multipart.exceptions.MultiPartIllegalFormatException) ParseException(javax.mail.internet.ParseException) RestException(com.linkedin.r2.message.rest.RestException) RestLiAttachmentReaderException(com.linkedin.restli.common.attachments.RestLiAttachmentReaderException) RestLiFilterChain(com.linkedin.restli.internal.server.filter.RestLiFilterChain) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) FilterChainCallback(com.linkedin.restli.internal.server.filter.FilterChainCallback) RestLiFilterResponseContextFactory(com.linkedin.restli.internal.server.filter.RestLiFilterResponseContextFactory) FilterRequestContextInternal(com.linkedin.restli.internal.server.filter.FilterRequestContextInternal) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) FilterChainCallbackImpl(com.linkedin.restli.internal.server.filter.FilterChainCallbackImpl)

Example 44 with RestResponse

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);
}
Also used : R2Client(org.apache.gobblin.r2.R2Client) HttpClient(org.apache.gobblin.http.HttpClient) R2Client(org.apache.gobblin.r2.R2Client) Client(com.linkedin.r2.transport.common.Client) R2ClientFactory(org.apache.gobblin.r2.R2ClientFactory)

Example 45 with RestResponse

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);
    }
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponse(com.linkedin.r2.message.rest.RestResponse)

Aggregations

RestResponse (com.linkedin.r2.message.rest.RestResponse)350 Test (org.testng.annotations.Test)277 RestRequest (com.linkedin.r2.message.rest.RestRequest)251 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)203 RequestContext (com.linkedin.r2.message.RequestContext)166 URI (java.net.URI)153 RestException (com.linkedin.r2.message.rest.RestException)78 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)73 ByteString (com.linkedin.data.ByteString)69 HashMap (java.util.HashMap)54 ExecutionException (java.util.concurrent.ExecutionException)53 FutureCallback (com.linkedin.common.callback.FutureCallback)52 Map (java.util.Map)51 Callback (com.linkedin.common.callback.Callback)48 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)38 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)36 IOException (java.io.IOException)31 BeforeTest (org.testng.annotations.BeforeTest)31 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)29 AfterTest (org.testng.annotations.AfterTest)28