JWS Task 2: Raw XML and Levenshtein distance

Vladimir Levenshtein
Write WS client that sends raw XML requests instead of using wrapper objects created from Service. Use Levenshtein distance as web method.
Example Solution
Service Endpoint Interface
package com.fuzilaga.jws.levenshtein;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
@WebService
@SOAPBinding(style=Style.RPC)
public interface LevenshteinService
{
@WebMethod
public int levenshteinDistance(String s[]);
}
Service Implementation Bean
package com.fuzilaga.jws.levenshtein;
import javax.jws.WebService;
@WebService(endpointInterface = "com.fuzilaga.jws.levenshtein.LevenshteinService")
public class LevenshteinServiceImpl implements LevenshteinService
{
public int levenshteinDistance(String s[])
{
String s1 = s[0];
String s2 = s[1];
if (null == s1 || null == s2)
{
return -1;
}
int s1len = s1.length();
int s2len = s2.length();
int i = 0, j = 0, cost = 0;
char s1i, s2j;
int matrix[][] = new int[s1len + 1][s2len + 1];
for (i = 0; i <= s1len; i++)
{
matrix[i][0] = i; // deletion
}
for (j = 0; j <= s2len; j++)
{
matrix[0][j] = j; // insertion
}
for (i = 1; i <= s1len; i++)
{
s1i = s1.charAt(i - 1);
for (j = 1; j <= s2len; j++)
{
s2j = s2.charAt(j - 1);
if (s1i == s2j)
{
cost = 0;
} else
{
cost = 1;
}
matrix[i][j] = min(matrix[i - 1][j] + 1, // deletion
matrix[i][j - 1] + 1, // insertion
matrix[i - 1][j - 1] + cost // substitution
);
}
}
return matrix[s1len][s2len];
}
private int min(int a, int b, int c)
{
int min = a;
if (b < min)
{
min = b;
}
if (c < min)
{
min = c;
}
return min;
}
}
Service Publisher
package com.fuzilaga.jws.levenshtein;
import javax.xml.ws.Endpoint;
public class LevenshteinServicePublisher
{
public static void main(String args[])
{
Endpoint.publish("http://localhost:8880/levenshtein", new LevenshteinServiceImpl());
}
}
Service Client
package com.fuzilaga.jws.levenshtein;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class LevenshteinClient
{
public static void main(String args[]) throws Exception
{
StringBuilder msg = new StringBuilder();
msg.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:lev=\"http://levenshtein.jws.fuzilaga.com/\">");
msg.append(" <soapenv:Header/>");
msg.append(" <soapenv:Body>");
msg.append(" <lev:levenshteinDistance>");
msg.append(" <arg0>");
msg.append(" <item>kitten</item>");
msg.append(" <item>sitting</item>");
msg.append(" </arg0>");
msg.append(" </lev:levenshteinDistance>");
msg.append(" </soapenv:Body>");
msg.append("</soapenv:Envelope>");
// get connection
URL url = new URL("http://localhost:8880/levenshtein");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "text/xml");
conn.setRequestProperty("accept", "text/xml");
conn.setDoInput(true);
conn.setDoOutput(true);
// send
DataOutputStream writer = new DataOutputStream(conn.getOutputStream());
writer.writeBytes(msg.toString());
writer.flush();
// read
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder response = new StringBuilder();
String next = "";
while ((next = reader.readLine()) != null)
{
response.append(next);
}
writer.close();
reader.close();
System.out.println("response = " + response.toString());
}
}
NOTE: Full source and instructions on how to run locally can be found here.