|
|
||
Marc Hadley's BlogRESTful Web Service Endpoints in JAX-WSPosted by mhadley on March 21, 2006 at 11:05 AM | Comments (2)In previous entries I've covered use of the client side JAX-WS A JAX-WS public interface Provider<T> {
public T invoke(T request);
}
You also have to pick the service mode in which the endpoint will operate and annotate the endpoint class to allow the JAX-WS runtime to identify the correct class to publish. Here's the start of our endpoint class (I'm going to interleave code and narrative but if you extract and stitch together the code segments below you'll end up with a complete class): @WebServiceProvider
@ServiceMode(value=Service.Mode.PAYLOAD)
public class SystemPropertiesProvider implements Provider<Source> {
@Resource
protected WebServiceContext context;
Arun has an informative blog entry that discusses the difference between PAYLOAD and MESSAGE mode and describes which types of The endpoint we'll develop here will allow a client to query the Java system properties of the endpoint implementation class, here is the implementation of the public Source invoke(Source source) {
Map<String, List<String>> query = getQueryParameters();
String replyData = getSystemInfoAsXML(query.get("property"));
StreamSource reply = new StreamSource(new StringReader(replyData));
return reply;
}
The protected Map<String, List<String>> getQueryParameters() {
MessageContext msgCtx = context.getMessageContext();
String queryString = (String)msgCtx.get(MessageContext.QUERY_STRING);
HashMap<String, List<String>> params =
new HashMap<String, List<String>>();
for (String s: queryString.split("&")) {
String[] keyVal = s.split("=");
try {
String key = URLDecoder.decode(keyVal[0], "UTF-8");
String val = URLDecoder.decode(keyVal[1], "UTF-8");
if (params.get(key) == null) {
ArrayList<String> list = new ArrayList<String>();
list.add(val);
params.put(key,list);
}
else {
ArrayList<String> list =
(ArrayList<String>)params.get(key);
list.add(val);
}
} catch (UnsupportedEncodingException ex) {
ex.printStackTrace();
}
}
return params;
}
Its entirely possible that the above functionality already exists in some other class but, if it does, I didn't find it in the brief search I made. The protected String getSystemInfoAsXML(List The
If we wanted to deploy the endpoint in a Java EE container then the above is all the code needed. However, for this example, we'll use the new JAX-WS public static void main(String[] args) {
Endpoint e = Endpoint.create(HTTPBinding.HTTP_BINDING,
new SystemPropertiesProvider());
e.publish("http://127.0.0.1:8084/system/info");
System.out.println("Endpoint running");
try {
Thread.sleep(30000);
} catch (InterruptedException ex) {
}
e.stop();
System.out.println("Endpoint stopped");
}
}
In the above we publish the endpoint for 30 seconds and then stop it, you could also wait for a keypress in the main thread or use some other scheme to determine when to stop the endpoint. Now, stitch together the code segments from above, compile and run the class. Fire up a browser, point it to http://127.0.0.1:8084/system/info?property=os.version&property=os.name and see what OS the Java runtime thinks the endpoint is hosted on. Here's the XSL stylesheet that converts the XML document into a HTML table: <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html><body><table border="1">
<tr>
<th>Property</th>
<th>Value</th>
</tr>
<xsl:for-each select="/properties/property">
<tr>
<td>
<xsl:value-of select="@name"/>
</td>
<td>
<xsl:value-of select="."/>
</td>
</tr>
</xsl:for-each>
</table></body></html>
</xsl:template>
</xsl:stylesheet>
Bookmark blog post: CommentsComments are listed in date ascending order (oldest first) | Post Comment
| ||
|
|