Uploaded image for project: 'Struts 2'
  1. Struts 2
  2. WW-4024

Support expressions for scheme attribute in s:url

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 2.3.8
    • Fix Version/s: 2.3.20
    • Component/s: Plugin - Tags
    • Labels:
      None

      Description

      I open this issue as of Lukasz Lenart's comment on this Stack Overflow question: http://stackoverflow.com/q/15569532/837154 . I think it would be nice to support expressions at "scheme" attribute in "s:url" tag. The situation I faced is as follows:

      I'm developing a secure web application. This application runs on an application server behind a load balancer. The load balancer receives HTTPS traffic from the internet but forwards requests to the application server with HTTP. Responses are sent back from the application server to the load balancer with HTTP and then the load balancer forwards them to the internet with HTTPS. The sysadmins disabled the HTTPS support in the application servers because of performance reasons, so when we developers need to directly access a particular server (e.g. for troubleshooting) we need to use HTTP.

      So we have a scenario where the same application, from the point of view of the final user, is accessed both by HTTP and HTTPS, but the application itself only receives HTTP traffic. Struts2, when rendering a s:url tag, by default chooses the scheme used by the request. In this case, always HTTP, which is wrong from the point of view of the final users who access the application through the load balancer.

      A work-around to this situation would be to support expressions in the scheme attribute of s:url, so with the following code:

      <s:url scheme="%{myScheme}" id="myUrl" action="MyAction" includeParams="none">
         <s:param name="someParam" value="blah"/>
      </s:url>
      
      <s:a href="%{myUrl}">click me</s:a>
      

      Struts would render either

      <a href="https://myhost.com/mywebapp/MyAction.action?someParam=blah">click me</a>
      

      or

      <a href="http://myhost.com/mywebapp/MyAction.action?someParam=blah">click me</a>
      

      instead of the current output:

      <a href="%{myScheme}://myhost.com/mywebapp/MyAction.action?someParam=blah">click me</a>
      

      In my case, I've overcome this situation by using a custom implementation of UrlRenderer, actually a near copy-paste of ServletUrlRenderer, with the only change being the line where Struts2 sets the default value for the scheme attribute. I've set this default value to my convenience, using a value from the Value Stack.

      Thanks for taking this into consideration!

      1. CustomUrlRenderer.java
        15 kB
        Octavi Ripolles-Querol
      2. CustomUrlRendererV2.java
        15 kB
        Octavi Ripolles-Querol
      3. WW-3999-v1-url-tag.patch
        14 kB
        Lukasz Lenart

        Issue Links

          Activity

          Hide
          hudson Hudson added a comment -

          FAILURE: Integrated in Struts-JDK6-develop #83 (See https://builds.apache.org/job/Struts-JDK6-develop/83/)
          WW-4024 Allows specify scheme via expression (lukaszlenart: rev 69baf692a44798ed4e1a5ab1e5e0236bba315b39)

          • core/src/main/java/org/apache/struts2/components/ServletUrlRenderer.java
          Show
          hudson Hudson added a comment - FAILURE: Integrated in Struts-JDK6-develop #83 (See https://builds.apache.org/job/Struts-JDK6-develop/83/ ) WW-4024 Allows specify scheme via expression (lukaszlenart: rev 69baf692a44798ed4e1a5ab1e5e0236bba315b39) core/src/main/java/org/apache/struts2/components/ServletUrlRenderer.java
          Hide
          jira-bot ASF subversion and git services added a comment -

          Commit 69baf692a44798ed4e1a5ab1e5e0236bba315b39 in struts's branch refs/heads/develop from Lukasz Lenart
          [ https://git-wip-us.apache.org/repos/asf?p=struts.git;h=69baf69 ]

          WW-4024 Allows specify scheme via expression

          Show
          jira-bot ASF subversion and git services added a comment - Commit 69baf692a44798ed4e1a5ab1e5e0236bba315b39 in struts's branch refs/heads/develop from Lukasz Lenart [ https://git-wip-us.apache.org/repos/asf?p=struts.git;h=69baf69 ] WW-4024 Allows specify scheme via expression
          Hide
          lukaszlenart Lukasz Lenart added a comment -

          Done, thanks for the patch!

          Show
          lukaszlenart Lukasz Lenart added a comment - Done, thanks for the patch!
          Hide
          lukaszlenart Lukasz Lenart added a comment -

          First attempt to implement the change, support for <s:url/> already added, problem is with <s:form/>

          Show
          lukaszlenart Lukasz Lenart added a comment - First attempt to implement the change, support for <s:url/> already added, problem is with <s:form/>
          Hide
          orique Octavi Ripolles-Querol added a comment -

          I attach a second version of my custom implementation (CustomUrlRendererV2.java).

          I modified the renderFormUrl method to act the same way as the renderUrl method.

          Show
          orique Octavi Ripolles-Querol added a comment - I attach a second version of my custom implementation (CustomUrlRendererV2.java). I modified the renderFormUrl method to act the same way as the renderUrl method.
          Hide
          orique Octavi Ripolles-Querol added a comment -

          I attach my implementation. I experienced some issues when I tried to simply override the method renderUrl in ServletUrlRenderer, such as having no visibility nor getter for "urlHelper" object, so I've come up with this modified copy of ServletUrlRenderer.

          Show
          orique Octavi Ripolles-Querol added a comment - I attach my implementation. I experienced some issues when I tried to simply override the method renderUrl in ServletUrlRenderer, such as having no visibility nor getter for "urlHelper" object, so I've come up with this modified copy of ServletUrlRenderer.

            People

            • Assignee:
              lukaszlenart Lukasz Lenart
              Reporter:
              orique Octavi Ripolles-Querol
            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development