Commits

cameronbraid  committed 376687b

Issue number: WW-241
ServletRedirectResult should redirect to contextPath + location

The redirect URL is now encoded using response.encodeRedirectURL
If the location is relative, the current action's namespace is prepended to the location
The redirect URL has the request.getContextPath() preprended, unless it is an absolute URL, or the parameter prependServletContext = false

git-svn-id: http://svn.opensymphony.com/svn/webwork/trunk@31573baa09-0c28-0410-bef9-dab3c582ae83

  • Participants
  • Parent commits 6fbaa69

Comments (0)

Files changed (1)

File src/java/com/opensymphony/webwork/dispatcher/ServletRedirectResult.java

     //~ Instance fields ////////////////////////////////////////////////////////
 
     private String location;
+    private boolean prependServletContext = true;
     private boolean parse = true;
 
     //~ Methods ////////////////////////////////////////////////////////////////
 
+    public void setPrependServletContext(boolean prependServletContext) {
+        this.prependServletContext = prependServletContext;
+    }
+
     public void setLocation(String location) {
         this.location = location;
     }
             location = TextParseUtil.translateVariables(location, stack);
         }
 
+        String redirectLocation = location;
+
+        if (isPathUrl(redirectLocation))
+    	{
+    		
+	        if (!redirectLocation.startsWith("/")) {
+	
+	            String actionPath = request.getServletPath();
+	            String namespace = ServletDispatcher.getNamespaceFromServletPath(actionPath);
+	
+	            if ((namespace != null) && (namespace.length() > 0)) {
+	                redirectLocation = namespace + "/" + redirectLocation;
+	            }
+	            else {
+	            	redirectLocation = "/" + redirectLocation;
+	            }
+	
+	        }
+	
+			// if the URL's are relative to the servlet context, append the servlet context path 
+			if (prependServletContext && request.getContextPath() != null && request.getContextPath().length() > 0)
+			{
+				redirectLocation = request.getContextPath() + redirectLocation; 
+			}
+
+			redirectLocation = response.encodeRedirectURL(redirectLocation);
+		}
+
         if (log.isDebugEnabled()) {
-            log.debug("Redirecting to location " + location);
+            log.debug("Redirecting to location " + redirectLocation);
         }
 
-        if (location.startsWith("/")) {
-            response.sendRedirect(location);
-        } else {
-            response.sendRedirect(request.getContextPath()+ "/" + location);
-        }
+        response.sendRedirect(redirectLocation);
     }
+
+	private static boolean isPathUrl(String url)
+	{
+		// filter out "http:", "https:", "mailto:", "file:", "ftp:"
+		// since the only valid places for : in URL's is before the path specification
+		// either before the port, or after the protocol
+		return (url.indexOf(':') == -1);
+	}
 }