Coverage Report - org.mule.transformers.encryption.AbstractEncryptionTransformer
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractEncryptionTransformer
0%
0/38
0%
0/10
2.778
 
 1  
 /*
 2  
  * $Id: AbstractEncryptionTransformer.java 7976 2007-08-21 14:26:13Z 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  0
     private UMOEncryptionStrategy strategy = null;
 30  0
     private String strategyName = null;
 31  
 
 32  
     public AbstractEncryptionTransformer()
 33  0
     {
 34  0
         registerSourceType(byte[].class);
 35  0
         registerSourceType(String.class);
 36  0
         setReturnClass(byte[].class);
 37  0
     }
 38  
 
 39  
     // @Override
 40  
     public Object clone() throws CloneNotSupportedException
 41  
     {
 42  0
         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  0
         clone.setStrategy(strategy);
 49  0
         clone.setStrategyName(strategyName);
 50  0
         return clone;
 51  
     }
 52  
 
 53  
     public Object doTransform(Object src, String encoding) throws TransformerException
 54  
     {
 55  
         byte[] buf;
 56  0
         if (src instanceof String)
 57  
         {
 58  0
             buf = src.toString().getBytes();
 59  
         }
 60  
         else
 61  
         {
 62  0
             buf = (byte[]) src;
 63  
         }
 64  
         try
 65  
         {
 66  0
             byte[] result = getTransformedBytes(buf);
 67  0
             if (getReturnClass().equals(String.class))
 68  
             {
 69  0
                 if (encoding != null)
 70  
                 {
 71  
                     try
 72  
                     {
 73  0
                         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  0
                 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  0
         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  0
         if (strategy == null)
 121  
         {
 122  0
             throw new InitialisationException(CoreMessages.encryptionStrategyNotSet(), this);
 123  
         }
 124  0
     }
 125  
 
 126  
     public UMOEncryptionStrategy getStrategy()
 127  
     {
 128  0
         return strategy;
 129  
     }
 130  
 
 131  
     public void setStrategy(UMOEncryptionStrategy strategy)
 132  
     {
 133  0
         this.strategy = strategy;
 134  0
     }
 135  
 
 136  
     public String getStrategyName()
 137  
     {
 138  0
         return strategyName;
 139  
     }
 140  
 
 141  
     public void setStrategyName(String strategyName)
 142  
     {
 143  0
         this.strategyName = strategyName;
 144  0
     }
 145  
 
 146  
 }