use of java.net.HttpCookie in project rest.li by linkedin.
the class RestLiResponseHandler method buildRestLiResponseData.
/**
* Build a RestLiResponseDataInternal from response object, incoming RestRequest and RoutingResult.
*
* @param request
* {@link RestRequest}
* @param routingResult
* {@link RoutingResult}
* @param responseObject
* response value
* @return {@link RestLiResponseEnvelope}
* @throws IOException
* if cannot build response
*/
public RestLiResponseData buildRestLiResponseData(final RestRequest request, final RoutingResult routingResult, final Object responseObject) throws IOException {
ServerResourceContext context = (ServerResourceContext) routingResult.getContext();
final ProtocolVersion protocolVersion = context.getRestliProtocolVersion();
Map<String, String> responseHeaders = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
responseHeaders.putAll(context.getResponseHeaders());
responseHeaders.put(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, protocolVersion.toString());
List<HttpCookie> responseCookies = context.getResponseCookies();
if (responseObject == null) {
//If we have a null result, we have to assign the correct response status
if (routingResult.getResourceMethod().getType().equals(ResourceMethod.ACTION)) {
RestLiResponseDataImpl responseData = new RestLiResponseDataImpl(HttpStatus.S_200_OK, responseHeaders, responseCookies);
responseData.setResponseEnvelope(new ActionResponseEnvelope(null, responseData));
return responseData;
} else if (routingResult.getResourceMethod().getType().equals(ResourceMethod.GET)) {
throw new RestLiServiceException(HttpStatus.S_404_NOT_FOUND, "Requested entity not found: " + routingResult.getResourceMethod());
} else {
//All other cases do not permit null to be returned
throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null returned by the resource method: " + routingResult.getResourceMethod());
}
}
RestLiResponseBuilder responseBuilder = chooseResponseBuilder(responseObject, routingResult);
if (responseBuilder == null) {
// this should not happen if valid return types are specified
ResourceMethodDescriptor resourceMethod = routingResult.getResourceMethod();
String fqMethodName = resourceMethod.getResourceModel().getResourceClass().getName() + '#' + routingResult.getResourceMethod().getMethod().getName();
throw new RestLiInternalException("Invalid return type '" + responseObject.getClass() + " from method '" + fqMethodName + '\'');
}
return responseBuilder.buildRestLiResponseData(request, routingResult, responseObject, responseHeaders, responseCookies);
}
use of java.net.HttpCookie in project rest.li by linkedin.
the class CookieResource method setResponseCookie.
/**
* Concatonates request cookies and add it as a single response cookie.
*/
private void setResponseCookie() {
ResourceContext context = getContext();
List<HttpCookie> requestCookies = context.getRequestCookies();
if (requestCookies.size() > 0) {
for (HttpCookie elem : requestCookies) {
context.addResponseCookie(new HttpCookie(elem.getValue(), elem.getName()));
}
} else {
context.addResponseCookie(new HttpCookie("empty_name", "empty_cookie"));
}
}
use of java.net.HttpCookie in project rest.li by linkedin.
the class TestCookieResource method testClearCookies.
/**
* Test the clear cookie functionality
*
* @throws RemoteInvocationException
*/
@Test
public void testClearCookies() throws RemoteInvocationException {
List<HttpCookie> requestCookies = Arrays.asList(new HttpCookie("will", "1"), new HttpCookie("lost", "1"));
GetRequest<Greeting> req = new CookieBuilders().get().id(1L).setCookies(requestCookies).clearCookies().build();
Response<Greeting> resp = REST_CLIENT.sendRequest(req).getResponse();
List<String> responseCookies = CookieUtil.encodeSetCookies(resp.getCookies());
// Since the cookies are cleared, there should be no response from the server
Assert.assertEquals(CookieUtil.decodeSetCookies(responseCookies), Collections.singletonList(new HttpCookie("empty_name", "empty_cookie")));
}
use of java.net.HttpCookie in project rest.li by linkedin.
the class TestCookieResource method testCookiesNormal.
/**
* A customized get test for inquring the cookies routing, look up in Cookie resource file
*
* @throws RemoteInvocationException
*/
@Test
public void testCookiesNormal() throws RemoteInvocationException {
List<HttpCookie> requestCookies = Arrays.asList(new HttpCookie("GET", "10"));
GetRequest<Greeting> req = new CookieBuilders().get().id(1L).setCookies(requestCookies).build();
Response<Greeting> resp = REST_CLIENT.sendRequest(req).getResponse();
Assert.assertEquals(resp.getCookies(), Collections.singletonList(new HttpCookie("10", "GET")));
}
use of java.net.HttpCookie in project rest.li by linkedin.
the class TestCookieResource method testCookieBatchGet.
/**
* Try a batch command get to see the cookie setting is ok or not
*
* @throws RemoteInvocationException
*/
@Test
public void testCookieBatchGet() throws RemoteInvocationException {
List<HttpCookie> requestCookies = Arrays.asList(new HttpCookie("B", "1"), new HttpCookie("A", "2"), new HttpCookie("G", "3"), new HttpCookie("E", "4"), new HttpCookie("T", "5"));
BatchGetRequest<Greeting> req = new CookieBuilders().batchGet().ids(1L, 2L).setCookies(requestCookies).build();
Response<BatchResponse<Greeting>> resp = REST_CLIENT.sendRequest(req).getResponse();
List<HttpCookie> getBackResponseCookie = Arrays.asList(new HttpCookie("1", "B"), new HttpCookie("2", "A"), new HttpCookie("3", "G"), new HttpCookie("4", "E"), new HttpCookie("5", "T"));
;
Assert.assertEquals(resp.getCookies(), getBackResponseCookie);
}
Aggregations