KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > util > core > SslUtil


1 package com.blandware.atleap.webapp.util.core;
2
3 import com.blandware.atleap.common.Constants;
4
5 import javax.servlet.ServletContext JavaDoc;
6 import javax.servlet.http.HttpServletRequest JavaDoc;
7 import java.util.HashMap JavaDoc;
8 import java.util.Map JavaDoc;
9
10
11 /**
12  * SslUtil utility class Good ol' copy-n-paste from <a
13  * HREF="http://www.javaworld.com/javaworld/jw-02-2002/ssl/utilityclass.txt">
14  * http://www.javaworld.com/javaworld/jw-02-2002/ssl/utilityclass.txt</a>
15  * which is referenced in the following article: <a
16  * HREF="http://www.javaworld.com/javaworld/jw-02-2002/jw-0215-ssl.html">
17  * http://www.javaworld.com/javaworld/jw-02-2002/jw-0215-ssl.html</a>
18  */

19 public class SslUtil {
20     //~ Static fields/initializers =============================================
21

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

33     /**
34      * Returns the redirect URL (and, if needed, stores request parameters in
35      * session).
36      *
37      * @param request The request
38      * @param ctx Servlet context
39      * @param isSecure Whether the returned URL should be secure (use SSL)
40      * @return Redirect URL
41      */

42     public static String JavaDoc getRedirectString(HttpServletRequest JavaDoc request,
43                                            ServletContext JavaDoc ctx, boolean isSecure) {
44         // get the port numbers from the application context
45
Map JavaDoc config = (HashMap JavaDoc) ctx.getAttribute(Constants.CONFIG);
46         HTTP_PORT = (String JavaDoc) config.get(Constants.HTTP_PORT);
47         HTTPS_PORT = (String JavaDoc) config.get(Constants.HTTPS_PORT);
48
49         // get the scheme we want to use for this page and
50
// get the scheme used in this request
51
String JavaDoc desiredScheme = isSecure ? HTTPS : HTTP;
52         String JavaDoc usingScheme = request.getScheme();
53
54         // Determine the port number we want to use
55
// and the port number we used in this request
56
String JavaDoc desiredPort = isSecure ? HTTPS_PORT : HTTP_PORT;
57         String JavaDoc usingPort = String.valueOf(request.getServerPort());
58
59         String JavaDoc urlString = null;
60
61         // Must also check ports, because of IE multiple redirect problem
62
if ( !desiredScheme.equals(usingScheme) ||
63                 !desiredPort.equals(usingPort) ) {
64             urlString =
65                     buildNewUrlString(request, desiredScheme, usingScheme,
66                             desiredPort, usingPort);
67
68             // Temporarily store attributes in session
69
RequestUtil.stowRequestAttributes(request);
70         } else {
71             // Retrieve attributes from session
72
RequestUtil.reclaimRequestAttributes(request);
73         }
74
75         return urlString;
76     }
77
78     /**
79      * Builds the URL that we will redirect to replacing some parts of URL that
80      * corresponds to the given request
81      *
82      * @param request The request
83      * @param desiredScheme Scheme that we want to use
84      * @param usingScheme Scheme that is used for given request
85      * @param desiredPort Port that we want to use
86      * @param usingPort Port that is used for given request
87      * @return Resulting URL
88      */

89     private static String JavaDoc buildNewUrlString(HttpServletRequest JavaDoc request,
90                                             String JavaDoc desiredScheme,
91                                             String JavaDoc usingScheme,
92                                             String JavaDoc desiredPort, String JavaDoc usingPort) {
93         StringBuffer JavaDoc url = request.getRequestURL();
94
95         url.replace(0, usingScheme.length(), desiredScheme);
96
97         // Find the port used within the URL string
98
int startIndex = url.toString().indexOf(usingPort);
99
100         if ( startIndex == -1 ) { // Port not found in URL
101

102             if ( (!(STD_HTTPS_PORT.equals(desiredPort) &&
103                     HTTPS.equals(desiredScheme))) &&
104                     (!(STD_HTTP_PORT.equals(desiredPort) &&
105                     HTTP.equals(desiredScheme))) ) {
106                 startIndex =
107                         url.toString().indexOf("/",
108                                 url.toString().indexOf("/",
109                                         url.toString()
110                         .indexOf("/") +
111                         1) + 1);
112                 url.insert(startIndex, ":" + desiredPort);
113             }
114         } else { // Port found in URL
115

116             if ( (STD_HTTPS_PORT.equals(desiredPort) &&
117                     HTTPS.equals(desiredScheme)) ||
118                     (STD_HTTP_PORT.equals(desiredPort) &&
119                     HTTP.equals(desiredScheme)) ) {
120                 url.delete(startIndex - 1, startIndex + usingPort.length());
121             } else { // desired port is not a default port
122

123                 // Replace requested port with desired port number in URL string
124
url.replace(startIndex, startIndex + usingPort.length(),
125                         desiredPort);
126             }
127         }
128
129         // add query string, if any
130
String JavaDoc queryString = request.getQueryString();
131
132         if ( (queryString != null) && (queryString.length() != 0) ) {
133             url.append("?" + queryString);
134         } else {
135             queryString = RequestUtil.getRequestParameters(request);
136
137             if ( (queryString != null) && (queryString.length() != 0) ) {
138                 url.append("?" + queryString);
139             }
140         }
141
142         return url.toString();
143     }
144 }
145
Popular Tags