Re: Setting a response header in a Tomcat Filter : Java
This is a discussion on Re: Setting a response header in a Tomcat Filter within the Java forums in Programming Languages category; On Aug 13, 4:05 am, Mr B <paul.benn...@akjl.co.uk> wrote: > I need to set a filter in Tomcat that changes the response header > slightly. i have used a filter to try and change the header SOAPAction > to a value of ebXML: > > public void doFilter(ServletRequest request, ServletResponse > response, FilterChain chain) > throws IOException, ServletException { > HttpServletResponse hrs = (HttpServletResponse)response; > hrs.setHeader("SOAPAction", "ebXML"); > > chain.doFilter(request, hrs); > > } > > But the header never changes. Can I do this? If I can then any clues > as to why the header is not being ...
![]() |
| | LinkBack | Thread Tools |
|
#1
| |||
| |||
| > I need to set a filter in Tomcat that changes the response header > slightly. i have used a filter to try and change the header SOAPAction > to a value of ebXML: > > public void doFilter(ServletRequest request, ServletResponse > response, FilterChain chain) > throws IOException, ServletException { > HttpServletResponse hrs = (HttpServletResponse)response; > hrs.setHeader("SOAPAction", "ebXML"); > > chain.doFilter(request, hrs); > > } > > But the header never changes. Can I do this? If I can then any clues > as to why the header is not being changed? > > thanks > > Paul Did you configure it correctly in web.xml? You might want to put debug statements to ensure it is initialized and is being invoked. -cheers, Manish |
|
#2
| |||
| |||
| On 13 Aug, 20:36, Manish Pandit <pandit.man...> wrote: > On Aug 13, 4:05 am, Mr B <paul.benn...@akjl.co.uk> wrote: > > > > > > > I need to set a filter in Tomcat that changes the response header > > slightly. i have used a filter to try and change the header SOAPAction > > to a value of ebXML: > > > public void doFilter(ServletRequest request, ServletResponse > > response, FilterChain chain) > > throws IOException, ServletException { > > HttpServletResponse hrs = (HttpServletResponse)response; > > hrs.setHeader("SOAPAction", "ebXML"); > > > chain.doFilter(request, hrs); > > > } > > > But the header never changes. Can I do this? If I can then any clues > > as to why the header is not being changed? > > > thanks > > > Paul > > Did you configure it correctly in web.xml? You might want to put debug > statements to ensure it is initialized and is being invoked. > > -cheers, > Manish- Hide quoted text - > > - Show quoted text - Hi Manish, thanks for the reply. Yes, it is configured OK and is working in that it manipultes the content of the response. The trouble is that I am calling the HttpServletResponse methods SetHeader (and also addHeader) to the response passed into the filter, but it doesn't matter what I do, the response header is not changed. The header value I am trying to change is the SOAPAction header and I am trying to change it to "ebXML" but its as though the header is read-only. Does this make sense? If so, should I be able to change the header? cheers Paul |
|
#3
| |||
| |||
| On Aug 14, 1:09 am, Mr B <paul.benn...@akjl.co.uk> wrote: > On 13 Aug, 20:36, Manish Pandit <pandit.man...> wrote: > > > > > > > On Aug 13, 4:05 am, Mr B <paul.benn...@akjl.co.uk> wrote: > > > > I need to set a filter in Tomcat that changes the response header > > > slightly. i have used a filter to try and change the header SOAPAction > > > to a value of ebXML: > > > > public void doFilter(ServletRequest request, ServletResponse > > > response, FilterChain chain) > > > throws IOException, ServletException { > > > HttpServletResponse hrs = (HttpServletResponse)response; > > > hrs.setHeader("SOAPAction", "ebXML"); > > > > chain.doFilter(request, hrs); > > > > } > > > > But the header never changes. Can I do this? If I can then any clues > > > as to why the header is not being changed? > > > > thanks > > > > Paul > > > Did you configure it correctly in web.xml? You might want to put debug > > statements to ensure it is initialized and is being invoked. > > > -cheers, > > Manish- Hide quoted text - > > > - Show quoted text - > > Hi Manish, thanks for the reply. Yes, it is configured OK and is > working in that it manipultes the content of the response. The trouble > is that I am calling the HttpServletResponse methods SetHeader (and > also addHeader) to the response passed into the filter, but it doesn't > matter what I do, the response header is not changed. The header value > I am trying to change is the SOAPAction header and I am trying to > change it to "ebXML" but its as though the header is read-only. Does > this make sense? If so, should I be able to change the header? > > cheers > > Paul- Hide quoted text - > > - Show quoted text - Hi Paul, This is interesting! How are you verifying that the header is added or not? How about after adding the header, verify if it is set (in the filter itself) by calling containsHeader( ) ? That way you will know if the filter is even adding the header correctly or not. -cheers, Manish |
|
#4
| |||
| |||
| On 14 Aug, 19:30, Manish Pandit <pandit.man...> wrote: > On Aug 14, 1:09 am, Mr B <paul.benn...@akjl.co.uk> wrote: > > > > > > > On 13 Aug, 20:36, Manish Pandit <pandit.man...> wrote: > > > > On Aug 13, 4:05 am, Mr B <paul.benn...@akjl.co.uk> wrote: > > > > > I need to set afilterin Tomcat that changes the response header > > > > slightly. i have used afilterto try and change the header SOAPAction > > > > to a value of ebXML: > > > > > public void doFilter(ServletRequest request, ServletResponse > > > > response, FilterChain chain) > > > > throws IOException, ServletException { > > > > HttpServletResponse hrs = (HttpServletResponse)response; > > > > hrs.setHeader("SOAPAction", "ebXML"); > > > > > chain.doFilter(request, hrs); > > > > > } > > > > > But the header never changes. Can I do this? If I can then any clues > > > > as to why the header is not being changed? > > > > > thanks > > > > > Paul > > > > Did you configure it correctly in web.xml? You might want to put debug > > > statements to ensure it is initialized and is being invoked. > > > > -cheers, > > > Manish- Hide quoted text - > > > > - Show quoted text - > > > Hi Manish, thanks for the reply. Yes, it is configured OK and is > > working in that it manipultes the content of the response. The trouble > > is that I am calling the HttpServletResponse methods SetHeader (and > > also addHeader) to the response passed into thefilter, but it doesn't > > matter what I do, the response header is not changed. The header value > > I am trying to change is the SOAPAction header and I am trying to > > change it to "ebXML" but its as though the header is read-only. Does > > this make sense? If so, should I be able to change the header? > > > cheers > > > Paul- Hide quoted text - > > > - Show quoted text - > > Hi Paul, > > This is interesting! How are you verifying that the header is added or > not? How about after adding the header, verify if it is set (in thefilteritself) by calling containsHeader( ) ? That way you will know > if thefilteris even adding the header correctly or not. > > -cheers, > Manish- Hide quoted text - > > - Show quoted text - Sorry about the delay in replying. I managed to sort my problem out, but the SOAPAction header was a bit of a "red herring". I finally managed to manipulte the content-length header as I needed to remove the contents of the 200 OK status response. All works fine now. Just to explain, I use the Reverse-via SOAP header to see whether the request was from an external source, then I simply remove the body and set the content-length to 0. The code is as follows: package com.webswell.filter; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; class KillGhostFilterStream extends ServletOutputStream { private OutputStream intStream; private ByteArrayOutputStream baStream; private boolean closed = false; private String text1; private String text2; public KillGhostFilterStream(OutputStream outStream, String t1, String t2) { intStream = outStream; baStream = new ByteArrayOutputStream(); text1 = t1; text2 = t2; } public void write(int i) throws java.io.IOException { baStream.write(i); } public void close() throws java.io.IOException { if (!closed) { processStream(); intStream.close(); closed = true; } } public void flush() throws java.io.IOException { if (baStream.size() != 0) { if (! closed) { processStream(); // need to synchronize the flush! baStream = new ByteArrayOutputStream(); } } } public void processStream() throws java.io.IOException { intStream.write(replaceContent(baStream.toByteArray())); intStream.flush(); } public byte [] replaceContent(byte [] inBytes) { String retVal =""; String firstPart=""; String tempFind=""; String origString = new String(inBytes); String srchString = (new String(inBytes)).toLowerCase(); if ((srchString.indexOf(text1.toLowerCase()) > -1) && (srchString.indexOf(text2.toLowerCase()) > -1)) { System.out.println("I found an empty Header and an empty body!"); return "".getBytes(); } return origString.getBytes(); } } class KillGhostFilterWrapper extends HttpServletResponseWrapper { private PrintWriter tpWriter; private KillGhostFilterStream tpStream; public KillGhostFilterWrapper(ServletResponse inResp, String text1, String text2) throws java.io.IOException { super((HttpServletResponse) inResp); tpStream = new KillGhostFilterStream(inResp.getOutputStream(), text1, text2); tpWriter = new PrintWriter(tpStream); } public ServletOutputStream getOutputStream() throws java.io.IOException { this.setContentLength(0); return tpStream; } public PrintWriter getWriter() throws java.io.IOException { return tpWriter; } } public final class KillGhostFilter implements Filter { private FilterConfig filterConfig = null; private String searchText1 = "<SOAP-ENV:Header/>"; private String searchText2 = "<SOAP-ENV:Body/>"; public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { java.util.Date date = new java.util.Date(); HttpServletRequest req = (HttpServletRequest) request; String rev = req.getHeader("Reverse-via"); if ((rev!=null) ) { System.out.println(date+": "+req.getHeader("Reverse-via")); System.out.println(date+": "+req.getRemoteAddr()); HttpServletResponse resp = (HttpServletResponse)response; KillGhostFilterWrapper myWrappedResp = new KillGhostFilterWrapper(resp, searchText1, searchText2); myWrappedResp.setContentLength(0); myWrappedResp.setHeader("SOAPAction","\"ebXML\""); chain.doFilter(request, myWrappedResp); } else { chain.doFilter(request, response); //HttpServletResponse resp = (HttpServletResponse)resp; //resp.setContentLength(0); //KillGhostFilterWrapper myWrappedResp = new KillGhostFilterWrapper(resp, searchText1, searchText2); //chain.doFilter(request, myWrappedResp); //myWrappedResp.setContentLength(0) ; //myWrappedResp.getOutputStream().close(); } } public void destroy() { } public void init(FilterConfig filterConfig) { this.filterConfig = filterConfig; } } |
![]() |
| Thread Tools | |
| |
| ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Adding a custom header to the response | usenet | DOTNET | 1 | 10-31-2007 10:53 AM |
| Frequency response of PI filter | usenet | DSP | 5 | 07-11-2007 08:57 AM |
| response content length header on asp.net 1.1 | usenet | DOTNET | 0 | 10-12-2006 06:58 PM |
| IIS (or Isapi) adds 'Connection' header to response | usenet | Inetserver | 5 | 12-24-2005 03:09 AM |
| Filter for Header Alteration | usenet | Pegasus | 0 | 11-09-2004 06:54 AM |


