users@jersey.java.net

setting headers

From: Suchitha Koneru (sukoneru) <"Suchitha>
Date: Tue, 23 Mar 2010 21:37:14 -0700

Hello Jersey Users,

               We are using Jersey version 1.1.1-ea . Can we set
headers on response of an API without returning a response object as
shown below. We want to make sure that consumer of restful services does
not cache the content.

 

@GET

      @Produces ("application/xml")

      @Path ("/getNewAlertData/{customerId}/{alertId}")

      public List<AlertData> getNewAlertData(@PathParam("customerId")
int customerId,_at_PathParam("alertId") long alertId) {

            List<AlertData> list = null;

            

            

            try {

                  

                  

      AlertReportSessionBeanLocal sessionBean =
(AlertReportSessionBeanLocal)

 
JNDIServiceLocator.getInstance().getJndiService("alerts/AlertReportSessi
onBean/local");

                  list = sessionBean.getNewAlertData(customerId,
alertId);

 

CacheControl cacheControl = new CacheControl();

                  cacheControl.setMaxAge(0);

                  

                  Response.ResponseBuilder builder = Response.ok();

                  builder.cacheControl(cacheControl);

 

 

            } catch (Exception NamingException) {

                NamingException.printStackTrace();

                throw new NotFoundException("Invalid request");

            }

 

            return list;

      }

 

 

We tried the above code and it did not work. We do not want to change
the signature of the API and the code such that it returns a response
object

 

i.e.

 

  public Response getNewAlertData(@PathParam("customerId") int
customerId,_at_PathParam("alertId") long alertId) {

 

            try {

                  

                  

                  

     AlertReportSessionBeanLocal sessionBean =
(AlertReportSessionBeanLocal)

JNDIServiceLocator.getInstance().getJndiService("alerts/AlertReportSessi
onBean/local");

                  list = sessionBean.getNewAlertData(customerId,
alertId);

                  

InputStream is = getStream(list);

 

                  CacheControl cacheControl = new CacheControl();

                  cacheControl.setMaxAge(0);

 

Response.ResponseBuilder builder = Response.ok(is, "application/xml");

                  builder.cacheControl(cacheControl);

 

 

            } catch (Exception NamingException) {

                NamingException.printStackTrace();

                throw new NotFoundException("Invalid request");

            }

 

            return builder.build();

 

 

}

 

Is there a way to achieve this without returning a response object ?
Can we set headers at the service level rather than at the API level ,
so that

All the apis in the restful service can use the same headers. Could you
please let me know,

 

Thanks,

Suchitha