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