Upload
ella
View
83
Download
0
Embed Size (px)
DESCRIPTION
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 接口. 连接. - PowerPoint PPT Presentation
Citation preview
J2EE—— 第 9 章SOAP with Attachments API for Jav
a
不带附件的 SOAPMessage 对象
带附件的 SOAPMessage 对象
SAAJ 和 DOM Node 接口扩展了 org.w3c.dom.Node 接口 SOAPElement 接口扩展了 Node 接口和 org.w3
c.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, endpoi
nt);
创建和发送简单消息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(bodyNam
e); 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(h
eaderName); headerElement.addAttribute(soapFactory.createName( "conformsT
o"), "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.addD
ocument(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 U
pbeat 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(dataHan
dler); 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(ship
ping);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.o
ut.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://giz
mos.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.co
m/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.javaName bodyName = soapFactory.createName("GetLastTradePrice",
"m", "http://wombats.ztrade.com");SOAPBodyElement bodyElement = body.addBodyElement(bodyNam
e);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.javaSOAPBodyElement 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.javaAttachmentPart 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:Envelopexmlns: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>