158 }
159 }
160 }
161
162 public boolean hasHeaders() {
163 parse();
164 return headers.hasHeaders();
165 }
166
167 public @NotNull MessageHeaders getHeaders() {
168 parse();
169 return headers;
170 }
171
172 /**
173 * Gets the attachments of this message
174 * (attachments live outside a message.)
175 */
176 @Override
177 public @NotNull AttachmentSet getAttachments() {
178 parse();
179 return attachmentSet;
180 }
181
182 /**
183 * Optimization hint for the derived class to check
184 * if we may have some attachments.
185 */
186 @Override
187 protected boolean hasAttachments() {
188 parse();
189 return attachmentSet!=null;
190 }
191
192 public @Nullable String getPayloadLocalPart() {
193 access();
194 return payloadLocalName;
195 }
196
197 public String getPayloadNamespaceURI() {
198 access();
199 return payloadNamespace;
200 }
201
202 public boolean hasPayload() {
203 access();
204 return payloadNamespace != null;
205 }
206
207 private void addAttributes(Element e, NamedNodeMap attrs) {
208 if(attrs == null)
209 return;
210 String elPrefix = e.getPrefix();
211 for(int i=0; i < attrs.getLength();i++) {
212 Attr a = (Attr)attrs.item(i);
213 //check if attr is ns declaration
214 if("xmlns".equals(a.getPrefix()) || "xmlns".equals(a.getLocalName())) {
215 if(elPrefix == null && a.getLocalName().equals("xmlns")) {
216 // the target element has already default ns declaration, dont' override it
217 continue;
218 } else if(elPrefix != null && "xmlns".equals(a.getPrefix()) && elPrefix.equals(a.getLocalName())) {
219 //dont bind the prefix to ns again, its already in the target element.
220 continue;
221 }
222 e.setAttributeNS(a.getNamespaceURI(),a.getName(),a.getValue());
223 continue;
224 }
310
311 }
312 return null;
313 }
314
315 /** @deprecated */
316 public <T> T readPayloadAsJAXB(Bridge<T> bridge) throws JAXBException {
317 access();
318 if (payload != null)
319 return bridge.unmarshal(payload,hasAttachments()? new AttachmentUnmarshallerImpl(getAttachments()) : null);
320 return null;
321 }
322 public <T> T readPayloadAsJAXB(XMLBridge<T> bridge) throws JAXBException {
323 access();
324 if (payload != null)
325 return bridge.unmarshal(payload,hasAttachments()? new AttachmentUnmarshallerImpl(getAttachments()) : null);
326 return null;
327 }
328
329 public XMLStreamReader readPayload() throws XMLStreamException {
330 access();
331 if (payload != null) {
332 DOMStreamReader dss = new DOMStreamReader();
333 dss.setCurrentNode(payload);
334 dss.nextTag();
335 assert dss.getEventType() == XMLStreamReader.START_ELEMENT;
336 return dss;
337 }
338 return null;
339 }
340
341 public void writePayloadTo(XMLStreamWriter sw) throws XMLStreamException {
342 access();
343 try {
344 for (Element part : bodyParts)
345 DOMUtil.serializeNode(part, sw);
346 } catch (XMLStreamException e) {
347 throw new WebServiceException(e);
348 }
349 }
350
351 public void writeTo(XMLStreamWriter writer) throws XMLStreamException {
352 try {
353 writer.writeStartDocument();
354 if (!parsedMessage) {
355 DOMUtil.serializeNode(sm.getSOAPPart().getEnvelope(), writer);
356 } else {
357 SOAPEnvelope env = sm.getSOAPPart().getEnvelope();
358 DOMUtil.writeTagWithAttributes(env, writer);
505 try {
506 if (!parsedMessage) {
507 return new SAAJMessage(readAsSOAPMessage());
508 } else {
509 SOAPMessage msg = soapVersion.getMessageFactory().createMessage();
510 SOAPBody newBody = msg.getSOAPPart().getEnvelope().getBody();
511 for (Element part : bodyParts) {
512 Node n = newBody.getOwnerDocument().importNode(part, true);
513 newBody.appendChild(n);
514 }
515 addAttributes(newBody, bodyAttrs);
516 return new SAAJMessage(getHeaders(), getAttachments(), msg, soapVersion);
517 }
518 } catch (SOAPException e) {
519 throw new WebServiceException(e);
520 }
521 }
522 private static final AttributesImpl EMPTY_ATTS = new AttributesImpl();
523 private static final LocatorImpl NULL_LOCATOR = new LocatorImpl();
524
525 private static class SAAJAttachment implements AttachmentEx {
526
527 final AttachmentPart ap;
528
529 String contentIdNoAngleBracket;
530
531 public SAAJAttachment(AttachmentPart part) {
532 this.ap = part;
533 }
534
535 /**
536 * Content ID of the attachment. Uniquely identifies an attachment.
537 */
538 public String getContentId() {
539 if (contentIdNoAngleBracket == null) {
540 contentIdNoAngleBracket = ap.getContentId();
541 if (contentIdNoAngleBracket != null && contentIdNoAngleBracket.charAt(0) == '<')
542 contentIdNoAngleBracket = contentIdNoAngleBracket.substring(1, contentIdNoAngleBracket.length()-1);
543 }
544 return contentIdNoAngleBracket;
545 }
634
635 public String getValue() {
636 return mh.getValue();
637 }
638 };
639 }
640
641 public void remove() {
642 throw new UnsupportedOperationException();
643 }
644 };
645 }
646 }
647
648 /**
649 * {@link AttachmentSet} for SAAJ.
650 *
651 * SAAJ wants '<' and '>' for the content ID, but {@link AttachmentSet}
652 * doesn't. S this class also does the conversion between them.
653 */
654 private static class SAAJAttachmentSet implements AttachmentSet {
655
656 private Map<String, Attachment> attMap;
657 private Iterator attIter;
658
659 public SAAJAttachmentSet(SOAPMessage sm) {
660 attIter = sm.getAttachments();
661 }
662
663 /**
664 * Gets the attachment by the content ID.
665 *
666 * @return null
667 * if no such attachment exist.
668 */
669 public Attachment get(String contentId) {
670 // if this is the first time then create the attachment Map
671 if (attMap == null) {
672 if (!attIter.hasNext())
673 return null;
674 attMap = createAttachmentMap();
697 }
698 return attMap.values().iterator();
699 }
700
701 private Map<String, Attachment> createAttachmentMap() {
702 HashMap<String, Attachment> map = new HashMap<String, Attachment>();
703 while (attIter.hasNext()) {
704 AttachmentPart ap = (AttachmentPart) attIter.next();
705 map.put(ap.getContentId(), new SAAJAttachment(ap));
706 }
707 return map;
708 }
709
710 public void add(Attachment att) {
711 attMap.put('<'+att.getContentId()+'>', att);
712 }
713 }
714
715 public SOAPVersion getSOAPVersion() {
716 return soapVersion;
717 }
718 }
|
158 }
159 }
160 }
161
162 public boolean hasHeaders() {
163 parse();
164 return headers.hasHeaders();
165 }
166
167 public @NotNull MessageHeaders getHeaders() {
168 parse();
169 return headers;
170 }
171
172 /**
173 * Gets the attachments of this message
174 * (attachments live outside a message.)
175 */
176 @Override
177 public @NotNull AttachmentSet getAttachments() {
178 if (attachmentSet == null) attachmentSet = new SAAJAttachmentSet(sm);
179 return attachmentSet;
180 }
181
182 /**
183 * Optimization hint for the derived class to check
184 * if we may have some attachments.
185 */
186 @Override
187 protected boolean hasAttachments() {
188 return !getAttachments().isEmpty();
189 }
190
191 public @Nullable String getPayloadLocalPart() {
192 soapBodyFirstChild();
193 return payloadLocalName;
194 }
195
196 public String getPayloadNamespaceURI() {
197 soapBodyFirstChild();
198 return payloadNamespace;
199 }
200
201 public boolean hasPayload() {
202 return soapBodyFirstChild() != null;
203 }
204
205 private void addAttributes(Element e, NamedNodeMap attrs) {
206 if(attrs == null)
207 return;
208 String elPrefix = e.getPrefix();
209 for(int i=0; i < attrs.getLength();i++) {
210 Attr a = (Attr)attrs.item(i);
211 //check if attr is ns declaration
212 if("xmlns".equals(a.getPrefix()) || "xmlns".equals(a.getLocalName())) {
213 if(elPrefix == null && a.getLocalName().equals("xmlns")) {
214 // the target element has already default ns declaration, dont' override it
215 continue;
216 } else if(elPrefix != null && "xmlns".equals(a.getPrefix()) && elPrefix.equals(a.getLocalName())) {
217 //dont bind the prefix to ns again, its already in the target element.
218 continue;
219 }
220 e.setAttributeNS(a.getNamespaceURI(),a.getName(),a.getValue());
221 continue;
222 }
308
309 }
310 return null;
311 }
312
313 /** @deprecated */
314 public <T> T readPayloadAsJAXB(Bridge<T> bridge) throws JAXBException {
315 access();
316 if (payload != null)
317 return bridge.unmarshal(payload,hasAttachments()? new AttachmentUnmarshallerImpl(getAttachments()) : null);
318 return null;
319 }
320 public <T> T readPayloadAsJAXB(XMLBridge<T> bridge) throws JAXBException {
321 access();
322 if (payload != null)
323 return bridge.unmarshal(payload,hasAttachments()? new AttachmentUnmarshallerImpl(getAttachments()) : null);
324 return null;
325 }
326
327 public XMLStreamReader readPayload() throws XMLStreamException {
328 return soapBodyFirstChildReader();
329 }
330
331 public void writePayloadTo(XMLStreamWriter sw) throws XMLStreamException {
332 access();
333 try {
334 for (Element part : bodyParts)
335 DOMUtil.serializeNode(part, sw);
336 } catch (XMLStreamException e) {
337 throw new WebServiceException(e);
338 }
339 }
340
341 public void writeTo(XMLStreamWriter writer) throws XMLStreamException {
342 try {
343 writer.writeStartDocument();
344 if (!parsedMessage) {
345 DOMUtil.serializeNode(sm.getSOAPPart().getEnvelope(), writer);
346 } else {
347 SOAPEnvelope env = sm.getSOAPPart().getEnvelope();
348 DOMUtil.writeTagWithAttributes(env, writer);
495 try {
496 if (!parsedMessage) {
497 return new SAAJMessage(readAsSOAPMessage());
498 } else {
499 SOAPMessage msg = soapVersion.getMessageFactory().createMessage();
500 SOAPBody newBody = msg.getSOAPPart().getEnvelope().getBody();
501 for (Element part : bodyParts) {
502 Node n = newBody.getOwnerDocument().importNode(part, true);
503 newBody.appendChild(n);
504 }
505 addAttributes(newBody, bodyAttrs);
506 return new SAAJMessage(getHeaders(), getAttachments(), msg, soapVersion);
507 }
508 } catch (SOAPException e) {
509 throw new WebServiceException(e);
510 }
511 }
512 private static final AttributesImpl EMPTY_ATTS = new AttributesImpl();
513 private static final LocatorImpl NULL_LOCATOR = new LocatorImpl();
514
515 protected static class SAAJAttachment implements AttachmentEx {
516
517 final AttachmentPart ap;
518
519 String contentIdNoAngleBracket;
520
521 public SAAJAttachment(AttachmentPart part) {
522 this.ap = part;
523 }
524
525 /**
526 * Content ID of the attachment. Uniquely identifies an attachment.
527 */
528 public String getContentId() {
529 if (contentIdNoAngleBracket == null) {
530 contentIdNoAngleBracket = ap.getContentId();
531 if (contentIdNoAngleBracket != null && contentIdNoAngleBracket.charAt(0) == '<')
532 contentIdNoAngleBracket = contentIdNoAngleBracket.substring(1, contentIdNoAngleBracket.length()-1);
533 }
534 return contentIdNoAngleBracket;
535 }
624
625 public String getValue() {
626 return mh.getValue();
627 }
628 };
629 }
630
631 public void remove() {
632 throw new UnsupportedOperationException();
633 }
634 };
635 }
636 }
637
638 /**
639 * {@link AttachmentSet} for SAAJ.
640 *
641 * SAAJ wants '<' and '>' for the content ID, but {@link AttachmentSet}
642 * doesn't. S this class also does the conversion between them.
643 */
644 protected static class SAAJAttachmentSet implements AttachmentSet {
645
646 private Map<String, Attachment> attMap;
647 private Iterator attIter;
648
649 public SAAJAttachmentSet(SOAPMessage sm) {
650 attIter = sm.getAttachments();
651 }
652
653 /**
654 * Gets the attachment by the content ID.
655 *
656 * @return null
657 * if no such attachment exist.
658 */
659 public Attachment get(String contentId) {
660 // if this is the first time then create the attachment Map
661 if (attMap == null) {
662 if (!attIter.hasNext())
663 return null;
664 attMap = createAttachmentMap();
687 }
688 return attMap.values().iterator();
689 }
690
691 private Map<String, Attachment> createAttachmentMap() {
692 HashMap<String, Attachment> map = new HashMap<String, Attachment>();
693 while (attIter.hasNext()) {
694 AttachmentPart ap = (AttachmentPart) attIter.next();
695 map.put(ap.getContentId(), new SAAJAttachment(ap));
696 }
697 return map;
698 }
699
700 public void add(Attachment att) {
701 attMap.put('<'+att.getContentId()+'>', att);
702 }
703 }
704
705 public SOAPVersion getSOAPVersion() {
706 return soapVersion;
707 }
708
709 private XMLStreamReader soapBodyFirstChildReader;
710
711 /**
712 * This allow the subclass to retain the XMLStreamReader.
713 */
714 protected XMLStreamReader getXMLStreamReader(SOAPElement soapElement) {
715 return null;
716 }
717
718 protected XMLStreamReader createXMLStreamReader(SOAPElement soapElement) {
719 DOMStreamReader dss = new DOMStreamReader();
720 dss.setCurrentNode(soapElement);
721 return dss;
722 }
723
724 protected XMLStreamReader soapBodyFirstChildReader() {
725 if (soapBodyFirstChildReader != null) return soapBodyFirstChildReader;
726 soapBodyFirstChild();
727 if (soapBodyFirstChild != null) {
728 soapBodyFirstChildReader = getXMLStreamReader(soapBodyFirstChild);
729 if (soapBodyFirstChildReader == null) soapBodyFirstChildReader =
730 createXMLStreamReader(soapBodyFirstChild);
731 if (soapBodyFirstChildReader.getEventType() == XMLStreamReader.START_DOCUMENT) {
732 try {
733 while(soapBodyFirstChildReader.getEventType() != XMLStreamReader.START_ELEMENT)
734 soapBodyFirstChildReader.next();
735 } catch (XMLStreamException e) {
736 throw new RuntimeException(e);
737 }
738 }
739 return soapBodyFirstChildReader;
740 } else {
741 payloadLocalName = null;
742 payloadNamespace = null;
743 return null;
744 }
745 }
746
747 private SOAPElement soapBodyFirstChild;
748
749 SOAPElement soapBodyFirstChild() {
750 if (soapBodyFirstChild != null) return soapBodyFirstChild;
751 try {
752 boolean foundElement = false;
753 for (Node n = sm.getSOAPBody().getFirstChild(); n != null && !foundElement; n = n.getNextSibling()) {
754 if (n.getNodeType() == Node.ELEMENT_NODE) {
755 foundElement = true;
756 if (n instanceof SOAPElement) {
757 soapBodyFirstChild = (SOAPElement) n;
758 payloadLocalName = soapBodyFirstChild.getLocalName();
759 payloadNamespace = soapBodyFirstChild.getNamespaceURI();
760 return soapBodyFirstChild;
761 }
762 }
763 }
764 if(foundElement) for(Iterator i = sm.getSOAPBody().getChildElements(); i.hasNext();){
765 Object o = i.next();
766 if (o instanceof SOAPElement) {
767 soapBodyFirstChild = (SOAPElement)o;
768 payloadLocalName = soapBodyFirstChild.getLocalName();
769 payloadNamespace = soapBodyFirstChild.getNamespaceURI();
770 return soapBodyFirstChild;
771 }
772 }
773 } catch (SOAPException e) {
774 throw new RuntimeException(e);
775 }
776 return soapBodyFirstChild;
777 }
778 }
|