View Javadoc

1   /*
2    * $Id: AbstractEncryptionTransformer.java 20310 2010-11-24 10:40:35Z esteban.robles $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.transformer.types.DataTypeFactory;
20  import org.mule.util.IOUtils;
21  
22  import java.io.ByteArrayInputStream;
23  import java.io.InputStream;
24  
25  /**
26   * <code>EncryptionTransformer</code> will transform an array of bytes or string
27   * into an encrypted array of bytes
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.INPUT_STREAM);
41      }
42  
43      @Override
44      public Object clone() throws CloneNotSupportedException
45      {
46          AbstractEncryptionTransformer clone = (AbstractEncryptionTransformer) super.clone();
47          /*
48           * The actual strategy is *shared* - not sure if this is right? both shallow
49           * and deep copy make sense - think about security, passwords, required
50           * external authentication dependencies etc. :(
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          InputStream input;
61          if (src instanceof String)
62          {
63              input = new ByteArrayInputStream(src.toString().getBytes());
64          }
65          else if (src instanceof InputStream)
66          {
67              input = (InputStream) src;
68          }
69          else
70          {
71              input = new ByteArrayInputStream((byte[]) src);
72          }
73          try
74          {
75              return this.primTransform(input);
76          }
77          catch (CryptoFailureException e)
78          {
79              throw new TransformerException(this, e);
80          }
81      }
82  
83      protected abstract InputStream primTransform(InputStream stream) throws CryptoFailureException;
84  
85      /**
86       * Template method were deriving classes can do any initialisation after the
87       * properties have been set on this transformer
88       * 
89       * @throws org.mule.api.lifecycle.InitialisationException
90       */
91      @Override
92      public void initialise() throws InitialisationException
93      {
94          if (strategyName != null)
95          {
96              if (endpoint.getMuleContext().getSecurityManager() == null)
97              {
98                  if (strategy == null)
99                  {
100                     throw new InitialisationException(CoreMessages.authSecurityManagerNotSet(), this);
101                 }
102             }
103             else
104             {
105                 strategy = endpoint.getMuleContext().getSecurityManager().getEncryptionStrategy(strategyName);
106             }
107         }
108         if (strategy == null)
109         {
110             throw new InitialisationException(CoreMessages.encryptionStrategyNotSet(), this);
111         }
112     }
113 
114     public EncryptionStrategy getStrategy()
115     {
116         return strategy;
117     }
118 
119     public void setStrategy(EncryptionStrategy strategy)
120     {
121         this.strategy = strategy;
122     }
123 
124     public String getStrategyName()
125     {
126         return strategyName;
127     }
128 
129     public void setStrategyName(String strategyName)
130     {
131         this.strategyName = strategyName;
132     }
133 
134 }