POST /PRopertyListing HTTP/1.1
Host: www.realproperties.com
Content-Type: Multipart/Related; boundary=MIME_boundary; type=text/xml; start="<property1234@realhouses.com>"
Content-Length: NNNN
--MIME_boundary
Content-Type: text/xml; charset=UTF-8
Content-Transfer-Encoding: 8bit
Content-ID: <property1234.xml@realhouses.com>
<?xml version='1.0'?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<realProperty:propertyListing id="property_432"
xmlns:realProperty="http://schemas.realhouses.com/listingSubmission">
<listingAgency>Really Nice Homes, Inc.</listingAgency>
<listingType>Add</listingType>
<propertyAddress>
<street>1234 Main St</street>
<city>Pleasantville</city>
<state>CA</state>
<zip>94323</zip>
</propertyAddress>
<listPrice>
250000
</listPrice>
<frontImage href="property1234_front.jpeg@realhouses.com"/>
<interiorImage href="property1234_interior.jpeg@realhouses.com"/>
</realProperty:propertyListing>
</SOAP-ENV: Body>
</SOAP-ENV: Envelope>
--MIME_boundary
Content-Type: image/jpeg
Content-ID:
....JPEG DATA .....
--MIME_boundary
Content-Type: image/jpeg
Content-ID:
....JPEG DATA .....
--MIME_boundary--
SOAPConnectionFactory spConFactory = SOAPConnectionFactory.newInstance();
SOAPConnection con = spConFactory.createConnection();
SOAPFactory soapFactory = SOAPFactory.newInstance();
SOAPMessage message = factory.createMessage();
SOAPHeader header = message.getSOAPHeader();
header.detachNode();
SOAPBody body = message.getSOAPBody();
Name listingElementName = soapFactory.createName(
"propertyListing", "realProperty",
"http://schemas.realhouses.com/listingSubmission");
SOAPBodyElement listingElement = body.addBodyElement(listingElementName);
Name attname = soapFactory.createName("id");
listingElement.addAttribute(attname, "property_1234");
SOAPElement listingAgency = listingElement.addChildElement("listingAgency");
listingAgency.addTextNode("Really Nice Homes, Inc");
SOAPElement listingType = listingElement.addChildElement("listingType");
listingType.addTextNode("add");
SOAPElement propertyAddress = listingElement.addChildElement("propertyAddress");
SOAPElement street = propertyAddress.addChildElement("street");
street.addTextNode("1234 Main St");
SOAPElement city = propertyAddress.addChildElement("city");
city.addTextNode("Pleasantville");
SOAPElement state = propertyAddress.addChildElement("state");
state.addTextNode("CA");
SOAPElement zip = propertyAddress.addChildElement("zip");
zip.addTextNode("94521");
SOAPElement listPrice = listingElement.addChildElement("listPrice");
listPrice.addTextNode("25000");
String frontImageID = "property1234_front_jpeg@realhouses.com";
SOAPElement frontImRef = listingElement.addChildElement("frontImage");
Name hrefAttName = soapFactory.createName("href");
frontImRef.addAttribute(hrefAttName, frontImageID);
String interiorID = "property1234_interior_jpeg@realhouses.com";
SOAPElement interiorImRef = listingElement.addChildElement("interiorImage");
interiorImRef.addAttribute(hrefAttName, interiorID);
URL url = new URL("file:///eXPort/files/pic1.jpg");
DataHandler dataHandler = new DataHandler(url);
AttachmentPart att = message.createAttachmentPart(dataHandler);
att.setContentId(frontImageID);
message.addAttachmentPart(att);
URL url2 = new URL("file:///export/files/pic2.jpg");
Image im = Toolkit.getDefaultToolkit().createImage(url2);
AttachmentPart att2 = message.createAttachmentPart(im, "image/jpeg");
att2.setContentId(interiorID);
message.addAttachmentPart(att2);
URL end point =
new URL("http://localhost:8080/saajtest/servlet/InvServlet");
SOAPMessage response = connection.call(message, end point);
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
...
MimeHeaders mimeHeaders = new MimeHeaders();
Enumeration en = req.getHeaderNames();
while (en.hasMoreElements()) {
String headerName = (String)en.nextElement();
String headerVal = req.getHeader(headerName);
StringTokenizer tk = new StringTokenizer(headerVal, ",");
while (tk.hasMoreTokens()){
mimeHeaders.addHeader(headerName, tk.nextToken().trim());
}
}
SOAPMessage message =
messageFactory.createMessage(mimeHeaders, req.getInputStream());
SOAPBody body = message.getSOAPBody();
Name listingElName = soapFactory.createName(
"propertyListing", "realProperty",
"http://schemas.realhouses.com/listingSubmission");
Iterator listings = body.getChildElements(listingElName);
while (listings.hasNext()) {
// The listing and its ID
SOAPElement listing = (SOAPElement)listings.next();
String listingID = listing.getAttribute("id");
// The listing agency name
Iterator ageIt = listing.getChildElements(soapFactory.createName("listingAgency"));
SOAPElement listingAgency = (SOAPElement)ageIt.next();
String agencyName = listingAgency.getValue();
...
}
Iterator frontImage = listing.getChildElements(soapFactory.createName("frontImage"));
SOAPElement front = (SOAPElement)frontImage.next();
if (front != null) {
String frontID = front.getAttribute("href");
Iterator attachments = message.getAttachments();
while (attachments.hasNext()) {
AttachmentPart att = (AttachmentPart)attachments.next();
if (att.getContentId().equals(frontID)) {
String contentType = att.getContentType();
String[] parts = contentType.split("/");
String fileExt = "UNK";
if (parts.length > 0) {
fileExt = parts[1];
}
InputStream is = att.getDataHandler().getInputStream();
FileOutputStream os =
new FileOutputStream("/tmp/" + listingID+ "_front." + fileExt);
byte[] buff = new byte[1024];
int read = 0;
while ((read = is.read(buff, 0, buff.length)) != -1) {
os.write(buff, 0, read);
}
os.flush();
os.close();
}
}
}
SOAPMessage reply = messageFactory.createMessage();
SOAPHeader header = reply.getSOAPHeader();
header.detachNode();
SOAPBody replyBody = reply.getSOAPBody();
SOAPBodyElement bodyElement = replyBody.addBodyElement(
soapFactory.createName("ack"));
bodyElement.addTextNode("OK");
res.setHeader("Content-Type", "text/xml");
OutputStream os = res.getOuputStream();
reply.writeTo(os);
os.flush();
冬临 ,java 爱好者,Matrixxml soap翻译小组成员进入讨论组讨论。
(出处:http://www.VeVb.com)
新闻热点
疑难解答