View Javadoc

1   /*
2    * $Id: AbstractEndpointSecurityFilter.java 7976 2007-08-21 14:26:13Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.impl.security;
12  
13  import org.mule.MuleManager;
14  import org.mule.config.i18n.CoreMessages;
15  import org.mule.umo.UMOEvent;
16  import org.mule.umo.endpoint.UMOEndpoint;
17  import org.mule.umo.endpoint.UMOImmutableEndpoint;
18  import org.mule.umo.lifecycle.InitialisationException;
19  import org.mule.umo.security.CryptoFailureException;
20  import org.mule.umo.security.EncryptionStrategyNotFoundException;
21  import org.mule.umo.security.SecurityException;
22  import org.mule.umo.security.SecurityProviderNotFoundException;
23  import org.mule.umo.security.UMOCredentialsAccessor;
24  import org.mule.umo.security.UMOEndpointSecurityFilter;
25  import org.mule.umo.security.UMOSecurityManager;
26  import org.mule.umo.security.UMOSecurityProvider;
27  import org.mule.umo.security.UnknownAuthenticationTypeException;
28  import org.mule.util.StringUtils;
29  
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  
33  /**
34   * <code>AbstractEndpointSecurityFilter</code> provides basic initialisation for
35   * all security filters, namely configuring the SecurityManager for this instance
36   */
37  
38  public abstract class AbstractEndpointSecurityFilter implements UMOEndpointSecurityFilter
39  {
40      /**
41       * logger used by this class
42       */
43      protected transient Log logger = LogFactory.getLog(getClass());
44  
45      private UMOSecurityManager securityManager;
46      private String securityProviders;
47      private UMOImmutableEndpoint endpoint;
48      private boolean inbound = false;
49      private boolean authenticate;
50      private UMOCredentialsAccessor credentialsAccessor;
51  
52      public final void initialise() throws InitialisationException
53      {
54          if (securityManager == null)
55          {
56              securityManager = MuleManager.getInstance().getSecurityManager();
57          }
58          if (securityManager == null)
59          {
60              throw new InitialisationException(CoreMessages.authSecurityManagerNotSet(), this);
61          }
62          if (endpoint == null)
63          {
64              throw new InitialisationException(CoreMessages.objectIsNull("Endpoint"), this);
65          }
66          // This filter may only allow authentication on a subset of registered
67          // security providers
68          if (securityProviders != null)
69          {
70              UMOSecurityManager localManager = new MuleSecurityManager();
71              String[] sp = StringUtils.splitAndTrim(securityProviders, ",");
72              for (int i = 0; i < sp.length; i++)
73              {
74                  UMOSecurityProvider provider = securityManager.getProvider(sp[i]);
75                  if (provider != null)
76                  {
77                      localManager.addProvider(provider);
78                  }
79                  else
80                  {
81                      throw new InitialisationException(
82                          CoreMessages.objectNotRegisteredWithManager(
83                              "Security Provider '" + sp[i] + "'"), this);
84                  }
85              }
86              securityManager = localManager;
87          }
88          if (endpoint.getType().equals(UMOEndpoint.ENDPOINT_TYPE_RECEIVER))
89          {
90              inbound = true;
91          }
92          else if (endpoint.getType().equals(UMOEndpoint.ENDPOINT_TYPE_SENDER))
93          {
94              inbound = false;
95          }
96          else
97          {
98              throw new InitialisationException(
99                  CoreMessages.authEndpointTypeForFilterMustBe(
100                     UMOEndpoint.ENDPOINT_TYPE_SENDER + " or " + UMOEndpoint.ENDPOINT_TYPE_RECEIVER,
101                     endpoint.getType()), this);
102         }
103         doInitialise();
104     }
105 
106     public boolean isAuthenticate()
107     {
108         return authenticate;
109     }
110 
111     public void setAuthenticate(boolean authenticate)
112     {
113         this.authenticate = authenticate;
114     }
115 
116     /**
117      * @param manager
118      */
119     public void setSecurityManager(UMOSecurityManager manager)
120     {
121         securityManager = manager;
122     }
123 
124     public UMOSecurityManager getSecurityManager()
125     {
126         return securityManager;
127     }
128 
129     public String getSecurityProviders()
130     {
131         return securityProviders;
132     }
133 
134     public void setSecurityProviders(String providers)
135     {
136         securityProviders = providers;
137     }
138 
139     public UMOImmutableEndpoint getEndpoint()
140     {
141         return endpoint;
142     }
143 
144     public void setEndpoint(UMOImmutableEndpoint endpoint)
145     {
146         this.endpoint = endpoint;
147     }
148 
149     public void authenticate(UMOEvent event)
150         throws SecurityException, UnknownAuthenticationTypeException, CryptoFailureException,
151         SecurityProviderNotFoundException, EncryptionStrategyNotFoundException
152     {
153         if (inbound)
154         {
155             authenticateInbound(event);
156         }
157         else
158         {
159             authenticateOutbound(event);
160         }
161     }
162 
163     public UMOCredentialsAccessor getCredentialsAccessor()
164     {
165         return credentialsAccessor;
166     }
167 
168     public void setCredentialsAccessor(UMOCredentialsAccessor credentialsAccessor)
169     {
170         this.credentialsAccessor = credentialsAccessor;
171     }
172 
173     protected abstract void authenticateInbound(UMOEvent event)
174         throws SecurityException, CryptoFailureException, SecurityProviderNotFoundException,
175         EncryptionStrategyNotFoundException, UnknownAuthenticationTypeException;
176 
177     protected abstract void authenticateOutbound(UMOEvent event)
178         throws SecurityException, SecurityProviderNotFoundException, CryptoFailureException;
179 
180     protected abstract void doInitialise() throws InitialisationException;
181 
182 }