View Javadoc

1   /*
2    * $Id: AbstractEncryptionTransformer.java 7963 2007-08-21 08:53:15Z 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.transformers.encryption;
12  
13  import org.mule.MuleManager;
14  import org.mule.config.i18n.CoreMessages;
15  import org.mule.transformers.AbstractTransformer;
16  import org.mule.umo.UMOEncryptionStrategy;
17  import org.mule.umo.lifecycle.InitialisationException;
18  import org.mule.umo.security.CryptoFailureException;
19  import org.mule.umo.transformer.TransformerException;
20  
21  import java.io.UnsupportedEncodingException;
22  
23  /**
24   * <code>EncryptionTransformer</code> will transform an array of bytes or string
25   * into an encrypted array of bytes
26   */
27  public abstract class AbstractEncryptionTransformer extends AbstractTransformer
28  {
29      private UMOEncryptionStrategy strategy = null;
30      private String strategyName = null;
31  
32      public AbstractEncryptionTransformer()
33      {
34          registerSourceType(byte[].class);
35          registerSourceType(String.class);
36          setReturnClass(byte[].class);
37      }
38  
39      // @Override
40      public Object clone() throws CloneNotSupportedException
41      {
42          AbstractEncryptionTransformer clone = (AbstractEncryptionTransformer) super.clone();
43          /*
44           * The actual strategy is *shared* - not sure if this is right? both shallow
45           * and deep copy make sense - think about security, passwords, required
46           * external authentication dependencies etc. :(
47           */
48          clone.setStrategy(strategy);
49          clone.setStrategyName(strategyName);
50          return clone;
51      }
52  
53      public Object doTransform(Object src, String encoding) throws TransformerException
54      {
55          byte[] buf;
56          if (src instanceof String)
57          {
58              buf = src.toString().getBytes();
59          }
60          else
61          {
62              buf = (byte[]) src;
63          }
64          try
65          {
66              byte[] result = getTransformedBytes(buf);
67              if (getReturnClass().equals(String.class))
68              {
69                  if (encoding != null)
70                  {
71                      try
72                      {
73                          return new String(result, encoding);
74                      }
75                      catch (UnsupportedEncodingException ex)
76                      {
77                          return new String(result);
78                      }
79                  }
80                  else
81                  {
82                      return new String(result);
83                  }
84              }
85              else
86              {
87                  return result;
88              }
89          }
90          catch (CryptoFailureException e)
91          {
92              throw new TransformerException(this, e);
93          }
94      }
95  
96      protected abstract byte[] getTransformedBytes(byte[] buffer) throws CryptoFailureException;
97  
98      /**
99       * Template method were deriving classes can do any initialisation after the
100      * properties have been set on this transformer
101      * 
102      * @throws org.mule.umo.lifecycle.InitialisationException
103      */
104     public void initialise() throws InitialisationException
105     {
106         if (strategyName != null)
107         {
108             if (MuleManager.getInstance().getSecurityManager() == null)
109             {
110                 if (strategy == null)
111                 {
112                     throw new InitialisationException(CoreMessages.authSecurityManagerNotSet(), this);
113                 }
114             }
115             else
116             {
117                 strategy = MuleManager.getInstance().getSecurityManager().getEncryptionStrategy(strategyName);
118             }
119         }
120         if (strategy == null)
121         {
122             throw new InitialisationException(CoreMessages.encryptionStrategyNotSet(), this);
123         }
124     }
125 
126     public UMOEncryptionStrategy getStrategy()
127     {
128         return strategy;
129     }
130 
131     public void setStrategy(UMOEncryptionStrategy strategy)
132     {
133         this.strategy = strategy;
134     }
135 
136     public String getStrategyName()
137     {
138         return strategyName;
139     }
140 
141     public void setStrategyName(String strategyName)
142     {
143         this.strategyName = strategyName;
144     }
145 
146 }