Search in sources :

Example 16 with HttpCookie

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);
}
Also used : ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ProtocolVersion(com.linkedin.restli.common.ProtocolVersion) TreeMap(java.util.TreeMap) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) RestLiInternalException(com.linkedin.restli.internal.server.RestLiInternalException) HttpCookie(java.net.HttpCookie)

Example 17 with HttpCookie

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"));
    }
}
Also used : ResourceContext(com.linkedin.restli.server.ResourceContext) HttpCookie(java.net.HttpCookie)

Example 18 with HttpCookie

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")));
}
Also used : CookieBuilders(com.linkedin.restli.examples.greetings.client.CookieBuilders) Greeting(com.linkedin.restli.examples.greetings.api.Greeting) HttpCookie(java.net.HttpCookie) Test(org.testng.annotations.Test)

Example 19 with HttpCookie

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")));
}
Also used : CookieBuilders(com.linkedin.restli.examples.greetings.client.CookieBuilders) Greeting(com.linkedin.restli.examples.greetings.api.Greeting) HttpCookie(java.net.HttpCookie) Test(org.testng.annotations.Test)

Example 20 with HttpCookie

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);
}
Also used : CookieBuilders(com.linkedin.restli.examples.greetings.client.CookieBuilders) Greeting(com.linkedin.restli.examples.greetings.api.Greeting) BatchResponse(com.linkedin.restli.common.BatchResponse) HttpCookie(java.net.HttpCookie) Test(org.testng.annotations.Test)

Aggregations

HttpCookie (java.net.HttpCookie)151 CookieManager (java.net.CookieManager)49 CookieStore (java.net.CookieStore)33 URI (java.net.URI)32 Test (org.junit.Test)31 IOException (java.io.IOException)16 Test (org.testng.annotations.Test)13 MockResponse (com.google.mockwebserver.MockResponse)11 MockWebServer (com.google.mockwebserver.MockWebServer)11 ArrayList (java.util.ArrayList)11 HashMap (java.util.HashMap)6 Map (java.util.Map)6 Project (com.kickstarter.models.Project)5 RestResponse (com.linkedin.r2.message.rest.RestResponse)5 Cookie (javax.servlet.http.Cookie)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 MockResponse (okhttp3.mockwebserver.MockResponse)5 MockWebServer (okhttp3.mockwebserver.MockWebServer)5 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)5 ByteString (com.linkedin.data.ByteString)4