You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Page <jp...@contentDSI.com> on 2008/06/12 21:40:15 UTC

adding a header in a struts 2 interceptor

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.

Re: adding a header in a struts 2 interceptor

Posted by Page <jp...@contentDSI.com>.


Page wrote:
> 
> - except that it can't modify the request. 
> 

sorry, meant can't modify the response
-- 
View this message in context: http://www.nabble.com/adding-a-header-in-a-struts-2-interceptor-tp17808069p17824575.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: adding a header in a struts 2 interceptor

Posted by Dave Newton <ne...@yahoo.com>.
Glad to hear it :)


--- On Fri, 6/13/08, Page <jp...@contentDSI.com> wrote:

> From: Page <jp...@contentDSI.com>
> Subject: Re: adding a header in a struts 2 interceptor
> To: user@struts.apache.org
> Date: Friday, June 13, 2008, 4:09 PM
> Thanks for you help Dave, that was exactly what I needed and
> all is working!
> -- 
> View this message in context:
> http://www.nabble.com/adding-a-header-in-a-struts-2-interceptor-tp17808069p17831109.html
> Sent from the Struts - User mailing list archive at
> Nabble.com.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail:
> user-help@struts.apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: adding a header in a struts 2 interceptor

Posted by Page <jp...@contentDSI.com>.

Thanks for you help Dave, that was exactly what I needed and all is working!
-- 
View this message in context: http://www.nabble.com/adding-a-header-in-a-struts-2-interceptor-tp17808069p17831109.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: adding a header in a struts 2 interceptor

Posted by Dave Newton <ne...@yahoo.com>.
--- On Fri, 6/13/08, Page <jp...@contentDSI.com> wrote:
> http://struts.apache.org/2.x/docs/writing-interceptors.html
> > 
> > The result has already been rendered after the invoke,
> so header
> > modification would be a bit superfluous ;)
> > 
> 
> I guess I'm going about this the wrong way. I want the
> other developers on
> the team to be able to write actions without worrying about
> adding in code
> to support the in-house framework, so an interceptor that
> could execute
> after the action seemed like a fit - except that it
> can't modify the
> request. So is there a better approach that could still
> keep my actions
> clean?

I think you may have missed the nub of my gist; I'm saying that the colored box at [1] with the blurb about the PreResultListener may be what you need.

Dave

[1] http://struts.apache.org/2.x/docs/writing-interceptors.html


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: adding a header in a struts 2 interceptor

Posted by Page <jp...@contentDSI.com>.

newton.dave wrote:
> 
> http://struts.apache.org/2.x/docs/writing-interceptors.html
> 
> The result has already been rendered after the invoke, so header
> modification would be a bit superfluous ;)
> 

I guess I'm going about this the wrong way. I want the other developers on
the team to be able to write actions without worrying about adding in code
to support the in-house framework, so an interceptor that could execute
after the action seemed like a fit - except that it can't modify the
request. So is there a better approach that could still keep my actions
clean?
-- 
View this message in context: http://www.nabble.com/adding-a-header-in-a-struts-2-interceptor-tp17808069p17824519.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: adding a header in a struts 2 interceptor

Posted by Dave Newton <ne...@yahoo.com>.
--- On Thu, 6/12/08, Page <jp...@contentDSI.com> wrote:
> 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;
>    }

http://struts.apache.org/2.x/docs/writing-interceptors.html

The result has already been rendered after the invoke, so header modification would be a bit superfluous ;)

Dave


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org