Methods Summary |
---|
public void | doGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp)Handles a request from a client to send a message.
// Only allow gets on the "request" handler
String path = req.getServletPath();
if (!path.equals("/request")) {
resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "Cannot use get on this URL");
return;
}
// Build and send a message
boolean sent = sendMessage();
// Wait until the echo service has replied,
// for a maximum of 30 seconds
if (sent) {
synchronized (this) {
replyMessage = null;
try {
if (replyMessage == null) {
wait(30000L);
}
} catch (InterruptedException ex) {
}
}
}
// Now send the reply to the caller.
try {
if (replyMessage == null) {
resp.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, "No reply received");
return;
}
OutputStream os = resp.getOutputStream();
resp.setContentType("text/html");
resp.setStatus(HttpServletResponse.SC_OK);
os.write("<html><P><XMP>".getBytes());
replyMessage.writeTo(os);
os.write("</XMP></html>".getBytes());
os.flush();
} catch (Exception ex) {
log("Exception in doGet", ex);
resp.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, "Exception: " + ex.getMessage());
}
replyMessage = null;
|
public void | doPost(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp)Handles a POST either from a client or as a
callback from the provider.
// Only allow posts to the "message" handler
String path = req.getServletPath();
if (path.equals("/message")) {
// This is allowed
super.doPost(req, resp);
} else {
// Cannot post to the request path
resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "Cannot post to this URL");
}
|
public void | init(javax.servlet.ServletConfig servletConfig)Initialize by installing the appropriate MessageFactory
super.init(servletConfig);
try {
// Create the connection to the provider
conn = ProviderConnectionFactory.newInstance().createConnection();
soapFactory = SOAPFactory.newInstance();
// Check that the soaprp profile is supported
ProviderMetaData metaData = conn.getMetaData();
String[] profiles = metaData.getSupportedProfiles();
boolean found = false;
for (int i = 0; i < profiles.length; i++) {
if (profiles[i].equals("soaprp")) {
found = true;
break;
}
}
if (!found) {
// No SOAPRP profile
log("soaprp profile not supported");
throw new ServletException("soaprp profile not supported");
}
// Get the message factory and build the message
msgFactory = conn.createMessageFactory("soaprp");
// Install the factory to use when receiving messages
setMessageFactory(msgFactory);
} catch (Exception e) {
e.printStackTrace();
throw new ServletException(
"Failed to initialize SOAPRP sender servlet " + e.getMessage());
}
|
public void | onMessage(javax.xml.soap.SOAPMessage message)Handles a received SOAP message - this is the
asynchronous reply from the echo service.
try {
synchronized (this) {
// Save the message for the benefit
// of the client.
replyMessage = message;
// Wake up the client
notify();
}
} catch (Exception ex) {
log("Exception", ex);
}
|
private boolean | sendMessage()Builds a message and sends it to the service
try {
// Build the SOAP-RP essage
SOAPRPMessageImpl message = (SOAPRPMessageImpl)msgFactory.createMessage();
message.setTo(new Endpoint("urn:SOAPRPEcho"));
message.setFrom(new Endpoint("urn:SOAPRPSender"));
SOAPElement element =
message.getSOAPPart().getEnvelope().getBody().addBodyElement(
soapFactory.createName("Sent", "tns", "urn:SOAPRPSender"));
element.addTextNode("This is the content");
// Send the message to the echo service
conn.send(message);
// Return indicating that the message was sent.
return true;
} catch (Exception ex) {
log("Failed when sending message", ex);
}
return false;
|