All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jboss.remoting.marshal.encryption.EncryptingUnMarshaller Maven / Gradle / Ivy

There is a newer version: 5.0.29.Final
Show newest version
/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.jboss.remoting.marshal.encryption;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream; 
import java.util.Map;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;

import org.jboss.remoting.marshal.UnMarshaller;
import org.jboss.remoting.marshal.VersionedUnMarshaller;
import org.jboss.remoting.marshal.serializable.SerializableUnMarshaller;
import org.jboss.remoting.serialization.SerializationManager;
import org.jboss.remoting.serialization.SerializationStreamFactory;


/**
 * EncryptingMarshaller and EncryptingUnMarshaller are a general
 * purpose encrypting marshaller / decompressing unmarshaller pair based on 
 * Java's crypto stream facilities.
 * 

* EncryptingUnMarshaller is subclassed from SerializableUnMarshaller, * and by default it uses super.read() to deserialize an object, once the object has been * decrypted. Optionally, it can wrap any other unmarshaller and use that instead of * SerializableUnMarshaller to unmarshall an encrypted input stream. For example, *

*

new EncryptingUnMarshaller(new HTTPUnMarshaller())
* will create an umarshaller that * uses an HTTPUnMarshaller to restore an unencrypted input stream. * * @author [email protected] * @version $Revision: 2385 $ */ public class EncryptingUnMarshaller extends SerializableUnMarshaller { /** The serialVersionUID */ private static final long serialVersionUID = 1L; public final static String DATATYPE = "encrypt"; private UnMarshaller wrappedUnMarshaller; private String cipherAlgorithm = EncryptionManager.DEFAULT_CIPHER_ALGORITHM; private Cipher cipher = EncryptionManager.getCipher(Cipher.DECRYPT_MODE, cipherAlgorithm); /** * Create a new EncryptingUnMarshaller. */ public EncryptingUnMarshaller() { } /** * Create a new EncryptingUnMarshaller. * * @param unMarshaller unmarshaller to be used to restore * unencrypted byte stream to original object */ public EncryptingUnMarshaller(UnMarshaller unMarshaller) { wrappedUnMarshaller = unMarshaller; } /** * Set the Cipher Algorithm to use * @param algo * @see EncryptionManager#DEFAULT_CIPHER_ALGORITHM */ public void setCipherAlgorithm(String algo) { this.cipherAlgorithm = algo; cipher = EncryptionManager.getCipher(Cipher.DECRYPT_MODE, this.cipherAlgorithm); } /** * Restores a encrypted, marshalled form of an object to its original state. * * @param inputStream InputStream from which marshalled form is to be retrieved * @param metadata can be any transport specific metadata (such as headers from http transport). * This can be null, depending on if transport supports metadata. * @param version wire format version * @return restored object * @throws IOException if there is a problem reading from inputStream * @throws ClassNotFoundException if there is a problem finding a class needed for unmarshalling */ public Object read(InputStream inputStream, Map metadata, int version) throws IOException, ClassNotFoundException { if(cipher == null) throw new IllegalStateException("Cipher is null for algo="+ this.cipherAlgorithm); CipherInputStream cis = new CipherInputStream(inputStream,cipher); SerializationManager sm = SerializationStreamFactory.getManagerInstance(getSerializationType()); ObjectInputStream ois = sm.createRegularInput(cis); Object obj = null; if(wrappedUnMarshaller != null) { if (wrappedUnMarshaller instanceof VersionedUnMarshaller) return ((VersionedUnMarshaller)wrappedUnMarshaller).read(ois, metadata, version); else obj = wrappedUnMarshaller.read(ois, metadata); } else { obj = super.read(ois, metadata, version); } return obj; } /** * Returns a new EncryptingUnMarshaller * * @return a new EncryptingUnMarshaller * @throws CloneNotSupportedException In practice no exceptions are thrown. */ public UnMarshaller cloneUnMarshaller() throws CloneNotSupportedException { EncryptingUnMarshaller um = new EncryptingUnMarshaller(wrappedUnMarshaller); um.setCipherAlgorithm(this.cipherAlgorithm); return um; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy