Coverage Report - org.mule.transformer.encryption.AbstractEncryptionTransformer
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractEncryptionTransformer
0%
0/34
0%
0/12
0
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.transformer.encryption;
 8  
 
 9  
 import org.mule.api.EncryptionStrategy;
 10  
 import org.mule.api.lifecycle.InitialisationException;
 11  
 import org.mule.api.security.CryptoFailureException;
 12  
 import org.mule.api.transformer.TransformerException;
 13  
 import org.mule.config.i18n.CoreMessages;
 14  
 import org.mule.transformer.AbstractTransformer;
 15  
 import org.mule.transformer.types.DataTypeFactory;
 16  
 import org.mule.util.IOUtils;
 17  
 
 18  
 import java.io.ByteArrayInputStream;
 19  
 import java.io.InputStream;
 20  
 
 21  
 /**
 22  
  * <code>EncryptionTransformer</code> will transform an array of bytes or string
 23  
  * into an encrypted array of bytes
 24  
  *
 25  
  */
 26  
 public abstract class AbstractEncryptionTransformer extends AbstractTransformer
 27  
 {
 28  0
     private EncryptionStrategy strategy = null;
 29  0
     private String strategyName = null;
 30  
 
 31  
     public AbstractEncryptionTransformer()
 32  0
     {
 33  0
         registerSourceType(DataTypeFactory.BYTE_ARRAY);
 34  0
         registerSourceType(DataTypeFactory.STRING);
 35  0
         registerSourceType(DataTypeFactory.INPUT_STREAM);
 36  0
         setReturnDataType(DataTypeFactory.INPUT_STREAM);
 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  
     @Override
 54  
     public Object doTransform(Object src, String outputEncoding) throws TransformerException
 55  
     {
 56  
         InputStream input;
 57  0
         if (src instanceof String)
 58  
         {
 59  0
             input = new ByteArrayInputStream(src.toString().getBytes());
 60  
         }
 61  0
         else if (src instanceof InputStream)
 62  
         {
 63  0
             input = (InputStream) src;
 64  
         }
 65  
         else
 66  
         {
 67  0
             input = new ByteArrayInputStream((byte[]) src);
 68  
         }
 69  
         try
 70  
         {
 71  0
             return this.primTransform(input);
 72  
         }
 73  0
         catch (CryptoFailureException e)
 74  
         {
 75  0
             throw new TransformerException(this, e);
 76  
         }
 77  
     }
 78  
 
 79  
     protected abstract InputStream primTransform(InputStream stream) throws CryptoFailureException;
 80  
 
 81  
     /**
 82  
      * Template method were deriving classes can do any initialisation after the
 83  
      * properties have been set on this transformer
 84  
      * 
 85  
      * @throws org.mule.api.lifecycle.InitialisationException
 86  
      */
 87  
     @Override
 88  
     public void initialise() throws InitialisationException
 89  
     {
 90  0
         if (strategyName != null)
 91  
         {
 92  0
             if (endpoint.getMuleContext().getSecurityManager() == null)
 93  
             {
 94  0
                 if (strategy == null)
 95  
                 {
 96  0
                     throw new InitialisationException(CoreMessages.authSecurityManagerNotSet(), this);
 97  
                 }
 98  
             }
 99  
             else
 100  
             {
 101  0
                 strategy = endpoint.getMuleContext().getSecurityManager().getEncryptionStrategy(strategyName);
 102  
             }
 103  
         }
 104  0
         if (strategy == null)
 105  
         {
 106  0
             throw new InitialisationException(CoreMessages.encryptionStrategyNotSet(), this);
 107  
         }
 108  0
     }
 109  
 
 110  
     public EncryptionStrategy getStrategy()
 111  
     {
 112  0
         return strategy;
 113  
     }
 114  
 
 115  
     public void setStrategy(EncryptionStrategy strategy)
 116  
     {
 117  0
         this.strategy = strategy;
 118  0
     }
 119  
 
 120  
     public String getStrategyName()
 121  
     {
 122  0
         return strategyName;
 123  
     }
 124  
 
 125  
     public void setStrategyName(String strategyName)
 126  
     {
 127  0
         this.strategyName = strategyName;
 128  0
     }
 129  
 
 130  
 }