View Javadoc

1   /*
2    * $Id: AbstractEncryptionTransformer.java 11517 2008-03-31 21:34:19Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.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   * <code>EncryptionTransformer</code> will transform an array of bytes or string
26   * into an encrypted array of bytes
27   *
28   */
29  public abstract class AbstractEncryptionTransformer extends AbstractTransformer
30  {
31      private EncryptionStrategy strategy = null;
32      private String strategyName = null;
33  
34      public AbstractEncryptionTransformer()
35      {
36          registerSourceType(byte[].class);
37          registerSourceType(String.class);
38          registerSourceType(InputStream.class);
39          setReturnClass(byte[].class);
40      }
41  
42      // @Override
43      public Object clone() throws CloneNotSupportedException
44      {
45          AbstractEncryptionTransformer clone = (AbstractEncryptionTransformer) super.clone();
46          /*
47           * The actual strategy is *shared* - not sure if this is right? both shallow
48           * and deep copy make sense - think about security, passwords, required
49           * external authentication dependencies etc. :(
50           */
51          clone.setStrategy(strategy);
52          clone.setStrategyName(strategyName);
53          return clone;
54      }
55  
56      public Object doTransform(Object src, String encoding) throws TransformerException
57      {
58          byte[] buf;
59          if (src instanceof String)
60          {
61              buf = src.toString().getBytes();
62          }
63          else if (src instanceof InputStream)
64          {
65              InputStream input = (InputStream) src;
66              try
67              {
68                  buf = IOUtils.toByteArray(input);
69              }
70              finally
71              {
72                  IOUtils.closeQuietly(input);
73              }
74          }
75          else
76          {
77              buf = (byte[]) src;
78          }
79          try
80          {
81              byte[] result = getTransformedBytes(buf);
82              if (getReturnClass().equals(String.class))
83              {
84                  if (encoding != null)
85                  {
86                      try
87                      {
88                          return new String(result, encoding);
89                      }
90                      catch (UnsupportedEncodingException ex)
91                      {
92                          return new String(result);
93                      }
94                  }
95                  else
96                  {
97                      return new String(result);
98                  }
99              }
100             else
101             {
102                 return result;
103             }
104         }
105         catch (CryptoFailureException e)
106         {
107             throw new TransformerException(this, e);
108         }
109     }
110 
111     protected abstract byte[] getTransformedBytes(byte[] buffer) throws CryptoFailureException;
112 
113     /**
114      * Template method were deriving classes can do any initialisation after the
115      * properties have been set on this transformer
116      * 
117      * @throws org.mule.api.lifecycle.InitialisationException
118      */
119     public void initialise() throws InitialisationException
120     {
121         if (strategyName != null)
122         {
123             if (endpoint.getMuleContext().getSecurityManager() == null)
124             {
125                 if (strategy == null)
126                 {
127                     throw new InitialisationException(CoreMessages.authSecurityManagerNotSet(), this);
128                 }
129             }
130             else
131             {
132                 strategy = endpoint.getMuleContext().getSecurityManager().getEncryptionStrategy(strategyName);
133             }
134         }
135         if (strategy == null)
136         {
137             throw new InitialisationException(CoreMessages.encryptionStrategyNotSet(), this);
138         }
139     }
140 
141     public EncryptionStrategy getStrategy()
142     {
143         return strategy;
144     }
145 
146     public void setStrategy(EncryptionStrategy strategy)
147     {
148         this.strategy = strategy;
149     }
150 
151     public String getStrategyName()
152     {
153         return strategyName;
154     }
155 
156     public void setStrategyName(String strategyName)
157     {
158         this.strategyName = strategyName;
159     }
160 
161 }