View Javadoc

1   package com.panogenesis.webapp.util;
2   
3   import java.util.HashMap;
4   import java.util.Map;
5   
6   import javax.servlet.ServletContext;
7   import javax.servlet.http.HttpServletRequest;
8   
9   import com.panogenesis.Constants;
10  
11  
12  /***
13   * SslUtil utility class Good ol' copy-n-paste from  <a
14   * href="http://www.javaworld.com/javaworld/jw-02-2002/ssl/utilityclass.txt">
15   * http://www.javaworld.com/javaworld/jw-02-2002/ssl/utilityclass.txt</a>
16   * which is referenced in the following article: <a
17   * href="http://www.javaworld.com/javaworld/jw-02-2002/jw-0215-ssl.html">
18   * http://www.javaworld.com/javaworld/jw-02-2002/jw-0215-ssl.html</a>
19   */
20  public class SslUtil {
21      //~ Static fields/initializers =============================================
22  
23      public static final String HTTP = "http";
24      public static final String HTTPS = "https";
25      public static final String HTTP_PORT_PARAM = "listenPort_http";
26      public static final String HTTPS_PORT_PARAM = "listenPort_https";
27      private static String HTTP_PORT = null;
28      private static String HTTPS_PORT = null;
29      public static final String STD_HTTP_PORT = "80";
30      public static final String STD_HTTPS_PORT = "443";
31  
32      //~ Methods ================================================================
33  
34      public static String getRedirectString(HttpServletRequest request,
35                                             ServletContext ctx, boolean isSecure) {
36          // get the port numbers from the application context
37          Map config = (HashMap) ctx.getAttribute(Constants.CONFIG);
38          HTTP_PORT = (String) config.get(Constants.HTTP_PORT);
39          HTTPS_PORT = (String) config.get(Constants.HTTPS_PORT);
40  
41          // get the scheme we want to use for this page and
42          // get the scheme used in this request
43          String desiredScheme = isSecure ? HTTPS : HTTP;
44          String usingScheme = request.getScheme();
45  
46          // Determine the port number we want to use
47          // and the port number we used in this request
48          String desiredPort = isSecure ? HTTPS_PORT : HTTP_PORT;
49          String usingPort = String.valueOf(request.getServerPort());
50  
51          String urlString = null;
52  
53          // Must also check ports, because of IE multiple redirect problem
54          if (!desiredScheme.equals(usingScheme) ||
55                  !desiredPort.equals(usingPort)) {
56              urlString =
57                  buildNewUrlString(request, desiredScheme, usingScheme,
58                                    desiredPort, usingPort);
59  
60              // Temporarily store attributes in session
61              RequestUtil.stowRequestAttributes(request);
62          } else {
63              // Retrieve attributes from session
64              RequestUtil.reclaimRequestAttributes(request);
65          }
66  
67          return urlString;
68      }
69  
70      /***
71       * Builds the URL that we will redirect to
72       *
73       * @param request DOCUMENT ME!
74       * @param desiredScheme DOCUMENT ME!
75       * @param usingScheme DOCUMENT ME!
76       * @param desiredPort DOCUMENT ME!
77       * @param usingPort DOCUMENT ME!
78       *
79       * @return DOCUMENT ME!
80       */
81      private static String buildNewUrlString(HttpServletRequest request,
82                                              String desiredScheme,
83                                              String usingScheme,
84                                              String desiredPort, String usingPort) {
85          StringBuffer url = request.getRequestURL();
86  
87          url.replace(0, usingScheme.length(), desiredScheme);
88  
89          // Find the port used within the URL string
90          int startIndex = url.toString().indexOf(usingPort);
91  
92          if (startIndex == -1) { // Port not found in URL
93  
94              if ((!(STD_HTTPS_PORT.equals(desiredPort) &&
95                      HTTPS.equals(desiredScheme))) &&
96                      (!(STD_HTTP_PORT.equals(desiredPort) &&
97                      HTTP.equals(desiredScheme)))) {
98                  startIndex =
99                      url.toString().indexOf("/",
100                                            url.toString().indexOf("/",
101                                                                   url.toString()
102                                                                      .indexOf("/") +
103                                                                   1) + 1);
104                 url.insert(startIndex, ":" + desiredPort);
105             }
106         } else { // Port found in URL
107 
108             if ((STD_HTTPS_PORT.equals(desiredPort) &&
109                     HTTPS.equals(desiredScheme)) ||
110                     (STD_HTTP_PORT.equals(desiredPort) &&
111                     HTTP.equals(desiredScheme))) {
112                 url.delete(startIndex - 1, startIndex + usingPort.length());
113             } else { // desired port is not a default port
114 
115                 // Replace requested port with desired port number in URL string
116                 url.replace(startIndex, startIndex + usingPort.length(),
117                             desiredPort);
118             }
119         }
120 
121         // add query string, if any
122         String queryString = request.getQueryString();
123 
124         if ((queryString != null) && (queryString.length() != 0)) {
125             url.append("?" + queryString);
126         } else {
127             queryString = RequestUtil.getRequestParameters(request);
128 
129             if ((queryString != null) && (queryString.length() != 0)) {
130                 url.append("?" + queryString);
131             }
132         }
133 
134         return url.toString();
135     }
136 }