KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tomcat > TomcatServletContainerAdapter


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

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 JavaDoc;
28 import javax.servlet.http.HttpServletResponse JavaDoc;
29 import javax.security.auth.login.LoginException JavaDoc;
30
31 public class TomcatServletContainerAdapter extends DefaultServletContainerAdapter
32 {
33     private static final Logger _log = Logger.getInstance( TomcatServletContainerAdapter.class );
34     
35     private static final String JavaDoc CATALINA_HOME_PROP = "catalina.home";
36     private static final String JavaDoc 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                 //
46
// See if our helper interface is in the common classloader.
47
//
48
DiscoveryUtils.getClassLoader().loadClass( HELPER_INTERFACE_CLASSNAME );
49             }
50             catch ( ClassNotFoundException JavaDoc 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     /**
78      * Tell whether a web application resource requires a secure transport protocol. This is
79      * determined from web.xml; for example, the following block specifies that all resources under
80      * /login require a secure transport protocol.
81      * <pre>
82      * &lt;security-constraint&gt;
83      * &lt;web-resource-collection&gt;
84      * &lt;web-resource-name&gt;Secure PageFlow - begin&lt;/web-resource-name&gt;
85      * &lt;url-pattern&gt;/login/*&lt;/url-pattern&gt;
86      * &lt;/web-resource-collection&gt;
87      * &lt;user-data-constraint&gt;
88      * &lt;transport-guarantee&gt;CONFIDENTIAL&lt;/transport-guarantee&gt;
89      * &lt;/user-data-constraint&gt;
90      * &lt;/security-constraint&gt;
91      * </pre>
92      *
93      * @param uri a webapp-relative URI for a resource. There must not be query parameters or a scheme
94      * on the URI.
95      * @param request the current request.
96      * @return <code>Boolean.TRUE</code> if a transport-guarantee of <code>CONFIDENTIAL</code> or
97      * <code>INTEGRAL</code> is associated with the given resource; <code>Boolean.FALSE</code>
98      * a transport-guarantee of <code>NONE</code> is associated with the given resource; or
99      * <code>null</code> if there is no transport-guarantee associated with the given resource.
100      */

101     
102     public SecurityProtocol getSecurityProtocol( String JavaDoc uri, HttpServletRequest JavaDoc request )
103     {
104         uri = ScopedServletUtils.normalizeURI( uri );
105         Boolean JavaDoc 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 JavaDoc uri, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response )
114     {
115         return getHelper( request ).doSecurityRedirect( uri, request, response, getServletContext() );
116     }
117
118     
119     public int getListenPort( HttpServletRequest JavaDoc request )
120     {
121         return getHelper( request ).getListenPort( request );
122     }
123
124     
125     public int getSecureListenPort( HttpServletRequest JavaDoc request )
126     {
127         return getHelper( request ).getSecureListenPort( request );
128     }
129
130     
131     public void login( String JavaDoc username, String JavaDoc password, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response )
132             throws LoginException JavaDoc
133     {
134         getHelper( request ).login( username, password, request );
135     }
136
137     
138     public void logout( boolean invalidateSessions, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response )
139     {
140         getHelper( request ).logout( invalidateSessions, request );
141     }
142
143     private static PageflowHelper getHelper( HttpServletRequest JavaDoc request )
144     {
145         HttpServletRequest JavaDoc outerRequest = ScopedServletUtils.getOuterRequest( request );
146         PageflowHelper helper = ( PageflowHelper ) outerRequest.getAttribute( PageflowHelper.PAGEFLOW_HELPER_KEY );
147         
148         if ( helper == null )
149         {
150             String JavaDoc 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 JavaDoc( msg );
154         }
155         
156         return helper;
157     }
158 }
159
Popular Tags