use of com.linkedin.restli.client.RestClient in project rest.li by linkedin.
the class TestExceptionsResource2 method testNonRestException.
@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "exceptionHandlingModesDataProvider")
public void testNonRestException(boolean explicit, ErrorHandlingBehavior errorHandlingBehavior, RootBuilderWrapper<Long, Greeting> builders) {
Response<Greeting> response = null;
RestClient brokenClient = new RestClient(getDefaultTransportClient(), "http://localhost:8888/");
try {
final Request<Greeting> req = builders.get().id(1L).build();
ResponseFuture<Greeting> future;
if (explicit) {
future = brokenClient.sendRequest(req, errorHandlingBehavior);
} else {
future = brokenClient.sendRequest(req);
}
response = future.getResponse();
Assert.fail("expected exception");
} catch (RemoteInvocationException e) {
Assert.assertEquals(e.getClass(), RemoteInvocationException.class);
}
}
use of com.linkedin.restli.client.RestClient in project rest.li by linkedin.
the class TestResponseCompression method testAcceptEncodingConfiguration.
@Test(dataProvider = "encodingsData")
public void testAcceptEncodingConfiguration(String responseContentEncodings, String expectedAcceptEncoding, String expectedContentEncoding) throws RemoteInvocationException {
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(HttpClientFactory.HTTP_RESPONSE_CONTENT_ENCODINGS, responseContentEncodings);
properties.put(HttpClientFactory.HTTP_USE_RESPONSE_COMPRESSION, "true");
Client client = newTransportClient(properties);
Long[] ids = new Long[100];
for (int i = 0; i < ids.length; i++) {
ids[i] = (long) i;
}
Request<BatchResponse<Greeting>> request = new GreetingsBuilders().batchGet().ids(Arrays.asList(ids)).setHeader(EXPECTED_ACCEPT_ENCODING, expectedAcceptEncoding).build();
RestClient restClient = new RestClient(client, FILTERS_URI_PREFIX);
Response<BatchResponse<Greeting>> response = restClient.sendRequest(request).getResponse();
Assert.assertEquals(response.getHeader(TestCompressionServer.CONTENT_ENCODING_SAVED), expectedContentEncoding);
}
use of com.linkedin.restli.client.RestClient in project rest.li by linkedin.
the class TestResponseCompression method testResponseCompression.
@Test(dataProvider = "requestData")
public void testResponseCompression(Boolean useResponseCompression, CompressionConfig responseCompressionConfig, RestliRequestOptions restliRequestOptions, int idCount, String expectedAcceptEncoding, String expectedCompressionThreshold, boolean responseShouldBeCompressed) throws RemoteInvocationException, CloneNotSupportedException {
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("R2 Netty Scheduler"));
Map<String, CompressionConfig> responseCompressionConfigs = new HashMap<String, CompressionConfig>();
if (responseCompressionConfig != null) {
responseCompressionConfigs.put(SERVICE_NAME, responseCompressionConfig);
}
HttpClientFactory httpClientFactory = new HttpClientFactory(FilterChains.empty(), new NioEventLoopGroup(0, /* use default settings */
new NamedThreadFactory("R2 Nio Event Loop")), true, executor, true, executor, false, AbstractJmxManager.NULL_JMX_MANAGER, Integer.MAX_VALUE, Collections.<String, CompressionConfig>emptyMap(), responseCompressionConfigs, true);
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(HttpClientFactory.HTTP_SERVICE_NAME, SERVICE_NAME);
if (useResponseCompression != null) {
properties.put(HttpClientFactory.HTTP_USE_RESPONSE_COMPRESSION, String.valueOf(useResponseCompression));
}
TransportClientAdapter clientAdapter1 = new TransportClientAdapter(httpClientFactory.getClient(properties));
RestClient client = new RestClient(clientAdapter1, FILTERS_URI_PREFIX);
Long[] ids = new Long[idCount];
for (int i = 0; i < ids.length; i++) {
ids[i] = (long) i;
}
BatchGetRequestBuilder<Long, Greeting> builder = new GreetingsBuilders(restliRequestOptions).batchGet().ids(Arrays.asList(ids)).setHeader(EXPECTED_ACCEPT_ENCODING, expectedAcceptEncoding);
if (expectedCompressionThreshold != null) {
builder.setHeader(EXPECTED_COMPRESSION_THRESHOLD, expectedCompressionThreshold);
}
Request<BatchResponse<Greeting>> request = builder.build();
Response<BatchResponse<Greeting>> response = client.sendRequest(request).getResponse();
if (responseShouldBeCompressed) {
Assert.assertEquals(response.getHeader(TestCompressionServer.CONTENT_ENCODING_SAVED), EncodingType.GZIP.getHttpName());
} else {
Assert.assertNull(response.getHeader(TestCompressionServer.CONTENT_ENCODING_SAVED));
}
}
use of com.linkedin.restli.client.RestClient in project rest.li by linkedin.
the class RestLiExampleBasicClient method main.
/**
* This is a stand-alone app to demo the use of client-side Pegasus API. To run in,
* com.linkedin.restli.example.RestLiExamplesServer has to be running.
*
* The only argument is the path to the resource on the photo server, e.g. /photos/1
*/
public static void main(String[] args) throws Exception {
// create HTTP Netty client with default properties
final HttpClientFactory http = new HttpClientFactory();
final TransportClient transportClient = http.getClient(Collections.<String, String>emptyMap());
// create an abstraction layer over the actual client, which supports both REST and RPC
final Client r2Client = new TransportClientAdapter(transportClient);
// REST client wrapper that simplifies the interface
final StringBuilder serverUrlBuilder = new StringBuilder("http://").append(SERVER_HOSTNAME).append(":").append(SERVER_PORT).append("/");
final RestClient restClient = new RestClient(r2Client, serverUrlBuilder.toString());
final RestLiExampleBasicClient photoClient = new RestLiExampleBasicClient(restClient);
String pathInfo = args.length == 0 ? "" : args[0];
photoClient.sendRequest(pathInfo, new PrintWriter(System.out));
photoClient.shutdown();
http.shutdown(new FutureCallback<None>());
}
use of com.linkedin.restli.client.RestClient in project rest.li by linkedin.
the class RestLiFortunesClient method main.
/**
* This stand-alone app demos the client-side Pegasus API.
* To see the demo, run RestLiFortuneServer, then start the client
*/
public static void main(String[] args) throws Exception {
// Create an HttpClient and wrap it in an abstraction layer
final HttpClientFactory http = new HttpClientFactory();
final Client r2Client = new TransportClientAdapter(http.getClient(Collections.<String, String>emptyMap()));
// Create a RestClient to talk to localhost:8080
RestClient restClient = new RestClient(r2Client, "http://localhost:8080/");
// Generate a random ID for a fortune cookie, in the range 0-5
long fortuneId = (long) (Math.random() * 5);
// Construct a request for the specified fortune
FortunesGetBuilder getBuilder = _fortuneBuilder.get();
Request<Fortune> getReq = getBuilder.id(fortuneId).build();
// Send the request and wait for a response
final ResponseFuture<Fortune> getFuture = restClient.sendRequest(getReq);
final Response<Fortune> resp = getFuture.getResponse();
// Print the response
System.out.println(resp.getEntity().getFortune());
// shutdown
restClient.shutdown(new FutureCallback<None>());
http.shutdown(new FutureCallback<None>());
}
Aggregations