Java Web Services & Application Architectures

by Eric J. Bruno



Example 1



<SOAP-ENV:Envelope xmlns:SOAP

 ENV="http://schemas.xmlsoap.org/soap/envelope/">

    <SOAP-ENV:Header /> 

    <SOAP-ENV:Body>

        <GetData>

           <symbol>MSFT</symbol> 

           <name>Microsoft Corp</name> 

           <CIK>0000789019</CIK> 

        </GetData>

    </SOAP-ENV:Body>

</SOAP-ENV:Envelope>





Listing One



public SOAPMessage onMessage(SOAPMessage message) 

{

    try {

        // The envelope contains the message header, body, and attachments

        SOAPEnvelope env = message.getSOAPPart().getEnvelope();

        SOAPBody bdy = env.getBody();

        // Get the ticker symbol 

        Iterator iter = bdy.getChildElements();

        SOAPElement node = getNode("GetData", iter);

        if ( node != null )

        {

            iter = node.getChildElements();

            symbol = getNodeValue("symbol", iter);

        }

        // Get the quote data from Yahoo

        String data = getQuoteData( symbol );

        // ...

    } 

    catch(Exception e) {

        return null;

    }

}





Listing Two



// Create the SOAP reply message

SOAPMessage replyMsg = msgFactory.createMessage(); // part of SAAJ

SOAPEnvelope env = replyMsg.getSOAPPart().getEnvelope();

SOAPBody bdy = env.getBody();

// Add the quote data to the message

bdy.addChildElement(env.createName("symbol")).addTextNode( symbol );

bdy.addChildElement(env.createName("last")).addTextNode( last );

bdy.addChildElement(env.createName("date")).addTextNode( date );

bdy.addChildElement(env.createName("time")).addTextNode( time );

bdy.addChildElement(env.createName("change")).addTextNode( change );

bdy.addChildElement(env.createName("open")).addTextNode( open );

bdy.addChildElement(env.createName("low")).addTextNode( low );

bdy.addChildElement(env.createName("high")).addTextNode( high );

bdy.addChildElement(env.createName("volume")).addTextNode( volume );



return replyMsg;



Listing Three



// Create the SOAP reply message

SOAPMessage replyMsg = msgFactory.createMessage();

SOAPEnvelope env = replyMsg.getSOAPPart().getEnvelope();

SOAPBody bdy = env.getBody();



// Add the fundamental data to the SOAP body

 ...



// Add the financial HTML as a SOAP Attachment

AttachmentPart ap = replyMsg.createAttachmentPart(financialHTML,"text/html");



replyMsg.addAttachmentPart(ap);



return replyMsg;





Listing Four



// Create a SOAP request message

MessageFactory msgFactory = MessageFactory.newInstance();

SOAPMessage soapMsg = msgFactory.createMessage();

SOAPEnvelope envelope = soapMsg.getSOAPPart().getEnvelope();

SOAPBody bdy = env.getBody();



// Add the request data to the SOAP body

SOAPBodyElement bdyElem = bdy.addBodyElement(

        envelope.createName("GetData"));

bdyElem.addChildElement(envelope.createName("symbol")).addTextNode( symbol );

bdyElem.addChildElement(envelope.createName("name")).addTextNode(companyName);

bdyElem.addChildElement(envelope.createName("CIK")).addTextNode( companyCIK );



SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();

con = scf.createConnection();



// Send the request to the Quote Service

URL urlEndpoint = new URL( "http://localhost:8080/quotes" );

SOAPMessage quoteReply = con.call( soapMsg, urlEndpoint );



// Send the request to the Fundamentals Service

urlEndpoint = new URL( "http://localhost:8080/fundamentals" );

SOAPMessage fundReply = con.call( soapMsg, urlEndpoint );





Listing Five



<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app

    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"

    "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

<web-app>

  <display-name>Financial Portal</display-name>

  <description>

    Displays company quote and financial data

  </description>

    <servlet>

        <servlet-name>

            fundservlet

        </servlet-name>

        <servlet-class>

            com.fundamentals.FundServlet

        </servlet-class>

    <load-on-startup>1</load-on-startup>

    </servlet>



    <servlet>

        <servlet-name>

            quoteservlet

        </servlet-name>

        <servlet-class>

            com.quotes.QuoteServlet

        </servlet-class>

    <load-on-startup>2</load-on-startup>

    </servlet>



    <servlet>

        <servlet-name>

            FinancialServlet

        </servlet-name>

        <servlet-class>

            com.financialportal.FinancialServlet

        </servlet-class>

    <load-on-startup>3</load-on-startup>

    </servlet>



    <servlet-mapping>

        <servlet-name>

            fundservlet

        </servlet-name>

        <url-pattern>

            /fundamentals

        </url-pattern>

    </servlet-mapping>



    <servlet-mapping>

        <servlet-name>

            quoteservlet

        </servlet-name>

        <url-pattern>

            /quotes

        </url-pattern>

    </servlet-mapping>



    <servlet-mapping>

        <servlet-name>

            FinancialServlet

        </servlet-name>

        <url-pattern>

            /financialportal

        </url-pattern>

    </servlet-mapping>



</web-app>





Listing Six



<Context 

  path="/financialportal"

  docBase="c:/dev/FinancialWebApp/FinancialPortal/FinancialPortal.war"

  debug="1">

  <Logger className="org.apache.catalina.logger.FileLogger"

          prefix="finance_log." 

          suffix=".txt"

          timestamp="true"/>

</Context>













4



