Coverage Report - org.mule.transformers.encryption.AbstractEncryptionTransformer
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractEncryptionTransformer
74%
28/38
59%
13/22
2.778
 
 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  22
     private UMOEncryptionStrategy strategy = null;
 30  22
     private String strategyName = null;
 31  
 
 32  
     public AbstractEncryptionTransformer()
 33  22
     {
 34  26
         registerSourceType(byte[].class);
 35  22
         registerSourceType(String.class);
 36  22
         setReturnClass(byte[].class);
 37  22
     }
 38  
 
 39  
     // @Override
 40  
     public Object clone() throws CloneNotSupportedException
 41  
     {
 42  2
         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  2
         clone.setStrategy(strategy);
 49  2
         clone.setStrategyName(strategyName);
 50  2
         return clone;
 51  
     }
 52  
 
 53  
     public Object doTransform(Object src, String encoding) throws TransformerException
 54  
     {
 55  
         byte[] buf;
 56  12
         if (src instanceof String)
 57  
         {
 58  6
             buf = src.toString().getBytes();
 59  
         }
 60  
         else
 61  
         {
 62  6
             buf = (byte[]) src;
 63  
         }
 64  
         try
 65  
         {
 66  12
             byte[] result = getTransformedBytes(buf);
 67  12
             if (getReturnClass().equals(String.class))
 68  
             {
 69  4
                 if (encoding != null)
 70  
                 {
 71  
                     try
 72  
                     {
 73  4
                         return new String(result, encoding);
 74  
                     }
 75  0
                     catch (UnsupportedEncodingException ex)
 76  
                     {
 77  0
                         return new String(result);
 78  
                     }
 79  
                 }
 80  
                 else
 81  
                 {
 82  0
                     return new String(result);
 83  
                 }
 84  
             }
 85  
             else
 86  
             {
 87  8
                 return result;
 88  
             }
 89  
         }
 90  0
         catch (CryptoFailureException e)
 91  
         {
 92  0
             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  20
         if (strategyName != null)
 107  
         {
 108  0
             if (MuleManager.getInstance().getSecurityManager() == null)
 109  
             {
 110  0
                 if (strategy == null)
 111  
                 {
 112  0
                     throw new InitialisationException(CoreMessages.authSecurityManagerNotSet(), this);
 113  
                 }
 114  
             }
 115  
             else
 116  
             {
 117  0
                 strategy = MuleManager.getInstance().getSecurityManager().getEncryptionStrategy(strategyName);
 118  
             }
 119  
         }
 120  20
         if (strategy == null)
 121  
         {
 122  0
             throw new InitialisationException(CoreMessages.encryptionStrategyNotSet(), this);
 123  
         }
 124  20
     }
 125  
 
 126  
     public UMOEncryptionStrategy getStrategy()
 127  
     {
 128  18
         return strategy;
 129  
     }
 130  
 
 131  
     public void setStrategy(UMOEncryptionStrategy strategy)
 132  
     {
 133  24
         this.strategy = strategy;
 134  24
     }
 135  
 
 136  
     public String getStrategyName()
 137  
     {
 138  6
         return strategyName;
 139  
     }
 140  
 
 141  
     public void setStrategyName(String strategyName)
 142  
     {
 143  4
         this.strategyName = strategyName;
 144  4
     }
 145  
 
 146  
 }