1

In Struts 6.2 I am using urlrewrite 4.0.3 with below config. ( explained at How to map a friendly URL to the action in Struts 2 answer)

<urlrewrite>
    <rule match-type="regex">
       <from>/callback/hamoon-handler.ib/sign/(\w+)</from>
       <to>/callback/hamoon-handler.ib?sign=$1</to>
    </rule>
</urlrewrite>

This will not work. To test I used curl to make some tests.

Below curl works ( which shows the struts action is mapped corretly):

curl "https://localhost/callback/hamoon-handler.ib?sign/v1" --header "Content-Type: application/json" --data "{ \"test\": \"1\"}"

but below ( which goes throw urlrewrite does not) and I get the error The requested resource [/callback/hamoon-handler.ib] is not available:

curl "https://localhost/callback/hamoon-handler.ib/sign/v1" --header "Content-Type: application/json" --data "{ \"test\": \"1\"}"

I see this in the log ( which shows that the rewriting is done correctly) :

utils.ServerNameMatcher DEBUG: looking for hostname match on current server name localhost
UrlRewriteFilter DEBUG: checking for status path on /callback/hamoon-handler.ib/sign/v1
UrlRewriter DEBUG: processing request for /callback/hamoon-handler.ib/sign/v1
RuleBase DEBUG: Rule 0 run called with /callback/hamoon-handler.ib/sign/v1
RuleBase DEBUG: matched "from"
substitution.MatcherReplacer DEBUG: found 1
substitution.MatcherReplacer DEBUG: replaced sb is /callback/hamoon-handler.ib?sign=v1
RuleExecutionOutput DEBUG: needs to be redirected to /callback/hamoon-handler.ib?sign=v1
UrlRewriter TRACE: got a rewritten url
RewrittenUrl TRACE: doRewrite called
RewrittenUrl TRACE: redirected to /callback/hamoon-handler.ib?sign=v1

To make a test I have change <to> to <to type="redirect">. and add -L to curl to follow redirects, by this change the action is found. (But I can not use this as my caller does not support redirect)

The UrlRewriteFilter is defined before StrutsPrepareAndExecuteFilter

The web.xml is:

<filter>
    <filter-name>UrlRewriteFilter</filter-name>
    <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
     <init-param>
        <param-name>logLevel</param-name>
        <param-value>TRACE</param-value>
    </init-param>
</filter>
 <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>UrlRewriteFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
</filter-mapping>
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

Is there any way I can fix it.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Alireza Fattahi
  • 42,517
  • 14
  • 123
  • 173

1 Answers1

1

The urlrewrite filter stands in front of the struts2 filter and it handles the request firstly. Then it forwards the request to the rewritten URL. You have to configure struts2 filter to accept FORWARD request by the dispatcher. You can see this answer to the similar problem How to forward request from servlet to action of struts2:

You have also configure struts2 filter to accept forward requests

<filter>
  <filter-name>struts2</filter-name>
  <filter-class>
    org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
  </filter-class>
</filter>
<filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
  <dispatcher>REQUEST</dispatcher>
  <dispatcher>FORWARD</dispatcher>
</filter-mapping> 
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • 1
    It worked, as i am adding struts filter dynamically I used this code `filter.addMappingForUrlPatterns( EnumSet.of( DispatcherType.FORWARD, DispatcherType.REQUEST), false, "/*");` – Alireza Fattahi Apr 05 '23 at 06:51