Hi again,
here is the code I used:
First the way I contributed the HttpServletRequestHandlerin my Tapestry
Filter Module:
@Contribute(HttpServletRequestHandler.*class*)
*public* *void*
contributeHttpServletRequestHandler(OrderedConfiguration<HttpServletRequestFilter>
configuration) {
configuration.add("JavascriptCommentFilter", *new**
JavascriptCommentFilter(**)*,
*after*("StoreIntoGlobals").build());
}
Here is the code of my HttpServletRequestFilter (the used
ServletResponseWrapperCopier I attached to this post):
*package* *com.xyz.common.services.requestfilter*;
*import* java.io.IOException;
*import* java.io.PrintWriter;
*import* javax.servlet.http.HttpServletRequest;
*import* javax.servlet.http.HttpServletResponse;
*import* org.apache.log4j.Logger;
*import* org.apache.tapestry5.services.HttpServletRequestFilter;
*import* org.apache.tapestry5.services.HttpServletRequestHandler;
*import* *com.xyz <http://com.xyz>*.utils.ServletResponseWrapperCopier;
*public* *class* JavascriptCommentFilter *implements*
HttpServletRequestFilter {
*private* *final* *static* Logger *LOG* = Logger.*getLogger*
(JavascriptCommentFilter.*class*);
@Override
*public* *boolean* service(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse,
HttpServletRequestHandler handler) *throws* IOException
{
String requestUrl = httpServletRequest
.getRequestURL().toString();
*boolean* handled = handler.service(httpServletRequest,
httpServletResponse);
*LOG*.debug(requestUrl + " handled by Tapestry filter: " +
handled);
*if* (!handled) {
}
*if* (requestUrl.endsWith(".js")) {
// CharResponseWrapper charResponseWrapper = new
CharResponseWrapper(httpServletResponse);
*ServletResponseWrapperCopier* capturingResponseWrapper
= *new* *ServletResponseWrapperCopier*(
httpServletResponse);
PrintWriter responseWriter = httpServletResponse
.getWriter();
// String originalContent =
charResponseWrapper.toString();
String originalContent = capturingResponseWrapper
.getCaptureAsString();
// String originalContent =
capturingResponseWrapper.getCaptureAsString();
*LOG*.debug("javascript resource detected with content:
" + originalContent);
//
// int commentEnd = originalContent.indexOf("*/");
// if (commentEnd > -1) {
//
// originalContent =
originalContent.substring(commentEnd);
// }
//
// originalContent =
originalContent.replaceAll("(?m)^//.*", "");
//
// responseWriter.write(originalContent);
}
*return* handled;
}
}
I tried several variations, like writing an own servlet filter and puting
it after the Tapestry Filter, but always the same: the content appears to
be empty.
Thanks in advance for your suggestions!
Best Regards, Ric
On Sun, Dec 1, 2019 at 3:38 PM Ric 2000 <[email protected]> wrote:
> Hi Cezary,
>
> first thanks for your reply. Then I was at least not completely wrong. Let
> me show my code in the next reply, I will restore the version from GIT
> first.
>
> Best Regards, Erich
>
>
> On Sat, Nov 30, 2019 at 8:38 PM Cezary Biernacki <[email protected]>
> wrote:
>
>> Hi,
>> as you suggested, it is possible to capture output of handling request by
>> subclassing HttpServletResponseWrapper, providing your own
>> ServletOutputStream, and wrapping the original HttpServletResponse inside
>> a
>> filter on the requested processing pipeline. This way is not specific to
>> Tapestry, maybe you only need to remember that Tapestry on the request
>> processing pipeline is implemented as a filter, not a servlet. Tapestry
>> itself uses this approach to implement compressing responses, see class
>> org.apache.tapestry5.internal.gzip.GZipFilter. I don't know why it is not
>> working for you, probably you have a bug, but without seeing your code it
>> hard to help you.
>>
>> Best regards,
>> Cezary
>>
>>
>>
>>
>> On Sat, Nov 30, 2019 at 10:38 AM Ric 2000 <[email protected]>
>> wrote:
>>
>> > Dear all,
>> >
>> > I'm struggling on how to filter and modify the response sent to the
>> client
>> > in a Tapestry web application.
>> > I tried first with a RequestFilter contributed to the RequestHandler
>> > service.
>> > The problem occurred when I tried to read the content of the current
>> > response out stream. There are different hints on the internet how to
>> do a
>> > "capture" of this output stream using a custom
>> HttpServletResponseWrapper
>> > and overwriting the write method of the ServletOutputStream, to get the
>> > current content of the outputstream as byte array.
>> >
>> > But this is always empty, regardless of how I try to do it.
>> >
>> > Can you tell me, what is the right way in Tapestry to do it? Which
>> service
>> > do I have to contribute, in which phase of the request processing the
>> > response is filled and can be read, BEFORE it is send to the client?
>> >
>> > Thanks a lot for your suggestions, I really appreciate them!
>> >
>> > Best Regards, Ric
>> >
>>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]