1 / 39

Sending and receiving XML in a Java application

Sending and receiving XML in a Java application. Sean C. Sullivan sean <at> seansullivan <dot> com September 2003. Agenda. XML XML via HTTP XML via JMS XML via JavaMail XML via FTP. Agenda. XML XML via HTTP XML via JMS XML via JavaMail XML via FTP. XML. <?xml version=“1.0”>

ace
Download Presentation

Sending and receiving XML in a Java application

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Sending and receiving XMLin a Java application Sean C. Sullivan sean <at> seansullivan <dot> com September 2003

  2. Agenda • XML • XML via HTTP • XML via JMS • XML via JavaMail • XML via FTP

  3. Agenda • XML • XML via HTTP • XML via JMS • XML via JavaMail • XML via FTP

  4. XML <?xml version=“1.0”> <company> <name>Sun Microsystems</name> <stocksymbol>SUNW</stocksymbol> </company> XML is a syntax for describing and structuring data

  5. Application integration “[If you look at] where XML is really, truly being used right now, it's [for] lightweight, quick and dirty enterprise application integration. […] you could achieve remarkably acceptable results in enterprise application integration simply by binding a set of XML messages to ship back and forth.” Tim Bray, 9/23/2003, news.com interview

  6. Data exchange Application A Application B

  7. XML request <?xml version=“1.0”> <raterequest> <origin>97210</origin> <destination>12208</destination> <weight>35</weight> <service>OVERNIGHT</service> </raterequest>

  8. XML response <?xml version=“1.0”> <rateresponse> <rate>68.00</rate> </rateresponse>

  9. Agenda • XML • XML via HTTP • XML via JMS • XML via JavaMail • XML via FTP

  10. HTTP protocol request HTTP client HTTP server

  11. HTTP protocol request HTTP client HTTP server response

  12. HTTP protocol methods • GET • POST • HEAD • …

  13. HTTP headers • Content-Type • Accept

  14. MIME types for XML • text/xml • application/xml • text/xml-external-parsed-entity • application/xml-external-parsed-entity • application/xml-dtd • application/soap+xml

  15. Jakarta Commons HTTPClient

  16. Example: HTTP POST import org.apache.commons.httpclient.*; import org.apache.commons.httpclient.methods*; PostMethod post = new PostMethod( “http://www.foo.com/bar”); post.setRequestBody(strXML); post.setRequestHeader( “Content-type”, “application/xml”);

  17. Example: HTTP POST post.setRequestHeader( “Accept”, “application/xml”); HttpClient client = new HttpClient(); int result = client.executeMethod(post); // … String strXMLResponse = post.getResponseBodyAsString(); post.releaseConnection();

  18. Receiving XML using a Servlet public class XMLServlet extends javax.servlet.http.HttpServlet { protected void doPost( HttpServletRequest req, HttpServletResponse resp) { // … method implementation … } }

  19. doPost method // … String type = req.getContentType(); int contentLength = req.getContentLength(); char[] xmlData = new char[contentLength]; BufferedReader reader = httpRequest.getReader(); // …

  20. doPost (continued) do { n = reader.read(xmlData, bytesRead, xmlData.length - bytesRead); if (n > 0) { bytesRead += n; } } while ( (n != -1) && (bytesRead < contentLength) )

  21. Agenda • XML • XML via HTTP • XML via JMS • XML via JavaMail • XML via FTP

  22. Java Message Service • messaging API for J2EE platform • Point-to-point • JMS Queue • Publish-Subscribe • JMS Topic • “at most once” delivery mode • transactional

  23. 3 2 1 JMS message queue Message Producer Message Queue Message Consumer

  24. Sending XML using JMS import javax.jms.*; import javax.naming.*; Queue queue; ConnectionFactory cf; queue = /* lookup via JNDI */; cf = /* lookup via JNDI */; Connection conn = cf.createConnection(); Session session = conn.createSession(…);

  25. Sending XML using JMS MessageProducer producer; producer = session.createProducer(queue); TextMessage msg; msg = session.createTextMessage(); msg.setText(strXML); producer.send(msg); session.commit();

  26. Receiving XML via JMS Choose one: • javax.jms.MessageConsumer • Message Driven Bean (MDB)

  27. Example: Message Driven Bean public void onMessage(Message msg) { // … if (msg instanceof TextMessage) { TextMessage tm = (Message) msg; String strXML = tm.getText(); } // … }

  28. Agenda • XML • XML via HTTP • XML via JMS • XML via JavaMail • XML via FTP

  29. JavaMail

  30. Example: XML via JavaMail import javax.mail.*; import javax.mail.internet.*; Session sess = /* JNDI lookup */; MimeMessage msg = new MimeMessage(sess); msg.setFrom(from); // …

  31. Example: XML via JavaMail // … msg.setRecipients( Message.RecipientType.TO, recipients); msg.setSubject(“Hello XML”); // …

  32. Example: XML via JavaMail // … MimeBodyPart mbp = new MimeBodyPart(); mbp.setContent(strXML, “application/xml”); Multipart mp = new MimeMultipart(); mp.addBodyPart(mbp); msg.setContent(mp); Transport.send(msg); // …

  33. Agenda • XML • XML via HTTP • XML via JMS • XML via JavaMail • XML via FTP

  34. FTP with Jakarta Commons Net

  35. Example: XML via FTP import org.apache.commons.net.ftp.*; FTPClient ftp = new FTPClient(); ftp.connect(“ftp.example.com”); ftp.login(username, password);

  36. Example: XML via FTP ftp.setFileType(FTP.BINARY_FILE_TYPE); ftp.enterLocalPassiveMode(); ftp.storeFile(“foo.xml”, xmlInputStream); ftp.logout(); ftp.disconnect();

  37. Additional topics… • Sockets • SOAP • XML-RPC • Binary encodings for XML

  38. Additional resources • http://www.w3.org/XML/ • http://www.xml.com/ • http://java.sun.com/xml/ • http://xml.apache.org/ • http://ws.apache.org/axis/ • http://jakarta.apache.org/

  39. Summary Use XML for data exchange between heterogeneous applications. Sending and receiving XML is easy.

More Related