Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set and check cookies wih JAX-RS?

I am a noobie with RESTful API and I am trying to build a Login service in which I provide an email and password and if the validation is successful - to store a cookie. In addition, how do I check the cookie(if stored)?

How can this be achieved?

@Path("/login") @POST @Produces(MediaType.APPLICATION_JSON) @Consumes({MediaType.APPLICATION_FORM_URLENCODED, MediaType.APPLICATION_JSON}) public Response Login(final String i_LoginDetails) throws JSONException {     final JSONObject obj = new JSONObject(i_LoginDetails);     try {         if (isValidUser(obj.getString("email"), obj.getString("password"))) {             // Set a cookie         } else {             // return error invalid-credentials message         }     } catch (Exception e) {         e.printStackTrace();     }     return Response.ok("TEST").build(); } 

And how do I check the cookie(if set)?

like image 938
Gil404 Avatar asked Jan 17 '15 21:01

Gil404


People also ask

How do I set cookies in REST client?

To set a cookie in REST API response, get the Response reference and use it's cookie() method.

What is CookieParam?

Annotation Type CookieParamBinds the value of a HTTP cookie to a resource method parameter, resource class field, or resource class bean property. A default value can be specified using the DefaultValue annotation. The type T of the annotated parameter, field or property must either: Be a primitive type.

What can a JAX-RS method return?

If the URI path template variable cannot be cast to the specified type, the JAX-RS runtime returns an HTTP 400 (“Bad Request”) error to the client. If the @PathParam annotation cannot be cast to the specified type, the JAX-RS runtime returns an HTTP 404 (“Not Found”) error to the client.

What are the implementations of JAX-RS?

JAX-RS is a standard defined in Java Specification Request 311 (JSR-311) and Jersey / RESTEasy are implementations of it.


1 Answers

You can do the following:

  • To store a new cookie:

    @GET @Path("/login") @Produces(MediaType.TEXT_PLAIN) public Response login() {     NewCookie cookie = new NewCookie("name", "123");     return Response.ok("OK").cookie(cookie).build(); } 
  • To retrieve the cookie (javax.ws.rs.core.Cookie):

    @GET @Path("/foo") @Produces(MediaType.TEXT_PLAIN) public Response foo(@CookieParam("name") Cookie cookie) {     if (cookie == null) {         return Response.serverError().entity("ERROR").build();     } else {         return Response.ok(cookie.getValue()).build();     } } 

    However, you may only want the value:

    @GET @Path("/foo") @Produces(MediaType.TEXT_PLAIN) public Response foo(@CookieParam("name") String value) {     System.out.println(value);     if (value == null) {         return Response.serverError().entity("ERROR").build();     } else {         return Response.ok(value).build();     } } 

By the way, you may want to try the following code:

@GET @Path("/logout") @Produces(MediaType.TEXT_PLAIN) public Response logout(@CookieParam("name") Cookie cookie) {     if (cookie != null) {         NewCookie newCookie = new NewCookie(cookie, null, 0, false);         return Response.ok("OK").cookie(newCookie).build();     }     return Response.ok("OK - No session").build(); } 

This removes the cookie in the browser. The behavior depends on the implementation of JAX-RS. With RESTEasy (JBoss AS 7.0) and Google Chrome works fine.

like image 158
Paul Vargas Avatar answered Sep 19 '22 04:09

Paul Vargas