Search in sources :

Example 16 with Request

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

the class TestMultiplexedRequestHandlerImpl method testIsMultiplexedRequest.

@Test(dataProvider = "multiplexerConfigurations")
public void testIsMultiplexedRequest(MultiplexerRunMode multiplexerRunMode) throws Exception {
    MultiplexedRequestHandlerImpl multiplexer = createMultiplexer(null, multiplexerRunMode);
    RestRequest request = fakeMuxRestRequest();
    assertTrue(multiplexer.isMultiplexedRequest(request));
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) Test(org.testng.annotations.Test)

Example 17 with Request

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

the class TestRestLiRouting method testRoutingDetailsSimpleGet.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "routingDetailsSimple")
public void testRoutingDetailsSimpleGet(ProtocolVersion version, String uri) throws Exception {
    Map<String, ResourceModel> pathRootResourceMap = buildResourceModels(TrendingResource.class);
    _router = new RestLiRouter(pathRootResourceMap);
    RestRequest request = createRequest(uri, "GET", version);
    RoutingResult result = _router.process(request, new RequestContext(), null);
    assertNotNull(result);
    ResourceMethodDescriptor resourceMethodDescriptor = result.getResourceMethod();
    assertNotNull(resourceMethodDescriptor);
    assertEquals(resourceMethodDescriptor.getType(), ResourceMethod.GET);
    assertNull(resourceMethodDescriptor.getActionName());
    assertNull(resourceMethodDescriptor.getFinderName());
    assertEquals(resourceMethodDescriptor.getMethod().getDeclaringClass(), TrendingResource.class);
    assertEquals(resourceMethodDescriptor.getMethod().getName(), "get");
    assertEquals(resourceMethodDescriptor.getMethod().getParameterTypes(), new Class<?>[] {});
    assertEquals(resourceMethodDescriptor.getResourceModel().getName(), "trending");
    assertNotNull(result.getContext());
    PathKeys keys = result.getContext().getPathKeys();
    assertNull(keys.getBatchIds());
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) PathKeys(com.linkedin.restli.server.PathKeys) RestLiRouter(com.linkedin.restli.internal.server.RestLiRouter) RestRequest(com.linkedin.r2.message.rest.RestRequest) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test)

Example 18 with Request

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

the class TestRestLiRouting method testRoutingDetailsCollectionUpdate.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "routingDetailsCollectionEntity")
public void testRoutingDetailsCollectionUpdate(ProtocolVersion version, String uri) throws Exception {
    Map<String, ResourceModel> pathRootResourceMap = buildResourceModels(StatusCollectionResource.class);
    _router = new RestLiRouter(pathRootResourceMap);
    RestRequest request = createRequest(uri, "PUT", version);
    RoutingResult result = _router.process(request, new RequestContext(), null);
    assertNotNull(result);
    ResourceMethodDescriptor resourceMethodDescriptor = result.getResourceMethod();
    assertNotNull(resourceMethodDescriptor);
    assertEquals(resourceMethodDescriptor.getType(), ResourceMethod.UPDATE);
    assertNull(resourceMethodDescriptor.getActionName());
    assertNull(resourceMethodDescriptor.getFinderName());
    assertEquals(resourceMethodDescriptor.getMethod().getDeclaringClass(), StatusCollectionResource.class);
    assertEquals(resourceMethodDescriptor.getMethod().getName(), "update");
    assertEquals(resourceMethodDescriptor.getMethod().getParameterTypes(), new Class<?>[] { Long.class, Status.class });
    assertEquals(resourceMethodDescriptor.getResourceModel().getName(), "statuses");
    assertNotNull(result.getContext());
    PathKeys keys = result.getContext().getPathKeys();
    assertEquals(keys.getAsLong("statusID"), new Long(1));
    assertNull(keys.getAsString("foo"));
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) PathKeys(com.linkedin.restli.server.PathKeys) RestLiRouter(com.linkedin.restli.internal.server.RestLiRouter) RestRequest(com.linkedin.r2.message.rest.RestRequest) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test)

Example 19 with Request

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

the class AbstractR2Servlet method readFromServletRequest.

