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

« back to all changes in this revision

Viewing changes to src/org/jboss/remoting/marshal/encryption/EncryptingMarshaller.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.ObjectOutputStream;
27
 
import java.io.OutputStream;
28
 
import java.security.Key;
29
 
 
30
 
import javax.crypto.Cipher;
31
 
import javax.crypto.CipherOutputStream; 
32
 
 
33
 
import org.jboss.remoting.marshal.Marshaller;
34
 
import org.jboss.remoting.marshal.VersionedMarshaller;
35
 
import org.jboss.remoting.marshal.serializable.SerializableMarshaller;
36
 
import org.jboss.remoting.serialization.SerializationManager;
37
 
import org.jboss.remoting.serialization.SerializationStreamFactory;
38
 
 
39
 
 
40
 
/**
41
 
 * <code>EncryptingMarshaller</code> and <code>EncryptingMarshaller</code> are a general
42
 
 * purpose encryption based marshaller / decompressing unmarshaller pair 
43
 
 * based on Java's Cipher facilities.
44
 
 * <p/>
45
 
 * <code>EncryptingMarshaller</code> is subclassed from <code>SerializableMarshaller</code>, 
46
 
 * and by default it uses <code>super.write()</code> to marshall an object, which is then
47
 
 * encrypted.  Optionally, it can wrap any other marshaller and use that instead of
48
 
 * <code>SerializableMarshaller</code> to marshall an object before it is encrypted.
49
 
 * For example,
50
 
 * <p/>
51
 
 * <center><code>new EncryptingMarshaller(new HTTPMarshaller())</code></center>
52
 
 * <p/>
53
 
 * will create a marshaller that encrypts the output of an <code>HTTPMarshaller</code>.
54
 
 *
55
 
 * @author <a href="mailto:anil.saldhana@jboss.com">Anil Saldhana</a> 
56
 
 */
57
 
 
58
 
public class EncryptingMarshaller extends SerializableMarshaller
59
 
{
60
 
   /** The serialVersionUID */
61
 
   private static final long serialVersionUID = 1L;
62
 
 
63
 
   public final static String DATATYPE = "encrypt";
64
 
 
65
 
   private Marshaller wrappedMarshaller; 
66
 
   
67
 
   private String cipherAlgorithm = EncryptionManager.DEFAULT_CIPHER_ALGORITHM;
68
 
   
69
 
   private Cipher cipher = EncryptionManager.getCipher(Cipher.ENCRYPT_MODE, cipherAlgorithm);
70
 
 
71
 
   /**
72
 
    * Create a new EncryptingMarshaller.
73
 
    */
74
 
   public EncryptingMarshaller()
75
 
   { 
76
 
   }
77
 
   
78
 
   /**
79
 
    * 
80
 
    * Create a new EncryptingMarshaller.
81
 
    * 
82
 
    * @param algo Cipher Algorithm
83
 
    * @param key Key
84
 
    * @see #setCipherAlgorithm(String)
85
 
    */
86
 
   public EncryptingMarshaller(String algo, Key key)
87
 
   { 
88
 
      cipher = EncryptionManager.getCipher(Cipher.ENCRYPT_MODE, algo, key);
89
 
   }
90
 
 
91
 
 
92
 
   /**
93
 
    * Create a new EncryptingMarshaller.
94
 
    *
95
 
    * @param marshaller A <code>Marshaller</code> which is used to turn objects into byte streams.
96
 
    */
97
 
   public EncryptingMarshaller(Marshaller marshaller)
98
 
   {
99
 
      wrappedMarshaller = marshaller;
100
 
   }
101
 
   
102
 
   /**
103
 
    * Set the Cipher Algorithm to use
104
 
    * @param algo
105
 
    * @see EncryptionManager#DEFAULT_CIPHER_ALGORITHM
106
 
    */
107
 
   public void setCipherAlgorithm(String algo)
108
 
   {
109
 
      this.cipherAlgorithm = algo;
110
 
      cipher = EncryptionManager.getCipher(Cipher.ENCRYPT_MODE, this.cipherAlgorithm);
111
 
   }
112
 
 
113
 
   public OutputStream getMarshallingStream(OutputStream outputStream) throws IOException
114
 
   {
115
 
      return outputStream;
116
 
   }
117
 
   
118
 
   /**
119
 
    * Writes encrypted, marshalled form of <code>dataObject</code> to <code>output</code>.
120
 
    *
121
 
    * @param dataObject arbitrary object to be marshalled
122
 
    * @param output     <code>OutputStream</code> to which <code>output</code> is to be marshalled
123
 
    * @param version    wire format version
124
 
    */
125
 
   public void write(Object dataObject, OutputStream output, int version) throws IOException
126
 
   { 
127
 
      if(cipher == null)
128
 
         throw new IllegalStateException("Cipher is null for algo="+ this.cipherAlgorithm);
129
 
      output.flush(); 
130
 
      
131
 
      //EOS intercepts the close() call and does not close the stream
132
 
      EncryptionOutputStream eos = new EncryptionOutputStream(output);
133
 
       
134
 
      CipherOutputStream cos = new CipherOutputStream(eos, cipher);
135
 
       
136
 
      SerializationManager sm = SerializationStreamFactory.getManagerInstance(getSerializationType());
137
 
      ObjectOutputStream oos = sm.createOutput(cos);
138
 
      
139
 
      if(wrappedMarshaller != null)
140
 
      {
141
 
         if (wrappedMarshaller instanceof VersionedMarshaller)
142
 
            ((VersionedMarshaller) wrappedMarshaller).write(dataObject, oos, version);
143
 
         else
144
 
            wrappedMarshaller.write(dataObject, oos);
145
 
      }
146
 
      else
147
 
      {
148
 
         super.write(dataObject, oos, version);
149
 
      }  
150
 
      oos.flush();  
151
 
      
152
 
      //Vagaries of CipherOutputStream which needs a close() to flush at the end
153
 
      cos.close(); //Tests fail without this statement - oos.close() should do it
154
 
      oos.close(); //There is a need to close cos
155
 
   }
156
 
 
157
 
   /**
158
 
    * Returns a <code>EncryptingMarshaller</code>.
159
 
    *
160
 
    * @return a <code>EncryptingMarshaller</code>.
161
 
    * @throws CloneNotSupportedException In practice no exceptions are thrown
162
 
    */
163
 
   public Marshaller cloneMarshaller() throws CloneNotSupportedException
164
 
   {
165
 
      EncryptingMarshaller em = new EncryptingMarshaller(wrappedMarshaller);
166
 
      em.setCipherAlgorithm(this.cipherAlgorithm);
167
 
      return em;
168
 
   } 
169
 
}
170