1 10 11 package org.mule.transformers.encryption; 12 13 import org.mule.MuleManager; 14 import org.mule.config.i18n.Message; 15 import org.mule.config.i18n.Messages; 16 import org.mule.transformers.AbstractTransformer; 17 import org.mule.umo.UMOEncryptionStrategy; 18 import org.mule.umo.lifecycle.InitialisationException; 19 import org.mule.umo.security.CryptoFailureException; 20 import org.mule.umo.transformer.TransformerException; 21 22 import java.io.UnsupportedEncodingException ; 23 24 31 public abstract class AbstractEncryptionTransformer extends AbstractTransformer 32 { 33 private UMOEncryptionStrategy strategy = null; 34 private String strategyName = null; 35 36 public AbstractEncryptionTransformer() 37 { 38 registerSourceType(byte[].class); 39 registerSourceType(String .class); 40 setReturnClass(byte[].class); 41 } 42 43 public Object doTransform(Object src, String encoding) throws TransformerException 44 { 45 byte[] buf; 46 if (src instanceof String ) 47 { 48 buf = src.toString().getBytes(); 49 } 50 else 51 { 52 buf = (byte[])src; 53 } 54 try 55 { 56 byte[] result = getTransformedBytes(buf); 57 if (getReturnClass().equals(String .class)) 58 { 59 if (encoding != null) 60 { 61 try 62 { 63 return new String (result, encoding); 64 } 65 catch (UnsupportedEncodingException ex) 66 { 67 return new String (result); 68 } 69 } 70 else 71 { 72 return new String (result); 73 } 74 } 75 else 76 { 77 return result; 78 } 79 } 80 catch (CryptoFailureException e) 81 { 82 throw new TransformerException(this, e); 83 } 84 } 85 86 protected abstract byte[] getTransformedBytes(byte[] buffer) throws CryptoFailureException; 87 88 94 public void initialise() throws InitialisationException 95 { 96 if (strategyName != null) 97 { 98 if (MuleManager.getInstance().getSecurityManager() == null) 99 { 100 if (strategy == null) 101 { 102 throw new InitialisationException(new Message(Messages.AUTH_SECURITY_MANAGER_NOT_SET), 103 this); 104 105 } 106 } 107 else 108 { 109 strategy = MuleManager.getInstance().getSecurityManager().getEncryptionStrategy(strategyName); 110 } 111 } 112 if (strategy == null) 113 { 114 throw new InitialisationException(new Message(Messages.ENCRYPT_STRATEGY_NOT_SET), this); 115 } 116 } 117 118 public UMOEncryptionStrategy getStrategy() 119 { 120 return strategy; 121 } 122 123 public void setStrategy(UMOEncryptionStrategy strategy) 124 { 125 this.strategy = strategy; 126 } 127 128 public String getStrategyName() 129 { 130 return strategyName; 131 } 132 133 public void setStrategyName(String strategyName) 134 { 135 this.strategyName = strategyName; 136 } 137 } 138 | Popular Tags |