Hi,
you get empty output because in line

             *boolean* handled = handler.service(httpServletRequest,
httpServletResponse);
you pass the original "httpServletResponse". This lines pass processing to
the rest of the filter pipeline where actual generation of the response
happens, and because the original object is passed, the content of the
response is sent to the client, not captured by your wrapper class.

You need to create your wrapper before calling "handler.service()", pass
the wrapper as the second argument, and process captured output after
"handler.service()". It should fix the problem with getting always an empty
output.

But I suspect you will have more problems and this way is not really
optimal. Tapestry has a special pipeline for handling assets like
JavaScript in the form of StreamableSource decorators,
ResourceTransformer-s and ResourceMinimisers. Files translated, minified
and compressed before sending to output - so your code would probably not
find any recognisable part to process. Tapestry also caches results of this
processing, so usually it is done only once, not on every requests as in
your code, which significantly improves performance. More importantly,
minification of JavaScript files removes comments. Minification is
controlled by "tapestry.enable-minification"
(see org.apache.tapestry5.SymbolConstants#MINIFICATION_ENABLED), which
normally is enabled in the production mode and disabled in the development
mode. So if your goal is to remove comments from JavaScript files, just run
your Tapestry application mode.

Best regards,
Cezary


On Sun, Dec 1, 2019 at 5:12 PM Ric 2000 <erich.gorma...@gmail.com> wrote:

> 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 <erich.gorma...@gmail.com> 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 <cezary...@gmail.com>
>> 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 <erich.gorma...@gmail.com>
>>> 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: users-unsubscr...@tapestry.apache.org
> For additional commands, e-mail: users-h...@tapestry.apache.org

Reply via email to