Search in sources :

Example 41 with Client

use of com.linkedin.r2.transport.common.Client in project rest.li by linkedin.

the class Bootstrap method createHttpClient.

public static Client createHttpClient(FilterChain filters, boolean restOverStream) {
    HashMap<String, String> properties = new HashMap<>();
    properties.put(HttpClientFactory.HTTP_PROTOCOL_VERSION, HttpProtocolVersion.HTTP_1_1.name());
    final TransportClient client = new HttpClientFactory.Builder().setFilterChain(filters).build().getClient(properties);
    return new TransportClientAdapter(client, restOverStream);
}
Also used : TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) HashMap(java.util.HashMap) TransportClientAdapter(com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter) HttpClientFactory(com.linkedin.r2.transport.http.client.HttpClientFactory)

Example 42 with Client

use of com.linkedin.r2.transport.common.Client in project rest.li by linkedin.

the class TestRestClientRequestBuilder method clientGeneratedStreamRequest.

//This is similar to clientGeneratedRestRequest above except that it will generate a StreamRequest instead
//of a RestRequest. Note that this will ONLY happen if either acceptResponseAttachments below is 'true' OR
//streamingAttachmentDataSources below is non-null with a size greater then 0. If both of these do not hold,
//then a StreamRequest will not be generated by the RestClient.
@SuppressWarnings({ "unchecked", "rawtypes", "deprecation" })
private <T extends Request> StreamRequest clientGeneratedStreamRequest(Class<T> requestClass, ResourceMethod method, DataMap entityBody, RestClient.ContentType contentType, List<RestClient.AcceptType> acceptTypes, boolean acceptContentTypePerClient, List<Object> streamingAttachmentDataSources, boolean acceptResponseAttachments) 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 there is a desire to receive response attachments, then we must use request options.
    if (!acceptContentTypePerClient || acceptResponseAttachments) {
        requestOptions = new RestliRequestOptions(ProtocolVersionOption.USE_LATEST_IF_AVAILABLE, null, null, contentType, acceptTypes, acceptResponseAttachments);
    }
    setCommonExpectations(mockRequest, method, mockResponseDecoder, requestOptions);
    if (streamingAttachmentDataSources != null && streamingAttachmentDataSources.size() > 0) {
        EasyMock.expect(mockRequest.getStreamingAttachments()).andReturn(streamingAttachmentDataSources).times(2);
    } else {
        EasyMock.expect(mockRequest.getStreamingAttachments()).andReturn(null).times(2);
    }
    setResourceMethodExpectations(method, mockRequest, mockRecordTemplate, entityBody);
    Capture<StreamRequest> streamRequestCapture = new Capture<StreamRequest>();
    EasyMock.expect(mockClient.getMetadata(new URI(HOST + SERVICE_NAME))).andReturn(Collections.<String, Object>emptyMap()).once();
    mockClient.streamRequest(EasyMock.capture(streamRequestCapture), (RequestContext) EasyMock.anyObject(), (Callback<StreamResponse>) 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 streamRequestCapture.getValue();
}
Also used : StreamResponse(com.linkedin.r2.message.stream.StreamResponse) ByteString(com.linkedin.data.ByteString) URI(java.net.URI) Capture(org.easymock.Capture) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) RestResponseDecoder(com.linkedin.restli.internal.client.RestResponseDecoder) RecordTemplate(com.linkedin.data.template.RecordTemplate) Client(com.linkedin.r2.transport.common.Client)

Example 43 with Client

use of com.linkedin.r2.transport.common.Client 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 44 with Client

use of com.linkedin.r2.transport.common.Client in project rest.li by linkedin.

the class ExampleRequestResponseGenerator method buildRequest.

