1 18 package org.apache.beehive.netui.tomcat; 19 20 import org.apache.beehive.netui.pageflow.scoping.ScopedServletUtils; 21 import org.apache.beehive.netui.pageflow.DefaultServletContainerAdapter; 22 import org.apache.beehive.netui.pageflow.SecurityProtocol; 23 import org.apache.beehive.netui.pageflow.adapter.AdapterContext; 24 import org.apache.beehive.netui.util.logging.Logger; 25 import org.apache.beehive.netui.util.internal.DiscoveryUtils; 26 27 import javax.servlet.http.HttpServletRequest ; 28 import javax.servlet.http.HttpServletResponse ; 29 import javax.security.auth.login.LoginException ; 30 31 public class TomcatServletContainerAdapter extends DefaultServletContainerAdapter 32 { 33 private static final Logger _log = Logger.getInstance( TomcatServletContainerAdapter.class ); 34 35 private static final String CATALINA_HOME_PROP = "catalina.home"; 36 private static final String HELPER_INTERFACE_CLASSNAME = "org.apache.beehive.netui.tomcat.PageflowHelper"; 37 38 39 public boolean accept( AdapterContext context ) 40 { 41 if ( System.getProperty( CATALINA_HOME_PROP ) != null ) 42 { 43 try 44 { 45 DiscoveryUtils.getClassLoader().loadClass( HELPER_INTERFACE_CLASSNAME ); 49 } 50 catch ( ClassNotFoundException e ) 51 { 52 _log.error( "Could not load helper interface " + HELPER_INTERFACE_CLASSNAME + "; cannot use " 53 + TomcatServletContainerAdapter.class.getName() + ". Make sure that the netui tomcat common" + 54 " and server jars are in Tomcat's common and server lib directories, respectively.", e ); 55 return false; 56 } 57 58 if ( _log.isInfoEnabled() ) 59 { 60 _log.info( "Using " + TomcatServletContainerAdapter.class.getName() + " as the ServletContainerAdapter." ); 61 } 62 63 return true; 64 } 65 else 66 { 67 if ( _log.isInfoEnabled() ) 68 { 69 _log.info( "Not running in Tomcat (" + CATALINA_HOME_PROP + " is not set); " 70 + TomcatServletContainerAdapter.class.getName() + " will not be used." ); 71 } 72 73 return false; 74 } 75 } 76 77 101 102 public SecurityProtocol getSecurityProtocol( String uri, HttpServletRequest request ) 103 { 104 uri = ScopedServletUtils.normalizeURI( uri ); 105 Boolean isSecure= getHelper( request ).isSecureResource( uri, request ); 106 107 return isSecure != null 108 ? ( isSecure.booleanValue() ? SecurityProtocol.SECURE : SecurityProtocol.UNSECURE ) 109 : SecurityProtocol.UNSPECIFIED; 110 } 111 112 113 public boolean doSecurityRedirect( String uri, HttpServletRequest request, HttpServletResponse response ) 114 { 115 return getHelper( request ).doSecurityRedirect( uri, request, response, getServletContext() ); 116 } 117 118 119 public int getListenPort( HttpServletRequest request ) 120 { 121 return getHelper( request ).getListenPort( request ); 122 } 123 124 125 public int getSecureListenPort( HttpServletRequest request ) 126 { 127 return getHelper( request ).getSecureListenPort( request ); 128 } 129 130 131 public void login( String username, String password, HttpServletRequest request, HttpServletResponse response ) 132 throws LoginException 133 { 134 getHelper( request ).login( username, password, request ); 135 } 136 137 138 public void logout( boolean invalidateSessions, HttpServletRequest request, HttpServletResponse response ) 139 { 140 getHelper( request ).logout( invalidateSessions, request ); 141 } 142 143 private static PageflowHelper getHelper( HttpServletRequest request ) 144 { 145 HttpServletRequest outerRequest = ScopedServletUtils.getOuterRequest( request ); 146 PageflowHelper helper = ( PageflowHelper ) outerRequest.getAttribute( PageflowHelper.PAGEFLOW_HELPER_KEY ); 147 148 if ( helper == null ) 149 { 150 String msg = 151 "Page Flow helper not found in session. Make sure PageflowValve is installed in this context."; 152 _log.error( msg ); 153 throw new UnsupportedOperationException ( msg ); 154 } 155 156 return helper; 157 } 158 } 159 | Popular Tags |