< prev index next >

src/java.xml.ws/share/classes/com/sun/xml/internal/messaging/saaj/soap/MessageImpl.java

Print this page


   1 /*
   2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  66     public static final String CONTENT_ID             = "Content-ID";
  67     public static final String CONTENT_LOCATION       = "Content-Location";
  68 
  69     protected static final Logger log =
  70         Logger.getLogger(LogDomainConstants.SOAP_DOMAIN,
  71                          "com.sun.xml.internal.messaging.saaj.soap.LocalStrings");
  72 
  73     protected static final int PLAIN_XML_FLAG      = 1;      // 00001
  74     protected static final int MIME_MULTIPART_FLAG = 2;      // 00010
  75     protected static final int SOAP1_1_FLAG = 4;             // 00100
  76     protected static final int SOAP1_2_FLAG = 8;             // 01000
  77     //protected static final int MIME_MULTIPART_XOP_FLAG = 14; // 01110
  78     protected static final int MIME_MULTIPART_XOP_SOAP1_1_FLAG = 6;  // 00110
  79     protected static final int MIME_MULTIPART_XOP_SOAP1_2_FLAG = 10; // 01010
  80     protected static final int XOP_FLAG = 13;                // 01101
  81     protected static final int FI_ENCODED_FLAG     = 16;     // 10000
  82 
  83     protected MimeHeaders headers;
  84     protected ContentType contentType;
  85     protected SOAPPartImpl soapPartImpl;
  86     protected FinalArrayList attachments;
  87     protected boolean saved = false;
  88     protected byte[] messageBytes;
  89     protected int messageByteCount;
  90     protected HashMap properties = new HashMap();
  91 
  92     // used for lazy attachment initialization
  93     protected MimeMultipart multiPart = null;
  94     protected boolean attachmentsInitialized = false;
  95 
  96     /**
  97      * True if this part is encoded using Fast Infoset.
  98      * MIME -> application/fastinfoset
  99      */
 100     protected boolean isFastInfoset = false;
 101 
 102     /**
 103      * True if the Accept header of this message includes
 104      * application/fastinfoset
 105      */
 106     protected boolean acceptFastInfoset = false;


 838 
 839     public int countAttachments() {
 840         try {
 841             initializeAllAttachments();
 842         } catch (Exception e) {
 843             throw new RuntimeException(e);
 844         }
 845         if (attachments != null)
 846             return attachments.size();
 847         return 0;
 848     }
 849 
 850     public void addAttachmentPart(AttachmentPart attachment) {
 851         try {
 852             initializeAllAttachments();
 853             this.optimizeAttachmentProcessing = true;
 854         } catch (Exception e) {
 855             throw new RuntimeException(e);
 856         }
 857         if (attachments == null)
 858             attachments = new FinalArrayList();
 859 
 860         attachments.add(attachment);
 861 
 862         needsSave();
 863     }
 864 
 865     static private final Iterator nullIter = Collections.EMPTY_LIST.iterator();
 866 
 867     public Iterator getAttachments() {
 868         try {
 869             initializeAllAttachments();
 870         } catch (Exception e) {
 871             throw new RuntimeException(e);
 872         }
 873         if (attachments == null)
 874             return nullIter;
 875         return attachments.iterator();
 876     }
 877 
 878     private void setFinalContentType(String charset) {
 879         ContentType ct = contentType();
 880         if (ct == null) {
 881             ct = new ContentType();
 882         }
 883         String[] split = getExpectedContentType().split("/");
 884         ct.setPrimaryType(split[0]);
 885         ct.setSubType(split[1]);
 886         ct.setParameter("charset", charset);
 887         headers.setHeader("Content-Type", ct.toString());
 888     }
 889 
 890     private class MimeMatchingIterator implements Iterator {
 891         public MimeMatchingIterator(MimeHeaders headers) {
 892             this.headers = headers;
 893             this.iter = attachments.iterator();
 894         }
 895 
 896         private Iterator iter;
 897         private MimeHeaders headers;
 898         private Object nextAttachment;
 899 
 900         public boolean hasNext() {
 901             if (nextAttachment == null)
 902                 nextAttachment = nextMatch();
 903             return nextAttachment != null;
 904         }
 905 
 906         public Object next() {
 907             if (nextAttachment != null) {
 908                 Object ret = nextAttachment;
 909                 nextAttachment = null;
 910                 return ret;
 911             }
 912 
 913             if (hasNext())
 914                 return nextAttachment;
 915 
 916             return null;
 917         }
 918 
 919         Object nextMatch() {
 920             while (iter.hasNext()) {
 921                 AttachmentPartImpl ap = (AttachmentPartImpl) iter.next();
 922                 if (ap.hasAllHeaders(headers))
 923                     return ap;
 924             }
 925             return null;
 926         }
 927 
 928         public void remove() {
 929             iter.remove();
 930         }
 931     }
 932 
 933     public Iterator getAttachments(MimeHeaders headers) {
 934         try {
 935             initializeAllAttachments();
 936         } catch (Exception e) {
 937             throw new RuntimeException(e);
 938         }
 939         if (attachments == null)
 940             return nullIter;
 941 
 942         return new MimeMatchingIterator(headers);
 943     }
 944 
 945     public void removeAttachments(MimeHeaders headers) {
 946         try {
 947             initializeAllAttachments();
 948         } catch (Exception e) {
 949             throw new RuntimeException(e);
 950         }
 951         if (attachments == null)
 952             return ;
 953 
 954         Iterator it =  new MimeMatchingIterator(headers);
 955         while (it.hasNext()) {
 956             int index = attachments.indexOf(it.next());
 957             attachments.set(index, null);
 958         }
 959         FinalArrayList f = new FinalArrayList();
 960         for (int i = 0; i < attachments.size(); i++) {
 961             if (attachments.get(i) != null) {
 962                 f.add(attachments.get(i));
 963             }
 964         }
 965         attachments = f;
 966        // needsSave();
 967     }
 968 
 969     public AttachmentPart createAttachmentPart() {
 970         return new AttachmentPartImpl();
 971     }
 972 
 973     public  AttachmentPart getAttachment(SOAPElement element)
 974         throws SOAPException {
 975         try {
 976             initializeAllAttachments();
 977         } catch (Exception e) {
 978             throw new RuntimeException(e);
 979         }


1049                             }
1050                         }
1051                     }
1052                 }
1053             }
1054 
1055         } catch (Exception se) {
1056             log.log(Level.SEVERE, "SAAJ0590.soap.unable.to.locate.attachment", new Object[] {uri});
1057             throw new SOAPExceptionImpl(se);
1058         }
1059         return _part;
1060     }
1061 
1062     private final InputStream getHeaderBytes()
1063         throws IOException {
1064         SOAPPartImpl sp = (SOAPPartImpl) getSOAPPart();
1065         return sp.getContentAsStream();
1066     }
1067 
1068     private String convertToSingleLine(String contentType) {
1069         StringBuffer buffer = new StringBuffer();
1070         for (int i = 0; i < contentType.length(); i ++) {
1071             char c = contentType.charAt(i);
1072             if (c != '\r' && c != '\n' && c != '\t')
1073                 buffer.append(c);
1074         }
1075         return buffer.toString();
1076     }
1077 
1078     private MimeMultipart getMimeMessage() throws SOAPException {
1079         try {
1080             SOAPPartImpl soapPart = (SOAPPartImpl) getSOAPPart();
1081             MimeBodyPart mimeSoapPart = soapPart.getMimePart();
1082 
1083             /*
1084              * Get content type from this message instead of soapPart
1085              * to ensure agreement if soapPart is transcoded (XML <-> FI)
1086              */
1087             ContentType soapPartCtype = new ContentType(getExpectedContentType());
1088 
1089             if (!isFastInfoset) {
1090                 soapPartCtype.setParameter("charset", initCharset());
1091             }
1092             mimeSoapPart.setHeader("Content-Type", soapPartCtype.toString());
1093 
1094             MimeMultipart headerAndBody = null;
1095 
1096             if (!switchOffBM && !switchOffLazyAttachment &&
1097                    (multiPart != null) && !attachmentsInitialized) {
1098                 headerAndBody = new BMMimeMultipart();
1099                 headerAndBody.addBodyPart(mimeSoapPart);
1100                 if (attachments != null) {
1101                     for (Iterator eachAttachment = attachments.iterator();
1102                          eachAttachment.hasNext();) {
1103                         headerAndBody.addBodyPart(
1104                             ((AttachmentPartImpl) eachAttachment.next())
1105                                 .getMimePart());
1106                     }
1107                 }
1108                 InputStream in = ((BMMimeMultipart)multiPart).getInputStream();
1109                 if (!((BMMimeMultipart)multiPart).lastBodyPartFound() &&
1110                     !((BMMimeMultipart)multiPart).isEndOfStream()) {
1111                     ((BMMimeMultipart)headerAndBody).setInputStream(in);
1112                     ((BMMimeMultipart)headerAndBody).setBoundary(
1113                         ((BMMimeMultipart)multiPart).getBoundary());
1114                     ((BMMimeMultipart)headerAndBody).
1115                         setLazyAttachments(lazyAttachments);
1116                 }
1117 
1118             } else {
1119                 headerAndBody = new MimeMultipart();
1120                 headerAndBody.addBodyPart(mimeSoapPart);
1121 


1407 
1408     public SOAPHeader getSOAPHeader() throws SOAPException {
1409         SOAPHeader hdr = getSOAPPart().getEnvelope().getHeader();
1410         /*if (hdr == null) {
1411             throw new SOAPException("No SOAP Header was found in the SOAP Message");
1412         }*/
1413         return hdr;
1414     }
1415 
1416     private void initializeAllAttachments ()
1417         throws MessagingException, SOAPException {
1418         if (switchOffBM || switchOffLazyAttachment) {
1419             return;
1420         }
1421 
1422         if (attachmentsInitialized || (multiPart == null)) {
1423             return;
1424         }
1425 
1426         if (attachments == null)
1427             attachments = new FinalArrayList();
1428 
1429         int count = multiPart.getCount();
1430         for (int i=0; i < count; i++ ) {
1431             initializeAttachment(multiPart.getBodyPart(i));
1432         }
1433         attachmentsInitialized = true;
1434         //multiPart = null;
1435         needsSave();
1436      }
1437 
1438     private void initializeAttachment(MimeBodyPart mbp) throws SOAPException {
1439         AttachmentPartImpl attachmentPart = new AttachmentPartImpl();
1440         DataHandler attachmentHandler = mbp.getDataHandler();
1441         attachmentPart.setDataHandler(attachmentHandler);
1442 
1443         AttachmentPartImpl.copyMimeHeaders(mbp, attachmentPart);
1444         attachments.add(attachmentPart);
1445     }
1446 
1447     private void initializeAttachment(MimeMultipart multiPart, int i)


   1 /*
   2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  66     public static final String CONTENT_ID             = "Content-ID";
  67     public static final String CONTENT_LOCATION       = "Content-Location";
  68 
  69     protected static final Logger log =
  70         Logger.getLogger(LogDomainConstants.SOAP_DOMAIN,
  71                          "com.sun.xml.internal.messaging.saaj.soap.LocalStrings");
  72 
  73     protected static final int PLAIN_XML_FLAG      = 1;      // 00001
  74     protected static final int MIME_MULTIPART_FLAG = 2;      // 00010
  75     protected static final int SOAP1_1_FLAG = 4;             // 00100
  76     protected static final int SOAP1_2_FLAG = 8;             // 01000
  77     //protected static final int MIME_MULTIPART_XOP_FLAG = 14; // 01110
  78     protected static final int MIME_MULTIPART_XOP_SOAP1_1_FLAG = 6;  // 00110
  79     protected static final int MIME_MULTIPART_XOP_SOAP1_2_FLAG = 10; // 01010
  80     protected static final int XOP_FLAG = 13;                // 01101
  81     protected static final int FI_ENCODED_FLAG     = 16;     // 10000
  82 
  83     protected MimeHeaders headers;
  84     protected ContentType contentType;
  85     protected SOAPPartImpl soapPartImpl;
  86     protected FinalArrayList<AttachmentPart> attachments;
  87     protected boolean saved = false;
  88     protected byte[] messageBytes;
  89     protected int messageByteCount;
  90     protected HashMap properties = new HashMap();
  91 
  92     // used for lazy attachment initialization
  93     protected MimeMultipart multiPart = null;
  94     protected boolean attachmentsInitialized = false;
  95 
  96     /**
  97      * True if this part is encoded using Fast Infoset.
  98      * MIME -> application/fastinfoset
  99      */
 100     protected boolean isFastInfoset = false;
 101 
 102     /**
 103      * True if the Accept header of this message includes
 104      * application/fastinfoset
 105      */
 106     protected boolean acceptFastInfoset = false;


 838 
 839     public int countAttachments() {
 840         try {
 841             initializeAllAttachments();
 842         } catch (Exception e) {
 843             throw new RuntimeException(e);
 844         }
 845         if (attachments != null)
 846             return attachments.size();
 847         return 0;
 848     }
 849 
 850     public void addAttachmentPart(AttachmentPart attachment) {
 851         try {
 852             initializeAllAttachments();
 853             this.optimizeAttachmentProcessing = true;
 854         } catch (Exception e) {
 855             throw new RuntimeException(e);
 856         }
 857         if (attachments == null)
 858             attachments = new FinalArrayList<AttachmentPart>();
 859 
 860         attachments.add(attachment);
 861 
 862         needsSave();
 863     }
 864 
 865     static private final Iterator nullIter = Collections.EMPTY_LIST.iterator();
 866 
 867     public Iterator getAttachments() {
 868         try {
 869             initializeAllAttachments();
 870         } catch (Exception e) {
 871             throw new RuntimeException(e);
 872         }
 873         if (attachments == null)
 874             return nullIter;
 875         return attachments.iterator();
 876     }
 877 
 878     private void setFinalContentType(String charset) {
 879         ContentType ct = contentType();
 880         if (ct == null) {
 881             ct = new ContentType();
 882         }
 883         String[] split = getExpectedContentType().split("/");
 884         ct.setPrimaryType(split[0]);
 885         ct.setSubType(split[1]);
 886         ct.setParameter("charset", charset);
 887         headers.setHeader("Content-Type", ct.toString());
 888     }
 889 
 890     private class MimeMatchingIterator implements Iterator<AttachmentPart> {
 891         public MimeMatchingIterator(MimeHeaders headers) {
 892             this.headers = headers;
 893             this.iter = attachments.iterator();
 894         }
 895 
 896         private Iterator<AttachmentPart> iter;
 897         private MimeHeaders headers;
 898         private AttachmentPart nextAttachment;
 899 
 900         public boolean hasNext() {
 901             if (nextAttachment == null)
 902                 nextAttachment = nextMatch();
 903             return nextAttachment != null;
 904         }
 905 
 906         public AttachmentPart next() {
 907             if (nextAttachment != null) {
 908                 AttachmentPart ret = nextAttachment;
 909                 nextAttachment = null;
 910                 return ret;
 911             }
 912 
 913             if (hasNext())
 914                 return nextAttachment;
 915 
 916             return null;
 917         }
 918 
 919         AttachmentPart nextMatch() {
 920             while (iter.hasNext()) {
 921                 AttachmentPartImpl ap = (AttachmentPartImpl) iter.next();
 922                 if (ap.hasAllHeaders(headers))
 923                     return ap;
 924             }
 925             return null;
 926         }
 927 
 928         public void remove() {
 929             iter.remove();
 930         }
 931     }
 932 
 933     public Iterator getAttachments(MimeHeaders headers) {
 934         try {
 935             initializeAllAttachments();
 936         } catch (Exception e) {
 937             throw new RuntimeException(e);
 938         }
 939         if (attachments == null)
 940             return nullIter;
 941 
 942         return new MimeMatchingIterator(headers);
 943     }
 944 
 945     public void removeAttachments(MimeHeaders headers) {
 946         try {
 947             initializeAllAttachments();
 948         } catch (Exception e) {
 949             throw new RuntimeException(e);
 950         }
 951         if (attachments == null)
 952             return ;
 953 
 954         Iterator<AttachmentPart> it =  new MimeMatchingIterator(headers);
 955         while (it.hasNext()) {
 956             int index = attachments.indexOf(it.next());
 957             attachments.set(index, null);
 958         }
 959         FinalArrayList<AttachmentPart> f = new FinalArrayList<AttachmentPart>();
 960         for (int i = 0; i < attachments.size(); i++) {
 961             if (attachments.get(i) != null) {
 962                 f.add(attachments.get(i));
 963             }
 964         }
 965         attachments = f;
 966        // needsSave();
 967     }
 968 
 969     public AttachmentPart createAttachmentPart() {
 970         return new AttachmentPartImpl();
 971     }
 972 
 973     public  AttachmentPart getAttachment(SOAPElement element)
 974         throws SOAPException {
 975         try {
 976             initializeAllAttachments();
 977         } catch (Exception e) {
 978             throw new RuntimeException(e);
 979         }


1049                             }
1050                         }
1051                     }
1052                 }
1053             }
1054 
1055         } catch (Exception se) {
1056             log.log(Level.SEVERE, "SAAJ0590.soap.unable.to.locate.attachment", new Object[] {uri});
1057             throw new SOAPExceptionImpl(se);
1058         }
1059         return _part;
1060     }
1061 
1062     private final InputStream getHeaderBytes()
1063         throws IOException {
1064         SOAPPartImpl sp = (SOAPPartImpl) getSOAPPart();
1065         return sp.getContentAsStream();
1066     }
1067 
1068     private String convertToSingleLine(String contentType) {
1069         StringBuilder buffer = new StringBuilder();
1070         for (int i = 0; i < contentType.length(); i ++) {
1071             char c = contentType.charAt(i);
1072             if (c != '\r' && c != '\n' && c != '\t')
1073                 buffer.append(c);
1074         }
1075         return buffer.toString();
1076     }
1077 
1078     private MimeMultipart getMimeMessage() throws SOAPException {
1079         try {
1080             SOAPPartImpl soapPart = (SOAPPartImpl) getSOAPPart();
1081             MimeBodyPart mimeSoapPart = soapPart.getMimePart();
1082 
1083             /*
1084              * Get content type from this message instead of soapPart
1085              * to ensure agreement if soapPart is transcoded (XML <-> FI)
1086              */
1087             ContentType soapPartCtype = new ContentType(getExpectedContentType());
1088 
1089             if (!isFastInfoset) {
1090                 soapPartCtype.setParameter("charset", initCharset());
1091             }
1092             mimeSoapPart.setHeader("Content-Type", soapPartCtype.toString());
1093 
1094             MimeMultipart headerAndBody = null;
1095 
1096             if (!switchOffBM && !switchOffLazyAttachment &&
1097                    (multiPart != null) && !attachmentsInitialized) {
1098                 headerAndBody = new BMMimeMultipart();
1099                 headerAndBody.addBodyPart(mimeSoapPart);
1100                 if (attachments != null) {
1101                     for (Iterator<AttachmentPart> eachAttachment = attachments.iterator();
1102                          eachAttachment.hasNext();) {
1103                         headerAndBody.addBodyPart(
1104                             ((AttachmentPartImpl) eachAttachment.next())
1105                                 .getMimePart());
1106                     }
1107                 }
1108                 InputStream in = ((BMMimeMultipart)multiPart).getInputStream();
1109                 if (!((BMMimeMultipart)multiPart).lastBodyPartFound() &&
1110                     !((BMMimeMultipart)multiPart).isEndOfStream()) {
1111                     ((BMMimeMultipart)headerAndBody).setInputStream(in);
1112                     ((BMMimeMultipart)headerAndBody).setBoundary(
1113                         ((BMMimeMultipart)multiPart).getBoundary());
1114                     ((BMMimeMultipart)headerAndBody).
1115                         setLazyAttachments(lazyAttachments);
1116                 }
1117 
1118             } else {
1119                 headerAndBody = new MimeMultipart();
1120                 headerAndBody.addBodyPart(mimeSoapPart);
1121 


1407 
1408     public SOAPHeader getSOAPHeader() throws SOAPException {
1409         SOAPHeader hdr = getSOAPPart().getEnvelope().getHeader();
1410         /*if (hdr == null) {
1411             throw new SOAPException("No SOAP Header was found in the SOAP Message");
1412         }*/
1413         return hdr;
1414     }
1415 
1416     private void initializeAllAttachments ()
1417         throws MessagingException, SOAPException {
1418         if (switchOffBM || switchOffLazyAttachment) {
1419             return;
1420         }
1421 
1422         if (attachmentsInitialized || (multiPart == null)) {
1423             return;
1424         }
1425 
1426         if (attachments == null)
1427             attachments = new FinalArrayList<AttachmentPart>();
1428 
1429         int count = multiPart.getCount();
1430         for (int i=0; i < count; i++ ) {
1431             initializeAttachment(multiPart.getBodyPart(i));
1432         }
1433         attachmentsInitialized = true;
1434         //multiPart = null;
1435         needsSave();
1436      }
1437 
1438     private void initializeAttachment(MimeBodyPart mbp) throws SOAPException {
1439         AttachmentPartImpl attachmentPart = new AttachmentPartImpl();
1440         DataHandler attachmentHandler = mbp.getDataHandler();
1441         attachmentPart.setDataHandler(attachmentHandler);
1442 
1443         AttachmentPartImpl.copyMimeHeaders(mbp, attachmentPart);
1444         attachments.add(attachmentPart);
1445     }
1446 
1447     private void initializeAttachment(MimeMultipart multiPart, int i)


< prev index next >