Creating a cookie inside jax-rs endpoint

I have a jax-rs endpoint. The purpose of the endpoint is to authorize the user. I need to login to a cookie. I have mentioned the related part of my code below.

public Response authorize(@Context HttpServletRequest request) throws URISyntaxException {
     if (authnResult.isAuthenticated()) {
     //TODO create a cookie to maintain login state
                        Cookie authCookie = new Cookie(FrameworkConstants.COMMONAUTH_COOKIE, "test");
                        authCookie.setSecure(true);
                        authCookie.setHttpOnly(false);
                        authCookie.setMaxAge(5 * 60);
}

      


EDIT:

This is my first time creating cookies. I followed some tutorials. In these tutorials, he added the generated cookie to the response. But inside the endpoint, I cannot access the answer. So how can I create a cookie? Please advise me.

Updated code:

public Response authorize(@Context HttpServletRequest request) throws URISyntaxException {
    NewCookie cookie = new NewCookie("CookieName","CookieValue");
                    Response.ResponseBuilder builder = Response.ok("Cool Stuff");
                    builder.cookie(cookie);
                    Response response=builder.build();
    Cookie[] cookies = request.getCookies();
}

      

I need to know how to access the newly created cookie.

+3


source to share


1 answer


You can create javax.ws.rs.core.NewCookie

. There are many different constructors, just go through the API docs.

Then you can add cookies via ResponseBuilder#cookie(NewCookie)

. For example:

@GET
public Response getCookie() {
    NewCookie cookie = new NewCookie("Name", "Value", "path", "domain",
                                     "comment", 300, true, true);
    ResponseBuilder builder = Response.ok("Cool Stuff");
    builder.cookie(cookie);
    return builder.build();
}

      


UPDATE (with complete example)



@Path("cookie")
public class CookieResource {

    @GET
    public Response getCookie(@CookieParam("A-Cookie") String cookie) {
        Response response = null;
        if (cookie == null) {
            response = Response.ok("A-Cookie: Cookie #1")
                    .cookie(new NewCookie("A-Cookie", "Cookie #1"))
                    .build();
            return response;
        } else {
            String cookieNum = cookie.substring(cookie.indexOf("#") + 1);
            int number = Integer.parseInt(cookieNum);
            number++;
            String updatedCookie = "Cookie #" + number;
            response = Response.ok("A-Cookie: " + updatedCookie)
                    .cookie(new NewCookie("A-Cookie", updatedCookie))
                    .build();
            return response;
        }
    }
}

      

After 38 queries, you can see the result. I used Firefox Firebug plugin . You can see the sent cookie # 37 and return cookie # 38

enter image description here

If you need help trying to access the cookie from the client (as pointed out in your comment) this might be appropriate for another SO question. Maybe not to discuss this discussion as it will rely on a different technology. If this is not what you are looking for, perhaps a better explanation of what you are trying to accomplish will help.

+1


source







All Articles