KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > util > SslUtil


1 package org.roller.presentation.util;
2
3 import javax.servlet.ServletContext JavaDoc;
4 import javax.servlet.http.HttpServletRequest JavaDoc;
5
6 import org.roller.config.RollerConfig;
7
8 /**
9  * SslUtil utility class Good ol' copy-n-paste from <a
10  * HREF="http://www.javaworld.com/javaworld/jw-02-2002/ssl/utilityclass.txt">
11  * http://www.javaworld.com/javaworld/jw-02-2002/ssl/utilityclass.txt</a>
12  * which is referenced in the following article: <a
13  * HREF="http://www.javaworld.com/javaworld/jw-02-2002/jw-0215-ssl.html">
14  * http://www.javaworld.com/javaworld/jw-02-2002/jw-0215-ssl.html</a>
15  */

16 public class SslUtil {
17     //~ Static fields/initializers =============================================
18

19     public static final String JavaDoc HTTP = "http";
20     public static final String JavaDoc HTTPS = "https";
21     public static final String JavaDoc HTTP_PORT_PARAM = "listenPort_http";
22     public static final String JavaDoc HTTPS_PORT_PARAM = "listenPort_https";
23     private static String JavaDoc HTTP_PORT = null;
24     private static String JavaDoc HTTPS_PORT = null;
25     public static final String JavaDoc STD_HTTP_PORT = "80";
26     public static final String JavaDoc STD_HTTPS_PORT = "443";
27
28     //~ Methods ================================================================
29

30     public static String JavaDoc getRedirectString(HttpServletRequest JavaDoc request,
31                                            ServletContext JavaDoc ctx, boolean isSecure) {
32         // get the port numbers from the application context
33
HTTP_PORT = RollerConfig.getProperty("securelogin.http.port");
34         HTTPS_PORT = RollerConfig.getProperty("securelogin.https.port");
35
36         // get the scheme we want to use for this page and
37
// get the scheme used in this request
38
String JavaDoc desiredScheme = isSecure ? HTTPS : HTTP;
39         String JavaDoc usingScheme = request.getScheme();
40
41         // Determine the port number we want to use
42
// and the port number we used in this request
43
String JavaDoc desiredPort = isSecure ? HTTPS_PORT : HTTP_PORT;
44         String JavaDoc usingPort = String.valueOf(request.getServerPort());
45
46         String JavaDoc urlString = null;
47
48         // Must also check ports, because of IE multiple redirect problem
49
if (!desiredScheme.equals(usingScheme) ||
50                 !desiredPort.equals(usingPort)) {
51             urlString =
52                 buildNewUrlString(request, desiredScheme, usingScheme,
53                                   desiredPort, usingPort);
54
55             // Temporarily store attributes in session
56
RequestUtil.stowRequestAttributes(request);
57         } else {
58             // Retrieve attributes from session
59
RequestUtil.reclaimRequestAttributes(request);
60         }
61
62         return urlString;
63     }
64
65     /**
66      * Builds the URL that we will redirect to
67      *
68      * @param request DOCUMENT ME!
69      * @param desiredScheme DOCUMENT ME!
70      * @param usingScheme DOCUMENT ME!
71      * @param desiredPort DOCUMENT ME!
72      * @param usingPort DOCUMENT ME!
73      *
74      * @return DOCUMENT ME!
75      */

76     private static String JavaDoc buildNewUrlString(HttpServletRequest JavaDoc request,
77                                             String JavaDoc desiredScheme,
78                                             String JavaDoc usingScheme,
79                                             String JavaDoc desiredPort, String JavaDoc usingPort) {
80         StringBuffer JavaDoc url = request.getRequestURL();
81
82         url.replace(0, usingScheme.length(), desiredScheme);
83
84         // Find the port used within the URL string
85
int startIndex = url.toString().indexOf(usingPort);
86
87         if (startIndex == -1) { // Port not found in URL
88

89             if ((!(STD_HTTPS_PORT.equals(desiredPort) &&
90                     HTTPS.equals(desiredScheme))) &&
91                     (!(STD_HTTP_PORT.equals(desiredPort) &&
92                     HTTP.equals(desiredScheme)))) {
93                 startIndex =
94                     url.toString().indexOf("/",
95                                            url.toString().indexOf("/",
96                                                                   url.toString()
97                                                                      .indexOf("/") +
98                                                                   1) + 1);
99                 url.insert(startIndex, ":" + desiredPort);
100             }
101         } else { // Port found in URL
102

103             if ((STD_HTTPS_PORT.equals(desiredPort) &&
104                     HTTPS.equals(desiredScheme)) ||
105                     (STD_HTTP_PORT.equals(desiredPort) &&
106                     HTTP.equals(desiredScheme))) {
107                 url.delete(startIndex - 1, startIndex + usingPort.length());
108             } else { // desired port is not a default port
109

110                 // Replace requested port with desired port number in URL string
111
url.replace(startIndex, startIndex + usingPort.length(),
112                             desiredPort);
113             }
114         }
115
116         // add query string, if any
117
String JavaDoc queryString = request.getQueryString();
118
119         if ((queryString != null) && (queryString.length() != 0)) {
120             url.append("?" + queryString);
121         } else {
122             queryString = RequestUtil.getRequestParameters(request);
123
124             if ((queryString != null) && (queryString.length() != 0)) {
125                 url.append("?" + queryString);
126             }
127         }
128
129         return url.toString();
130     }
131 }
132
Popular Tags