Hi Pawel,
Here is a sample java code that you can use to send xml files to your partners server. All you need is a working jvm on your machine and to use a working keystore containing your private key, public key and CA certificate,
The code is
‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
public class WmTNInvoker {
public static void showUsage() {
System.out
.println("Usage: java WmTNInvoker <url> <username> <password> [-file <file to send>] [-keystorename <keystorename> -keystorepassword <keystore password>]");
System.out.println("Keystore name and password needed if you create a HTTPS connection");
}
public static void process (URL theUrl, String username, String password, String fileName, String keystoreName, String keystorePassword)
throws IOException
{
System.out.println (">>> Processing " + theUrl.toExternalForm());
if (fileName != null)
System.out.println (">>> File : " + fileName);
if (keystoreName != null)
{
System.out.println(">>> Using keystore " + keystoreName);
System.setProperty("javax.net.ssl.keyStore", keystoreName);
System.setProperty("javax.net.ssl.keyStorePassword", keystorePassword);
System.setProperty("javax.net.ssl.trustStore", keystoreName);
System.setProperty("javax.net.ssl.trustStorePassword", keystorePassword);
}
HttpURLConnection conn = (HttpURLConnection) theUrl.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
boolean authspec = false;
if (username.equals("") && password.equals(""))
authspec = false;
else
authspec = true;
if (authspec)
{
String authString = username + ":" + password;
String auth = "Basic "
+ new sun.misc.BASE64Encoder().encode(authString.getBytes());
conn.setRequestProperty("Authorization", auth);
}
conn.setDoOutput(true);
conn.setDoInput(true);
OutputStream os = conn.getOutputStream();
if (fileName != null) {
BufferedReader in = new BufferedReader(new FileReader(fileName));
boolean ready = false;
String newLine = System.getProperty("line.separator");
while (!ready) {
String readString = in.readLine();
if (readString != null) {
os.write(readString.getBytes());
os.write(newLine.getBytes());
} else
ready = true;
}
}
os.close();
InputStream is = conn.getInputStream();
InputStreamReader fromWm = new InputStreamReader(is);
BufferedReader fromWmReader = new BufferedReader(fromWm);
boolean ready = false;
while (!ready) {
String readString = fromWmReader.readLine();
if (readString != null) {
System.out.println(readString);
} else
ready = true;
}
os.close();
System.out.println (">>> Processing done");
}
/**
* @throws IOException
*/
public static void main(String[] args) throws IOException {
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
System.out.println("Warning: URL Host: "+urlHostName+" vs. "+session.getPeerHost());
return true;
}
};
HttpsURLConnection.setDefaultHostnameVerifier(hv);
long startTime = System.currentTimeMillis();
URL theUrl = null;
String username = null;
String password = null;
int argCounter = 0;
try {
String urlText = args[argCounter++];
theUrl = new URL(urlText);
username = args[argCounter++];
password = args[argCounter++];
} catch (ArrayIndexOutOfBoundsException e) {
showUsage();
return;
} catch (MalformedURLException mue) {
mue.printStackTrace();
showUsage ();
return;
}
String keystoreName = null;
String keystorePassword = null;
String fileName = null;
boolean allArgsParsed = false;
while (!allArgsParsed)
{
try
{
String switchName = args[argCounter++];
String value = null;
try
{
value = args[argCounter++];
}
catch (ArrayIndexOutOfBoundsException e)
{
showUsage ();
return;
}
if (!value.startsWith("-"))
{
if (switchName.toLowerCase().equals("-file"))
{
fileName = value;
}
else if (switchName.toLowerCase().equals("-keystorename"))
{
keystoreName = value;
}
else if (switchName.toLowerCase().equals("-keystorepassword"))
{
keystorePassword = value;
}
else
{
showUsage ();
return;
}
}
else
argCounter--;
}
catch (ArrayIndexOutOfBoundsException e)
{
allArgsParsed = true;
}
}
if ((fileName != null) && (fileName.indexOf("#") != -1))
{
System.out.println ("Multiple files specified");
boolean ready = false;
int counter = 1;
while (!ready)
{
String thisFileName = fileName;
thisFileName = thisFileName.replaceAll("#",new Integer(counter).toString());
File thisFile = new File (thisFileName);
if (thisFile.exists())
{
try
{
long processStartTime = System.currentTimeMillis();
process (theUrl, username, password, thisFileName, keystoreName, keystorePassword);
long processEndTime = System.currentTimeMillis();
System.out.println (">>> This document took : " + (processEndTime-processStartTime) + " ms.");
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
counter++;
}
else
ready = true;
}
}
else
{
try
{
process (theUrl, username, password, fileName, keystoreName, keystorePassword);
}
catch (IOException ioe)
{
ioe.printStackTrace ();
}
}
long endTime = System.currentTimeMillis();
System.out.println (">>> Execution time : " + (endTime-startTime) + " ms.");
}
}
‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’’
You can run the above code using command given below.
For using https
java WmTNInvoker https://host:port/invoke/wm.tn/receive “” “” -file filenameOfTheFile youWantToSend -keystorename yourKeyStore.jks -keystorepassword passwordForYourKeyStore
For Using http
java WmTNInvoker http://host:port/invoke/wm.tn/receive -file filenameOfTheFile youWantToSend
The above code just supports secure post of documents.Does not support AS2.
HTH
Yogesh