Coverage Report - org.mule.transformer.encryption.AbstractEncryptionTransformer
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractEncryptionTransformer
51%
23/45
50%
8/16
2.889
 
 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  16
     private EncryptionStrategy strategy = null;
 32  16
     private String strategyName = null;
 33  
 
 34  
     public AbstractEncryptionTransformer()
 35  16
     {
 36  16
         registerSourceType(byte[].class);
 37  16
         registerSourceType(String.class);
 38  16
         registerSourceType(InputStream.class);
 39  16
         setReturnClass(byte[].class);
 40  16
     }
 41  
 
 42  
     // @Override
 43  
     public Object clone() throws CloneNotSupportedException
 44  
     {
 45  0
         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  0
         clone.setStrategy(strategy);
 52  0
         clone.setStrategyName(strategyName);
 53  0
         return clone;
 54  
     }
 55  
 
 56  
     public Object doTransform(Object src, String encoding) throws TransformerException
 57  
     {
 58  
         byte[] buf;
 59  12
         if (src instanceof String)
 60  
         {
 61  6
             buf = src.toString().getBytes();
 62  
         }
 63  6
         else if (src instanceof InputStream)
 64  
         {
 65  0
             InputStream input = (InputStream) src;
 66  
             try
 67  
             {
 68  0
                 buf = IOUtils.toByteArray(input);
 69  
             }
 70  
             finally
 71  
             {
 72  0
                 IOUtils.closeQuietly(input);
 73  0
             }
 74  0
         }
 75  
         else
 76  
         {
 77  6
             buf = (byte[]) src;
 78  
         }
 79  
         try
 80  
         {
 81  12
             byte[] result = getTransformedBytes(buf);
 82  12
             if (getReturnClass().equals(String.class))
 83  
             {
 84  4
                 if (encoding != null)
 85  
                 {
 86  
                     try
 87  
                     {
 88  4
                         return new String(result, encoding);
 89  
                     }
 90  0
                     catch (UnsupportedEncodingException ex)
 91  
                     {
 92  0
                         return new String(result);
 93  
                     }
 94  
                 }
 95  
                 else
 96  
                 {
 97  0
                     return new String(result);
 98  
                 }
 99  
             }
 100  
             else
 101  
             {
 102  8
                 return result;
 103  
             }
 104  
         }
 105  0
         catch (CryptoFailureException e)
 106  
         {
 107  0
             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  16
         if (strategyName != null)
 122  
         {
 123  0
             if (endpoint.getMuleContext().getSecurityManager() == null)
 124  
             {
 125  0
                 if (strategy == null)
 126  
                 {
 127  0
                     throw new InitialisationException(CoreMessages.authSecurityManagerNotSet(), this);
 128  
                 }
 129  
             }
 130  
             else
 131  
             {
 132  0
                 strategy = endpoint.getMuleContext().getSecurityManager().getEncryptionStrategy(strategyName);
 133  
             }
 134  
         }
 135  16
         if (strategy == null)
 136  
         {
 137  0
             throw new InitialisationException(CoreMessages.encryptionStrategyNotSet(), this);
 138  
         }
 139  16
     }
 140  
 
 141  
     public EncryptionStrategy getStrategy()
 142  
     {
 143  12
         return strategy;
 144  
     }
 145  
 
 146  
     public void setStrategy(EncryptionStrategy strategy)
 147  
     {
 148  16
         this.strategy = strategy;
 149  16
     }
 150  
 
 151  
     public String getStrategyName()
 152  
     {
 153  0
         return strategyName;
 154  
     }
 155  
 
 156  
     public void setStrategyName(String strategyName)
 157  
     {
 158  0
         this.strategyName = strategyName;
 159  0
     }
 160  
 
 161  
 }