1 16 17 package org.apache.velocity.tools.struts; 18 19 import java.util.Iterator ; 20 import javax.servlet.ServletContext ; 21 import javax.servlet.http.HttpServletRequest ; 22 23 import org.apache.velocity.tools.view.tools.LinkTool; 24 import org.apache.velocity.tools.struts.StrutsUtils; 25 26 import org.apache.struts.config.ModuleConfig; 27 import org.apache.struts.config.SecureActionConfig; 28 import org.apache.struts.action.SecurePlugInInterface; 29 import org.apache.struts.Globals; 30 31 58 public class SecureLinkTool extends LinkTool 59 { 60 61 private static final String HTTP = "http"; 62 private static final String HTTPS = "https"; 63 private static final String STD_HTTP_PORT = "80"; 64 private static final String STD_HTTPS_PORT = "443"; 65 66 67 78 public SecureLinkTool setAction(String action) 79 { 80 String link = StrutsUtils.getActionMappingURL(application, request, action); 81 return (SecureLinkTool)copyWith(computeURL(request, application, link)); 82 } 83 84 95 public SecureLinkTool setForward(String forward) 96 { 97 String url = StrutsUtils.getForwardURL(request, application, forward); 98 if (url == null) 99 { 100 return null; 101 } 102 return (SecureLinkTool)copyWith(url); 103 } 104 105 115 public String computeURL(HttpServletRequest request, 116 ServletContext app, String link) 117 { 118 StringBuffer url = new StringBuffer (link); 119 120 String contextPath = request.getContextPath(); 121 122 SecurePlugInInterface securePlugin = (SecurePlugInInterface)app.getAttribute(SecurePlugInInterface.SECURE_PLUGIN); 123 124 if (securePlugin.getSslExtEnable() && 125 url.toString().startsWith(contextPath)) 126 { 127 String usingScheme = request.getScheme(); 129 String usingPort = String.valueOf(request.getServerPort()); 130 131 String linkString = url.toString().substring(contextPath.length()); 133 134 SecureActionConfig secureConfig = getActionConfig(request, app, linkString); 136 137 if (secureConfig != null && 139 !SecureActionConfig.ANY.equalsIgnoreCase(secureConfig.getSecure())) 140 { 141 String desiredScheme = Boolean.valueOf(secureConfig.getSecure()).booleanValue() ? 142 HTTPS : HTTP; 143 String desiredPort = Boolean.valueOf(secureConfig.getSecure()).booleanValue() ? 144 securePlugin.getHttpsPort() : securePlugin.getHttpPort(); 145 146 if (!desiredScheme.equals(usingScheme) || 148 !desiredPort.equals(usingPort)) 149 { 150 url.insert(0, startNewUrlString(request, desiredScheme, desiredPort)); 151 152 if (securePlugin.getSslExtAddSession() && url.toString().indexOf(";jsessionid=") < 0) 158 { 159 url = new StringBuffer (toEncoded(url.toString(), 161 request.getSession().getId())); 162 } 163 } 164 } 165 } 166 return url.toString(); 167 } 168 169 179 private static SecureActionConfig getActionConfig(HttpServletRequest 180 request, 181 ServletContext app, 182 String linkString) 183 { 184 ModuleConfig moduleConfig = StrutsUtils.selectModule(linkString, app); 185 186 linkString = linkString.substring(moduleConfig.getPrefix().length()); 188 189 192 SecurePlugInInterface spi = (SecurePlugInInterface)app.getAttribute( 193 SecurePlugInInterface.SECURE_PLUGIN); 194 Iterator mappingItr = spi.getServletMappings().iterator(); 195 while (mappingItr.hasNext()) 196 { 197 String servletMapping = (String )mappingItr.next(); 198 199 int starIndex = servletMapping != null ? servletMapping.indexOf('*') 200 : -1; 201 if (starIndex == -1) 202 { 203 continue; 204 } 206 String prefix = servletMapping.substring(0, starIndex); 207 String suffix = servletMapping.substring(starIndex + 1); 208 209 int jsession = linkString.indexOf(";jsessionid="); 211 if (jsession >= 0) 212 { 213 linkString = linkString.substring(0, jsession); 214 } 215 216 int question = linkString.indexOf("?"); 219 if (question >= 0) 220 { 221 linkString = linkString.substring(0, question); 222 } 223 224 int anchor = linkString.indexOf("#"); 226 if (anchor >= 0) 227 { 228 linkString = linkString.substring(0, anchor); 229 } 230 231 232 if (!(linkString.startsWith(prefix) && linkString.endsWith(suffix))) 234 { 235 continue; 236 } 237 238 linkString = linkString.substring(prefix.length()); 240 linkString = linkString.substring(0, 241 linkString.length() 242 - suffix.length()); 243 if (!linkString.startsWith("/")) 244 { 245 linkString = "/" + linkString; 246 } 247 248 SecureActionConfig secureConfig = (SecureActionConfig)moduleConfig. 249 findActionConfig(linkString); 250 251 return secureConfig; 252 } 253 return null; 254 255 } 256 257 264 private static StringBuffer startNewUrlString(HttpServletRequest request, 265 String desiredScheme, 266 String desiredPort) 267 { 268 StringBuffer url = new StringBuffer (); 269 String serverName = request.getServerName(); 270 url.append(desiredScheme).append("://").append(serverName); 271 272 if ((HTTP.equals(desiredScheme) && !STD_HTTP_PORT.equals(desiredPort)) || 273 (HTTPS.equals(desiredScheme) && !STD_HTTPS_PORT.equals(desiredPort))) 274 { 275 url.append(":").append(desiredPort); 276 } 277 return url; 278 } 279 280 288 public String toEncoded(String url, String sessionId) 289 { 290 if (url == null || sessionId == null) 291 { 292 return (url); 293 } 294 295 String path = url; 296 String query = ""; 297 String anchor = ""; 298 299 int pound = url.indexOf('#'); 301 if (pound >= 0) 302 { 303 path = url.substring(0, pound); 304 anchor = url.substring(pound); 305 } 306 int question = path.indexOf('?'); 307 if (question >= 0) 308 { 309 query = path.substring(question); 310 path = path.substring(0, question); 311 } 312 StringBuffer sb = new StringBuffer (path); 313 if (sb.length() > 0) 315 { 316 sb.append(";jsessionid="); 317 sb.append(sessionId); 318 } 319 sb.append(query); 320 sb.append(anchor); 321 return sb.toString(); 322 } 323 324 } 325 | Popular Tags |