1 18 package org.apache.beehive.netui.tomcat; 19 20 import org.apache.catalina.*; 21 import org.apache.catalina.deploy.SecurityConstraint; 22 import org.apache.catalina.deploy.SecurityCollection; 23 import org.apache.catalina.authenticator.Constants; 24 import org.apache.coyote.tomcat5.CoyoteConnector; 25 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.http.HttpServletResponse ; 28 import javax.servlet.ServletContext ; 29 import javax.security.auth.login.LoginException ; 30 import java.io.IOException ; 31 32 public class PageflowHelperImpl implements PageflowHelper 33 { 34 35 private HttpRequest _request = null; 36 private HttpResponse _response = null; 37 private PageflowValve _valve = null; 38 39 public void login( String username, String password, HttpServletRequest request ) 40 throws LoginException 41 { 42 _valve.login( username, password, _request, _response ); 43 } 44 45 public void logout(boolean invalidateSessions, HttpServletRequest request) 46 { 47 _valve.logout( invalidateSessions, _request, _response ); 48 } 49 50 59 public boolean doSecurityRedirect( String uri, HttpServletRequest request, HttpServletResponse response, 60 ServletContext servletContext ) 61 { 62 SecurityConstraint constraint = findBestMatchSecurityConstraint( uri, _valve.getContext() ); 63 64 if ( constraint == null ) 65 return false; 66 67 try 68 { 69 return _valve.checkSecurity(_request, _response, constraint); 70 } 71 catch ( IOException e ) 72 { 73 e.printStackTrace(); return false; 75 } 76 } 77 78 102 public Boolean isSecureResource( String uri, HttpServletRequest request ) 103 { 104 Boolean result = null; 105 Context ctx = _valve.getContext(); 106 107 SecurityConstraint constraint = findBestMatchSecurityConstraint( uri, ctx ); 108 if ( constraint != null ) 109 { 110 String userDataConstraint = constraint.getUserConstraint(); 111 if ( userDataConstraint != null ) 112 { 113 String transportGuarantee = userDataConstraint; 114 if ( transportGuarantee.equalsIgnoreCase( Constants.CONFIDENTIAL_TRANSPORT ) 115 || transportGuarantee.equalsIgnoreCase( Constants.INTEGRAL_TRANSPORT ) ) 116 { 117 return Boolean.TRUE; 119 } 120 else if ( transportGuarantee.equalsIgnoreCase( Constants.NONE_TRANSPORT ) ) 121 { 122 return Boolean.FALSE; 124 } 125 } 126 } 127 128 return result; 129 } 130 131 public int getListenPort( HttpServletRequest request ) 132 { 133 int port = -1; 134 Connector conn = _request.getConnector(); 135 if ( conn instanceof CoyoteConnector ) 136 { 137 CoyoteConnector cc = (CoyoteConnector)conn; 138 if ( !cc.getSecure() ) 139 { 140 port = cc.getPort(); 142 } 143 else 144 { 145 Service svc = cc.getService(); 147 Connector[] connectors = svc.findConnectors(); 148 for ( int i=0; i<connectors.length; i++ ) 149 { 150 if ( connectors[i] != cc && connectors[i] instanceof CoyoteConnector && 151 !connectors[i].getSecure() && connectors[i].getRedirectPort() == cc.getPort() ) 152 { 153 port = ((CoyoteConnector)connectors[i]).getPort(); 154 break; 155 } 156 } 157 158 if ( port == -1 ) 159 { 160 for ( int i=0; i<connectors.length; i++ ) 162 { 163 if ( connectors[i] != cc && connectors[i] instanceof CoyoteConnector && 164 !connectors[i].getSecure() ) 165 { 166 port = ((CoyoteConnector)connectors[i]).getPort(); 167 break; 168 } 169 } 170 } 171 } 172 } 173 return port; 174 } 175 176 public int getSecureListenPort( HttpServletRequest request ) 177 { 178 int port = -1; 179 Connector conn = _request.getConnector(); 180 if ( conn instanceof CoyoteConnector ) 181 { 182 CoyoteConnector cc = (CoyoteConnector)conn; 183 if ( cc.getSecure() ) 184 { 185 port = cc.getPort(); 187 } 188 else 189 { 190 port = cc.getRedirectPort(); 192 } 193 } 194 else 195 { 196 port = conn.getRedirectPort(); 198 } 199 return port; 200 } 201 202 203 210 private SecurityConstraint findBestMatchSecurityConstraint( String uri, Context ctx ) 211 { 212 215 SecurityConstraint[] securityConstraints = ctx.findConstraints(); 216 217 if ( securityConstraints == null || securityConstraints.length == 0 ) 218 { 219 return null; 220 } 221 222 String fileExtension = getFileExtension( uri ); 223 SecurityConstraint matchingConstraint = null; 224 int matchingPathLen = -1; 225 boolean foundExact = false; 226 227 for ( int i = 0; i < securityConstraints.length && ! foundExact; ++i ) 228 { 229 SecurityConstraint securityConstraint = securityConstraints[i]; 230 SecurityCollection[] wrcs = securityConstraint.findCollections(); 231 232 for ( int j = 0; j < wrcs.length && ! foundExact; ++j ) 233 { 234 String [] urlPatterns = wrcs[j].findPatterns(); 235 236 for ( int k = 0; k < urlPatterns.length; ++k ) 237 { 238 String pattern = urlPatterns[k]; 239 240 if ( pattern.length() > matchingPathLen && pattern.endsWith( "/*" ) ) 241 { 242 if ( uri.startsWith( pattern.substring( 0, pattern.length() - 1 ) ) ) 243 { 244 matchingConstraint = securityConstraint; 245 matchingPathLen = pattern.length(); 246 } 247 } 248 else if ( matchingConstraint == null && pattern.equals( "*." + fileExtension ) ) 249 { 250 matchingConstraint = securityConstraint; 251 } 252 else if ( pattern.equals( uri ) ) 253 { 254 matchingConstraint = securityConstraint; 255 foundExact = true; 256 break; 257 } 258 } 259 } 260 } 261 262 return matchingConstraint; 263 } 264 265 private static String getFileExtension( String filename ) 266 { 267 int lastDot = filename.lastIndexOf( '.' ); 268 return lastDot != -1 ? filename.substring( lastDot + 1 ) : ""; 269 } 270 271 void initRequest( HttpRequest request, HttpResponse response, PageflowValve valve ) 272 { 273 _request = request; 274 _response = response; 275 _valve = valve; 276 } 277 } 278 | Popular Tags |