KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > jbi > serviceengine > bridge > transport > NMRConnection


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.jbi.serviceengine.bridge.transport;
24
25
26 import com.sun.xml.ws.spi.runtime.WSConnection;
27 import java.io.ByteArrayInputStream JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.io.OutputStream JavaDoc;
30 import java.nio.ByteBuffer JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.io.ByteArrayOutputStream JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.io.ObjectInputStream JavaDoc;
38 import java.io.ObjectOutputStream JavaDoc;
39 import javax.jbi.messaging.MessageExchange;
40 import javax.xml.soap.SOAPMessage JavaDoc;
41 import javax.xml.soap.MessageFactory JavaDoc;
42 /**
43  *
44  * @author Manisha Umbarje
45  */

46 public abstract class NMRConnection implements WSConnection {
47
48     Map JavaDoc<String JavaDoc, List JavaDoc<String JavaDoc>> headers = new HashMap JavaDoc<String JavaDoc, List JavaDoc<String JavaDoc>>();
49     public OutputStream JavaDoc debugStream = null;
50     public OutputStream JavaDoc outputStream = null;
51     public InputStream JavaDoc inputStream = null;
52     int statusCode;
53     static String JavaDoc CONTENT_TYPE_HEADER = "Content-Type";
54     static String JavaDoc XML_CONTENT_TYPE = "text/xml";
55            
56     public int getStatus () {
57         return statusCode;
58     }
59
60     public void setStatus (int statusCode) {
61         this.statusCode = statusCode;
62     }
63
64     public OutputStream JavaDoc getDebug () {
65         return debugStream;
66     }
67
68     /**
69      * @return outputStream
70      *
71      * Returns the OutputStream on which the outbound message is written.
72      * Any stream or connection initialization, pre-processing is done here.
73      */

74     public OutputStream JavaDoc getOutput() {
75         outputStream = new ByteArrayOutputStream JavaDoc();
76         
77         return outputStream;
78     }
79     
80     /**
81      * @return inputStream
82      *
83      * Returns the InputStream on which the inbound message is received.
84      * Any post-processing of message is done here.
85      */

86     public InputStream JavaDoc getInput() {
87         
88         // Receive Message
89
return inputStream;
90     }
91
92     public Map JavaDoc<String JavaDoc, List JavaDoc<String JavaDoc>> getHeaders () {
93         return headers;
94     }
95     
96     public void setHeaders (Map JavaDoc<String JavaDoc, List JavaDoc<String JavaDoc>> headers) {
97         this.headers = headers;
98     }
99
100    
101     protected void createSOAPMessageInputStream(SOAPMessage JavaDoc soapMessage) {
102         if(soapMessage != null) {
103             try {
104                 
105                 ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
106                 soapMessage.writeTo(baos);
107                 inputStream = new ByteArrayInputStream JavaDoc(baos.toByteArray());
108             } catch(Exception JavaDoc soapException) {
109                 soapException.printStackTrace();
110                 throw new TransportFailedException(soapException.getMessage());
111             }
112         }
113     }
114     
115     protected SOAPMessage JavaDoc createSOAPMessageFromOutputStream(){
116         ObjectInputStream JavaDoc objInputStream = null;
117         try {
118             
119             if (outputStream != null) {
120                 outputStream.flush();
121                 outputStream.close();
122                 byte[] message = ((ByteArrayOutputStream JavaDoc)outputStream).toByteArray();
123                 
124                 ByteArrayInputStream JavaDoc is = new ByteArrayInputStream JavaDoc(message);
125                 SOAPMessage JavaDoc soapMessage = MessageFactory.newInstance().createMessage(null,is);
126                 return soapMessage;
127             }
128         } catch(Exception JavaDoc e) {
129             throw new TransportFailedException(e.getMessage());
130         } finally {
131             try {
132                 if(objInputStream != null)
133                     objInputStream.close();
134             } catch(Exception JavaDoc ioe) {
135                 ioe.printStackTrace();
136             }
137         }
138         return null;
139     }
140     
141     protected void setContentType() {
142        List JavaDoc<String JavaDoc> ctHeader = new ArrayList JavaDoc<String JavaDoc>();
143        ctHeader.add(XML_CONTENT_TYPE);
144        headers.put(CONTENT_TYPE_HEADER, ctHeader);
145     }
146     
147     public void closeInput() {}
148     public void closeOutput() {}
149     public void close() {}
150     
151 }
152
Popular Tags