Change filter dispatcher

Splash Forums Rewrite Users Change filter dispatcher

This topic contains 4 replies, has 2 voices, and was last updated by  fmb 2 years, 1 month ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #26304

    fmb
    Participant

    Hi,

    I am using Rewrite filter to forward the url of the request to an specific resource.
    I am also using a custom filter to include in the response a specific jsp file based on the requested resource.

    Rewrite filter read REQUEST dispatcher, my custom filter read REQUEST and FORWARD dispatchers (to be included inside Rewrite’s forwards)

    Now the problem:
    I am not declaring any tags inside my web.xml. How would I set RewriteFilter as REQUEST dispatcher only? I noticed the filter executes for every request/forward and so far I have tried:

    1. extending RewriteFilter and applying the @WebServlet annotation with dispatchers specified in the annotation itself
    2. extending RewriteFilter and applying the @WebServlet annotation with a “filterName” and specifying the dispatchers inside the web.xml with a reference to the filter

    I hope it is clear enough, I am just trying to change the dispatcher configuration from the filter and avoid verbosity inside the web deployment descriptor.

    Using JBoss EAP 6.1, Servlet 3.0.1

    #26308

    If you want to change the filter configuration, you could set metadata-complete="true" in your web.xml. In this case you completely disable the automatic processing of web-fragment.xml files. Then you can copy the original web-fragment.xml content into your web.xml and modify it as you like. But please note that this affects ALL web-fragment.xml files.

    However, I strongly recommend to NOT change the dispatcher type of the Rewrite filter. Rewrite needs to be able hook into the complete request processing lifecycle.

    What exactly is the reason you want to change the dispatch type? Is anything not working correctly?

    #26309

    fmb
    Participant

    As I said, I do use forwards internaly with a specific URL scheme. Its like:

    “/site/user/page” which loads the mapped servlet and a jsp file inside “/jsp/site/user/page.jsp” in the filesystem

    In this case I use rewrite to map requests from “/{site}/{user}/page” to proper “/site/user/page?site={site}&user={user}”, so the url is like site.com/somecity/someuser/

    I am using a custom filter to include the JSPs and rewrite on top of it, rewrite is only being used to forward the original request and change the URL.
    The problem with using all dispatchers is that if I create a rule like “/{page}” forwarded to “/{page}” it ends up recursive. Assigning rewrite filter only for requests, and my URL scheme for forwards, solves this issue.

    You may be asking: “but why would you create such rule”?
    Well, lets say you are accessing “/about/me” page and “/{site}/me” dynamically. “/{site}/me” should be redirected to “/site/me?site={site}”, but if my {site} is not registered in the application (its a specific entity that represents an internal application context), then I want to access “/about/me” instead (“about” directory with a “me” page).

    The rule would be something like this:

    .addRule()
    .when( Path.matches( "/{dir}/{page}" ) )
    .perform( Forward.to( "/{dir}/{page}" ) )
    .where( "dir" ).constrainedBy( NOT_VALID_SITE )

    and

    .addRule()
    .when( Path.matches( "/{site}/{page}" ) )
    .perform( Forward.to( "/site/{page}?site={site}" ) )
    .where( "site" ).constrainedBy( VALID_SITE );

    Thats is the reason I want to change the rewrite filter’s dispatch type.

    And by the way, I found a way to override the filter dispatcher by declaring it in the web.xml like this:

    <filter-mapping>
    	<filter-name>OCPsoft Rewrite Filter</filter-name>
    	<url-pattern>/*</url-pattern>
    	<dispatcher>REQUEST</dispatcher>
    </filter-mapping>

    It seems the filter-name thats is being declared in the web-fragment allows it to be overriden.

    Does it make sense?

    • This reply was modified 2 years, 1 month ago by  fmb.
    • This reply was modified 2 years, 1 month ago by  fmb.
    • This reply was modified 2 years, 1 month ago by  fmb.
    #26313

    I think there is an easier way for you to achieve this. You can restrict rules to a certain dispatch type in your rule definition. Just add the DispatchType condition to your rule:

    .addRule()
    .when( DispatchType.isRequest().and( Path.matches( "/foo" ) ) )
    .perform( Forward.to( "/bar.jsp" ) )
    
    #26314

    fmb
    Participant

    Indeed that is much better than globally changing the filter settings, thanks very much.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.

Comments are closed.