View Javadoc

1   package org.springframework.security.util;
2   
3   import org.springframework.util.Assert;
4   
5   /**
6    * Internal class for building redirect URLs.
7    *
8    * Could probably make more use of the classes in java.net for this.
9    *
10   * @author Luke Taylor
11   * @version $Id: RedirectUrlBuilder.java 2604 2008-02-06 16:38:47Z luke_t $
12   * @since 2.0
13   */
14  public class RedirectUrlBuilder {
15      private String scheme;
16      private String serverName;
17      private int port;
18      private String contextPath;
19      private String servletPath;
20      private String pathInfo;
21      private String query;
22  
23      public void setScheme(String scheme) {
24          if(! ("http".equals(scheme) | "https".equals(scheme)) ) {
25              throw new IllegalArgumentException("Unsupported scheme '" + scheme + "'");
26          }
27          this.scheme = scheme;
28      }
29  
30      public void setServerName(String serverName) {
31          this.serverName = serverName;
32      }
33  
34      public void setPort(int port) {
35          this.port = port;
36      }
37  
38      public void setContextPath(String contextPath) {
39          this.contextPath = contextPath;
40      }
41  
42      public void setServletPath(String servletPath) {
43          this.servletPath = servletPath;
44      }
45  
46      public void setPathInfo(String pathInfo) {
47          this.pathInfo = pathInfo;
48      }
49  
50      public void setQuery(String query) {
51          this.query = query;
52      }
53  
54      public String getUrl() {
55          StringBuffer sb = new StringBuffer();
56  
57          Assert.notNull(scheme);
58          Assert.notNull(serverName);
59  
60          sb.append(scheme).append("://").append(serverName);
61  
62          // Append the port number if it's not standard for the scheme
63          if (port != (scheme.equals("http") ? 80 : 443)) {
64              sb.append(":").append(Integer.toString(port));
65          }
66  
67          if (contextPath != null) {
68              sb.append(contextPath);
69          }
70  
71          if (servletPath != null) {
72              sb.append(servletPath);
73          }
74  
75          if (pathInfo != null) {
76              sb.append(pathInfo);
77          }
78  
79          if (query != null) {
80              sb.append("?").append(query);
81          }
82          
83          return sb.toString();
84      }
85  }