1
2
3
4
5
6
7 package org.mule.transformer.encryption;
8
9 import org.mule.api.EncryptionStrategy;
10 import org.mule.api.lifecycle.InitialisationException;
11 import org.mule.api.security.CryptoFailureException;
12 import org.mule.api.transformer.TransformerException;
13 import org.mule.config.i18n.CoreMessages;
14 import org.mule.transformer.AbstractTransformer;
15 import org.mule.transformer.types.DataTypeFactory;
16 import org.mule.util.IOUtils;
17
18 import java.io.ByteArrayInputStream;
19 import java.io.InputStream;
20
21
22
23
24
25
26 public abstract class AbstractEncryptionTransformer extends AbstractTransformer
27 {
28 private EncryptionStrategy strategy = null;
29 private String strategyName = null;
30
31 public AbstractEncryptionTransformer()
32 {
33 registerSourceType(DataTypeFactory.BYTE_ARRAY);
34 registerSourceType(DataTypeFactory.STRING);
35 registerSourceType(DataTypeFactory.INPUT_STREAM);
36 setReturnDataType(DataTypeFactory.INPUT_STREAM);
37 }
38
39 @Override
40 public Object clone() throws CloneNotSupportedException
41 {
42 AbstractEncryptionTransformer clone = (AbstractEncryptionTransformer) super.clone();
43
44
45
46
47
48 clone.setStrategy(strategy);
49 clone.setStrategyName(strategyName);
50 return clone;
51 }
52
53 @Override
54 public Object doTransform(Object src, String outputEncoding) throws TransformerException
55 {
56 InputStream input;
57 if (src instanceof String)
58 {
59 input = new ByteArrayInputStream(src.toString().getBytes());
60 }
61 else if (src instanceof InputStream)
62 {
63 input = (InputStream) src;
64 }
65 else
66 {
67 input = new ByteArrayInputStream((byte[]) src);
68 }
69 try
70 {
71 return this.primTransform(input);
72 }
73 catch (CryptoFailureException e)
74 {
75 throw new TransformerException(this, e);
76 }
77 }
78
79 protected abstract InputStream primTransform(InputStream stream) throws CryptoFailureException;
80
81
82
83
84
85
86
87 @Override
88 public void initialise() throws InitialisationException
89 {
90 if (strategyName != null)
91 {
92 if (endpoint.getMuleContext().getSecurityManager() == null)
93 {
94 if (strategy == null)
95 {
96 throw new InitialisationException(CoreMessages.authSecurityManagerNotSet(), this);
97 }
98 }
99 else
100 {
101 strategy = endpoint.getMuleContext().getSecurityManager().getEncryptionStrategy(strategyName);
102 }
103 }
104 if (strategy == null)
105 {
106 throw new InitialisationException(CoreMessages.encryptionStrategyNotSet(), this);
107 }
108 }
109
110 public EncryptionStrategy getStrategy()
111 {
112 return strategy;
113 }
114
115 public void setStrategy(EncryptionStrategy strategy)
116 {
117 this.strategy = strategy;
118 }
119
120 public String getStrategyName()
121 {
122 return strategyName;
123 }
124
125 public void setStrategyName(String strategyName)
126 {
127 this.strategyName = strategyName;
128 }
129
130 }