View Javadoc

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