Search in sources :

Example 36 with Server

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

the class RestClient method sendRestRequestImpl.

/**
 * Sends an untyped REST request using a callback.
 *
 * @param requestContext context for the request
 * @param uri for resource
 * @param method to perform
 * @param dataMap request body entity
 * @param headers additional headers to be added to the request
 * @param cookies the cookies to be sent with the request
 * @param methodName the method name (used for finders and actions)
 * @param protocolVersion the version of the Rest.li protocol used to build this request
 * @param requestOptions contains compression force on/off overrides, request content type and accept types
 * @param callback to call on request completion. In the event of an error, the callback
 *                 will receive a {@link com.linkedin.r2.RemoteInvocationException}. If a valid
 *                 error response was received from the remote server, the callback will receive
 *                 a {@link com.linkedin.r2.message.rest.RestException} containing the error details.
 */
private void sendRestRequestImpl(RequestContext requestContext, URI uri, ResourceMethod method, DataMap dataMap, Map<String, String> headers, List<String> cookies, String methodName, ProtocolVersion protocolVersion, RestliRequestOptions requestOptions, Callback<RestResponse> callback) {
    try {
        TimingContextUtil.beginTiming(requestContext, FrameworkTimingKeys.CLIENT_REQUEST_RESTLI_SERIALIZATION.key());
        RestRequest request = buildRestRequest(uri, method, dataMap, headers, cookies, protocolVersion, requestOptions.getContentType(), requestOptions.getAcceptTypes(), false);
        TimingContextUtil.endTiming(requestContext, FrameworkTimingKeys.CLIENT_REQUEST_RESTLI_SERIALIZATION.key());
        String operation = OperationNameGenerator.generate(method, methodName);
        requestContext.putLocalAttr(R2Constants.OPERATION, operation);
        requestContext.putLocalAttr(R2Constants.REQUEST_COMPRESSION_OVERRIDE, requestOptions.getRequestCompressionOverride());
        requestContext.putLocalAttr(R2Constants.RESPONSE_COMPRESSION_OVERRIDE, requestOptions.getResponseCompressionOverride());
        TimingContextUtil.endTiming(requestContext, FrameworkTimingKeys.CLIENT_REQUEST_RESTLI.key());
        TimingContextUtil.beginTiming(requestContext, FrameworkTimingKeys.CLIENT_REQUEST_R2.key());
        final Callback<RestResponse> wrappedCallback = new TimingCallback.Builder<>(callback, requestContext).addEndTimingKey(FrameworkTimingKeys.CLIENT_RESPONSE_R2.key()).addBeginTimingKey(FrameworkTimingKeys.CLIENT_RESPONSE_RESTLI.key()).build();
        _client.restRequest(request, requestContext, wrappedCallback);
    } catch (Exception e) {
        // No need to wrap the exception; RestLiCallbackAdapter.onError() will take care of that
        callback.onError(e);
    }
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponse(com.linkedin.r2.message.rest.RestResponse) ByteString(com.linkedin.data.ByteString) TimingCallback(com.linkedin.r2.message.timing.TimingCallback) URISyntaxException(java.net.URISyntaxException) MimeTypeParseException(javax.activation.MimeTypeParseException) IOException(java.io.IOException) ServiceUnavailableException(com.linkedin.d2.balancer.ServiceUnavailableException)

Example 37 with Server

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

the class RestClient method sendStreamRequestImpl.

/**
 * Sends an untyped stream request using a callback.
 *
 * @param requestContext context for the request
 * @param uri for resource
 * @param method to perform
 * @param dataMap request body entity
 * @param headers additional headers to be added to the request
 * @param cookies the cookies to be sent with the request
 * @param methodName the method name (used for finders and actions)
 * @param protocolVersion the version of the Rest.li protocol used to build this request
 * @param requestOptions contains compression force on/off overrides, request content type and accept types
 * @param callback to call on request completion. In the event of an error, the callback
 *                 will receive a {@link com.linkedin.r2.RemoteInvocationException}. If a valid
 *                 error response was received from the remote server, the callback will receive
 *                 a {@link com.linkedin.r2.message.rest.RestException} containing the error details.
 */
private void sendStreamRequestImpl(RequestContext requestContext, URI uri, ResourceMethod method, DataMap dataMap, Map<String, String> headers, List<String> cookies, String methodName, ProtocolVersion protocolVersion, RestliRequestOptions requestOptions, List<Object> streamingAttachments, Callback<StreamResponse> callback) {
    try {
        TimingContextUtil.beginTiming(requestContext, FrameworkTimingKeys.CLIENT_REQUEST_RESTLI_SERIALIZATION.key());
        final StreamRequest request = buildStreamRequest(uri, method, dataMap, headers, cookies, protocolVersion, requestOptions.getContentType(), requestOptions.getAcceptTypes(), requestOptions.getAcceptResponseAttachments(), streamingAttachments);
        TimingContextUtil.endTiming(requestContext, FrameworkTimingKeys.CLIENT_REQUEST_RESTLI_SERIALIZATION.key());
        String operation = OperationNameGenerator.generate(method, methodName);
        requestContext.putLocalAttr(R2Constants.OPERATION, operation);
        requestContext.putLocalAttr(R2Constants.REQUEST_COMPRESSION_OVERRIDE, requestOptions.getRequestCompressionOverride());
        requestContext.putLocalAttr(R2Constants.RESPONSE_COMPRESSION_OVERRIDE, requestOptions.getResponseCompressionOverride());
        TimingContextUtil.endTiming(requestContext, FrameworkTimingKeys.CLIENT_REQUEST_RESTLI.key());
        TimingContextUtil.beginTiming(requestContext, FrameworkTimingKeys.CLIENT_REQUEST_R2.key());
        final Callback<StreamResponse> wrappedCallback = new TimingCallback.Builder<>(callback, requestContext).addEndTimingKey(FrameworkTimingKeys.CLIENT_RESPONSE_R2.key()).addBeginTimingKey(FrameworkTimingKeys.CLIENT_RESPONSE_RESTLI.key()).build();
        _client.streamRequest(request, requestContext, wrappedCallback);
    } catch (Exception e) {
        // No need to wrap the exception; RestLiCallbackAdapter.onError() will take care of that
        callback.onError(e);
    }
}
Also used : StreamResponse(com.linkedin.r2.message.stream.StreamResponse) ByteString(com.linkedin.data.ByteString) TimingCallback(com.linkedin.r2.message.timing.TimingCallback) URISyntaxException(java.net.URISyntaxException) MimeTypeParseException(javax.activation.MimeTypeParseException) IOException(java.io.IOException) ServiceUnavailableException(com.linkedin.d2.balancer.ServiceUnavailableException) StreamRequest(com.linkedin.r2.message.stream.StreamRequest)

Example 38 with Server

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

the class TestStreamingGreetings method fullStreamTest.

@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestBuilderDataProvider")
public void fullStreamTest(final RootBuilderWrapper<Long, Greeting> builders) throws RemoteInvocationException {
    // Perform a create to the server to store some bytes via an attachment.
    final byte[] clientSuppliedBytes = "ClientSupplied".getBytes();
    final RestLiTestAttachmentDataSource greetingAttachment = new RestLiTestAttachmentDataSource("1", ByteString.copy(clientSuppliedBytes));
    final RootBuilderWrapper.MethodBuilderWrapper<Long, Greeting, EmptyRecord> methodBuilderWrapper = builders.create();
    methodBuilderWrapper.appendSingleAttachment(greetingAttachment);
    // Provide a header to verify the server's ability to transform the first part into the RestRequest.
    methodBuilderWrapper.setHeader("createHeader", "createHeaderValue");
    final Greeting greeting = new Greeting().setMessage("A greeting with an attachment");
    final Request<EmptyRecord> createRequest = methodBuilderWrapper.input(greeting).build();
    try {
        final Response<EmptyRecord> createResponse = getClient().sendRequest(createRequest).getResponse();
        Assert.assertEquals(createResponse.getStatus(), 201);
        // Verify that headers propagate properly.
        Assert.assertEquals(createResponse.getHeader("createHeader"), "createHeaderValue");
    } catch (final RestLiResponseException responseException) {
        Assert.fail("We should not reach here!", responseException);
    }
    // Then perform a GET and verify the bytes are present
    try {
        final Request<Greeting> getRequest = builders.get().id(1l).setHeader("getHeader", "getHeaderValue").build();
        final Response<Greeting> getResponse = getClient().sendRequest(getRequest).getResponse();
        Assert.assertEquals(getResponse.getStatus(), 200);
        // Verify that headers propagate properly.
        Assert.assertEquals(getResponse.getHeader("getHeader"), "getHeaderValue");
        Assert.assertEquals(getResponse.getHeader(RestConstants.HEADER_CONTENT_TYPE), RestConstants.HEADER_VALUE_APPLICATION_JSON);
        Assert.assertEquals(getResponse.getEntity().getMessage(), "Your greeting has an attachment since you were kind and decided you wanted to read it!");
        Assert.assertTrue(getResponse.hasAttachments(), "We must have some response attachments");
        RestLiAttachmentReader attachmentReader = getResponse.getAttachmentReader();
        final CountDownLatch latch = new CountDownLatch(1);
        final GreetingBlobReaderCallback greetingBlobReaderCallback = new GreetingBlobReaderCallback(latch);
        attachmentReader.registerAttachmentReaderCallback(greetingBlobReaderCallback);
        try {
            latch.await(3000, TimeUnit.SECONDS);
            Assert.assertEquals(greetingBlobReaderCallback.getAttachmentList().size(), 1);
            Assert.assertEquals(greetingBlobReaderCallback.getAttachmentList().get(0), ByteString.copy(clientSuppliedBytes));
        } catch (Exception exception) {
            Assert.fail();
        }
    } catch (final RestLiResponseException responseException) {
        Assert.fail("We should not reach here!", responseException);
    }
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) EmptyRecord(com.linkedin.restli.common.EmptyRecord) RootBuilderWrapper(com.linkedin.restli.test.util.RootBuilderWrapper) CountDownLatch(java.util.concurrent.CountDownLatch) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) RestLiResponseException(com.linkedin.restli.client.RestLiResponseException) RemoteInvocationException(com.linkedin.r2.RemoteInvocationException) RestLiResponseException(com.linkedin.restli.client.RestLiResponseException) RestLiTestAttachmentDataSource(com.linkedin.restli.internal.testutils.RestLiTestAttachmentDataSource) RestLiAttachmentReader(com.linkedin.restli.common.attachments.RestLiAttachmentReader) Test(org.testng.annotations.Test)

Example 39 with Server

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

the class MockHttpServerFactory method create.

/**
 * Creates a {@link HttpServer} that contains a {@link RestLiServer} to be used for testing a set of Rest.li
 * resources.
 *
 * The {@link HttpServer} uses an empty {@link FilterChain} and uses "/" as the context path.
 *
 * If the server is run in async mode (by calling this function with the last parameter {@code true}), the
 * timeout used is {@link #ASYNC_TIMEOUT}.
 *
 * Both the async and sync servers will use {@link #NUM_THREADS} threads.
 *
 * @param port the port the server will run on on localhost
 * @param config the {@link RestLiConfig} to be used by the {@link RestLiServer}
 * @param beans beans you want to inject into your Rest.li resource.
 * @param enableAsync true if the server should be async, false otherwise
 */
private static HttpServer create(int port, RestLiConfig config, Map<String, ?> beans, boolean enableAsync) {
    final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(NUM_THREADS);
    final ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS);
    EngineBuilder engineBuilder = new EngineBuilder().setTaskExecutor(scheduler).setTimerScheduler(scheduler);
    com.linkedin.parseq.AsyncCallableTask.register(engineBuilder, executor);
    final Engine engine = engineBuilder.build();
    ResourceFactory resourceFactory = createResourceFactory(beans);
    TransportDispatcher dispatcher = new DelegatingTransportDispatcher(new RestLiServer(config, resourceFactory, engine));
    final FilterChain fc = FilterChains.empty().addLastRest(new SimpleLoggingFilter());
    final HttpServer server = new HttpServerFactory(fc).createServer(port, HttpServerFactory.DEFAULT_CONTEXT_PATH, NUM_THREADS, dispatcher, enableAsync ? HttpJettyServer.ServletType.ASYNC_EVENT : HttpJettyServer.ServletType.RAP, enableAsync ? ASYNC_TIMEOUT : -1);
    return new HttpServer() {

        @Override
        public void start() throws IOException {
            server.start();
        }

        @Override
        public void stop() throws IOException {
            server.stop();
            engine.shutdown();
            executor.shutdown();
            scheduler.shutdown();
        }

        @Override
        public void waitForStop() throws InterruptedException {
            server.waitForStop();
        }
    };
}
Also used : HttpServerFactory(com.linkedin.r2.transport.http.server.HttpServerFactory) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) RestLiServer(com.linkedin.restli.server.RestLiServer) DelegatingTransportDispatcher(com.linkedin.restli.server.DelegatingTransportDispatcher) FilterChain(com.linkedin.r2.filter.FilterChain) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) HttpServer(com.linkedin.r2.transport.http.server.HttpServer) ResourceFactory(com.linkedin.restli.server.resources.ResourceFactory) InjectMockResourceFactory(com.linkedin.restli.server.mock.InjectMockResourceFactory) DelegatingTransportDispatcher(com.linkedin.restli.server.DelegatingTransportDispatcher) TransportDispatcher(com.linkedin.r2.transport.common.bridge.server.TransportDispatcher) EngineBuilder(com.linkedin.parseq.EngineBuilder) SimpleLoggingFilter(com.linkedin.r2.filter.logging.SimpleLoggingFilter) Engine(com.linkedin.parseq.Engine)

Example 40 with Server

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

the class EmailClientExample method sendTraffic.

private static void sendTraffic(Map<String, Long> trafficProportion, D2Client d2Client) throws Exception {
    for (Map.Entry<String, Long> proportion : trafficProportion.entrySet()) {
        long queryPerSecond = proportion.getValue();
        String serviceName = proportion.getKey();
        Random random = new Random();
        for (int i = 0; i < queryPerSecond; i++) {
            final URI uri = new URI("d2://" + serviceName + "?user=" + random.nextInt());
            RestRequestBuilder requestBuilder = new RestRequestBuilder(uri).setMethod("get");
            RestRequest request = requestBuilder.build();
            // we don't care about the result from the server after all,
            // you can see the traffic hits the echo server from stdout
            d2Client.restRequest(request, new Callback<RestResponse>() {

                @Override
                public void onError(Throwable e) {
                    System.err.println("URI = " + uri.toString() + " didn't get any response");
                }

                @Override
                public void onSuccess(RestResponse result) {
                    System.out.println("URI = " + uri.toString() + " was served by " + result.getEntity().asString("UTF-8"));
                }
            });
        }
    }
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) URI(java.net.URI) RestRequest(com.linkedin.r2.message.rest.RestRequest) Random(java.util.Random) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) Map(java.util.Map)

Aggregations

Test (org.testng.annotations.Test)82 RestRequest (com.linkedin.r2.message.rest.RestRequest)52 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)50 RequestContext (com.linkedin.r2.message.RequestContext)49 URI (java.net.URI)43 RestResponse (com.linkedin.r2.message.rest.RestResponse)41 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)32 FutureCallback (com.linkedin.common.callback.FutureCallback)30 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)30 CountDownLatch (java.util.concurrent.CountDownLatch)24 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)23 RestException (com.linkedin.r2.message.rest.RestException)21 ExecutionException (java.util.concurrent.ExecutionException)21 None (com.linkedin.common.util.None)20 Server (org.eclipse.jetty.server.Server)20 ByteString (com.linkedin.data.ByteString)19 HttpServerBuilder (com.linkedin.r2.testutils.server.HttpServerBuilder)19 HashMap (java.util.HashMap)15 RemoteInvocationException (com.linkedin.r2.RemoteInvocationException)13 ArrayList (java.util.ArrayList)13