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