Hi Jukka,

I have now fixed the wrong method. Maybe you can test again ? Thanks.

OTOH, thinking again, I don't think, that the wrong response wrapper should have any influence here .... hmm, strange.

Regards
Felix

Felix Meschberger schrieb:
Hi Jukka,

Jukka Zitting schrieb:
Hi,

I'm trying to use a RequestDispatcher and a
(Sling)HttpServletResponseWrapper in a servlet component to capture
output from another components. My current attempt is:

    final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    final ServletOutputStream stream = new ServletOutputStream() {
        @Override
        public void write(int b) throws IOException {
            buffer.write(b);
        }
    };
    final PrintWriter writer = new PrintWriter(stream);

    HttpServletResponseWrapper wrapper =
        new HttpServletResponseWrapper(response) {
            @Override
            public ServletOutputStream getOutputStream() {
                return stream;
            }
            @Override
            public PrintWriter getWriter() throws IOException {
                return writer;
             }
        };

    RequestDispatcher dispatcher = request.getRequestDispatcher(path);
    dispatcher.include(request, wrapper);

I traced the code to figure out that the RequestDispatcher correctly
finds the target resource I've identified, but then in
RequestData.service (called by SlingComponentFilterChain.render) the
call requestData.getContentData().getServlet() gives the original
servlet (where I'm trying to do the capture) instead of the component
associated with the target resource.

What am I doing wrong?

Maybe you should use a SlingHttpServletResponseWrapper instead of a HttpServletResponseWrapper ? This should do the trick (for now).

Point is, that your way should also work. But there is a bug in the RequestData.toSlingHttpServletResponse method which should create SlingHttpServletResponseWrapper around your HttpServletResponseWrapper delegating to the SlingHttpServletResponse wrapped by your HttpServletResponseWrapper ...... I opened an issue for that problem [1].

Regards
Felix


[1] https://issues.apache.org/jira/browse/SLING-631

Reply via email to