J2EE - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

J2EE

Description:

Title: J2EE 9 SOAP with Attachments API for Java Author: lli Last modified by: lli Created Date: 9/19/2006 1:10:49 AM Document presentation format – PowerPoint PPT presentation

Number of Views:83
Avg rating:3.0/5.0
Slides: 26
Provided by: lli60
Category:
Tags: j2ee | wombat

less

Transcript and Presenter's Notes

Title: J2EE


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.createConnecti
    on()
  • . . . // 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.newInstanc
    e()
  • 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("GetLastTra
    dePrice",
  • "m", "http//wombat.ztrade.com")
  • SOAPBodyElement bodyElement body.addBodyElement(
    bodyName)
  • Name name soapFactory.createName("symbol")
  • SOAPElement symbol bodyElement.addChildElement(n
    ame)
  • symbol.addTextNode("SUNW")
  • ltSOAP-ENVEnvelope
  • xmlnsSOAP-ENV"http//schemas.xmlsoap.org/soa
    p/envelope/"gt
  • ltSOAP-ENVBodygt
  • ltmGetLastTradePrice xmlnsm"http//wombat.zt
    rade.com"gt
  • ltsymbolgtSUNWlt/symbolgt
  • lt/mGetLastTradePricegt
  • lt/SOAP-ENVBodygt lt/SOAP-ENVEnvelopegt

8
???????
  • SOAPBody soapBody response.getSOAPBody()
  • java.util.Iterator iterator soapBody.getChildEle
    ments(bodyName)
  • while (iterator.hasNext())
  • SOAPBodyElement bodyElement (SOAPBodyElement)ite
    rator.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
    /")
  • ltSOAP-ENVHeadergt
  • ltwsiClaim conformsTo"http//ws-i.org/profile
    s/basic1.0/"
  • xmlnswsi"http//ws-i.org/schemas/conform
    anceClaim/"/gt
  • lt/SOAP-ENVHeadergt

10
?????SOAPPart??
  • DocumentBuilderFactory dbFactory
  • DocumentBuilderFactory.newInstance()
  • dbFactory.setNamespaceAware(true)
  • DocumentBuilder builder dbFactory.newDocumentBui
    lder()
  • Document document builder.parse("file///music/o
    rder/soap.xml")
  • DOMSource domSource new DOMSource(document)
  • SOAPPart soapPart message.getSOAPPart()
  • soapPart.setContent(domSource)
  • ltSOAP-ENVEnvelope xmlns"http//schemas.xmlsoap.o
    rg/soap/envelope/"gt
  • ltSOAP-ENVBodygt
  • ...
  • lt/SOAP-ENVBodygt
  • lt/SOAP-ENVEnvelopegt

11
?????SOAP?
  • SOAPBody body message.getSOAPBody()
  • SOAPBodyElement docElement body.addDocument(docu
    ment)
  • ??SAAJ?DOM API??????
  • ???DOM API
  • ???SAAJ API
  • ??SAAJ API,?????DOM API
  • ??DOM API,?????SAAJ API

12
????
  • AttachmentPart attachment message.createAttachme
    ntPart()
  • 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.createAttachme
    ntPart(dataHandler)
  • attachment.setContentId("attached_image")
  • message.addAttachmentPart(attachment)

13
??AttachmentPart??
  • java.util.Iterator iterator message.getAttachmen
    ts()
  • 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")
  • ltperson id"Person7"gt ...lt/persongt
  • String attributeValue person.getAttributeValue(a
    ttributeName)
  • 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("shippingDe
    sk",
  • nameSpace, nameSpaceURI)
  • SOAPHeaderElement shippingHeader
    header.addHeaderElement(shipping)
  • shippingHeader.setActor("http//gizmos.com/shippin
    g")

16
??actor???????
  • java.util.Iterator headerElements
  • header.examineHeaderElements("http//gizmos.co
    m/orders")
  • java.util.Iterator headerElements
  • header.extractHeaderElements("http//gizmos.co
    m/orders")
  • Iterator allHeaders header.examineAllHeaderEleme
    nts()
  • 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")
  • ltSOAP-ENVHeadergt
  • lttTransaction xmlnst"http//gizmos.com/ord
    ers
  • SOAP-ENVmustUnderstand"1"gt
  • 5
  • lt/tTransactiongt
  • lt/SOAP-ENVHeadergt

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(entryNam
    e)
  • 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("GetLastTra
    dePrice", "m", "http//wombats.ztrade.com")
  • SOAPBodyElement bodyElement body.addBodyElement(
    bodyName)
  • Name name soapFactory.createName("symbol")
  • SOAPElement symbol bodyElement.addChildElement(n
    ame)
  • 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(body
    Name)
  • 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", "", "urnuddi-orgapi_v2"))
  • findBusiness.addAttribute(soapFactory.createName(
    "generic"), "2.0") findBusiness.addAttribute(soap
    Factory.createName( "maxRows"), "100")
  • SOAPElement businessName findBusiness.addChildEl
    ement( soapFactory.createName("name"))
    businessName.addTextNode(args1)
  • URL endpoint new URL( System.getProperties().get
    Property("URL"))
  • SOAPMessage reply connection.call(message,
    endpoint)
  • reply.writeTo(System.out)

23
????DOMExample.java?DOMSrcExample.java
  • DocumentBuilderFactory factory DocumentBuilderFac
    tory.newInstance()
  • factory.setNamespaceAware(true)
  • DocumentBuilder builder factory.newDocumentBuild
    er()
  • document builder.parse( new File(args0) )
  • SOAPBodyElement docElement body.addDocument(docu
    ment)
  • DOMSource domSource new DOMSource(document)
  • SOAPMessage message messageFactory.createMessage
    ()
  • SOAPPart soapPart message.getSOAPPart()
  • soapPart.setContent(domSource)

24
????Attachments.java
  • AttachmentPart attachment1 message.createAttachm
    entPart()
  • 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
  • ltSOAP-ENVEnvelope
  • xmlnsSOAP-ENV"http//schemas.xmlsoap.org/soap/en
    velope/"gt
  • ltSOAP-ENVHeader/gt
  • ltSOAP-ENVBodygt
  • ltSOAP-ENVFaultgtltfaultcodegtSOAP-ENVClientlt/faultc
    odegt
  • ltfaultstringgtMessage does not have necessary
    infolt/faultstringgt
  • ltfaultactorgthttp//gizmos.com/orderlt/faultactorgt
  • ltdetailgt
  • ltPOorder xmlnsPO"http//gizmos.com/orders/"gt
  • Quantity element does not have a valuelt/POordergt
  • ltPOconfirmation xmlnsPO"http//gizmos.com/confi
    rm"gt
  • Incomplete address no zip codelt/POconfirmationgt
  • lt/detailgtlt/SOAP-ENVFaultgt
  • lt/SOAP-ENVBodygtlt/SOAP-ENVEnvelopegt
Write a Comment
User Comments (0)
About PowerShow.com