001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)SoapClient.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package jbidemo;
030:
031: import java.io.File;
032: import java.io.FileInputStream;
033: import java.io.FileOutputStream;
034: import java.io.IOException;
035: import java.util.Properties;
036: import javax.xml.soap.MessageFactory;
037: import javax.xml.soap.MimeHeaders;
038: import javax.xml.soap.SOAPConnection;
039: import javax.xml.soap.SOAPConnectionFactory;
040: import javax.xml.soap.SOAPException;
041: import javax.xml.soap.SOAPMessage;
042: import javax.xml.soap.SOAPPart;
043: import javax.xml.transform.Source;
044: import javax.xml.transform.Transformer;
045: import javax.xml.transform.TransformerConfigurationException;
046: import javax.xml.transform.TransformerException;
047: import javax.xml.transform.TransformerFactory;
048: import javax.xml.transform.stream.StreamResult;
049: import javax.xml.transform.stream.StreamSource;
050:
051: public class SoapClient {
052: /**
053: * Test of inbound SOAP Request processing.
054: */
055: private static MessageFactory messageFactory;
056: private static SOAPConnectionFactory soapConnFactory;
057: private static SOAPConnection connection;
058:
059: static {
060: try {
061: messageFactory = MessageFactory.newInstance();
062: soapConnFactory = SOAPConnectionFactory.newInstance();
063: connection = soapConnFactory.createConnection();
064: } catch (SOAPException ex) {
065: ex.printStackTrace();
066: }
067: }
068:
069: public static void testInboundSOAPRequest(String propFilePath)
070: throws Exception {
071: try {
072: FileInputStream fis = null;
073: Properties props = null;
074: try {
075: fis = new FileInputStream(new File(propFilePath));
076: props = new java.util.Properties();
077: props.load(fis);
078: } finally {
079: try {
080: if (fis != null) {
081: fis.close();
082: }
083: } catch (Exception ex) {
084: // ignore
085: }
086: }
087:
088: String soapAction = (String) props.get("soapaction");
089: String destination = (String) props.get("destination");
090: String inputFilePath = (String) props.get("input");
091: String outputFilePath = (String) props.get("output");
092:
093: sendAndCheck(destination, inputFilePath, outputFilePath,
094: soapAction);
095: } catch (Exception ex) {
096: System.out.println(" Failed.");
097: throw ex;
098: } catch (Error er) {
099: System.out.println(" Failed.");
100: throw er;
101: }
102: }
103:
104: /**
105: * Send a soap message read in from the given input file, compare the output
106: * to the given output file
107: */
108: private static void sendAndCheck(String destination,
109: String inputFilePath, String outputFilePath,
110: String soapAction) throws Exception {
111: SOAPMessage message = loadMessage(inputFilePath);
112: SOAPMessage response = sendMessage(destination, message,
113: soapAction);
114: outputResponse(response, inputFilePath, outputFilePath);
115: }
116:
117: private static void outputResponse(SOAPMessage response,
118: String inputFilePath, String outputFilePath) {
119: try {
120: if (outputFilePath == null || outputFilePath.equals("")) {
121: outputFilePath = inputFilePath + ".out";
122: }
123: System.out.println("output file: " + outputFilePath);
124: File outputFile = new File(outputFilePath);
125: FileOutputStream fos = new FileOutputStream(outputFile);
126: TransformerFactory transformerFactory = TransformerFactory
127: .newInstance();
128: Transformer transformer = transformerFactory
129: .newTransformer();
130:
131: if (response != null) {
132: SOAPPart replySOAPPart = response.getSOAPPart();
133: Source sourceContent = replySOAPPart.getContent();
134: StreamResult result = new StreamResult(fos);
135: transformer.transform(sourceContent, result);
136: }
137: } catch (IOException ex) {
138: ex.printStackTrace();
139: } catch (TransformerConfigurationException ex) {
140: ex.printStackTrace();
141: } catch (SOAPException ex) {
142: ex.printStackTrace();
143: } catch (TransformerException ex) {
144: ex.printStackTrace();
145: }
146: }
147:
148: /**
149: * read in a soap message from the given input file
150: */
151: private static SOAPMessage loadMessage(String testMsgFileName)
152: throws SOAPException, IOException {
153: //Create and populate the message from a file
154: SOAPMessage message = messageFactory.createMessage();
155: SOAPPart soapPart = message.getSOAPPart();
156: StreamSource preppedMsgSrc = new StreamSource(
157: new FileInputStream(testMsgFileName));
158: soapPart.setContent(preppedMsgSrc);
159: message.saveChanges();
160: return message;
161: }
162:
163: /**
164: * Send a soap message
165: * @param destination URL to send to
166: * @param message message to send
167: * @param expectedHttpStatus expected http status code or null if success is expected
168: * @return reply soap message
169: */
170: private static SOAPMessage sendMessage(String destination,
171: SOAPMessage message, String soapAction)
172: throws SOAPException {
173:
174: // Add soapAction if not null
175: if (soapAction != null) {
176: MimeHeaders hd = message.getMimeHeaders();
177: hd.setHeader("SOAPAction", soapAction);
178: }
179:
180: // Send the message and get a reply
181: SOAPMessage reply = null;
182: long start = 0;
183:
184: boolean httpSuccess = true;
185: try {
186: reply = connection.call(message, destination);
187: } catch (SOAPException ex) {
188: ex.printStackTrace();
189: }
190: long end = 0;
191:
192: return reply;
193: }
194:
195: public final static void main(String[] args) {
196: if (args.length != 1) {
197: System.out.println("java SoapClient test.properties");
198: return;
199: }
200:
201: String propFilePath = args[0];
202: System.out.println("file path: " + propFilePath);
203: try {
204: SoapClient.testInboundSOAPRequest(propFilePath);
205: } catch (Exception ex) {
206: ex.printStackTrace();
207: }
208: }
209: }
|