I'm on a project which is using struts 2 on the backend and an inhouse
javascript framework on the client side that receives  status information
from the server in an X-JSON header. 

I have an interceptor that I'm writing to handle all the needs of the client
side framework and a second that manages the json objects called (not
clever) JsonInterceptor. It grabs json strings from the header and
instantiates them into JSONObjects which it stores in the request before the
invoke. After the invoke I wanted to check the value stack for a particular
object and if it exists, add a new X-JSON header to the response. 

Problem: it seems I can't alter the response. It doesn't give me an error if
I user addHeader - but if I check for the header immediately after with
containsHeader, it doesn't exist. below is a stripped down version of the
interceptor





   public String intercept(ActionInvocation actInv) throws Exception {      

      // ...checks header and adds objects from X-JSON header to request
attributes
      
      //--------   BEFORE   -----------
      String result = actInv.invoke();
      //--------   AFTER    -----------

      //check for response object. If one doesn't exist, add default 200
response
      SnipsResponse snipResp =
(SnipsResponse)actInv.getStack().findValue("snipsResponse");
      if (snipResp == null) {
         snipResp = new SnipsResponse(200,"Autogenerated OK response");         
      }
      //get the response
      HttpServletResponse response =
(HttpServletResponse)actInv.getInvocationContext().get(StrutsStatics.HTTP_RESPONSE);
      if (response==null) {
         LOG.error("Unable to retrieve HttpServletResponse from invocation
context");
      } else {
         response.addHeader("X-JSON", snipResp.getJSONString());
         if (response.containsHeader("X-JSON")) {
            LOG.debug("Added X-JSON header: "+snipResp.getJSONString());        
    
         } else LOG.debug("adding header failed");
                  
      }
      
      return result;
   }


-- 
View this message in context: 
http://www.nabble.com/adding-a-header-in-a-struts-2-interceptor-tp17808069p17808069.html
Sent from the Struts - User mailing list archive at Nabble.com.

Reply via email to