001: /*
002: * Copyright 2001-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package samples.transport.tcp;
018:
019: import org.apache.axis.AxisFault;
020: import org.apache.axis.Message;
021: import org.apache.axis.MessageContext;
022: import org.apache.axis.components.logger.LogFactory;
023: import org.apache.axis.handlers.BasicHandler;
024: import org.apache.commons.logging.Log;
025:
026: import java.io.BufferedInputStream;
027: import java.io.OutputStream;
028: import java.net.Socket;
029:
030: /**
031: * This is meant to be used on a SOAP Client to call a SOAP server.
032: *
033: * @author Rob Jellinghaus (robj@unrealities.com)
034: * @author Doug Davis (dug@us.ibm.com)
035: */
036: public class TCPSender extends BasicHandler {
037: static Log log = LogFactory.getLog(TCPSender.class.getName());
038:
039: public void invoke(MessageContext msgContext) throws AxisFault {
040: log.info("Enter: TCPSender::invoke");
041:
042: /* Find the service we're invoking so we can grab it's options */
043: /***************************************************************/
044: String targetURL = null;
045: Message outMsg = null;
046: String reqEnv = null;
047:
048: targetURL = msgContext.getStrProp(MessageContext.TRANS_URL);
049: try {
050: String host = msgContext.getStrProp(TCPTransport.HOST);
051: int port = Integer.parseInt(msgContext
052: .getStrProp(TCPTransport.PORT));
053: byte[] buf = new byte[4097];
054: int rc = 0;
055:
056: Socket sock = null;
057:
058: sock = new Socket(host, port);
059: log.info("Created an insecure HTTP connection");
060:
061: reqEnv = (String) msgContext.getRequestMessage()
062: .getSOAPPartAsString();
063:
064: //System.out.println("Msg: " + reqEnv);
065:
066: BufferedInputStream inp = new BufferedInputStream(sock
067: .getInputStream());
068: OutputStream out = sock.getOutputStream();
069:
070: byte[] bytes = reqEnv.getBytes();
071: String length = "" + bytes.length + "\r\n";
072: out.write(length.getBytes());
073: out.write(bytes);
074: out.flush();
075:
076: log.debug("XML sent:");
077: log
078: .debug("---------------------------------------------------");
079: log.debug(reqEnv);
080:
081: if (false) {
082: // Special case - if the debug level is this high then something
083: // really bad must be going on - so just dump the input stream
084: // to stdout.
085: byte b;
086: while ((b = (byte) inp.read()) != -1)
087: System.err.print((char) b);
088: System.err.println("");
089: }
090:
091: outMsg = new Message(inp);
092: if (log.isDebugEnabled()) {
093: log.debug("\nNo Content-Length");
094: log.debug("\nXML received:");
095: log
096: .debug("-----------------------------------------------");
097: log.debug((String) outMsg.getSOAPPartAsString());
098: }
099:
100: msgContext.setResponseMessage(outMsg);
101: } catch (Exception e) {
102: log.error(e);
103: e.printStackTrace();
104: throw AxisFault.makeFault(e);
105: }
106: log.info("Exit: TCPSender::invoke");
107: }
108: };
|