protected RestRequest readFromServletRequest(HttpServletRequest req) throws IOException, ServletException, URISyntaxException {
    StringBuilder sb = new StringBuilder();
    sb.append(extractPathInfo(req));
    String query = req.getQueryString();
    if (query != null) {
        sb.append('?');
        sb.append(query);
    }
    URI uri = new URI(sb.toString());
    RestRequestBuilder rb = new RestRequestBuilder(uri);
    rb.setMethod(req.getMethod());
    for (Enumeration<String> headerNames = req.getHeaderNames(); headerNames.hasMoreElements(); ) {
        String headerName = headerNames.nextElement();
        if (headerName.equalsIgnoreCase(HttpConstants.REQUEST_COOKIE_HEADER_NAME)) {
            for (Enumeration<String> cookies = req.getHeaders(headerName); cookies.hasMoreElements(); ) {
                rb.addCookie(cookies.nextElement());
            }
        } else {
            for (Enumeration<String> headerValues = req.getHeaders(headerName); headerValues.hasMoreElements(); ) {
                rb.addHeaderValue(headerName, headerValues.nextElement());
            }
        }
    }
    int length = req.getContentLength();
    if (length > 0) {
        rb.setEntity(ByteString.read(req.getInputStream(), length));
    } else {
        // Known cases for not sending a content-length header in a request
        // 1. Chunked transfer encoding
        // 2. HTTP/2
        rb.setEntity(ByteString.read(req.getInputStream()));
    }
    return rb.build();
}
Also used : RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) ByteString(com.linkedin.data.ByteString) URI(java.net.URI)

Example 20 with Request

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

the class HttpDispatcher method handleRequest.

/**
   * handle a {@link com.linkedin.r2.message.stream.StreamRequest} using the given request context.
   * @see com.linkedin.r2.transport.common.bridge.server.TransportDispatcher#handleStreamRequest
   *
   * @param req the request to be handled.
   * @param context the request context.
   * @param callback the callback to be invoked with the response or error.
   */
public void handleRequest(StreamRequest req, RequestContext context, final TransportCallback<StreamResponse> callback) {
    final Map<String, String> headers = new HashMap<String, String>(req.getHeaders());
    final Map<String, String> wireAttrs = WireAttributeHelper.removeWireAttributes(headers);
    final BaseConnector connector = new BaseConnector();
    try {
        MessageType.Type msgType = MessageType.getMessageType(wireAttrs, MessageType.Type.REST);
        switch(msgType) {
            default:
            case REST:
                req.getEntityStream().setReader(connector);
                StreamRequest newReq = req.builder().build(EntityStreams.newEntityStream(connector));
                // decorate the call back so that if response is error or response finishes streaming,
                // we cancel the request stream
                TransportCallback<StreamResponse> decorateCallback = new TransportCallback<StreamResponse>() {

                    @Override
                    public void onResponse(TransportResponse<StreamResponse> response) {
                        // no need to check StreamException because that's handled by HttpBridge.httpToStreamCallback
                        if (response.hasError()) {
                            connector.cancel();
                        } else {
                            Observer observer = new Observer() {

                                @Override
                                public void onDataAvailable(ByteString data) {
                                // do nothing
                                }

                                @Override
                                public void onDone() {
                                    connector.cancel();
                                }

                                @Override
                                public void onError(Throwable e) {
                                    connector.cancel();
                                }
                            };
                            response.getResponse().getEntityStream().addObserver(observer);
                        }
                        callback.onResponse(response);
                    }
                };
                _dispatcher.handleStreamRequest(HttpBridge.toStreamRequest(newReq, headers), wireAttrs, context, HttpBridge.httpToStreamCallback(decorateCallback));
        }
    } catch (Exception e) {
        connector.cancel();
        callback.onResponse(TransportResponseImpl.<StreamResponse>error(e, Collections.<String, String>emptyMap()));
    }
}
Also used : TransportCallback(com.linkedin.r2.transport.common.bridge.common.TransportCallback) HashMap(java.util.HashMap) BaseConnector(com.linkedin.r2.message.stream.entitystream.BaseConnector) ByteString(com.linkedin.data.ByteString) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) ByteString(com.linkedin.data.ByteString) TransportResponse(com.linkedin.r2.transport.common.bridge.common.TransportResponse) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) Observer(com.linkedin.r2.message.stream.entitystream.Observer) MessageType(com.linkedin.r2.transport.common.MessageType)

Aggregations

Test (org.testng.annotations.Test)209 RestRequest (com.linkedin.r2.message.rest.RestRequest)189 RequestContext (com.linkedin.r2.message.RequestContext)124 URI (java.net.URI)112 RestResponse (com.linkedin.r2.message.rest.RestResponse)111 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)105 ByteString (com.linkedin.data.ByteString)67 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)61 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)59 RestException (com.linkedin.r2.message.rest.RestException)54 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)50 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)47 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)44 FutureCallback (com.linkedin.common.callback.FutureCallback)41 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)38 HashMap (java.util.HashMap)38 Callback (com.linkedin.common.callback.Callback)34 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)34 ExecutionException (java.util.concurrent.ExecutionException)33 BeforeTest (org.testng.annotations.BeforeTest)31