View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.security;
8   
9   import org.mule.api.MuleContext;
10  import org.mule.api.MuleEvent;
11  import org.mule.api.MuleException;
12  import org.mule.api.MuleMessage;
13  import org.mule.api.context.MuleContextAware;
14  import org.mule.api.lifecycle.InitialisationException;
15  import org.mule.api.security.CryptoFailureException;
16  import org.mule.api.security.EncryptionStrategyNotFoundException;
17  import org.mule.api.security.SecurityException;
18  import org.mule.api.security.SecurityFilter;
19  import org.mule.api.security.SecurityManager;
20  import org.mule.api.security.SecurityProvider;
21  import org.mule.api.security.SecurityProviderNotFoundException;
22  import org.mule.api.security.UnknownAuthenticationTypeException;
23  import org.mule.config.i18n.CoreMessages;
24  import org.mule.transformer.TransformerTemplate;
25  import org.mule.util.StringUtils;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  
30  /**
31   * <code>AbstractSecurityFilter</code> provides basic initialisation for
32   * all security filters, namely configuring the SecurityManager for this instance
33   */
34  public abstract class AbstractSecurityFilter implements MuleContextAware, SecurityFilter
35  {
36  
37      protected transient Log logger = LogFactory.getLog(getClass());
38  
39      protected SecurityManager securityManager;
40      protected MuleContext muleContext;
41  
42      private String securityProviders;
43  
44      public void setMuleContext(MuleContext context)
45      {
46          this.muleContext = context;
47      }
48  
49      public final void initialise() throws InitialisationException
50      {
51          if (securityManager == null)
52          {
53              securityManager = muleContext.getSecurityManager();
54          }
55          
56          if (securityManager == null)
57          {
58              throw new InitialisationException(CoreMessages.authSecurityManagerNotSet(), this);
59          }
60  
61          // This filter may only allow authentication on a subset of registered
62          // security providers
63          if (securityProviders != null)
64          {
65              SecurityManager localManager = new MuleSecurityManager();
66              String[] securityProviders = StringUtils.splitAndTrim(this.securityProviders, ",");
67              for (String sp : securityProviders)
68              {
69                  SecurityProvider provider = securityManager.getProvider(sp);
70                  if (provider != null)
71                  {
72                      localManager.addProvider(provider);
73                  }
74                  else
75                  {
76                      throw new InitialisationException(
77                              CoreMessages.objectNotRegistered(
78                                      "Security Provider", sp), this);
79                  }
80              }
81              securityManager = localManager;
82          }
83          
84          doInitialise();
85      }
86  
87      protected void doInitialise() throws InitialisationException
88      {
89      }
90  
91      /** @param manager  */
92      public void setSecurityManager(SecurityManager manager)
93      {
94          securityManager = manager;
95      }
96  
97      public SecurityManager getSecurityManager()
98      {
99          return securityManager;
100     }
101 
102     public String getSecurityProviders()
103     {
104         return securityProviders;
105     }
106 
107     public void setSecurityProviders(String providers)
108     {
109         securityProviders = providers;
110     }
111 
112     public abstract void doFilter(MuleEvent event)
113             throws SecurityException, UnknownAuthenticationTypeException, CryptoFailureException,
114             SecurityProviderNotFoundException, EncryptionStrategyNotFoundException,
115             InitialisationException;
116     
117     protected void updatePayload(MuleMessage message, final Object payload, MuleEvent event) throws MuleException
118     {
119         TransformerTemplate trans = new TransformerTemplate(new TransformerTemplate.TransformerCallback()
120         {
121             public Object doTransform(MuleMessage message) throws Exception
122             {
123                 return payload;
124             }
125         });
126 
127         message.applyTransformers(event, trans);
128     }
129 }