View Javadoc

1   /*
2    * $Id: AbstractNamedEncryptionStrategy.java 20310 2010-11-24 10:40:35Z esteban.robles $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.security;
12  
13  import org.mule.api.EncryptionStrategy;
14  import org.mule.api.security.CryptoFailureException;
15  
16  import java.io.ByteArrayInputStream;
17  import java.io.IOException;
18  import java.io.InputStream;
19  import java.io.OutputStream;
20  
21  import org.apache.commons.io.IOUtils;
22  
23  public abstract class AbstractNamedEncryptionStrategy implements EncryptionStrategy
24  {
25  
26      private String name;
27  
28      public String getName()
29      {
30          return name;
31      }
32  
33      public void setName(String name)
34      {
35          this.name = name;
36      }
37      
38      public byte[] encrypt(byte[] data, Object info) throws CryptoFailureException {
39          InputStream io = this.encrypt(new ByteArrayInputStream(data), info);
40          try
41          {
42              return IOUtils.toByteArray(io);
43          }
44          catch (IOException e)
45          {
46              throw new CryptoFailureException(this, e);
47          }
48      }
49  
50      public byte[] decrypt(byte[] data, Object info) throws CryptoFailureException {
51          InputStream io = this.decrypt(new ByteArrayInputStream(data), info);
52          try
53          {
54              return IOUtils.toByteArray(io);
55          }
56          catch (IOException e)
57          {
58              throw new CryptoFailureException(this, e);
59          }
60      }
61  }