1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transformer.encryption; |
12 | |
|
13 | |
import org.mule.api.EncryptionStrategy; |
14 | |
import org.mule.api.lifecycle.InitialisationException; |
15 | |
import org.mule.api.security.CryptoFailureException; |
16 | |
import org.mule.api.transformer.TransformerException; |
17 | |
import org.mule.config.i18n.CoreMessages; |
18 | |
import org.mule.transformer.AbstractTransformer; |
19 | |
import org.mule.util.IOUtils; |
20 | |
|
21 | |
import java.io.InputStream; |
22 | |
import java.io.UnsupportedEncodingException; |
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
public abstract class AbstractEncryptionTransformer extends AbstractTransformer |
30 | |
{ |
31 | 16 | private EncryptionStrategy strategy = null; |
32 | 16 | private String strategyName = null; |
33 | |
|
34 | |
public AbstractEncryptionTransformer() |
35 | 16 | { |
36 | 16 | registerSourceType(byte[].class); |
37 | 16 | registerSourceType(String.class); |
38 | 16 | registerSourceType(InputStream.class); |
39 | 16 | setReturnClass(byte[].class); |
40 | 16 | } |
41 | |
|
42 | |
|
43 | |
public Object clone() throws CloneNotSupportedException |
44 | |
{ |
45 | 0 | AbstractEncryptionTransformer clone = (AbstractEncryptionTransformer) super.clone(); |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | 0 | clone.setStrategy(strategy); |
52 | 0 | clone.setStrategyName(strategyName); |
53 | 0 | return clone; |
54 | |
} |
55 | |
|
56 | |
public Object doTransform(Object src, String encoding) throws TransformerException |
57 | |
{ |
58 | |
byte[] buf; |
59 | 12 | if (src instanceof String) |
60 | |
{ |
61 | 6 | buf = src.toString().getBytes(); |
62 | |
} |
63 | 6 | else if (src instanceof InputStream) |
64 | |
{ |
65 | 0 | InputStream input = (InputStream) src; |
66 | |
try |
67 | |
{ |
68 | 0 | buf = IOUtils.toByteArray(input); |
69 | |
} |
70 | |
finally |
71 | |
{ |
72 | 0 | IOUtils.closeQuietly(input); |
73 | 0 | } |
74 | 0 | } |
75 | |
else |
76 | |
{ |
77 | 6 | buf = (byte[]) src; |
78 | |
} |
79 | |
try |
80 | |
{ |
81 | 12 | byte[] result = getTransformedBytes(buf); |
82 | 12 | if (getReturnClass().equals(String.class)) |
83 | |
{ |
84 | 4 | if (encoding != null) |
85 | |
{ |
86 | |
try |
87 | |
{ |
88 | 4 | return new String(result, encoding); |
89 | |
} |
90 | 0 | catch (UnsupportedEncodingException ex) |
91 | |
{ |
92 | 0 | return new String(result); |
93 | |
} |
94 | |
} |
95 | |
else |
96 | |
{ |
97 | 0 | return new String(result); |
98 | |
} |
99 | |
} |
100 | |
else |
101 | |
{ |
102 | 8 | return result; |
103 | |
} |
104 | |
} |
105 | 0 | catch (CryptoFailureException e) |
106 | |
{ |
107 | 0 | throw new TransformerException(this, e); |
108 | |
} |
109 | |
} |
110 | |
|
111 | |
protected abstract byte[] getTransformedBytes(byte[] buffer) throws CryptoFailureException; |
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
public void initialise() throws InitialisationException |
120 | |
{ |
121 | 16 | if (strategyName != null) |
122 | |
{ |
123 | 0 | if (endpoint.getMuleContext().getSecurityManager() == null) |
124 | |
{ |
125 | 0 | if (strategy == null) |
126 | |
{ |
127 | 0 | throw new InitialisationException(CoreMessages.authSecurityManagerNotSet(), this); |
128 | |
} |
129 | |
} |
130 | |
else |
131 | |
{ |
132 | 0 | strategy = endpoint.getMuleContext().getSecurityManager().getEncryptionStrategy(strategyName); |
133 | |
} |
134 | |
} |
135 | 16 | if (strategy == null) |
136 | |
{ |
137 | 0 | throw new InitialisationException(CoreMessages.encryptionStrategyNotSet(), this); |
138 | |
} |
139 | 16 | } |
140 | |
|
141 | |
public EncryptionStrategy getStrategy() |
142 | |
{ |
143 | 12 | return strategy; |
144 | |
} |
145 | |
|
146 | |
public void setStrategy(EncryptionStrategy strategy) |
147 | |
{ |
148 | 16 | this.strategy = strategy; |
149 | 16 | } |
150 | |
|
151 | |
public String getStrategyName() |
152 | |
{ |
153 | 0 | return strategyName; |
154 | |
} |
155 | |
|
156 | |
public void setStrategyName(String strategyName) |
157 | |
{ |
158 | 0 | this.strategyName = strategyName; |
159 | 0 | } |
160 | |
|
161 | |
} |