~ubuntu-branches/ubuntu/oneiric/libjboss-remoting-java/oneiric

« back to all changes in this revision

Viewing changes to src/main/org/jboss/remoting/marshal/encryption/EncryptingUnMarshaller.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: package-import@ubuntu.com-20110909140103-hqokx61534tas9rg
Tags: 2.5.3.SP1-1
* Newer but not newest upstream release. Do not build samples.
* Change debian/watch to upstream's svn repo.
* Add patch to fix compile error caused by tomcat update.
  (Closes: #628303)
* Switch to source format 3.0.
* Switch to debhelper level 7.
* Remove useless Depends.
* Update Standards-Version: 3.9.2.
* Update README.source.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
* JBoss, Home of Professional Open Source
 
3
* Copyright 2005, JBoss Inc., and individual contributors as indicated
 
4
* by the @authors tag. See the copyright.txt in the distribution for a
 
5
* full listing of individual contributors.
 
6
*
 
7
* This is free software; you can redistribute it and/or modify it
 
8
* under the terms of the GNU Lesser General Public License as
 
9
* published by the Free Software Foundation; either version 2.1 of
 
10
* the License, or (at your option) any later version.
 
11
*
 
12
* This software is distributed in the hope that it will be useful,
 
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
15
* Lesser General Public License for more details.
 
16
*
 
17
* You should have received a copy of the GNU Lesser General Public
 
18
* License along with this software; if not, write to the Free
 
19
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 
20
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 
21
*/
 
22
 
 
23
package org.jboss.remoting.marshal.encryption;
 
24
 
 
25
import java.io.IOException;
 
26
import java.io.InputStream;
 
27
import java.io.ObjectInputStream; 
 
28
import java.util.Map;
 
29
 
 
30
import javax.crypto.Cipher;
 
31
import javax.crypto.CipherInputStream;
 
32
 
 
33
import org.jboss.remoting.marshal.UnMarshaller;
 
34
import org.jboss.remoting.marshal.VersionedUnMarshaller;
 
35
import org.jboss.remoting.marshal.serializable.SerializableUnMarshaller;
 
36
import org.jboss.remoting.serialization.SerializationManager;
 
37
import org.jboss.remoting.serialization.SerializationStreamFactory;
 
38
 
 
39
 
 
40
/**
 
41
 * <code>EncryptingMarshaller</code> and <code>EncryptingUnMarshaller</code> are a general
 
42
 * purpose encrypting marshaller / decompressing unmarshaller pair based on 
 
43
 * Java's crypto stream facilities.
 
44
 * <p/>
 
45
 * <code>EncryptingUnMarshaller</code> is subclassed from <code>SerializableUnMarshaller</code>,
 
46
 * and by default it uses <code>super.read()</code> to deserialize an object, once the object has been
 
47
 * decrypted.  Optionally, it can wrap any other unmarshaller and use that instead of
 
48
 * <code>SerializableUnMarshaller</code> to unmarshall an encrypted input stream.  For example,
 
49
 * <p/>
 
50
 * <center><code>new EncryptingUnMarshaller(new HTTPUnMarshaller())</code></center
 
51
 * <p/>
 
52
 * will create an umarshaller that
 
53
 * uses an <code>HTTPUnMarshaller</code> to restore an unencrypted input stream.
 
54
 *
 
55
 * @author Anil.Saldhana@jboss.org
 
56
 * @version $Revision: 3584 $ 
 
57
 */
 
58
 
 
59
public class EncryptingUnMarshaller extends SerializableUnMarshaller
 
60
{
 
61
   /** The serialVersionUID */
 
62
   private static final long serialVersionUID = 1L;
 
63
 
 
64
   public final static String DATATYPE = "encrypt";
 
65
 
 
66
   private UnMarshaller wrappedUnMarshaller; 
 
67
   
 
68
   private String cipherAlgorithm = EncryptionManager.DEFAULT_CIPHER_ALGORITHM;
 
69
   
 
70
   private Cipher cipher = EncryptionManager.getCipher(Cipher.DECRYPT_MODE, cipherAlgorithm);
 
71
   
 
72
 
 
73
   /**
 
74
    * Create a new EncryptingUnMarshaller.
 
75
    */
 
76
   public EncryptingUnMarshaller()
 
77
   {
 
78
   }
 
79
 
 
80
 
 
81
   /**
 
82
    * Create a new EncryptingUnMarshaller.
 
83
    *
 
84
    * @param unMarshaller unmarshaller to be used to restore 
 
85
    * unencrypted byte stream to original object
 
86
    */
 
87
   public EncryptingUnMarshaller(UnMarshaller unMarshaller)
 
88
   {
 
89
      wrappedUnMarshaller = unMarshaller;
 
90
   }
 
91
 
 
92
   /**
 
93
    * Set the Cipher Algorithm to use
 
94
    * @param algo
 
95
    * @see EncryptionManager#DEFAULT_CIPHER_ALGORITHM
 
96
    */
 
97
   public void setCipherAlgorithm(String algo)
 
98
   {
 
99
      this.cipherAlgorithm = algo;
 
100
      cipher = EncryptionManager.getCipher(Cipher.DECRYPT_MODE, this.cipherAlgorithm);
 
101
   }
 
102
 
 
103
   public InputStream getMarshallingStream(InputStream inputStream) throws IOException
 
104
   {
 
105
      return inputStream;
 
106
   }
 
107
   
 
108
   /**
 
109
    * Restores a encrypted, marshalled form of an object to its original state.
 
110
    *
 
111
    * @param inputStream <code>InputStream</code> from which marshalled form is to be retrieved
 
112
    * @param metadata    can be any transport specific metadata (such as headers from http transport).
 
113
    *                    This can be null, depending on if transport supports metadata.
 
114
    * @param version     wire format version
 
115
    * @return restored object
 
116
    * @throws IOException            if there is a problem reading from <code>inputStream</code>
 
117
    * @throws ClassNotFoundException if there is a problem finding a class needed for unmarshalling
 
118
    */
 
119
   public Object read(InputStream inputStream, Map metadata, int version) throws IOException, ClassNotFoundException
 
120
   {  
 
121
      if(cipher == null)
 
122
         throw new IllegalStateException("Cipher is null for algo="+ this.cipherAlgorithm);
 
123
      CipherInputStream cis = new CipherInputStream(inputStream,cipher);
 
124
      SerializationManager sm = SerializationStreamFactory.getManagerInstance(getSerializationType());
 
125
      ObjectInputStream ois = sm.createRegularInput(cis);
 
126
       
 
127
      Object obj = null;
 
128
      if(wrappedUnMarshaller != null)
 
129
      {
 
130
         if (wrappedUnMarshaller instanceof VersionedUnMarshaller)
 
131
            return ((VersionedUnMarshaller)wrappedUnMarshaller).read(ois, metadata, version);
 
132
         else
 
133
            obj = wrappedUnMarshaller.read(ois, metadata); 
 
134
      }
 
135
      else
 
136
      {
 
137
         obj = super.read(ois, metadata, version);
 
138
      } 
 
139
      return obj;
 
140
   } 
 
141
 
 
142
   /**
 
143
    * Returns a new <code>EncryptingUnMarshaller</code>
 
144
    *
 
145
    * @return a new <code>EncryptingUnMarshaller</code>
 
146
    * @throws CloneNotSupportedException In practice no exceptions are thrown.
 
147
    */
 
148
   public UnMarshaller cloneUnMarshaller() throws CloneNotSupportedException
 
149
   {
 
150
      EncryptingUnMarshaller um = new EncryptingUnMarshaller(wrappedUnMarshaller);
 
151
      um.setCipherAlgorithm(this.cipherAlgorithm);
 
152
      return um;
 
153
   } 
 
154
}
 
155