soapconnectionimpl.java
来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 528 行 · 第 1/2 页
JAVA
528 行
private void toSAAJElement(SOAPElement saajEle,
OMNode omNode,
javax.xml.soap.SOAPMessage saajSOAPMsg) throws SOAPException {
if (omNode instanceof OMText) {
return; // simply return since the text has already been added to saajEle
}
if (omNode instanceof OMElement) {
OMElement omEle = (OMElement)omNode;
for (Iterator childIter = omEle.getChildren(); childIter.hasNext();) {
OMNode omChildNode = (OMNode)childIter.next();
SOAPElement saajChildEle = null;
if (omChildNode instanceof OMText) {
// check whether the omtext refers to an attachment
final OMText omText = (OMText)omChildNode;
if (omText.isOptimized()) { // is this an attachment?
final DataHandler datahandler = (DataHandler)omText.getDataHandler();
AttachmentPart attachment = saajSOAPMsg.createAttachmentPart(datahandler);
final String id = IDGenerator.generateID();
attachment.setContentId(id);
attachment.setContentType(datahandler.getContentType());
saajSOAPMsg.addAttachmentPart(attachment);
saajEle.addAttribute(
saajSOAPMsg.getSOAPPart().getEnvelope().createName("href"),
"cid:" + id);
} else {
saajChildEle = saajEle.addTextNode(omText.getText());
}
} else if (omChildNode instanceof OMElement) {
OMElement omChildEle = (OMElement)omChildNode;
final QName omChildQName = omChildEle.getQName();
saajChildEle =
saajEle.addChildElement(omChildQName.getLocalPart(),
omChildQName.getPrefix(),
omChildQName.getNamespaceURI());
for (Iterator attribIter = omChildEle.getAllAttributes();
attribIter.hasNext();) {
OMAttribute attr = (OMAttribute)attribIter.next();
final QName attrQName = attr.getQName();
saajChildEle.addAttribute(saajSOAPMsg.getSOAPPart().getEnvelope().
createName(attrQName.getLocalPart(),
attrQName.getPrefix(),
attrQName.getNamespaceURI()),
attr.getAttributeValue());
}
}
// go down the tree adding child elements, till u reach a leaf(i.e. text element)
toSAAJElement(saajChildEle, omChildNode, saajSOAPMsg);
}
}
}
/**
* Converts a SAAJ SOAPMessage to an OM SOAPEnvelope
*
* @param saajSOAPMsg
* @return
* @throws SOAPException
*/
protected org.apache.axiom.soap.SOAPEnvelope toOMSOAPEnvelope(SOAPMessage saajSOAPMsg)
throws SOAPException {
final org.apache.axiom.soap.SOAPEnvelope omSOAPEnv =
SAAJUtil.toOMSOAPEnvelope(saajSOAPMsg.getSOAPPart().getDocumentElement());
Map attachmentMap = new HashMap();
final Iterator attachments = saajSOAPMsg.getAttachments();
while (attachments.hasNext()) {
final AttachmentPart attachment = (AttachmentPart)attachments.next();
if (attachment.getContentId() == null ||
attachment.getContentId().trim().length() == 0) {
attachment.setContentId(IDGenerator.generateID());
}
if (attachment.getDataHandler() == null) {
throw new SOAPException("Attachment with NULL DataHandler");
}
attachmentMap.put(attachment.getContentId(), attachment);
}
//Get keys of attachments to a hashmap
//This hashmap will be updated when attachment is accessed atleast once.
//Doing this here instead of inside insertAttachmentNodes()is much simpler
//as insertAttachmentNodes() has recursive calls
Set keySet = attachmentMap.keySet();
Iterator keySetItr = keySet.iterator();
HashMap keyAccessStatus = new HashMap();
while (keySetItr.hasNext()) {
String key = (String)keySetItr.next();
keyAccessStatus.put(key, "not-accessed");
}
insertAttachmentNodes(attachmentMap, omSOAPEnv, keyAccessStatus);
unaccessedAttachments =
getUnReferencedAttachmentNodes(attachmentMap, omSOAPEnv, keyAccessStatus);
return omSOAPEnv;
}
/**
* Inserts the attachments in the proper places
*
* @param attachments
* @param omEnvelope
* @throws SOAPException
*/
private void insertAttachmentNodes(Map attachments,
OMElement omEnvelope, HashMap keyAccessStatus)
throws SOAPException {
Iterator childIter = omEnvelope.getChildElements();
while (childIter.hasNext()) {
OMElement child = (OMElement)childIter.next();
final OMAttribute hrefAttr = child.getAttribute(new QName("href"));
String contentID = getContentID(hrefAttr);
if (contentID != null) {//This is an omEnvelope referencing an attachment
child.build();
AttachmentPart ap = ((AttachmentPart)attachments.get(contentID.trim()));
//update the key status as accessed
keyAccessStatus.put(contentID.trim(), "accessed");
OMText text = new OMTextImpl(ap.getDataHandler(), true,
omEnvelope.getOMFactory());
child.removeAttribute(hrefAttr);
child.addChild(text);
} else {
//possibly there can be references in the children of this omEnvelope
//so recurse through.
insertAttachmentNodes(attachments, child, keyAccessStatus);
}
}
}
private HashMap getUnReferencedAttachmentNodes(Map attachments,
OMElement omEnvelope, HashMap keyAccessStatus)
throws SOAPException {
HashMap unaccessedAttachments = new HashMap();
//now check for unaccessed keys
Set keySet = keyAccessStatus.keySet();
Iterator keySetItr = keySet.iterator();
while (keySetItr.hasNext()) {
String key = (String)keySetItr.next();
String keyStatus = (String)keyAccessStatus.get(key);
if ("not-accessed".equals(keyStatus)) {
//The value for this key has not been accessed in the
//referencing attachment scenario.Hence it must be an
//unreferenced one.
AttachmentPart ap = ((AttachmentPart)attachments.get(key));
unaccessedAttachments.put(key, ap);
keyAccessStatus.put(key, "accessed");
}
}
return unaccessedAttachments;
}
/**
* This method checks the value of attribute and if it is a valid CID then returns the contentID
* (with cid: prefix stripped off) or else returns null. A null return value can be assumed that
* this attribute is not an attachment referencing attribute
*
* @return the ContentID
*/
private String getContentID(OMAttribute attr) {
String contentId;
if (attr != null) {
contentId = attr.getAttributeValue();
} else {
return null;
}
if (contentId.startsWith("cid:")) {
contentId = contentId.substring(4);
return contentId;
}
return null;
}
/** overrided SOAPConnection's get() method */
public SOAPMessage get(Object to) throws SOAPException {
URL url = null;
try {
url = (to instanceof URL) ? (URL)to : new URL(to.toString());
} catch (MalformedURLException e) {
throw new SOAPException(e);
}
int responseCode;
boolean isFailure = false;
HttpURLConnection httpCon = null;
try {
httpCon = (HttpURLConnection)url.openConnection();
httpCon.setDoOutput(true);
httpCon.setDoInput(true);
httpCon.setUseCaches(false);
httpCon.setRequestMethod("GET");
HttpURLConnection.setFollowRedirects(true);
httpCon.connect();
responseCode = httpCon.getResponseCode();
// 500 is allowed for SOAP faults
if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) {
isFailure = true;
} else if ((responseCode / 100) != 2) {
throw new SOAPException("Error response: (" + responseCode
+ httpCon.getResponseMessage());
}
} catch (IOException e) {
throw new SOAPException(e);
}
//Construct the soapmessage from http response
SOAPMessage soapMessage = null;
if (responseCode == HttpURLConnection.HTTP_OK) {
try {
//read http headers & load mimeheaders
MimeHeaders mimeHeaders = new MimeHeaders();
String key, value;
// skip status line
int i = 1;
while (true) {
key = httpCon.getHeaderFieldKey(i);
value = httpCon.getHeaderField(i);
if (key == null && value == null) {
break;
}
if (key != null) {
StringTokenizer values = new StringTokenizer(value, ",");
while (values.hasMoreTokens()) {
mimeHeaders.addHeader(key, values.nextToken().trim());
}
}
i++;
}
InputStream httpInputStream;
if (isFailure) {
httpInputStream = httpCon.getErrorStream();
} else {
httpInputStream = httpCon.getInputStream();
}
soapMessage = new SOAPMessageImpl(httpInputStream, mimeHeaders);
httpInputStream.close();
httpCon.disconnect();
} catch (SOAPException e) {
throw e;
} catch (Exception e) {
throw new SOAPException(e.getMessage());
}
}
return soapMessage;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?