4

I know that v3.0 has method getHeader() but what about 2.3? Maybe it possible to get from steam?

UPDATE:

Actually, I need the HTTP response header RESTful application. In some reason I have decided to do this in servlet filter... but without success... Solution @javax.ws.rs.core.Context HttpHeaders requestHeaders.

For example,

@javax.ws.rs.GET
public String invoceRestMethod(@Context HttpHeaders requestHeaders){
      MultivaluedMap<String, String> map = headers.getRequestHeaders();
      for (Map.Entry<String, List<String>> entry : map.entrySet()) {
      //  processing header.... 
      }     
}

Maybe in will help for someone. But any case, for Servlet issue still is opened

Tioma
  • 2,120
  • 9
  • 35
  • 52

1 Answers1

5

You cannot get the header from the stream*.

What you have to do is insert a proxy response object into the filter chain before your Servlet is called, and have that capture the header.


* Actually, you could potentially capture stuff from the stream using a proxy response and decode the headers. But if you are inserting a proxy response, it is simpler to capture the headers directly.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • I guess you talk about HttpServletResponseWrapper(HttpServletResponse) . Yeap, I think about. But how I can know the length of header? – Tioma Oct 04 '12 at 13:48
  • 1
    You don't need to know that. You subclass the wrapper, overriding the `setHeader` method to record the headers that you are interested in. – Stephen C Oct 04 '12 at 14:43
  • Hi Stephen. I have tried to impalement this. It's work only in case if I invoke method setHeader in Servlet myself. But I need full HTTP header. Have you any idea? – Tioma Oct 08 '12 at 07:52
  • 1
    Look at example #3 on this page: http://www.di.unipi.it/~ghelli/didattica/bdldoc/A97329_03/web.902/a95878/filters.htm. And the examples linked to this Answer: http://stackoverflow.com/a/7046900/139985 – Stephen C Oct 08 '12 at 09:21
  • Thanks for your help. I have done the same as you suggested http://stackoverflow.com/questions/3415004/log-only-http-servlet-response-headers . but it works only if I set header myself - in servlet class invoke response.setHeader(name, value). this value will be added but not full Header. On case of example #3 - it allow to get only body. Looks it's not possible to get header for servlet 2.5 :( – Tioma Oct 08 '12 at 09:48
  • 1
    That should work in 2.5 provided that you add the filter far enough up the filter chain. I'm not sure what you mean by "I need full HTTP header". – Stephen C Oct 09 '12 at 07:25