JAX RS – 14 – @CookieParam

Merhaba Arkadaslar
Bu bolumde @CookieParam ‘i inceleyecegiz.

Cookie ile ilgili on bilgi icin ;
Servlet & JSP – 16 – Session – 03

The @javax.ws.rs.CookieParam annotation allows you to inject cookies 
sent by a client request into your JAX-RS resource methods.

UserResource.java

package _08.cookieParam.service;

import javax.ws.rs.CookieParam;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Cookie;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.NewCookie;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;

@Path("/user-cookieParam")
public class UserResource {

	@GET
	@Path("/addCookie")

	public Response addCookie() {

		String result = "addCookie is called.";
		ResponseBuilder builder = Response.ok(result);
		builder.cookie(new NewCookie("myCookie", "myCookieValue"));

		return builder.build();

		// way two
		// return Response.status(200).entity(result).cookie(new NewCookie("myCookie", "myCookieValue")).build();
	}

	@GET
	@Path("/getCookie")	
	public Response getCookie(@CookieParam("myCookie") String cookie) {
		String result = "getCookie is called. <br/>";

		ResponseBuilder builder = Response.ok(result + " myCookie : " + cookie);
		return builder.build();

		// way two
		// return Response.status(200).entity(result + " myCookie : " + cookie).build();
	}

	@GET
	@Path("/getCookieWithContext")
	public Response getCookieWithContext(@Context HttpHeaders headers) {

		String result = "getCookieWithContext is called. <br/>";
		for (String name : headers.getCookies().keySet()) {
			Cookie cookie = headers.getCookies().get(name);
			result = result + "Cookie: " + name + "=" + cookie.getValue() + "<br/>";
		}
		
		ResponseBuilder builder = Response.ok(result);
		return builder.build();

		//way two
		//return Response.status(200).entity(result).build();
	}

}

Simdiye kadar orneklerimizde metotlarin donus tipi java.lang.String tipindeydi. Buradaki orneklerde javax.ws.rs.core.Response donus tipi olarak tanimladik. Ilerleyen bolumlerde javax.ws.rs.core.Response abstract sinifini daha detayli olarak inceleyecegiz.
javax.ws.rs.core.Response$ResponseBuilder sinifini kullaniyoruz.

Bu noktada basit bir Cookie ornegi icin kullaniyoruz.

public abstract ResponseBuilder cookie(NewCookie... cookies);

javax.ws.rs.core.NewCookie tipinde yeni Cookie olusturuyoruz ve cookie() metoduna veriyoruz.

getCookie metodunda @CookieParam annotation kullanarak ekledigimiz Cookie bilgisine ulasabiliriz.
Burada once addCookie metodunu calistirmaliyiz sonrasinda getCookie metodunu calistirmaliyiz ki Cookie bilgisine eriselim.

getCookieWithContext metodunda @Context annotation’ini kullanarak HttpHeaders’i inject ediyoruz. getCookies metodunu kullanarak mevcut Cookie bilgilerine ulasabiliriz.

javax.ws.rs.core.Cookie

package javax.ws.rs.core;
public class Cookie {
public String getName() {...} 
public String getValue() {...} 
public int getVersion() {...} 
public String getDomain() {...} 
public String getPath() {...}
... 
}

addCookie

http://localhost:8080/injavawetrust.jersey.tutorial/user-cookieParam/addCookie
http://localhost:8080/injavawetrust.resteasy.tutorial/user-cookieParam/addCookie

getCookie

http://localhost:8080/injavawetrust.jersey.tutorial/user-cookieParam/getCookie
http://localhost:8080/injavawetrust.resteasy.tutorial/user-cookieParam/getCookie

getCookieWithContext

http://localhost:8080/injavawetrust.jersey.tutorial/user-cookieParam/getCookieWithContext
http://localhost:8080/injavawetrust.rest.tutorial/user-cookieParam/getCookieWithContext

cookie-param

Github kaynak kodlar / source folder
injavawetrust.resteasy
injavawetrust.jersey

Yazimi burada sonlandiriyorum.
Herkese Bol Javali Gunler dilerim.
Be an oracle man , import java.*;
Levent Erguder
OCP, Java SE 6 Programmer
OCE, Java EE 6 Web Component Developer

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published.