1 package org.roller.presentation.util; 2 3 import javax.servlet.ServletContext ; 4 import javax.servlet.http.HttpServletRequest ; 5 6 import org.roller.config.RollerConfig; 7 8 16 public class SslUtil { 17 19 public static final String HTTP = "http"; 20 public static final String HTTPS = "https"; 21 public static final String HTTP_PORT_PARAM = "listenPort_http"; 22 public static final String HTTPS_PORT_PARAM = "listenPort_https"; 23 private static String HTTP_PORT = null; 24 private static String HTTPS_PORT = null; 25 public static final String STD_HTTP_PORT = "80"; 26 public static final String STD_HTTPS_PORT = "443"; 27 28 30 public static String getRedirectString(HttpServletRequest request, 31 ServletContext ctx, boolean isSecure) { 32 HTTP_PORT = RollerConfig.getProperty("securelogin.http.port"); 34 HTTPS_PORT = RollerConfig.getProperty("securelogin.https.port"); 35 36 String desiredScheme = isSecure ? HTTPS : HTTP; 39 String usingScheme = request.getScheme(); 40 41 String desiredPort = isSecure ? HTTPS_PORT : HTTP_PORT; 44 String usingPort = String.valueOf(request.getServerPort()); 45 46 String urlString = null; 47 48 if (!desiredScheme.equals(usingScheme) || 50 !desiredPort.equals(usingPort)) { 51 urlString = 52 buildNewUrlString(request, desiredScheme, usingScheme, 53 desiredPort, usingPort); 54 55 RequestUtil.stowRequestAttributes(request); 57 } else { 58 RequestUtil.reclaimRequestAttributes(request); 60 } 61 62 return urlString; 63 } 64 65 76 private static String buildNewUrlString(HttpServletRequest request, 77 String desiredScheme, 78 String usingScheme, 79 String desiredPort, String usingPort) { 80 StringBuffer url = request.getRequestURL(); 81 82 url.replace(0, usingScheme.length(), desiredScheme); 83 84 int startIndex = url.toString().indexOf(usingPort); 86 87 if (startIndex == -1) { 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 { 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 { 110 url.replace(startIndex, startIndex + usingPort.length(), 112 desiredPort); 113 } 114 } 115 116 String 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 |