Coverage Report - org.mule.transformer.encryption.AbstractEncryptionTransformer
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractEncryptionTransformer
0%
0/45
0%
0/16
0
 
 1  
 /*
 2  
  * $Id: AbstractEncryptionTransformer.java 19250 2010-08-30 16:53:14Z dirk.olmes $
 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.InputStream;
 23  
 import java.io.UnsupportedEncodingException;
 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  0
     private EncryptionStrategy strategy = null;
 33  0
     private String strategyName = null;
 34  
 
 35  
     public AbstractEncryptionTransformer()
 36  0
     {
 37  0
         registerSourceType(DataTypeFactory.BYTE_ARRAY);
 38  0
         registerSourceType(DataTypeFactory.STRING);
 39  0
         registerSourceType(DataTypeFactory.INPUT_STREAM);
 40  0
         setReturnDataType(DataTypeFactory.BYTE_ARRAY);
 41  0
     }
 42  
 
 43  
     @Override
 44  
     public Object clone() throws CloneNotSupportedException
 45  
     {
 46  0
         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  0
         clone.setStrategy(strategy);
 53  0
         clone.setStrategyName(strategyName);
 54  0
         return clone;
 55  
     }
 56  
 
 57  
     @Override
 58  
     public Object doTransform(Object src, String outputEncoding) throws TransformerException
 59  
     {
 60  
         byte[] buf;
 61  0
         if (src instanceof String)
 62  
         {
 63  0
             buf = src.toString().getBytes();
 64  
         }
 65  0
         else if (src instanceof InputStream)
 66  
         {
 67  0
             InputStream input = (InputStream) src;
 68  
             try
 69  
             {
 70  0
                 buf = IOUtils.toByteArray(input);
 71  
             }
 72  
             finally
 73  
             {
 74  0
                 IOUtils.closeQuietly(input);
 75  0
             }
 76  0
         }
 77  
         else
 78  
         {
 79  0
             buf = (byte[]) src;
 80  
         }
 81  
         try
 82  
         {
 83  0
             byte[] result = getTransformedBytes(buf);
 84  0
             if (getReturnDataType().equals(DataTypeFactory.STRING))
 85  
             {
 86  0
                 if (outputEncoding != null)
 87  
                 {
 88  
                     try
 89  
                     {
 90  0
                         return new String(result, outputEncoding);
 91  
                     }
 92  0
                     catch (UnsupportedEncodingException ex)
 93  
                     {
 94  0
                         return new String(result);
 95  
                     }
 96  
                 }
 97  
                 else
 98  
                 {
 99  0
                     return new String(result);
 100  
                 }
 101  
             }
 102  
             else
 103  
             {
 104  0
                 return result;
 105  
             }
 106  
         }
 107  0
         catch (CryptoFailureException e)
 108  
         {
 109  0
             throw new TransformerException(this, e);
 110  
         }
 111  
     }
 112  
 
 113  
     protected abstract byte[] getTransformedBytes(byte[] buffer) throws CryptoFailureException;
 114  
 
 115  
     /**
 116  
      * Template method were deriving classes can do any initialisation after the
 117  
      * properties have been set on this transformer
 118  
      * 
 119  
      * @throws org.mule.api.lifecycle.InitialisationException
 120  
      */
 121  
     @Override
 122  
     public void initialise() throws InitialisationException
 123  
     {
 124  0
         if (strategyName != null)
 125  
         {
 126  0
             if (endpoint.getMuleContext().getSecurityManager() == null)
 127  
             {
 128  0
                 if (strategy == null)
 129  
                 {
 130  0
                     throw new InitialisationException(CoreMessages.authSecurityManagerNotSet(), this);
 131  
                 }
 132  
             }
 133  
             else
 134  
             {
 135  0
                 strategy = endpoint.getMuleContext().getSecurityManager().getEncryptionStrategy(strategyName);
 136  
             }
 137  
         }
 138  0
         if (strategy == null)
 139  
         {
 140  0
             throw new InitialisationException(CoreMessages.encryptionStrategyNotSet(), this);
 141  
         }
 142  0
     }
 143  
 
 144  
     public EncryptionStrategy getStrategy()
 145  
     {
 146  0
         return strategy;
 147  
     }
 148  
 
 149  
     public void setStrategy(EncryptionStrategy strategy)
 150  
     {
 151  0
         this.strategy = strategy;
 152  0
     }
 153  
 
 154  
     public String getStrategyName()
 155  
     {
 156  0
         return strategyName;
 157  
     }
 158  
 
 159  
     public void setStrategyName(String strategyName)
 160  
     {
 161  0
         this.strategyName = strategyName;
 162  0
     }
 163  
 
 164  
 }