private RestRequest buildRequest(Request<?> request) {
    ProtocolVersion protocolVersion;
    switch(_requestOptions.getProtocolVersionOption()) {
        case FORCE_USE_LATEST:
            protocolVersion = AllProtocolVersions.LATEST_PROTOCOL_VERSION;
            break;
        case USE_LATEST_IF_AVAILABLE:
            protocolVersion = AllProtocolVersions.BASELINE_PROTOCOL_VERSION;
            break;
        case FORCE_USE_NEXT:
            protocolVersion = AllProtocolVersions.NEXT_PROTOCOL_VERSION;
            break;
        case FORCE_USE_PREVIOUS:
            protocolVersion = AllProtocolVersions.PREVIOUS_PROTOCOL_VERSION;
            break;
        default:
            throw new IllegalArgumentException("Unsupported enum value: " + _requestOptions.getProtocolVersionOption());
    }
    URI uri = RestliUriBuilderUtil.createUriBuilder(request, "", protocolVersion).build();
    RestRequestBuilder requestBuilder = new RestRequestBuilder(uri);
    requestBuilder.setMethod(request.getMethod().getHttpMethod().name());
    // unfortunately some headers get set in RestClient, and since we're not using rest client, we
    // replicate that behavior here
    requestBuilder.setHeader(RestConstants.HEADER_ACCEPT, RestConstants.HEADER_VALUE_APPLICATION_JSON);
    requestBuilder.setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, protocolVersion.toString());
    if (request.getMethod().getHttpMethod() == HttpMethod.POST) {
        requestBuilder.setHeader(RestConstants.HEADER_RESTLI_REQUEST_METHOD, request.getMethod().toString());
    }
    if (request.getInputRecord() != null) {
        requestBuilder.setHeader(RestConstants.HEADER_CONTENT_TYPE, RestConstants.HEADER_VALUE_APPLICATION_JSON);
        writeEntity(request, protocolVersion, requestBuilder);
    }
    return requestBuilder.build();
}
Also used : RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) ProtocolVersion(com.linkedin.restli.common.ProtocolVersion) URI(java.net.URI)

Example 45 with Client

use of com.linkedin.r2.transport.common.Client in project rest.li by linkedin.

the class RestLiExampleBasicClient method sendRequest.

public void sendRequest(String pathInfo, PrintWriter respWriter) {
    /* Supported URLs
     *  /fail: just fail
     *  /album/<album_id>: display album
     *  all others: make photos, get photos, purge photos
     */
    try {
        if (pathInfo.equals("/fail")) {
            getNonPhoto();
        } else {
            if (pathInfo.startsWith("/album/")) {
                getAlbum(respWriter, Long.parseLong(pathInfo.substring("/album/".length())));
            } else {
                // this track does not make any assumption on server
                // we need to create photo first, find it and clean them up
                // we use both sync and async approaches for creating
                final long newPhotoId = createPhoto(respWriter);
                final CountDownLatch latch = new CountDownLatch(1);
                createPhotoAsync(respWriter, latch, newPhotoId);
                getPhoto(respWriter, newPhotoId);
                findPhoto(respWriter);
                partialUpdatePhoto(respWriter, newPhotoId);
                // photos and albums have IDs starting from 1
                getAlbumSummary(respWriter, (long) new Random().nextInt(10) + 1);
                purgeAllPhotos(respWriter);
                try {
                    latch.await();
                } catch (InterruptedException e) {
                    respWriter.println(e.getMessage());
                }
            }
        }
    } catch (RemoteInvocationException e) {
        respWriter.println("Error in example client: " + e.getMessage());
    }
    respWriter.flush();
}
Also used : Random(java.util.Random) RemoteInvocationException(com.linkedin.r2.RemoteInvocationException) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

Test (org.testng.annotations.Test)126 RequestContext (com.linkedin.r2.message.RequestContext)94 URI (java.net.URI)82 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)65 FutureCallback (com.linkedin.common.callback.FutureCallback)62 RestRequest (com.linkedin.r2.message.rest.RestRequest)62 HashMap (java.util.HashMap)61 RestResponse (com.linkedin.r2.message.rest.RestResponse)48 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)44 ArrayList (java.util.ArrayList)37 None (com.linkedin.common.util.None)36 Client (com.linkedin.r2.transport.common.Client)36 TransportClient (com.linkedin.r2.transport.common.bridge.client.TransportClient)36 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)35 CountDownLatch (java.util.concurrent.CountDownLatch)35 TrackerClient (com.linkedin.d2.balancer.clients.TrackerClient)34 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)33 ExecutionException (java.util.concurrent.ExecutionException)33 RemoteInvocationException (com.linkedin.r2.RemoteInvocationException)26 TransportClientFactory (com.linkedin.r2.transport.common.TransportClientFactory)26