📄 cb4.html
字号:
<div class="pPreformattedRelative"><pre class="pPreformattedRelative">URL endpoint = new URL(url);SOAPMessage reply = con.call(msg, endpoint);con.close();<a name="wp68087"> </a></pre></div><a name="wp68602"> </a><p class="pBody">Because we map the given endpoint to <code class="cCode">ConfirmationServlet</code>, the J2EE server executes that servlet (discussed in <a href="CB4.html#wp65933">Returning the Order Confirmation</a>) to create and return the <code class="cCode">SOAPMessage</code> object <code class="cCode">reply</code>.</p><a name="wp68064"> </a><h5 class="pHeading4">Retrieving the Order Confirmation</h5><a name="wp68096"> </a><p class="pBody">The rest of the <code class="cCode">placeOrder</code> method retrieves the information returned in <code class="cCode">reply</code>. The client knows what elements are in it because they are specified in <code class="cCode">confirm.dtd</code>. After accessing the <code class="cCode">SOAPBody</code> object, the code retrieves the <code class="cCode">confirmation</code> element and gets the text of the <code class="cCode">orderID</code> and <code class="cCode">ship-date</code> elements. Finally, it constructs and returns a <code class="cCode">ConfirmationBean</code> with this information.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">SOAPBody sBody = reply.getSOAPPart().getEnvelope().getBody();Iterator bodyIt = sBody.getChildElements();SOAPBodyElement sbEl = (SOAPBodyElement)bodyIt.next();Iterator bodyIt2 = sbEl.getChildElements();SOAPElement ID = (SOAPElement)bodyIt2.next();String id = ID.getValue();SOAPElement sDate = (SOAPElement)bodyIt2.next();String shippingDate = sDate.getValue();SimpleDateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");Date date = df.parse(shippingDate);Calendar cal = new GregorianCalendar();cal.setTime(date);cb = new ConfirmationBean(id, cal); <a name="wp72489"> </a></pre></div><a name="wp67322"> </a><h3 class="pHeading2">SAAJ Service</h3><a name="wp65894"> </a><p class="pBody">The SAAJ coffee distributor, the SAAJ server in this scenario, provides the response part of the request-response paradigm. When SAAJ messaging is being used, the server code is a servlet. The core part of each servlet is made up of three <code class="cCode">javax.servlet.HttpServlet</code> methods: <code class="cCode">init</code>, <code class="cCode">doPost</code>, and <code class="cCode">onMessage</code>. The <code class="cCode">init</code> and <code class="cCode">doPost</code> methods set up the response message, and the <code class="cCode">onMessage</code> method gives the message its content.</p><a name="wp65908"> </a><h4 class="pHeading3">Returning the Price List</h4><a name="wp67305"> </a><p class="pBody">This section takes you through the servlet <code class="cCode"><a href="../examples/cb/saaj/src/com/sun/cb/PriceListServlet.java" target="_blank">PriceListServlet</a></code>. This servlet creates the message with the current price list that is returned to the method <code class="cCode">call</code>, invoked in <code class="cCode">PriceListRequest</code>. </p><a name="wp68357"> </a><p class="pBody">Any servlet extends a <code class="cCode">javax.servlet</code> class. Being part of a Web application, this servlet extends <code class="cCode">HttpServlet</code>. It first creates a static <code class="cCode">MessageFactory</code> object that will be used later to create the <code class="cCode">SOAPMessage</code> object that is returned.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public class PriceListServlet extends HttpServlet { static MessageFactory fac = null; static { try { fac = MessageFactory.newInstance(); } catch (Exception ex) { ex.printStackTrace(); } };<a name="wp68254"> </a></pre></div><a name="wp68618"> </a><p class="pBody">Every servlet has an <code class="cCode">init</code> method. This <code class="cCode">init</code> method initializes the servlet with the configuration information that the J2EE server passed to it.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public void init(ServletConfig servletConfig) throws ServletException { super.init(servletConfig);}<a name="wp68377"> </a></pre></div><a name="wp68626"> </a><p class="pBody">The next method defined in <code class="cCode">PriceListServlet</code> is <code class="cCode">doPost</code>, which does the real work of the servlet by calling the <code class="cCode">onMessage</code> method. (The <code class="cCode">onMessage</code> method is discussed later in this section.) The J2EE server passes the <code class="cCode">doPost</code> method two arguments. The first argument, the <code class="cCode">HttpServletRequest</code> object <code class="cCode">req</code>, holds the content of the message sent in <code class="cCode">PriceListRequest</code>. The <code class="cCode">doPost</code> method gets the content from <code class="cCode">req</code> and puts it in the <code class="cCode">SOAPMessage</code> object <code class="cCode">msg</code> so that it can pass it to the <code class="cCode">onMessage</code> method. The second argument, the <code class="cCode">HttpServletResponse</code> object <code class="cCode">resp</code>, will hold the message generated by executing the method <code class="cCode">onMessage</code>. </p><a name="wp68651"> </a><p class="pBody">In the following code fragment, <code class="cCode">doPost</code> calls the methods <code class="cCode">getHeaders</code> and <code class="cCode">putHeaders</code>, defined immediately after <code class="cCode">doPost</code>, to read and write the headers in <code class="cCode">req</code>. It then gets the content of <code class="cCode">req</code> as a stream and passes the headers and the input stream to the method <code class="cCode">MessageFactory.createMessage</code>. The result is that the <code class="cCode">SOAPMessage</code> object <code class="cCode">msg</code> contains the request for a price list. Note that in this case, <code class="cCode">msg</code> does not have any headers because the message sent in <code class="cCode">PriceListRequest</code> did not have any headers.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { try { // Get all the headers from the HTTP request MimeHeaders headers = getHeaders(req); // Get the body of the HTTP request InputStream is = req.getInputStream(); // Now internalize the contents of the HTTP request // and create a SOAPMessage SOAPMessage msg = fac.createMessage(headers, is);<a name="wp68627"> </a></pre></div><a name="wp68647"> </a><p class="pBody">Next, the code declares the <code class="cCode">SOAPMessage</code> object <code class="cCode">reply</code> and populates it by calling the method <code class="cCode">onMessage</code>. </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"> SOAPMessage reply = null; reply = onMessage(msg);<a name="wp68648"> </a></pre></div><a name="wp68871"> </a><p class="pBody">If <code class="cCode">reply</code> has anything in it, its contents are saved, the status of <code class="cCode">resp</code> is set to OK, and the headers and content of <code class="cCode">reply</code> are written to <code class="cCode">resp</code>. If <code class="cCode">reply</code> is empty, the status of <code class="cCode">resp</code> is set to indicate that there is no content.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"> if (reply != null) { /* * Need to call saveChanges because we're * going to use the MimeHeaders to set HTTP * response information. These MimeHeaders * are generated as part of the save. */ if (reply.saveRequired()) { reply.saveChanges(); } resp.setStatus(HttpServletResponse.SC_OK); putHeaders(reply.getMimeHeaders(), resp); // Write out the message on the response stream OutputStream os = resp.getOutputStream(); reply.writeTo(os); os.flush(); } else { resp.setStatus( HttpServletResponse.SC_NO_CONTENT); } } catch (Exception ex) { throw new ServletException( "SAAJ POST failed: " + ex.getMessage()); } }<a name="wp73403"> </a></pre></div><a name="wp69755"> </a><p class="pBody">The methods <code class="cCode">getHeaders</code> and <code class="cCode">putHeaders</code> are not standard methods in a servlet the way <code class="cCode">init</code>, <code class="cCode">doPost</code>, and <code class="cCode">onMessage</code> are. The method <code class="cCode">doPost</code> calls <code class="cCode">getHeaders</code> and passes it the <code class="cCode">HttpServletRequest</code> object <code class="cCode">req</code> that the J2EE server passed to it. It returns a <code class="cCode">MimeHeaders</code> object populated with the headers from <code class="cCode">req</code>.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">static MimeHeaders getHeaders(HttpServletRequest req) { Enumeration enum = req.getHeaderNames(); MimeHeaders headers = new MimeHeaders(); while (enum.hasMoreElements()) { String headerName = (String)enum.nextElement(); String headerValue = req.getHeader(headerName); StringTokenizer values = new StringTokenizer(headerValue, ","); while (values.hasMoreTokens()) { headers.addHeader(headerName, values.nextToken().trim()); } } return headers; }<a name="wp68265"> </a></pre></div><a name="wp69760"> </a><p class="pBody">The <code class="cCode">doPost</code> method calls <code class="cCode">putHeaders</code> and passes it the <code class="cCode">MimeHeaders</code> object <code class="cCode">headers</code>, which was returned by the method <code class="cCode">getHeaders</code>. The method <code class="cCode">putHeaders</code> writes the headers in <code class="cCode">headers</code> to <code class="cCode">res</code>, the second argument passed to it. The result is that <code class="cCode">res</code>, the response that the J2EE server will return to the method <code class="cCode">call</code>, now contains the headers that were in the original request.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">static void putHeaders(MimeHeaders headers, HttpServletResponse res) { Iterator it = headers.getAllHeaders(); while (it.hasNext()) { MimeHeader header = (MimeHeader)it.next(); String[] values = headers.getHeader(header.getName()); if (values.length == 1) res.setHeader(header.getName(), header.getValue()); else { StringBuffer concat = new StringBuffer(); int i = 0; while (i < values.length) { if (i != 0) { concat.append(','); } concat.append(values[i++]); } res.setHeader(header.getName(), concat.toString());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -