📄 bookorderservlet.java
字号:
// Fig. 11.22: BookOrderServlet.java// Class BookOrderServlet receives an ebXML message that// contains a book's ISBN and the quantity of that book to order.package com.deitel.jws1.jaxm.bookseller.receiver;// Java core packagesimport java.io.*;import java.util.*;// Java extension packagesimport javax.servlet.http.*;import javax.servlet.*;import javax.xml.messaging.*;import javax.xml.soap.*;// ebXML packagesimport com.sun.xml.messaging.jaxm.ebxml.*;// Deitel packagesimport com.deitel.jws1.services.*;public class BookOrderServlet extends JAXMServlet implements OnewayListener { private ProviderConnection sellerProvider; private MessageFactory messageFactory; // source and destination endpoints for messages private String from; // setup connection to message provider public void init( ServletConfig servletConfig ) throws ServletException { super.init( servletConfig ); // establish connection to provider try { ProviderConnectionFactory providerFactory = ProviderConnectionFactory.newInstance(); sellerProvider = providerFactory.createConnection(); // obtain URL of properties file java.net.URL endpointURL = getClass().getResource( "endpoint.properties" ); // load properties file Properties endpointProperties = new Properties(); endpointProperties.load( new FileInputStream( endpointURL.getPath() ) ); from = endpointProperties.getProperty( "from" ); // obtain supported profiles for provider ProviderMetaData metaData = sellerProvider.getMetaData(); String[] profiles = metaData.getSupportedProfiles(); // determine whether ebXML profile is supported boolean isProfileSupported = false; for ( int i = 0; i < profiles.length; i++ ) if ( profiles[ i ].equals( "ebxml" ) ) isProfileSupported = true; // use ebXML profile, if supported if ( isProfileSupported ) messageFactory = sellerProvider.createMessageFactory( "ebxml" ); else throw new ServletException( "Profile ebxml is " + "not supported." ); } // handle exception in connecting to provider catch ( JAXMException jaxmException ) { throw new ServletException( jaxmException.getMessage() + "\nUnable to connect to message provider." ); } // handle exception if unable to locate Endpoint.properties catch ( FileNotFoundException fileNotFoundException ) { throw new ServletException( fileNotFoundException.getMessage() + "\nUnable to locate endpoint.properties." ); } // handle exception in loading properties file catch ( IOException ioException ) { throw new ServletException( ioException.getMessage() + "\nUnable to load endpoint.properties." ); } } // end method init // invoked when SellerProvider sends message public void onMessage( SOAPMessage requestMessage ) { // call Book Order Web service and return result to sender try { // create response message from request message EbXMLMessageImpl responseMessage = new EbXMLMessageImpl( requestMessage ); // specify that the message should be returned to sender String to = responseMessage.getFrom().toString(); // specify sender and receiver for message responseMessage.setReceiver( new Party( to ) ); responseMessage.setSender( new Party( from ) ); // obtain ISBN and Quantity attachments from message Iterator attachments = responseMessage.getAttachments(); AttachmentPart isbnAttachment = ( AttachmentPart ) attachments.next(); AttachmentPart quantityAttachment = ( AttachmentPart ) attachments.next(); // obtain ISBN and Quantity from attachments String isbn = ( String ) isbnAttachment.getContent(); Integer quantity = new Integer( ( String ) quantityAttachment.getContent() ); // invoke Book Order Web service to place order BookOrder service = new BookOrderImpl(); Double price = new Double( service.orderBook( isbn, quantity.intValue() ) ); // store price in message attachment AttachmentPart priceAttachment = responseMessage.createAttachmentPart(); priceAttachment.setContent( price.toString(), "text/plain" ); // add price attachments to message responseMessage.addAttachmentPart( priceAttachment ); // send message back to sending provider sellerProvider.send( responseMessage ); } // handle exception in invoking Book Order Web service catch ( SOAPException soapException ) { soapException.printStackTrace(); } // handle exception in EbXMLMessageImpl creation catch ( IOException ioException ) { ioException.printStackTrace(); } } // end method onMessage} // end class BookOrderServlet
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -