struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lukaszlen...@apache.org
Subject svn commit: r1519919 - in /struts/struts2/trunk/core/src: main/java/org/apache/struts2/views/util/DefaultUrlHelper.java test/java/org/apache/struts2/views/util/DefaultUrlHelperTest.java
Date Wed, 04 Sep 2013 06:52:27 GMT
Author: lukaszlenart
Date: Wed Sep  4 06:52:26 2013
New Revision: 1519919

URL: http://svn.apache.org/r1519919
Log:
Cleans up code, removes usage of ActionContext directly, uses @Inject instead

Modified:
    struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/util/DefaultUrlHelper.java
    struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/util/DefaultUrlHelperTest.java

Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/util/DefaultUrlHelper.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/util/DefaultUrlHelper.java?rev=1519919&r1=1519918&r2=1519919&view=diff
==============================================================================
--- struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/util/DefaultUrlHelper.java
(original)
+++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/util/DefaultUrlHelper.java
Wed Sep  4 06:52:26 2013
@@ -21,16 +21,11 @@
 
 package org.apache.struts2.views.util;
 
-import com.opensymphony.xwork2.ActionContext;
-import com.opensymphony.xwork2.inject.Container;
 import com.opensymphony.xwork2.inject.Inject;
-import com.opensymphony.xwork2.util.TextParseUtil;
-import com.opensymphony.xwork2.util.ValueStack;
 import com.opensymphony.xwork2.util.logging.Logger;
 import com.opensymphony.xwork2.util.logging.LoggerFactory;
 import org.apache.commons.lang3.StringEscapeUtils;
 import org.apache.commons.lang3.StringUtils;
-import org.apache.struts2.ServletActionContext;
 import org.apache.struts2.StrutsConstants;
 
 import javax.servlet.http.HttpServletRequest;
@@ -52,7 +47,12 @@ public class DefaultUrlHelper implements
 
     private static final Logger LOG = LoggerFactory.getLogger(DefaultUrlHelper.class);
 
+    public static final String HTTP_PROTOCOL = "http";
+    public static final String HTTPS_PROTOCOL = "https";
+
     private String encoding = "UTF-8";
+    private int httpPort = DEFAULT_HTTP_PORT;
+    private int httpsPort = DEFAULT_HTTPS_PORT;
 
     @Inject(StrutsConstants.STRUTS_I18N_ENCODING)
     public void setEncoding(String encoding) {
@@ -61,6 +61,16 @@ public class DefaultUrlHelper implements
         }
     }
 
+    @Inject(StrutsConstants.STRUTS_URL_HTTP_PORT)
+    public void setHttpPort(String httpPort) {
+        this.httpPort = Integer.parseInt(httpPort);
+    }
+
+    @Inject(StrutsConstants.STRUTS_URL_HTTPS_PORT)
+    public void setHttpsPort(String httpsPort) {
+        this.httpsPort = Integer.parseInt(httpsPort);
+    }
+
     public String buildUrl(String action, HttpServletRequest request, HttpServletResponse
response, Map<String, Object> params) {
         return buildUrl(action, request, response, params, null, true, true);
     }
@@ -77,15 +87,10 @@ public class DefaultUrlHelper implements
 
     public String buildUrl(String action, HttpServletRequest request, HttpServletResponse
response, Map<String, Object> params, String scheme,
                            boolean includeContext, boolean encodeResult, boolean forceAddSchemeHostAndPort,
boolean escapeAmp) {
-        StringBuilder link = new StringBuilder();
 
+        StringBuilder link = new StringBuilder();
         boolean changedScheme = false;
 
-        // FIXME: temporary hack until class is made a properly injected bean
-        Container cont = ActionContext.getContext().getContainer();
-        int httpPort = Integer.parseInt(cont.getInstance(String.class, StrutsConstants.STRUTS_URL_HTTP_PORT));
-        int httpsPort = Integer.parseInt(cont.getInstance(String.class, StrutsConstants.STRUTS_URL_HTTPS_PORT));
-
         // only append scheme if it is different to the current scheme *OR*
         // if we explicity want it to be appended by having forceAddSchemeHostAndPort = true
         if (forceAddSchemeHostAndPort) {
@@ -98,31 +103,30 @@ public class DefaultUrlHelper implements
             if (scheme != null) {
                 // If switching schemes, use the configured port for the particular scheme.
                 if (!scheme.equals(reqScheme)) {
-                    if ((scheme.equals("http") && (httpPort != DEFAULT_HTTP_PORT))
|| (scheme.equals("https") && httpsPort != DEFAULT_HTTPS_PORT)) {
+                    if ((HTTP_PROTOCOL.equals(scheme) && (httpPort != DEFAULT_HTTP_PORT))
|| (HTTPS_PROTOCOL.equals(scheme) && httpsPort != DEFAULT_HTTPS_PORT)) {
                         link.append(":");
-                        link.append(scheme.equals("http") ? httpPort : httpsPort);
+                        link.append(HTTP_PROTOCOL.equals(scheme) ? httpPort : httpsPort);
                     }
                 // Else use the port from the current request.
                 } else {
                     int reqPort = request.getServerPort();
 
-                    if ((scheme.equals("http") && (reqPort != DEFAULT_HTTP_PORT))
|| (scheme.equals("https") && reqPort != DEFAULT_HTTPS_PORT)) {
+                    if ((scheme.equals(HTTP_PROTOCOL) && (reqPort != DEFAULT_HTTP_PORT))
|| (scheme.equals(HTTPS_PROTOCOL) && reqPort != DEFAULT_HTTPS_PORT)) {
                         link.append(":");
                         link.append(reqPort);
                     }
                 }
             }
-        }
-        else if ((scheme != null) && !scheme.equals(request.getScheme())) {
+        } else if ((scheme != null) && !scheme.equals(request.getScheme())) {
             changedScheme = true;
             link.append(scheme);
             link.append("://");
             link.append(request.getServerName());
 
-            if ((scheme.equals("http") && (httpPort != DEFAULT_HTTP_PORT)) || (scheme.equals("https")
&& httpsPort != DEFAULT_HTTPS_PORT))
+            if ((scheme.equals(HTTP_PROTOCOL) && (httpPort != DEFAULT_HTTP_PORT))
|| (HTTPS_PROTOCOL.equals(scheme) && httpsPort != DEFAULT_HTTPS_PORT))
             {
                 link.append(":");
-                link.append(scheme.equals("http") ? httpPort : httpsPort);
+                link.append(HTTP_PROTOCOL.equals(scheme) ? httpPort : httpsPort);
             }
         }
 
@@ -208,24 +212,23 @@ public class DefaultUrlHelper implements
                 Object value = entry.getValue();
 
                 if (value instanceof Iterable) {
-                    for (Iterator iterator = ((Iterable) value).iterator(); iterator
-                        .hasNext();) {
+                    for (Iterator iterator = ((Iterable) value).iterator(); iterator.hasNext();)
{
                         Object paramValue = iterator.next();
-                        link.append(buildParameterSubstring(name, paramValue
-                            .toString()));
+                        link.append(buildParameterSubstring(name, paramValue.toString()));
 
-                        if (iterator.hasNext())
+                        if (iterator.hasNext()) {
                             link.append(paramSeparator);
+                        }
                     }
                 } else if (value instanceof Object[]) {
                     Object[] array = (Object[]) value;
                     for (int i = 0; i < array.length; i++) {
                         Object paramValue = array[i];
-                        link.append(buildParameterSubstring(name, paramValue
-                            .toString()));
+                        link.append(buildParameterSubstring(name, paramValue.toString()));
 
-                        if (i < array.length - 1)
+                        if (i < array.length - 1) {
                             link.append(paramSeparator);
+                        }
                     }
                 } else {
                     link.append(buildParameterSubstring(name, value != null ? value.toString()
: StringUtils.EMPTY));
@@ -238,7 +241,6 @@ public class DefaultUrlHelper implements
         }
     }
 
-
     private String buildParameterSubstring(String name, String value) {
         StringBuilder builder = new StringBuilder();
         builder.append(encode(name));

Modified: struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/util/DefaultUrlHelperTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/util/DefaultUrlHelperTest.java?rev=1519919&r1=1519918&r2=1519919&view=diff
==============================================================================
--- struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/util/DefaultUrlHelperTest.java
(original)
+++ struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/util/DefaultUrlHelperTest.java
Wed Sep  4 06:52:26 2013
@@ -276,8 +276,8 @@ public class DefaultUrlHelperTest extend
 
         String expectedString = "https://www.mydomain.com:7002/mywebapp/MyAction.action?foo=bar&amp;hello=earth&amp;hello=mars";
 
-        setProp(StrutsConstants.STRUTS_URL_HTTP_PORT, "7001");
-        setProp(StrutsConstants.STRUTS_URL_HTTPS_PORT, "7002");
+        urlHelper.setHttpPort("7001");
+        urlHelper.setHttpsPort("7002");
 
         Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
         mockHttpServletRequest.expectAndReturn("getServerName", "www.mydomain.com");
@@ -305,8 +305,8 @@ public class DefaultUrlHelperTest extend
 
         String expectedString = "http://www.mydomain.com:7001/mywebapp/MyAction.action?foo=bar&amp;hello=earth&amp;hello=mars";
 
-        setProp(StrutsConstants.STRUTS_URL_HTTP_PORT, "7001");
-        setProp(StrutsConstants.STRUTS_URL_HTTPS_PORT, "7002");
+        urlHelper.setHttpPort("7001");
+        urlHelper.setHttpsPort("7002");
 
         Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
         mockHttpServletRequest.expectAndReturn("getServerName", "www.mydomain.com");



Mime
View raw message