I need to develop a Java client that will send an XML file to an existing flow service on Integration Server. I can create most of the client by using Tools > Generate Code from Developer.
Since the input to the flow service will be an XML file, or an Object called node, how do I instantiate a com.wm.lang.xml.Node? This isn’t documented in the Java API.
If you just want to send an xml file to an IS service from an external java app, why not have the app just read the xml doc from the file system, create a URLConnection and post it? I have an example that does this.
A com.wm.lang.xml.Node is a parsed XML document similar to a DOM document. To do this using public built-in services have your java service invoke pub.xml:xmlStringToXMLNode.
I took Mark’s suggestion. I used a Java application to post an XML file to IS. I wrote the app so the target URL and the file to be posted are stored in a config file. This way I can install the app on any machine.
I found an open source tool that allows developers to install Java apps as Windows or UNIX services (http://wrapper.tanukisoftware.org). This makes it easy to manage the client app.
Chris,
You could also exposed the webMethods service as a doc/literal web service. You could then use something like Axis along with your java client to post to the web service. Axis with generate the Java client for you based upon the WSDL. The nice thing about this is that then the java client can just manipulate java objects using getter and setter methods created by Axis and doesn’t have to worry about XML. Plus you get the benefits of a more standards based interface.
Of course if you input into the Java client is an XML file (are you reading in an XML file from the file system?), you would have to parse (you could use something like JAXM) that to use the above functionality.
Here’s my crude test client that reads a soap message from a file in the file system and posts it to the server of your choice. It will accomodate only HTTP and not HTTPS connections.
package conneva.soap.utils;
import java.net.*;
import java.net.URLEncoder;
import java.io.*;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import sun.misc.BASE64Encoder;
/**
*
* This is a simple text mode application that reads a properly formed SOAP message (including the envelope) from an input file
* and sends it to the specified URL. Authorization information can be provided from the command line.
*
* Basic performance metrics are provided to measure elapsed time to send and receive the SOAP messages.
*
*/
public class SendSoapMsgHTTP {
public final static String DEFAULT_SERVER
= "http://localhost:8080/soap/default";
public final static String SOAP_ACTION
= "";
public final static String DEFAULT_USERNAME = "esb_user";
public final static String DEFAULT_PASSWORD = "esb_password";
public static void main(String[] args) {
if (args.length == 0) {
if ((args.length != 1) && (args.length != 2) && (args.length != 4) && (args.length != 5)) {
System.out.println();
System.out.println("Usage: java SendSoapMsg <filename> [<url>] [<username> <password>]");
System.out.println("<filename> - valid Soap Message file to be posted");
System.out.println("<url> - valid URL (e.g. http://localhost:8080/soap/default");
System.out.println("<username> - username");
System.out.println("<password> - password");
System.out.println("<printmsgs> - 'true' displays the soap request and response msgs");
System.out.println();
return;
}
}
String fileName = args[0];
String server = DEFAULT_SERVER;
String userName = DEFAULT_USERNAME;
String password = DEFAULT_PASSWORD;
if (args.length >= 2) server = args[1];
if (args.length >2) {
userName = args[2];
password = args[3];
}
String verboseStr = "";
boolean verbose = false;
if (args.length == 5) {
verboseStr = args[4];
if (verboseStr.equals("true")) verbose = true;
}
try {
//Get soap message from input file
File inputFile = new File(fileName);
FileInputStream in = new FileInputStream(inputFile);
byte bt[] = new byte[(int)inputFile.length()];
in.read(bt);
String soapMsg = new String(bt);
in.close();
//Get the HTTP connection setup start time
Calendar cal_csu = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
String csu_starttime = sdf.format(cal_csu.getTime());
long csu_startmillis = System.currentTimeMillis();
//Create connection to the server
URL u = new URL(server);
URLConnection uc = u.openConnection();
HttpURLConnection connection = (HttpURLConnection) uc;
//Set connection parms
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
//Set connection authorization
String userInfo = userName + ":" + password;
BASE64Encoder encoder = new BASE64Encoder();
byte[] userInfoBytes = userInfo.getBytes(); // I18n bug here!
String authInfo = "Basic " + encoder.encode(userInfoBytes);
connection.setRequestProperty("Authorization", authInfo);
System.out.println("\nHTTP connection established. Sending soap request...\n");
//Get an output stream on the connection and create a writer
OutputStream out = connection.getOutputStream();
Writer wout = new OutputStreamWriter(out);
// Print the soap request message to the console
if (verbose) System.out.println("SOAP Request Msg: "+soapMsg+"\n");
System.out.println("SOAP request message sent. Waiting for response...\n");
//Get the start time
Calendar cal = Calendar.getInstance();
String startTime = sdf.format(cal.getTime());
long startmillis = System.currentTimeMillis();
//Send the soap message using the HTTP connection
wout.write(soapMsg);
wout.flush();
wout.close();
//Get an inputstream and read the soap response
InputStream instream = connection.getInputStream();
int c;
while ((c = instream.read()) != -1) if (verbose) System.out.write(c);
instream.close();
//Get send soap msg stop time and calculate elapsed time
Calendar cal2 = Calendar.getInstance();
String endTime = sdf.format(cal2.getTime());
long endmillis = System.currentTimeMillis();
long csu_elapsedTime = (startmillis - csu_startmillis);
long elapsedTime = (endmillis - startmillis);
//Print performance summary
System.out.println("\nHTTP setup start : "+csu_starttime);
System.out.println("HTTP setup ms : "+ csu_elapsedTime+"\n");
System.out.println("SoapRequest sent : "+startTime);
System.out.println("SoapResponse rcvd : "+endTime);
System.out.println("Elapsed time ms : "+ elapsedTime);
}
//catch exceptions
catch (IOException e) {
System.err.println(e);
}
} // end main
} // end SendSoapMsg