KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > samples > transport > tcp > TCPSender


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package samples.transport.tcp;
18
19 import org.apache.axis.AxisFault;
20 import org.apache.axis.Message;
21 import org.apache.axis.MessageContext;
22 import org.apache.axis.components.logger.LogFactory;
23 import org.apache.axis.handlers.BasicHandler;
24 import org.apache.commons.logging.Log;
25
26 import java.io.BufferedInputStream JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.net.Socket JavaDoc;
29
30 /**
31  * This is meant to be used on a SOAP Client to call a SOAP server.
32  *
33  * @author Rob Jellinghaus (robj@unrealities.com)
34  * @author Doug Davis (dug@us.ibm.com)
35  */

36 public class TCPSender extends BasicHandler {
37     static Log log =
38             LogFactory.getLog(TCPSender.class.getName());
39
40     public void invoke(MessageContext msgContext) throws AxisFault {
41         log.info( "Enter: TCPSender::invoke" );
42
43         /* Find the service we're invoking so we can grab it's options */
44         /***************************************************************/
45         String JavaDoc targetURL = null ;
46         Message outMsg = null ;
47         String JavaDoc reqEnv = null ;
48
49         targetURL = msgContext.getStrProp( MessageContext.TRANS_URL);
50         try {
51             String JavaDoc host = msgContext.getStrProp(TCPTransport.HOST);
52             int port = Integer.parseInt(msgContext.getStrProp(TCPTransport.PORT));
53             byte[] buf = new byte[4097];
54             int rc = 0 ;
55
56             Socket JavaDoc sock = null ;
57
58             sock = new Socket JavaDoc( host, port );
59             log.info( "Created an insecure HTTP connection");
60
61             reqEnv = (String JavaDoc) msgContext.getRequestMessage().getSOAPPartAsString();
62
63             //System.out.println("Msg: " + reqEnv);
64

65             BufferedInputStream JavaDoc inp = new BufferedInputStream JavaDoc(sock.getInputStream());
66             OutputStream JavaDoc out = sock.getOutputStream();
67
68             byte[] bytes = reqEnv.getBytes();
69             String JavaDoc length = "" + bytes.length + "\r\n";
70             out.write(length.getBytes());
71             out.write( bytes );
72             out.flush();
73
74             log.debug( "XML sent:" );
75             log.debug( "---------------------------------------------------");
76             log.debug( reqEnv );
77
78             if ( false ) {
79                 // Special case - if the debug level is this high then something
80
// really bad must be going on - so just dump the input stream
81
// to stdout.
82
byte b;
83                 while ( (b = (byte) inp.read()) != -1 )
84                     System.err.print((char)b);
85                 System.err.println("");
86             }
87
88             outMsg = new Message( inp );
89             if (log.isDebugEnabled()) {
90                 log.debug( "\nNo Content-Length" );
91                 log.debug( "\nXML received:" );
92                 log.debug( "-----------------------------------------------");
93                 log.debug( (String JavaDoc) outMsg.getSOAPPartAsString() );
94             }
95
96             msgContext.setResponseMessage( outMsg );
97         }
98         catch( Exception JavaDoc e ) {
99             log.error( e );
100             e.printStackTrace();
101             throw AxisFault.makeFault(e);
102         }
103         log.info( "Exit: TCPSender::invoke" );
104     }
105 };
106
Popular Tags