250 likes | 462 Views
J2EE —— 第 9 章 SOAP with Attachments API for Java. 不带附件的 SOAPMessage 对象. 带附件的 SOAPMessage 对象. SAAJ 和 DOM. Node 接口扩展了 org.w3c.dom.Node 接口 SOAPElement 接口扩展了 Node 接口和 org.w3c.dom.Element 接口 SOAPPart 类实现了 org.w3c.dom.Document 接口 Text 接口扩展了 org.w3c.dom.Text 接口. 连接.
E N D
SAAJ和DOM • Node接口扩展了org.w3c.dom.Node接口 • SOAPElement接口扩展了Node接口和org.w3c.dom.Element接口 • SOAPPart类实现了org.w3c.dom.Document接口 • Text接口扩展了org.w3c.dom.Text接口
连接 SOAPConnectionFactory factory = SOAPConnectionFactory.newInstance(); SOAPConnection connection = factory.createConnection(); . . . // create a request message and give it content java.net.URL endpoint = new URL("http://fabulous.com/gizmo/order"); SOAPMessage response = connection.call(request, endpoint);
创建和发送简单消息 MessageFactory factory = MessageFactory.newInstance(); SOAPMessage message = factory.createMessage(); SOAPPart soapPart = message.getSOAPPart(); SOAPEnvelope envelope = soapPart.getEnvelope(); SOAPHeader header = envelope.getHeader(); SOAPBody body = envelope.getBody(); //SOAPHeader header = message.getSOAPHeader(); //SOAPBody body = message.getSOAPBody(); header.detachNode();
给体添加内容 SOAPFactory soapFactory = SOAPFactory.newInstance(); Name bodyName = soapFactory.createName("GetLastTradePrice", "m", "http://wombat.ztrade.com"); SOAPBodyElement bodyElement = body.addBodyElement(bodyName); Name name = soapFactory.createName("symbol"); SOAPElement symbol = bodyElement.addChildElement(name); symbol.addTextNode("SUNW"); <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> <SOAP-ENV:Body> <m:GetLastTradePrice xmlns:m="http://wombat.ztrade.com"> <symbol>SUNW</symbol> </m:GetLastTradePrice> </SOAP-ENV:Body> </SOAP-ENV:Envelope>
获得消息的内容 SOAPBody soapBody = response.getSOAPBody(); java.util.Iterator iterator = soapBody.getChildElements(bodyName); while (iterator.hasNext()) { SOAPBodyElement bodyElement = (SOAPBodyElement)iterator.next(); String lastPrice = bodyElement.getValue(); System.out.print("The last price for SUNW is "); System.out.println(lastPrice); }
在头中添加内容 SOAPHeader header = message.getSOAPHeader(); Name headerName = soapFactory.createName("Claim", "wsi", "http://ws-i.org/schemas/conformanceClaim/"); SOAPHeaderElement headerElement = header.addHeaderElement(headerName); headerElement.addAttribute(soapFactory.createName( "conformsTo"), "http://ws-i.org/profiles/basic1.0/"); <SOAP-ENV:Header> <wsi:Claim conformsTo="http://ws-i.org/profiles/basic1.0/" xmlns:wsi="http://ws-i.org/schemas/conformanceClaim/"/> </SOAP-ENV:Header>
添加内容到SOAPPart对象 DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); dbFactory.setNamespaceAware(true); DocumentBuilder builder = dbFactory.newDocumentBuilder(); Document document = builder.parse("file:///music/order/soap.xml"); DOMSource domSource = new DOMSource(document); SOAPPart soapPart = message.getSOAPPart(); soapPart.setContent(domSource); <SOAP-ENV:Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"> <SOAP-ENV:Body> ... </SOAP-ENV:Body> </SOAP-ENV:Envelope>
添加文档到SOAP体 SOAPBody body = message.getSOAPBody(); SOAPBodyElement docElement = body.addDocument(document); • 使用SAAJ和DOM API操纵消息内容 • 只使用DOM API • 只使用SAAJ API • 使用SAAJ API,然后切换到DOM API • 使用DOM API,然后切换到SAAJ API
添加附件 AttachmentPart attachment = message.createAttachmentPart(); String stringContent = "Update address for Sunny Skies " + "Inc., to 10 Upbeat Street, Pleasant Grove, CA 95439"; attachment.setContent(stringContent, "text/plain"); attachment.setContentId("update_address"); message.addAttachmentPart(attachment); URL url = new URL("http://greatproducts.com/gizmos/img.jpg"); DataHandler dataHandler = new DataHandler(url); AttachmentPart attachment = message.createAttachmentPart(dataHandler); attachment.setContentId("attached_image"); message.addAttachmentPart(attachment);
访问AttachmentPart对象 java.util.Iterator iterator = message.getAttachments(); while (iterator.hasNext()) { AttachmentPart attachment = (AttachmentPart)iterator.next(); String id = attachment.getContentId(); String type = attachment.getContentType(); System.out.print("Attachment " + id + " has content type " + type); If (type == "text/plain") { Object content = attachment.getContent(); System.out.println("Attachment " + "contains:\n" + content); } }
添加属性 Name attributeName = envelope.createName("id"); person.addAttribute(attributeName, "Person7"); <person id="Person7"> ...</person> String attributeValue = person.getAttributeValue(attributeName); Iterator iterator = person.getAllAttributes(); while (iterator.hasNext()){ Name attributeName = (Name) iterator.next(); System.out.println("Attribute name is " + attributeName.getQualifiedName()); System.out.println("Attribute value is " + element.getAttributeValue(attributeName)); }
SOAPHeaderElement的actor属性 • 指明头元素的最终接收者 String nameSpace = "ns"; String nameSpaceURI = "http://gizmos.com/NSURI"; Name order = soapFactory.createName("orderDesk", nameSpace, nameSpaceURI); SOAPHeaderElement orderHeader = header.addHeaderElement(order); orderHeader.setActor("http://gizmos.com/orders"); Name shipping = soapFactory.createName("shippingDesk", nameSpace, nameSpaceURI); SOAPHeaderElement shippingHeader = header.addHeaderElement(shipping); shippingHeader.setActor("http://gizmos.com/shipping");
使用actor属性读取头元素 java.util.Iterator headerElements = header.examineHeaderElements("http://gizmos.com/orders"); java.util.Iterator headerElements = header.extractHeaderElements("http://gizmos.com/orders"); Iterator allHeaders = header.examineAllHeaderElements(); while (allHeaders.hasNext()) { SOAPHeaderElement headerElement = (SOAPHeaderElement)allHeaders.next(); Name headerName = headerElement.getElementName(); System.out.println("\nHeader name is " + headerName.getQualifiedName()); System.out.println("Actor is " + headerElement.getActor()); }
SOAPHeaderElement的mustUnderstand属性 • mustUnderstand为true时,actor必须知道头项的语义并正确进行处理,否则就要将一个SOAP fault发送给发送者 SOAPHeader header = message.getSOAPHeader(); Name name = soapFactory.createName("Transaction", "t", "http://gizmos.com/orders"); SOAPHeaderElement transaction = header.addHeaderElement(name); transaction.setMustUnderstand(true); transaction.addTextNode("5"); <SOAP-ENV:Header> <t:Transaction xmlns:t="http://gizmos.com/orders“ SOAP-ENV:mustUnderstand="1"> 5 </t:Transaction> </SOAP-ENV:Header>
使用SOAP Fault • Fault代码 • VersionMismatch • MustUnderstand • Client • Server • Fault字符串 • Fault actor • Detail对象
创建和填充SOAPFault对象 SOAPBody body = message.getSOAPBody(); SOAPFault fault = body.addFault(); Name faultName = soapFactory.createName("Server", "", SOAPConstants.URI_NS_SOAP_ENVELOPE); fault.setFaultCode(faultName); fault.setFaultActor("http://gizmos.com/orders"); fault.setFaultString("Server not responding"); Name faultName = soapFactory.createName("Client", "", SOAPConstants.URI_NS_SOAP_ENVELOPE); fault.setFaultCode(faultName); fault.setFaultString("Message does not have necessary info"); Detail detail = fault.addDetail(); Name entryName = soapFactory.createName("order", "PO", "http://gizmos.com/orders/"); DetailEntry entry = detail.addDetailEntry(entryName); entry.addTextNode("Quantity element does not have a value");
获得Fault信息 SOAPBody body = newMessage.getSOAPBody(); if ( body.hasFault() ) { SOAPFault newFault = body.getFault(); Name code = newFault.getFaultCodeAsName(); String string = newFault.getFaultString(); String actor = newFault.getFaultActor(); Detail newDetail = newFault.getDetail(); if (newDetail != null) { Iterator entries = newDetail.getDetailEntries(); while ( entries.hasNext() ) { DetailEntry newEntry = (DetailEntry)entries.next(); String value = newEntry.getValue(); System.out.println(" Detail entry = " + value);
代码示例Request.java Name bodyName = soapFactory.createName("GetLastTradePrice", "m", "http://wombats.ztrade.com"); SOAPBodyElement bodyElement = body.addBodyElement(bodyName); Name name = soapFactory.createName("symbol"); SOAPElement symbol = bodyElement.addChildElement(name); symbol.addTextNode("SUNW"); URL endpoint = new URL ("http://wombat.ztrade.com/quotes"); SOAPMessage response = connection.call(message, endpoint); connection.close(); SOAPBody soapBody = response.getSOAPBody(); Iterator iterator = soapBody.getChildElements(bodyName); bodyElement = (SOAPBodyElement)iterator.next(); String lastPrice = bodyElement.getValue(); System.out.print("The last price for SUNW is "); System.out.println(lastPrice);
代码示例MyUddiPing.java SOAPBodyElement findBusiness = body.addBodyElement(soapFactory.createName( "find_business", "", "urn:uddi-org:api_v2")); findBusiness.addAttribute(soapFactory.createName( "generic"), "2.0"); findBusiness.addAttribute(soapFactory.createName( "maxRows"), "100"); SOAPElement businessName = findBusiness.addChildElement( soapFactory.createName("name")); businessName.addTextNode(args[1]); URL endpoint = new URL( System.getProperties().getProperty("URL")); SOAPMessage reply = connection.call(message, endpoint); reply.writeTo(System.out);
代码示例DOMExample.java和DOMSrcExample.java DocumentBuilderFactory factory =DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); document = builder.parse( new File(args[0]) ); SOAPBodyElement docElement = body.addDocument(document); DOMSource domSource = new DOMSource(document); SOAPMessage message = messageFactory.createMessage(); SOAPPart soapPart = message.getSOAPPart(); soapPart.setContent(domSource);
代码示例Attachments.java AttachmentPart attachment1 = message.createAttachmentPart(); attachment1.setContent(stringContent, "text/plain"); attachment1.setContentId("attached_text"); message.addAttachmentPart(attachment1); URL url = new URL("file:///../xml-pic.jpg"); DataHandler dataHandler = new DataHandler(url); AttachmentPart attachment2 = message.createAttachmentPart(dataHandler); attachment2.setContentId("attached_image"); message.addAttachmentPart(attachment2);
代码示例SOAPFaultTest.java <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> <SOAP-ENV:Header/> <SOAP-ENV:Body> <SOAP-ENV:Fault><faultcode>SOAP-ENV:Client</faultcode> <faultstring>Message does not have necessary info</faultstring> <faultactor>http://gizmos.com/order</faultactor> <detail> <PO:order xmlns:PO="http://gizmos.com/orders/"> Quantity element does not have a value</PO:order> <PO:confirmation xmlns:PO="http://gizmos.com/confirm"> Incomplete address: no zip code</PO:confirmation> </detail></SOAP-ENV:Fault> </SOAP-ENV:Body></SOAP-ENV:Envelope>