Vikas V
Vikas V

Reputation: 3186

Fixing No PolicyContextHandler for key=javax.servlet.http.HttpServletResponse exception

Am using Jboss 5.1.0

In my bean, I want to get Response object and using this I want to set some values back to the cookie.

Below is my code in bean,

HttpServletResponse response = null;
response = (HttpServletResponse) PolicyContext.getContext("javax.servlet.http.HttpServletResponse"); 
//Line number 1230


HttpServletRequest req = null;
req = (HttpServletRequest) PolicyContext.getContext("javax.servlet.http.HttpServletRequest");

Cookie[] cookies =  req.getCookies();

for(int i=0;i<cookies.length;i++)
 {
   if(cookies[i].getName().contains("myCookie"))
                {
                    cookies[i].setValue("");
                    cookies[i].setPath("/");
                    cookies[i].setMaxAge(0);
                    response.addCookie(cookies[i]);
                }
}

But, am getting below exception at line number 1230

java.lang.IllegalArgumentException: No PolicyContextHandler for key=javax.servlet.http.HttpServletResponse
13:24:16,457 ERROR [STDERR]     at javax.security.jacc.PolicyContext.getContext(PolicyContext.java:107)
13:24:16,457 ERROR [STDERR]     at myPackage.myBean(myBean.java:1230)
13:24:16,457 ERROR [STDERR]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
13:24:16,457 ERROR [STDERR]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
13:24:16,457 ERROR [STDERR]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
13:24:16,457 ERROR [STDERR]     at java.lang.reflect.Method.invoke(Method.java:597)
13:24:16,457 ERROR [STDERR]     at org.apache.el.parser.AstValue.invoke(AstValue.java:170)
13:24:16,457 ERROR [STDERR]     at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
13:24:16,457 ERROR [STDERR]     at com.sun.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:68)
13:24:16,457 ERROR [STDERR]     at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
13:24:16,457 ERROR [STDERR]     at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
13:24:16,457 ERROR [STDERR]     at javax.faces.component.UICommand.broadcast(UICommand.java:387)
13:24:16,457 ERROR [STDERR]     at org.ajax4jsf.component.AjaxViewRoot.processEvents(AjaxViewRoot.java:329)
13:24:16,457 ERROR [STDERR]     at org.ajax4jsf.component.AjaxViewRoot.broadcastEventsForPhase(AjaxViewRoot.java:304)
13:24:16,457 ERROR [STDERR]     at org.ajax4jsf.component.AjaxViewRoot.processPhase(AjaxViewRoot.java:261)

Am getting request object but not response object. How to fix this ?

Upvotes: 1

Views: 1925

Answers (1)

BalusC
BalusC

Reputation: 1108692

Using the PolicyContext is unnecessary in managed beans. In managed beans you have already direct access to HttpServletRequest and HttpServletResponse via ExternalContext.

ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
// ...

The PolicyContext is only useful in service classes like EJBs where you don't directly have access to them. Your concrete problem is caused because the PolicyContext simply doesn't remember the HTTP servlet response, but only the HTTP servlet request, because the information about the currently logged-in user is stored in there. In essence, the HTTP servlet response is irrelevant to policy context.

Note that the ExternalContext also offers a convenient getRequestCookieMap() method which returns a mapping of all cookies so that you don't need to manually traverse the Cookie[].

Cookie cookie = (Cookie) externalContext.getRequestCookieMap().get("myCookie");
// ...

I know that you're using JSF 1.2, but if you were using JSF 2.0 or newer, then you could also have used the convenient addResponseCookie() method to add a cookie without the need to obtain the "raw" servlet response.

Upvotes: 1

Related Questions