KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > wsm > axis > security > model > AxisSecurityModel


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

22
23 import java.util.Collection JavaDoc;
24
25
26 import org.apache.beehive.wsm.axis.security.SecurityModel ;
27
28 import org.apache.axis.MessageContext;
29 import org.apache.axis.components.logger.LogFactory;
30 import org.apache.axis.security.AuthenticatedUser;
31 import org.apache.axis.security.SecurityProvider;
32 import org.apache.axis.security.simple.SimpleSecurityProvider;
33 import org.apache.axis.security.servlet.ServletSecurityProvider;
34 import org.apache.log4j.Logger;
35
36 public class AxisSecurityModel implements SecurityModel {
37
38     protected static Logger logger = Logger.getLogger(AxisSecurityModel.class);
39
40     public void init ( MessageContext msgContext )
41     {
42         // do nothing
43
}
44
45     /**
46      * mostly copied from org/apache/axis/handlers/SimpleAuthenticationHandler.java
47      */

48     public boolean isUserInRole ( MessageContext msgContext, Collection JavaDoc<String JavaDoc> rolesAllowed ){
49
50         if (logger.isDebugEnabled()) {
51             logger.debug("Enter: AxisSecurityModel::isUserInRole");
52         }
53
54         SecurityProvider provider = (SecurityProvider)msgContext.getProperty(MessageContext.SECURITY_PROVIDER);
55         if ( provider instanceof ServletSecurityProvider )
56         {
57             // SecurityProvider must not be an instance of ServletSecurityProvider for AxisSecurityModel.
58
// Thus, provides SimpleSecurityProvider forcelly.
59
provider = new SimpleSecurityProvider();
60         }
61
62         if (provider != null) {
63             String JavaDoc userID = msgContext.getUsername();
64             if (logger.isDebugEnabled()) {
65                 logger.debug("user : " + userID );
66             }
67
68             // in order to authenticate, the user must exist
69
if ( userID == null || userID.equals("") )
70             {
71                 logger.debug("userID is null");
72                 return false;
73             }
74
75             String JavaDoc passwd = msgContext.getPassword();
76             if (logger.isDebugEnabled()) {
77                 logger.debug("password : " + passwd );
78             }
79
80             AuthenticatedUser authUser = provider.authenticate(msgContext);
81
82             // if a password is defined, then it must match
83
if ( authUser == null) {
84                 logger.debug("authuser is null");
85                 return false;
86             }
87
88             for ( String JavaDoc role : rolesAllowed )
89             {
90                 if (provider.userMatches( authUser, role ) )
91                 {
92                     // BINGO !!
93

94                     if (logger.isDebugEnabled()) {
95                         logger.debug( "auth : " + userID + " is in role [" + role + "]");
96                     }
97                     msgContext.setProperty(SecurityModel.BEEHIVE_AUTHUSER, authUser);
98                     return true;
99                 }
100             }
101         }
102
103         if (logger.isDebugEnabled()) {
104             logger.debug("Exit: AxisSecurityModel::isUserInRole");
105         }
106
107         return false;
108     }
109
110
111 }
112
Popular Tags