Hi,
Has anyone used the JAXM from Sun yet?
I am trying to run the 'simple example' and trying to extract the contents of
the elements in the SoapBody but I am not able to do it. Please give me
some ideas. I am new to XML and Soap.
Please look at the package simple.receiver.
Basically, at the receiver endpoint, I try retrieve data from the
Message.
Thank you for your help
The codes are below:
file://receiver
section
package simple.receiver;
import javax.xml.messaging.*;
import javax.xml.soap.*; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import javax.xml.transform.Source; import javax.xml.transform.stream.StreamSource; import javax.activation.DataHandler; import java.util.*; import org.jdom.*; /** * Sample servlet that receives messages. * * @author Rajiv Mordani ([EMAIL PROTECTED]) */ public class ReceivingServlet extends JAXMServlet { // Cache a copy of the
connection.
Connection conn; public void init(ServletConfig
servletConfig) {
ServletContext ctxt = servletConfig.getServletContext(); ConnectionFactory cf = (ConnectionFactory)ctxt. getAttribute("Senders"); try { conn = cf.createConnection(); MessageFactory mf = conn.createMessageFactory(); // Need to invoke this so
the doPost knows what factory to use
// to construct the message being delivered via http post. setMessageFactory(mf); System.err.println("ReceivingServlet initd"); } catch(Exception e) { e.printStackTrace(); } } // This is the application code
for handling the message.. Once the
// message is received the application can retrieve the soap part, the // attachment part if there are any, or any other information from the // message. public Message onMessage(Message
message) {
System.err.println("On message called in receiving servlet"); System.err.println("Receiver getting message..."); Message msg = null; try { Source src = "message.getSOAPPart().getContentAs( StreamSource.FEATURE); SOAPPart sp = message.getSOAPPart(); file://orig msg = msgFactory.createMessage(); // SOAPPart sp = msg.getSOAPPart(); // System.out.println("conten location is: " + sp.getContentLocation()); SOAPEnvelope sel = sp.getSOAPEnvelope(); System.err.println(" after soapenv"); SOAPBody sb = sel.getSOAPBody(); sb. System.err.println(" after soapenv1"); Document doc = sb.getDocument(); doc.getMixedContent(); System.out.println("root ele: " + rootEl.getName()); Element parentEl = sb.getParent(); String bodyName = parentEl.getName(); System.out.println("body element is: " + bodyName); Namespace namespace = parentEl.getNamespace(); System.out.println("name space is: " + namespace.getURI()); sp.setContent(src); } catch(Exception e) { e.printStackTrace(); } return msg; } } +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
file://sender
section
package simple.sender;
import javax.servlet.http.*;
import javax.servlet.*; import javax.xml.messaging.*; import javax.xml.soap.*; import org.jdom.*; import javax.activation.DataHandler; import java.net.*; import java.io.*; /**
* Sample servlet that is used for sending the message. * * @author Rajiv Mordani ([EMAIL PROTECTED]) */ public class SendingServlet extends HttpServlet { // Cache a copy of the
connection to send messages.
private Connection con; public void init(ServletConfig
servletConfig) throws ServletException {
// We are using the ServletContext as the naming service to look up // the ConnectionFactory. Ideally should be using JNDI. This will // be fixed soon. ServletContext ctxt = servletConfig.getServletContext(); try { file://Look up a connection factory for the client to use. ConnectionFactory cf = (ConnectionFactory)ctxt.getAttribute( "Senders"); file://Create a connection from the connection factory looked up. con = cf.createConnection(); } catch(Exception e) { e.printStackTrace(); } } public void
doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException {
try { // Create a message factory from the connection obtained // earlier. MessageFactory mf = con.createMessageFactory(); // Create a message from
the message factory.
Message msg = mf.createMessage(); // Message creation takes
care of creating the SOAPPart - a
// required part of the message as per the SOAP 1.1 // specification. SOAPPart sp = msg.getSOAPPart(); // Retrieve the envelope
from the soap part to start building
// the soap message. SOAPEnvelope envelope = sp.getSOAPEnvelope(); // Create a soap header
from the
envelope.
SOAPHeader hdr = envelope.createSOAPHeader(); // Create a soap body from
the
envelope.
SOAPBody bdy = envelope.createSOAPBody(); // Add a soap header
element to the
header.
SOAPHeaderElement transaction = hdr.createSOAPHeaderElement("Transaction", Namespace.getNamespace("t", "some-URI")); transaction.setMustUnderstand(true); transaction.addContent("5"); // Add a soap body element to the soap body SOAPBodyElement gltp = bdy.createSOAPBodyElement("GetLastTradePrice", Namespace.getNamespace("m", "some-other-URI"));
gltp.createSOAPElement("symbol", Namespace.getNamespace("m",
"some-other-URI")).addContent("DEF");
// Set the soap header and
soap body on the
envelope.
envelope.setSOAPHeader(hdr); envelope.setSOAPBody(bdy);
System.err.println("Created SOAPPart");
// Want to set an
attachment from the following
url.
URL url = new URL("http://localhost:8080/simple/web.xml");
AttachmentPart
ap
= msg.createAttachmentPart(new DataHandler(url));
ap.setContentType("text/xml");
// Add the attachment part
to the
message.
msg.addAttachmentPart(ap); System.err.println("Added
attachment");
// Create an endpoint for
the recipient of the message.
Endpoint endPoint = new Endpoint("http://192.168.1.29:8080"); System.err.println("Sent
message by Hoai");
// Send the message to the endpoint using the connection. con.send(msg, endPoint); file://end point is defined in web-in\provider.xml // Send a response to the
client.. Need to check the status if
// the message was actually sent. Not done that here as yet. String retval = "<html><H4>Sent Message by Hoai</H4></html>"; OutputStream os = resp.getOutputStream(); os.write(retval.getBytes()); os.flush(); os.close(); }catch(Exception e) { e.printStackTrace(); } } } |
- problem running the StockQuote example Danh Hoai
- Re: problem running the StockQuote example Yogesh Patange
- Danh Hoai