6.8.1 Highlights
6.8.2 Best Practices and PitfallsBest Practices
Pitfalls
6.8.3 ChallengeAn international investment bank, which is a key member of FXAll trading portal, is providing a private labeling online Foreign Exchange service for some small securities firms in Asia. The small securities firms do not have presence in Asia. By adapting the white labeling service, these securities firms can provide online FX trading services to their local customers using the international investment bank's infrastructure and delivery channels, but under the label of their own brands. The international investment bank has spent six months integrating with FXAll. The bank's IT management indicates to the securities firms that the white labeling integration service for the next securities firms should be three to five months. The international investment bank is traditionally a legacy mainframe shop, with limited integration experience with Unix and external customers. Its FX trading platform is an off-the-shelf package implemented on a Unix platform using a proprietary publish-subscribe message-oriented middleware. Assuming most small-size securities firms are not sophisticated in technology and that they run either a Unix-based or a Windows-based application platform, what integration technology (EAI or Web Serv-ices) would you recommend to the international bank? Hint: Use one of the Broker Patterns. 6.8.4 Paper and PencilBackgroundebXML messaging is often used as the underlying technology for business process collaboration and reliable business transactions. This is particularly important to Business-to-Business integration. The following exercise will illustrate how to use JAXM to write simple ebXML messaging to place a Foreign Exchange option order. ObjectiveTo use the ebXML message provider using JAXM to send an XML message. Exercise
Figure 6-33 SendingServlet.java Sends an XML Message in ebXML Using JAXMpackage myRemote.sender; import java.net.*; import java.io.*; import java.util.*; import javax.servlet.http.*; import javax.servlet.*; import javax.xml.messaging.*; import javax.xml.soap.*; import javax.activation.*; import com.sun.xml.messaging.jaxm.ebxml.*; import org.apache.commons.logging.*; public class SendingServlet extends HttpServlet { private static Log logger = LogFactory.getLog("Samples/myRemote"); private String from ="http://www.wombats.com/remote/sender"; private String to = "http://www.wombats.com/remote/sender"; private String data = "http://127.0.0.1:8080/myRemote/index.html"; private ProviderConnectionFactory pcf; private ProviderConnection pc; private MessageFactory mf = null; private static final String providerURI = "http://java.sun.com/xml/jaxm/provider"; public void init(ServletConfig servletConfig) throws ServletException { super.init(servletConfig); try { pcf = ProviderConnectionFactory.newInstance(); pc = pcf.createConnection(); } catch(Exception e) { logger.error("Unable to open connection to the provider", e); } InputStream in = servletConfig.getServletContext(). getResourceAsStream("/WEB-INF/address.properties"); if (in != null) { Properties props = new Properties(); try { props.load(in); String from = props.getProperty("from"); String to = props.getProperty("to"); String data = props.getProperty("data"); if (from != null) this.from = from; if (to != null) this.to = to; if (data != null) this.data = data; } catch (IOException ex) { // Ignore } } } public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException { try { // Create a message factory. if (mf == null) { ProviderMetaData metaData = pc.getMetaData(); String[] supportedProfiles = metaData.getSupportedProfiles(); String profile = null; for(int i=0; i < supportedProfiles.length; i++) { if(supportedProfiles[i].equals("ebxml")) { profile = supportedProfiles[i]; break; } } mf = pc.createMessageFactory(profile); } // Create a message from the message factory. EbXMLMessageImpl ebxmlMsg = (EbXMLMessageImpl)mf.createMessage(); ebxmlMsg.setSender(new Party(from)); ebxmlMsg.setReceiver(new Party(to)); Service service = new Service("FXOrderProcessing"); ebxmlMsg.setRefToMessageId("20001209-133003-28572@example.com"); ebxmlMsg.setCPAId("http://www.nextfrontiers.com/cpas/ourcpawithyou.xml"); ebxmlMsg.setConversationId("20001209-133003-28572"); ebxmlMsg.setService(service); ebxmlMsg.setAction("NewFXSpotOrder"); Manifest manifest = new Manifest("manifest", "1.0"); Reference ref = new Reference("pay01", "cid:pay01", "http://regrep.org/gci/fxOrder"); Schema schema = new Schema("http://regrep.org/gci/fpML.xsd", "1.0"); ref.setSchema(schema); Description desc = new Description("en-us"); desc.setText("FX Spot Rate Order for Next Frontiers Ltd."); ref.setDescription(desc); manifest.addReference(ref); ebxmlMsg.setManifest(manifest); SOAPPart soapPart = ebxmlMsg.getSOAPPart(); SOAPEnvelope envelope = soapPart.getEnvelope(); Name name = envelope.createName("trade" , "fx", "http://www.nextfrontiers.com/fxOption/"); SOAPBody body = envelope.getBody(); SOAPBodyElement trade = body.addBodyElement(name); SOAPHeader header = envelope.getHeader(); SOAPHeaderElement headerElement = header.addHeaderElement(name); // create tradeHeader Name childName = envelope.createName("tradeHeader"); SOAPElement tradeHeader = trade.addChildElement(childName); childName = envelope.createName("partyTradeIdentifier"); SOAPElement partyTradeIdentifier = tradeHeader.addChildElement(childName); childName = envelope.createName("partyReference"); SOAPElement partyReference = partyTradeIdentifier.addChildElement(childName); childName = envelope.createName("href"); partyReference.addAttribute(childName, "#XYZ"); Name childName2 = envelope.createName("partyTradeIdentifier"); SOAPElement partyTradeIdentifier2 = tradeHeader.addChildElement(childName2); childName2 = envelope.createName("partyReference"); SOAPElement partyReference2 = partyTradeIdentifier2.addChildElement(childName2); childName2 = envelope.createName("href"); partyReference2.addAttribute(childName2, "#ABC"); // create fxSimpleOption Name childName3 = envelope.createName("fxSimpleOption"); SOAPElement fxSimpleOption = trade.addChildElement(childName3); childName3 = envelope.createName("productType"); SOAPElement productType = fxSimpleOption.addChildElement(childName3); productType.addTextNode("Nondeliverable options"); childName3 = envelope.createName("buyerPartyReference"); SOAPElement buyerPartyReference = fxSimpleOption.addChildElement(childName3); childName3 = envelope.createName("href"); buyerPartyReference.addAttribute(childName3, "#XYZ"); childName3 = envelope.createName("sellerPartyReference"); SOAPElement sellerPartyReference = fxSimpleOption.addChildElement(childName3); childName3 = envelope.createName("href"); sellerPartyReference.addAttribute(childName3, "#ABC"); childName3 = envelope.createName("expiryDateTime"); SOAPElement expiryDateTime = fxSimpleOption.addChildElement(childName3); childName3 = envelope.createName("expiryDate"); SOAPElement expiryDate = expiryDateTime.addChildElement(childName3); expiryDate.addTextNode("2002-04-09"); childName3 = envelope.createName("hourMinuteTime"); SOAPElement hourMinuteTime = expiryDateTime.addChildElement(childName3); hourMinuteTime.addTextNode("1000"); childName3 = envelope.createName("businessCenter"); SOAPElement businessCenter = expiryDateTime.addChildElement(childName3); businessCenter.addTextNode("USNY"); childName3 = envelope.createName("exerciseStyle"); SOAPElement exerciseStyle = fxSimpleOption.addChildElement(childName3); exerciseStyle.addTextNode("European"); // create party information Name childName4 = envelope.createName("party"); SOAPElement party = trade.addChildElement(childName4); childName4 = envelope.createName("id"); party.addAttribute(childName4, "XYZ"); childName4 = envelope.createName("partyId"); SOAPElement partyId = party.addChildElement(childName4); partyId.addTextNode("CHASUS33"); childName4 = envelope.createName("partyName"); SOAPElement partyName = party.addChildElement(childName4); partyName.addTextNode("XYZ BUYER BANK"); Name childName5 = envelope.createName("party"); SOAPElement party2 = trade.addChildElement(childName5); childName5 = envelope.createName("id"); party2.addAttribute(childName5, "ABCN"); childName5 = envelope.createName("partyId"); SOAPElement partyId2 = party2.addChildElement(childName5); partyId2.addTextNode("ABCANL2A"); childName5 = envelope.createName("partyName"); SOAPElement partyName2 = party2.addChildElement(childName5); partyName2.addTextNode("ABC Seller Bank"); URL url = new URL(data); AttachmentPart ap = ebxmlMsg.createAttachmentPart(new DataHandler(url)); ap.setContentType("text/html"); // Add the attachment part to the message. // You can attach an EDI (UN/EDIFACT or ANSI X12) message, image or digital signature ebxmlMsg.addAttachmentPart(ap); System.err.println("Sending FX Spot Order in fpML to : "+ebxmlMsg.getTo()); System.err.println("Sent message is logged in \"sent.msg\""); FileOutputStream sentFile = new FileOutputStream("sent.msg"); ebxmlMsg.writeTo(sentFile); sentFile.close(); pc.send(ebxmlMsg); String retval = "<html> <H4> FX Spot Rate Order delivered to Broker Firm.</H4></html>"; OutputStream os = resp.getOutputStream(); os.write(retval.getBytes()); os.flush(); os.close(); } catch(Throwable e) { e.printStackTrace(); logger.error("Error in constructing or sending message " +e.getMessage()); } } } The recipient client ReceivingServlet.java creates a connection to the Message Factory class using an ebXML implementation. The underlying messaging is still using SOAP messaging via JAXM. The source code is shown in Figure 6-34: Figure 6-34 ReceivingServlet.java Receives an XML Message Using JAXMpackage myRemote.receiver; import javax.xml.messaging.*; import javax.xml.soap.*; import javax.servlet.*; import javax.servlet.http.*; import javax.xml.transform.*; import java.io.*; import java.util.Enumeration; import org.apache.commons.logging.*; import com.sun.xml.messaging.jaxm.ebxml.*; public class ReceivingServlet extends JAXMServlet implements OnewayListener { private static Log logger = LogFactory.getLog("Samples/myRemote"); private ProviderConnectionFactory pcf; private ProviderConnection pc; private static final String providerURI = "http://java.sun.com/xml/jaxm/provider"; //private MessageFactory messageFactory; public void init(ServletConfig servletConfig) throws ServletException { super.init(servletConfig); try { pcf = ProviderConnectionFactory.newInstance(); pc = pcf.createConnection(); setMessageFactory(new EbXMLMessageFactoryImpl()); }catch (Exception e) { e.printStackTrace(); throw new ServletException("Couldn't initialize Receiving servlet " + e.getMessage()); } } public void onMessage(SOAPMessage message) { System.out.println("On message called in receiving servlet"); try { System.out.println("Here's the message: "); message.saveChanges(); message.writeTo(System.out); } catch(Exception e) { logger.error("Error in processing or replying to a message", e); } } }
The war file created is placed under D:\opt\temp\war\myRemote.war . This needs to be copied to the webapps directory of your Web Container (for example, D:\Dev\WSDP\webapps). Figure 6-36 Deploy myRemote Servlets D:\opt\myRemote> ant war Buildfile: build.xml set.available: check.jaxm: check.saaj: check.appname: check.servlet: checks: build.dir.webapps: build.dir.local: init: [echo] ---- Building myRemote to /opt/temp prepare.build: prepare: compile: main: war: [jar] Building jar: D:\opt\temp\war\myRemote.war BUILD SUCCESSFUL Total time: 2 seconds
6.8.5 References
|