1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
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 | |
|
32 | |
|
33 | |
|
34 | 0 | public abstract class AbstractSecurityFilter implements MuleContextAware, SecurityFilter |
35 | |
{ |
36 | |
|
37 | 0 | 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 | 0 | this.muleContext = context; |
47 | 0 | } |
48 | |
|
49 | |
public final void initialise() throws InitialisationException |
50 | |
{ |
51 | 0 | if (securityManager == null) |
52 | |
{ |
53 | 0 | securityManager = muleContext.getSecurityManager(); |
54 | |
} |
55 | |
|
56 | 0 | if (securityManager == null) |
57 | |
{ |
58 | 0 | throw new InitialisationException(CoreMessages.authSecurityManagerNotSet(), this); |
59 | |
} |
60 | |
|
61 | |
|
62 | |
|
63 | 0 | if (securityProviders != null) |
64 | |
{ |
65 | 0 | SecurityManager localManager = new MuleSecurityManager(); |
66 | 0 | String[] securityProviders = StringUtils.splitAndTrim(this.securityProviders, ","); |
67 | 0 | for (String sp : securityProviders) |
68 | |
{ |
69 | 0 | SecurityProvider provider = securityManager.getProvider(sp); |
70 | 0 | if (provider != null) |
71 | |
{ |
72 | 0 | localManager.addProvider(provider); |
73 | |
} |
74 | |
else |
75 | |
{ |
76 | 0 | throw new InitialisationException( |
77 | |
CoreMessages.objectNotRegistered( |
78 | |
"Security Provider", sp), this); |
79 | |
} |
80 | |
} |
81 | 0 | securityManager = localManager; |
82 | |
} |
83 | |
|
84 | 0 | doInitialise(); |
85 | 0 | } |
86 | |
|
87 | |
protected void doInitialise() throws InitialisationException |
88 | |
{ |
89 | 0 | } |
90 | |
|
91 | |
|
92 | |
public void setSecurityManager(SecurityManager manager) |
93 | |
{ |
94 | 0 | securityManager = manager; |
95 | 0 | } |
96 | |
|
97 | |
public SecurityManager getSecurityManager() |
98 | |
{ |
99 | 0 | return securityManager; |
100 | |
} |
101 | |
|
102 | |
public String getSecurityProviders() |
103 | |
{ |
104 | 0 | return securityProviders; |
105 | |
} |
106 | |
|
107 | |
public void setSecurityProviders(String providers) |
108 | |
{ |
109 | 0 | securityProviders = providers; |
110 | 0 | } |
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 | 0 | TransformerTemplate trans = new TransformerTemplate(new TransformerTemplate.TransformerCallback() |
120 | 0 | { |
121 | |
public Object doTransform(MuleMessage message) throws Exception |
122 | |
{ |
123 | 0 | return payload; |
124 | |
} |
125 | |
}); |
126 | |
|
127 | 0 | message.applyTransformers(event, trans); |
128 | 0 | } |
129 | |
} |