sending and receiving xml in java application

Upload: mohan

Post on 30-May-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 Sending and receiving xml in java application

    1/39

    Sending and receiving XML

    in a Java application

    Sean C. Sullivansean seansullivan comSeptember 2003

  • 8/14/2019 Sending and receiving xml in java application

    2/39

    Agenda

    XML

    XML via HTTP

    XML via JMS XML via JavaMail

    XML via FTP

  • 8/14/2019 Sending and receiving xml in java application

    3/39

    Agenda

    XML

    XML via HTTP

    XML via JMS XML via JavaMail

    XML via FTP

  • 8/14/2019 Sending and receiving xml in java application

    4/39

    XML

    Sun Microsystems

    SUNW

    XML is a syntax for describing and structuring data

  • 8/14/2019 Sending and receiving xml in java application

    5/39

    Application integration

    [If you look at] where XML is really, trulybeing used right now, it's [for] lightweight,quick and dirty enterprise application

    integration. [] you could achieveremarkably acceptable results in enterpriseapplication integration simply by binding aset of XML messages to ship back and

    forth.

    Tim Bray, 9/23/2003, news.com interview

  • 8/14/2019 Sending and receiving xml in java application

    6/39

    Data exchange

    Application BApplication A

  • 8/14/2019 Sending and receiving xml in java application

    7/39

    XML request

    97210

    12208

    35

    OVERNIGHT

  • 8/14/2019 Sending and receiving xml in java application

    8/39

    XML response

    68.00

  • 8/14/2019 Sending and receiving xml in java application

    9/39

    Agenda

    XML

    XML via HTTP

    XML via JMS XML via JavaMail

    XML via FTP

  • 8/14/2019 Sending and receiving xml in java application

    10/39

    HTTP protocol

    HTTP

    server

    HTTP

    client

    request

  • 8/14/2019 Sending and receiving xml in java application

    11/39

    HTTP protocol

    HTTP

    server

    HTTP

    client

    request

    response

  • 8/14/2019 Sending and receiving xml in java application

    12/39

    HTTP protocol methods

    GET

    POST

    HEAD

  • 8/14/2019 Sending and receiving xml in java application

    13/39

    HTTP headers

    Content-Type

    Accept

  • 8/14/2019 Sending and receiving xml in java application

    14/39

    MIME types for XML

    text/xml

    application/xml

    text/xml-external-parsed-entity application/xml-external-parsed-entity

    application/xml-dtd

    application/soap+xml

  • 8/14/2019 Sending and receiving xml in java application

    15/39

    Jakarta Commons HTTPClient

  • 8/14/2019 Sending and receiving xml in java application

    16/39

    Example: HTTP POST

    import org.apache.commons.httpclient.*;

    importorg.apache.commons.httpclient.methods*;

    PostMethod post = new PostMethod(

    http://www.foo.com/bar);

    post.setRequestBody(strXML);

    post.setRequestHeader(

    Content-type,

    application/xml);

  • 8/14/2019 Sending and receiving xml in java application

    17/39

    Example: HTTP POST

    post.setRequestHeader(

    Accept,

    application/xml);

    HttpClient client = new HttpClient();

    int result = client.executeMethod(post);

    //

    String strXMLResponse =post.getResponseBodyAsString();

    post.releaseConnection();

  • 8/14/2019 Sending and receiving xml in java application

    18/39

    Receiving XML using a Servlet

    public class XMLServlet extends

    javax.servlet.http.HttpServlet

    {

    protected void doPost(

    HttpServletRequest req,

    HttpServletResponse resp)

    {// method implementation

    }

    }

  • 8/14/2019 Sending and receiving xml in java application

    19/39

    doPost method

    //

    String type = req.getContentType();

    int contentLength =

    req.getContentLength();

    char[] xmlData = newchar[contentLength];

    BufferedReader reader =

    httpRequest.getReader();

    //

  • 8/14/2019 Sending and receiving xml in java application

    20/39

    doPost (continued)

    do

    {

    n = reader.read(xmlData, bytesRead,

    xmlData.length - bytesRead);if (n > 0)

    {

    bytesRead += n;

    }

    } while ( (n != -1)

    && (bytesRead < contentLength) )

  • 8/14/2019 Sending and receiving xml in java application

    21/39

    Agenda

    XML

    XML via HTTP

    XML via JMS XML via JavaMail

    XML via FTP

  • 8/14/2019 Sending and receiving xml in java application

    22/39

    Java Message Service

    messaging API for J2EE platform

    Point-to-point

    JMS Queue

    Publish-Subscribe

    JMS Topic

    at most once delivery mode

    transactional

  • 8/14/2019 Sending and receiving xml in java application

    23/39

    JMS message queue

    Message

    Producer

    Message Queue

    123Message

    Consumer

  • 8/14/2019 Sending and receiving xml in java application

    24/39

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

  • 8/14/2019 Sending and receiving xml in java application

    25/39

  • 8/14/2019 Sending and receiving xml in java application

    26/39

    Receiving XML via JMS

    Choose one:

    javax.jms.MessageConsumer

    Message Driven Bean (MDB)

  • 8/14/2019 Sending and receiving xml in java application

    27/39

    Example: Message Driven Bean

    public void onMessage(Message msg)

    {

    //

    if (msg instanceof TextMessage)

    {

    TextMessage tm = (Message) msg;

    String strXML = tm.getText();}

    //

    }

  • 8/14/2019 Sending and receiving xml in java application

    28/39

  • 8/14/2019 Sending and receiving xml in java application

    29/39

  • 8/14/2019 Sending and receiving xml in java application

    30/39

    Example: XML via JavaMail

    import javax.mail.*;

    import javax.mail.internet.*;

    Session sess = /* JNDI lookup */;MimeMessage msg = newMimeMessage(sess);

    msg.setFrom(from);//

  • 8/14/2019 Sending and receiving xml in java application

    31/39

    Example: XML via JavaMail

    //

    msg.setRecipients(

    Message.RecipientType.TO,recipients);

    msg.setSubject(Hello XML);

    //

  • 8/14/2019 Sending and receiving xml in java application

    32/39

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

    //

  • 8/14/2019 Sending and receiving xml in java application

    33/39

    Agenda

    XML

    XML via HTTP

    XML via JMS XML via JavaMail

    XML via FTP

  • 8/14/2019 Sending and receiving xml in java application

    34/39

    FTP with Jakarta Commons Net

  • 8/14/2019 Sending and receiving xml in java application

    35/39

    Example: XML via FTP

    import org.apache.commons.net.ftp.*;

    FTPClient ftp = new FTPClient();

    ftp.connect(ftp.example.com);

    ftp.login(username, password);

  • 8/14/2019 Sending and receiving xml in java application

    36/39

    Example: XML via FTP

    ftp.setFileType(FTP.BINARY_FILE_TYPE);

    ftp.enterLocalPassiveMode();

    ftp.storeFile(foo.xml,

    xmlInputStream);

    ftp.logout();

    ftp.disconnect();

  • 8/14/2019 Sending and receiving xml in java application

    37/39

    Additional topics

    Sockets

    SOAP

    XML-RPC Binary encodings for XML

  • 8/14/2019 Sending and receiving xml in java application

    38/39

    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/

  • 8/14/2019 Sending and receiving xml in java application

    39/39