Download presentation
Presentation is loading. Please wait.
Published byNicholas Evans Modified over 9 years ago
1
J2EE —— 第 9 章 SOAP with Attachments API for Java
2
不带附件的 SOAPMessage 对象
3
带附件的 SOAPMessage 对象
4
SAAJ 和 DOM Node 接口扩展了 org.w3c.dom.Node 接口 SOAPElement 接口扩展了 Node 接口和 org.w3c.dom.Element 接口 SOAPPart 类实现了 org.w3c.dom.Document 接口 Text 接口扩展了 org.w3c.dom.Text 接口
5
连接 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);
6
创建和发送简单消息 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();
7
给体添加内容 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/"> SUNW
8
获得消息的内容 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); }
9
在头中添加内容 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/"); <wsi:Claim conformsTo="http://ws-i.org/profiles/basic1.0/" xmlns:wsi="http://ws-i.org/schemas/conformanceClaim/"/>
10
添加内容到 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);...
11
添加文档到 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
12
添加附件 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);
13
访问 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); }
14
添加属性 Name attributeName = envelope.createName("id"); person.addAttribute(attributeName, "Person7");... 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)); }
15
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");
16
使用 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()); }
17
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"); <t:Transaction xmlns:t="http://gizmos.com/orders “ SOAP-ENV:mustUnderstand="1"> 5
18
使用 SOAP Fault Fault 代码 VersionMismatch MustUnderstand Client Server Fault 字符串 Fault actor Detail 对象
19
创建和填充 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");
20
获得 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);
21
代码示例 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);
22
代码示例 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);
23
代码示例 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);
24
代码示例 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);
25
代码示例 SOAPFaultTest.java <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> SOAP-ENV:Client Message does not have necessary info http://gizmos.com/order Quantity element does not have a value Incomplete address: no zip code
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.