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);
}
}
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);
}
}
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);
}
}
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();
}
};
}
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"));
}
});
}
}
}
Aggregations