001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.mts.base;
028:
029: import java.io.ByteArrayInputStream;
030: import java.io.ByteArrayOutputStream;
031: import java.io.InputStream;
032: import java.io.ObjectInputStream;
033: import java.io.ObjectOutputStream;
034: import java.io.OutputStream;
035:
036: import org.cougaar.core.component.ServiceBroker;
037: import org.cougaar.core.component.ServiceProvider;
038: import org.cougaar.core.mts.MessageAddress;
039: import org.cougaar.core.mts.MessageAttributes;
040: import org.cougaar.core.mts.ProtectedInputStream;
041: import org.cougaar.core.mts.ProtectedOutputStream;
042: import org.cougaar.core.service.MessageProtectionService;
043:
044: /**
045: * This {@link ServiceProvider} both provides and implements the
046: * {@link MessageProtectionService}. It's only created as a default,
047: * if no other MessageProtection Service is available. The service
048: * implementation does nothing useful.
049: */
050: public class MessageProtectionServiceImpl implements
051: MessageProtectionService, ServiceProvider {
052:
053: public Object getService(ServiceBroker sb, Object requestor,
054: Class serviceClass) {
055: if (serviceClass == MessageProtectionService.class) {
056: return this ;
057: } else {
058: return null;
059: }
060: }
061:
062: public void releaseService(ServiceBroker sb, Object requestor,
063: Class serviceClass, Object service) {
064: }
065:
066: public byte[] protectHeader(MessageAttributes attributes,
067: MessageAddress source, MessageAddress destination)
068: throws java.security.GeneralSecurityException,
069: java.io.IOException {
070: ByteArrayOutputStream bos = new ByteArrayOutputStream();
071: ObjectOutputStream oos = new ObjectOutputStream(bos);
072: oos.writeObject(attributes);
073: oos.close();
074: byte[] header = bos.toByteArray();
075: return header;
076: // For testing security exception handling
077: // throw new java.security.GeneralSecurityException("protectHeader");
078: }
079:
080: public MessageAttributes unprotectHeader(byte[] rawData,
081: MessageAddress source, MessageAddress destination)
082: throws java.security.GeneralSecurityException,
083: java.io.IOException {
084: MessageAttributes attributes = null;
085:
086: ByteArrayInputStream bis = new ByteArrayInputStream(rawData);
087: ObjectInputStream ois = new ObjectInputStream(bis);
088: try {
089: attributes = (MessageAttributes) ois.readObject();
090: } catch (ClassNotFoundException cnf) {
091: // ???
092: }
093: ois.close();
094: return attributes;
095: // For testing security exception handling
096: // throw new java.security.GeneralSecurityException("unprotectHeader");
097: }
098:
099: public ProtectedOutputStream getOutputStream(OutputStream os,
100: MessageAddress src, MessageAddress dst,
101: MessageAttributes attrs) {
102: return new DummyOutputStream(os);
103: }
104:
105: public ProtectedInputStream getInputStream(InputStream is,
106: MessageAddress src, MessageAddress dst,
107: MessageAttributes attrs) {
108: return new DummyInputStream(is);
109: }
110:
111: private class DummyOutputStream extends ProtectedOutputStream {
112: DummyOutputStream(OutputStream stream) {
113: super (stream);
114: }
115:
116: public void finishOutput(MessageAttributes attr)
117: throws java.io.IOException {
118: }
119: }
120:
121: private class DummyInputStream extends ProtectedInputStream {
122: DummyInputStream(InputStream stream) {
123: super (stream);
124: }
125:
126: public void finishInput(MessageAttributes attr)
127: throws java.io.IOException {
128: }
129:
130: }
131:
132: }
|