Sending And Receiving Xml In Java Application

  • Uploaded by: Mohan
  • 0
  • 0
  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Sending And Receiving Xml In Java Application as PDF for free.

More details

  • Words: 658
  • Pages: 39
Sending and receiving XML in a Java application Sean C. Sullivan sean 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 is a syntax for describing and structuring data Sun Microsystems <stocksymbol>SUNW

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

Data exchange

Application A

Application B

XML request 97210 <destination>12208 <weight>35 <service>OVERNIGHT

XML response 68.00

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

HTTP protocol

request HTTP client

HTTP server

HTTP protocol

request HTTP client

HTTP server response

HTTP protocol methods • GET • POST • HEAD • …

HTTP headers • Content-Type • Accept

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

Jakarta Commons HTTPClient

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”);

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

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

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

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

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

Java Message Service • messaging API for J2EE platform • Point-to-point – JMS Queue

• Publish-Subscribe – JMS Topic

• “at most once” delivery mode • transactional

JMS message queue

Message Producer

Message Queue 3 2 1

Message Consumer

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(…);

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

Receiving XML via JMS Choose one: •

javax.jms.MessageConsumer



Message Driven Bean (MDB)

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

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

JavaMail

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

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

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); // …

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

FTP with Jakarta Commons Net

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

Example: XML via FTP

ftp.setFileType(FTP.BINARY_FILE_TYPE); ftp.enterLocalPassiveMode(); ftp.storeFile(“foo.xml”, xmlInputStream); ftp.logout(); ftp.disconnect();

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

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/

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

Related Documents

Xml Programming In Java
December 2019 13
Using Xml In Java
December 2019 24
Java And Xml
November 2019 24
Java And Xml
November 2019 14
Java Application
November 2019 10

More Documents from ""

Tcs Latest
May 2020 19
Hr Department - Fwd
May 2020 17
Titanic
May 2020 26
Drive By Wire
May 2020 15