Search in sources :

Example 16 with RestRequestBuilder

use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.

the class TestQueryTunnel method getResponse.

private RestResponse getResponse(String query, RequestContext requestContext) throws Exception {
    URI uri = URI.create("http://localhost:" + _port + "/checkQuery?" + query);
    RestRequestBuilder builder = new RestRequestBuilder(uri);
    return _client.restRequest(builder.build(), requestContext).get(5000, TimeUnit.MILLISECONDS);
}
Also used : RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) URI(java.net.URI)

Example 17 with RestRequestBuilder

use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.

the class TestRestCompressionEcho method testResponseCompression.

@Test(dataProvider = "compressionEchoData")
public void testResponseCompression(Client client, long bytes) throws InterruptedException, TimeoutException, ExecutionException {
    RestRequestBuilder builder = new RestRequestBuilder((Bootstrap.createHttpURI(PORT, ECHO_URI)));
    byte[] content = new byte[(int) bytes];
    for (int i = 0; i < bytes; i++) {
        content[i] = (byte) (i % 256);
    }
    RestRequest request = builder.setEntity(content).build();
    final FutureCallback<RestResponse> callback = new FutureCallback<RestResponse>();
    RequestContext requestContext = new RequestContext();
    // OPERATION is required to enabled response compression
    requestContext.putLocalAttr(R2Constants.OPERATION, "get");
    client.restRequest(request, requestContext, callback);
    final RestResponse response = callback.get(60, TimeUnit.SECONDS);
    Assert.assertEquals(response.getStatus(), RestStatus.OK);
    Assert.assertEquals(response.getEntity().copyBytes(), content);
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 18 with RestRequestBuilder

use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.

the class TestHttpClient method testSimpleURI.

@Test(dataProvider = "configs")
public void testSimpleURI(Server server, Client client) throws Exception {
    try {
        server.start();
        // Note no trailing slash; the point of the test is to ensure this URI will
        // send a Request-URI of "/".
        URI uri = URI.create("http://localhost:" + PORT);
        RestRequestBuilder rb = new RestRequestBuilder(uri);
        rb.setMethod("GET");
        RestRequest request = rb.build();
        Future<RestResponse> f = client.restRequest(request);
        // This will block
        RestResponse response = f.get();
        assertEquals(response.getStatus(), 200);
        final FutureCallback<None> callback = new FutureCallback<None>();
        client.shutdown(callback);
        callback.get();
    } finally {
        server.stop();
    }
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) URI(java.net.URI) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 19 with RestRequestBuilder

use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.

the class RestClient method buildRestRequest.

// This throws Exception to remind the caller to deal with arbitrary exceptions including RuntimeException
// in a way appropriate for the public method that was originally invoked.
private RestRequest buildRestRequest(URI uri, ResourceMethod method, DataMap dataMap, Map<String, String> headers, List<String> cookies, ProtocolVersion protocolVersion, ContentType contentType, List<AcceptType> acceptTypes, boolean acceptResponseAttachments) throws Exception {
    RestRequestBuilder requestBuilder = new RestRequestBuilder(uri).setMethod(method.getHttpMethod().toString());
    requestBuilder.setHeaders(headers);
    requestBuilder.setCookies(cookies);
    addAcceptHeaders(requestBuilder, acceptTypes, acceptResponseAttachments);
    final ContentType type = resolveContentType(requestBuilder, dataMap, contentType);
    if (type != null) {
        requestBuilder.setHeader(RestConstants.HEADER_CONTENT_TYPE, type.getHeaderKey());
        switch(type) {
            case PSON:
                requestBuilder.setEntity(PSON_DATA_CODEC.mapToBytes(dataMap));
                break;
            case JSON:
                requestBuilder.setEntity(JACKSON_DATA_CODEC.mapToBytes(dataMap));
                break;
            default:
                throw new IllegalStateException("Unknown ContentType:" + type);
        }
    }
    addProtocolVersionHeader(requestBuilder, protocolVersion);
    if (method.getHttpMethod() == HttpMethod.POST) {
        requestBuilder.setHeader(RestConstants.HEADER_RESTLI_REQUEST_METHOD, method.toString());
    }
    return requestBuilder.build();
}
Also used : RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder)

Example 20 with RestRequestBuilder

use of com.linkedin.r2.message.rest.RestRequestBuilder in project incubator-gobblin by apache.

the class R2RestRequestBuilderTest method testBuildWriteRequest.

/**
 * Build a {@link RestRequest} from a {@link GenericRecord}
 */
public void testBuildWriteRequest() throws URISyntaxException, IOException {
    String urlTemplate = "http://www.test.com/a/part1:${part1}/a/part2:${part2}";
    String verb = "update";
    String protocolVersion = "2.0.0";
    R2RestRequestBuilder builder = spy(new R2RestRequestBuilder(urlTemplate, verb, protocolVersion));
    ArgumentCaptor<RestRequestBuilder> requestBuilderArgument = ArgumentCaptor.forClass(RestRequestBuilder.class);
    Queue<BufferedRecord<GenericRecord>> queue = HttpTestUtils.createQueue(1, false);
    AsyncRequest<GenericRecord, RestRequest> request = builder.buildRequest(queue);
    verify(builder).build(requestBuilderArgument.capture());
    RestRequestBuilder expected = new RestRequestBuilder(new URI("http://www.test.com/a/part1:01/a/part2:02?param1=01"));
    expected.setMethod("PUT");
    expected.setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, protocolVersion);
    expected.setHeader(RestConstants.HEADER_RESTLI_REQUEST_METHOD, verb.toLowerCase());
    expected.setHeader(RestConstants.HEADER_CONTENT_TYPE, RestConstants.HEADER_VALUE_APPLICATION_JSON);
    DataMap data = new DataMap();
    data.put("id", "id0");
    expected.setEntity(JACKSON_DATA_CODEC.mapToBytes(data));
    HttpTestUtils.assertEqual(requestBuilderArgument.getValue(), expected);
    Assert.assertEquals(request.getRecordCount(), 1);
    Assert.assertEquals(queue.size(), 0);
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) R2RestRequestBuilder(org.apache.gobblin.r2.R2RestRequestBuilder) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) R2RestRequestBuilder(org.apache.gobblin.r2.R2RestRequestBuilder) BufferedRecord(org.apache.gobblin.async.BufferedRecord) GenericRecord(org.apache.avro.generic.GenericRecord) URI(java.net.URI) DataMap(com.linkedin.data.DataMap)

Aggregations

RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)336 RestRequest (com.linkedin.r2.message.rest.RestRequest)290 Test (org.testng.annotations.Test)267 URI (java.net.URI)220 RestResponse (com.linkedin.r2.message.rest.RestResponse)192 RequestContext (com.linkedin.r2.message.RequestContext)155 ExecutionException (java.util.concurrent.ExecutionException)55 ByteString (com.linkedin.data.ByteString)46 FutureCallback (com.linkedin.common.callback.FutureCallback)43 RestException (com.linkedin.r2.message.rest.RestException)42 HashMap (java.util.HashMap)36 TimeoutException (java.util.concurrent.TimeoutException)29 AfterTest (org.testng.annotations.AfterTest)26 BeforeTest (org.testng.annotations.BeforeTest)26 Callback (com.linkedin.common.callback.Callback)25 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)25 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)25 CountDownLatch (java.util.concurrent.CountDownLatch)24 TransportCallbackAdapter (com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter)21 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)20