jaxws/src/share/classes/com/sun/xml/internal/messaging/saaj/soap/MessageImpl.java
author tbell
Mon, 04 May 2009 21:10:41 -0700
changeset 2719 99d59312294b
parent 2678 57cf2a1c1a05
child 3530 18fb7507984b
permissions -rw-r--r--
6658158: Mutable statics in SAAJ (findbugs) 6658163: txw2.DatatypeWriter.BUILDIN is a mutable static (findbugs) Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8
474761f14bca Initial load
duke
parents:
diff changeset
     1
/*
2678
57cf2a1c1a05 6831313: update jaxws in OpenJDK7 to 2.1 plus bug fixes from OpenJDK 6
tbell
parents: 8
diff changeset
     2
 * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
8
474761f14bca Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
474761f14bca Initial load
duke
parents:
diff changeset
     4
 *
474761f14bca Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
474761f14bca Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
474761f14bca Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
474761f14bca Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
474761f14bca Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
474761f14bca Initial load
duke
parents:
diff changeset
    10
 *
474761f14bca Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
474761f14bca Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
474761f14bca Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
474761f14bca Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
474761f14bca Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
474761f14bca Initial load
duke
parents:
diff changeset
    16
 *
474761f14bca Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
474761f14bca Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
474761f14bca Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
474761f14bca Initial load
duke
parents:
diff changeset
    20
 *
474761f14bca Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
474761f14bca Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
474761f14bca Initial load
duke
parents:
diff changeset
    23
 * have any questions.
474761f14bca Initial load
duke
parents:
diff changeset
    24
 */
2678
57cf2a1c1a05 6831313: update jaxws in OpenJDK7 to 2.1 plus bug fixes from OpenJDK 6
tbell
parents: 8
diff changeset
    25
/*
2719
99d59312294b 6658158: Mutable statics in SAAJ (findbugs)
tbell
parents: 2678
diff changeset
    26
 *
99d59312294b 6658158: Mutable statics in SAAJ (findbugs)
tbell
parents: 2678
diff changeset
    27
 *
99d59312294b 6658158: Mutable statics in SAAJ (findbugs)
tbell
parents: 2678
diff changeset
    28
 *
2678
57cf2a1c1a05 6831313: update jaxws in OpenJDK7 to 2.1 plus bug fixes from OpenJDK 6
tbell
parents: 8
diff changeset
    29
 */
57cf2a1c1a05 6831313: update jaxws in OpenJDK7 to 2.1 plus bug fixes from OpenJDK 6
tbell
parents: 8
diff changeset
    30
57cf2a1c1a05 6831313: update jaxws in OpenJDK7 to 2.1 plus bug fixes from OpenJDK 6
tbell
parents: 8
diff changeset
    31
8
474761f14bca Initial load
duke
parents:
diff changeset
    32
package com.sun.xml.internal.messaging.saaj.soap;
474761f14bca Initial load
duke
parents:
diff changeset
    33
474761f14bca Initial load
duke
parents:
diff changeset
    34
import java.io.*;
474761f14bca Initial load
duke
parents:
diff changeset
    35
import java.util.*;
474761f14bca Initial load
duke
parents:
diff changeset
    36
import java.util.logging.Level;
474761f14bca Initial load
duke
parents:
diff changeset
    37
import java.util.logging.Logger;
474761f14bca Initial load
duke
parents:
diff changeset
    38
474761f14bca Initial load
duke
parents:
diff changeset
    39
import javax.activation.DataHandler;
474761f14bca Initial load
duke
parents:
diff changeset
    40
import javax.activation.DataSource;
474761f14bca Initial load
duke
parents:
diff changeset
    41
import javax.xml.soap.*;
474761f14bca Initial load
duke
parents:
diff changeset
    42
import javax.xml.transform.Source;
474761f14bca Initial load
duke
parents:
diff changeset
    43
import javax.xml.transform.stream.StreamSource;
474761f14bca Initial load
duke
parents:
diff changeset
    44
474761f14bca Initial load
duke
parents:
diff changeset
    45
import com.sun.xml.internal.messaging.saaj.packaging.mime.Header;
474761f14bca Initial load
duke
parents:
diff changeset
    46
import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.*;
474761f14bca Initial load
duke
parents:
diff changeset
    47
import com.sun.xml.internal.messaging.saaj.packaging.mime.util.*;
474761f14bca Initial load
duke
parents:
diff changeset
    48
import com.sun.xml.internal.messaging.saaj.packaging.mime.MessagingException;
474761f14bca Initial load
duke
parents:
diff changeset
    49
474761f14bca Initial load
duke
parents:
diff changeset
    50
import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl;
474761f14bca Initial load
duke
parents:
diff changeset
    51
import com.sun.xml.internal.messaging.saaj.soap.impl.EnvelopeImpl;
474761f14bca Initial load
duke
parents:
diff changeset
    52
import com.sun.xml.internal.messaging.saaj.util.*;
474761f14bca Initial load
duke
parents:
diff changeset
    53
474761f14bca Initial load
duke
parents:
diff changeset
    54
/**
474761f14bca Initial load
duke
parents:
diff changeset
    55
 * The message implementation for SOAP messages with
474761f14bca Initial load
duke
parents:
diff changeset
    56
 * attachments. Messages for specific profiles will likely extend this
474761f14bca Initial load
duke
parents:
diff changeset
    57
 * MessageImpl class and add more value for that particular profile.
474761f14bca Initial load
duke
parents:
diff changeset
    58
 *
474761f14bca Initial load
duke
parents:
diff changeset
    59
 * @author Anil Vijendran (akv@eng.sun.com)
474761f14bca Initial load
duke
parents:
diff changeset
    60
 * @author Rajiv Mordani (rajiv.mordani@sun.com)
474761f14bca Initial load
duke
parents:
diff changeset
    61
 * @author Manveen Kaur (manveen.kaur@sun.com)
474761f14bca Initial load
duke
parents:
diff changeset
    62
 */
474761f14bca Initial load
duke
parents:
diff changeset
    63
474761f14bca Initial load
duke
parents:
diff changeset
    64
public abstract class MessageImpl
474761f14bca Initial load
duke
parents:
diff changeset
    65
    extends SOAPMessage
474761f14bca Initial load
duke
parents:
diff changeset
    66
    implements SOAPConstants {
474761f14bca Initial load
duke
parents:
diff changeset
    67
474761f14bca Initial load
duke
parents:
diff changeset
    68
474761f14bca Initial load
duke
parents:
diff changeset
    69
    public static final String CONTENT_ID             = "Content-ID";
474761f14bca Initial load
duke
parents:
diff changeset
    70
    public static final String CONTENT_LOCATION       = "Content-Location";
474761f14bca Initial load
duke
parents:
diff changeset
    71
2719
99d59312294b 6658158: Mutable statics in SAAJ (findbugs)
tbell
parents: 2678
diff changeset
    72
    protected static final Logger log =
8
474761f14bca Initial load
duke
parents:
diff changeset
    73
        Logger.getLogger(LogDomainConstants.SOAP_DOMAIN,
474761f14bca Initial load
duke
parents:
diff changeset
    74
                         "com.sun.xml.internal.messaging.saaj.soap.LocalStrings");
474761f14bca Initial load
duke
parents:
diff changeset
    75
474761f14bca Initial load
duke
parents:
diff changeset
    76
    protected static final int PLAIN_XML_FLAG      = 1;      // 00001
474761f14bca Initial load
duke
parents:
diff changeset
    77
    protected static final int MIME_MULTIPART_FLAG = 2;      // 00010
474761f14bca Initial load
duke
parents:
diff changeset
    78
    protected static final int SOAP1_1_FLAG = 4;             // 00100
474761f14bca Initial load
duke
parents:
diff changeset
    79
    protected static final int SOAP1_2_FLAG = 8;             // 01000
474761f14bca Initial load
duke
parents:
diff changeset
    80
    protected static final int MIME_MULTIPART_XOP_FLAG = 14; // 01110
474761f14bca Initial load
duke
parents:
diff changeset
    81
    protected static final int XOP_FLAG = 13;                // 01101
474761f14bca Initial load
duke
parents:
diff changeset
    82
    protected static final int FI_ENCODED_FLAG     = 16;     // 10000
474761f14bca Initial load
duke
parents:
diff changeset
    83
474761f14bca Initial load
duke
parents:
diff changeset
    84
    protected MimeHeaders headers;
474761f14bca Initial load
duke
parents:
diff changeset
    85
    protected SOAPPartImpl soapPart;
474761f14bca Initial load
duke
parents:
diff changeset
    86
    protected FinalArrayList attachments;
474761f14bca Initial load
duke
parents:
diff changeset
    87
    protected boolean saved = false;
474761f14bca Initial load
duke
parents:
diff changeset
    88
    protected byte[] messageBytes;
474761f14bca Initial load
duke
parents:
diff changeset
    89
    protected int messageByteCount;
474761f14bca Initial load
duke
parents:
diff changeset
    90
    protected HashMap properties = new HashMap();
474761f14bca Initial load
duke
parents:
diff changeset
    91
474761f14bca Initial load
duke
parents:
diff changeset
    92
    // used for lazy attachment initialization
474761f14bca Initial load
duke
parents:
diff changeset
    93
    protected MimeMultipart multiPart = null;
474761f14bca Initial load
duke
parents:
diff changeset
    94
    protected boolean attachmentsInitialized = false;
474761f14bca Initial load
duke
parents:
diff changeset
    95
474761f14bca Initial load
duke
parents:
diff changeset
    96
    /**
474761f14bca Initial load
duke
parents:
diff changeset
    97
     * True if this part is encoded using Fast Infoset.
474761f14bca Initial load
duke
parents:
diff changeset
    98
     * MIME -> application/fastinfoset
474761f14bca Initial load
duke
parents:
diff changeset
    99
     */
474761f14bca Initial load
duke
parents:
diff changeset
   100
    protected boolean isFastInfoset = false;
474761f14bca Initial load
duke
parents:
diff changeset
   101
474761f14bca Initial load
duke
parents:
diff changeset
   102
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   103
     * True if the Accept header of this message includes
474761f14bca Initial load
duke
parents:
diff changeset
   104
     * application/fastinfoset
474761f14bca Initial load
duke
parents:
diff changeset
   105
     */
474761f14bca Initial load
duke
parents:
diff changeset
   106
    protected boolean acceptFastInfoset = false;
474761f14bca Initial load
duke
parents:
diff changeset
   107
474761f14bca Initial load
duke
parents:
diff changeset
   108
    protected MimeMultipart mmp = null;
474761f14bca Initial load
duke
parents:
diff changeset
   109
474761f14bca Initial load
duke
parents:
diff changeset
   110
    // if attachments are present, don't read the entire message in byte stream in saveTo()
474761f14bca Initial load
duke
parents:
diff changeset
   111
    private boolean optimizeAttachmentProcessing = true;
474761f14bca Initial load
duke
parents:
diff changeset
   112
474761f14bca Initial load
duke
parents:
diff changeset
   113
    // switch back to old MimeMultipart incase of problem
474761f14bca Initial load
duke
parents:
diff changeset
   114
    private static boolean switchOffBM = false;
474761f14bca Initial load
duke
parents:
diff changeset
   115
    private static boolean switchOffLazyAttachment = false;
474761f14bca Initial load
duke
parents:
diff changeset
   116
474761f14bca Initial load
duke
parents:
diff changeset
   117
    static {
474761f14bca Initial load
duke
parents:
diff changeset
   118
        try {
474761f14bca Initial load
duke
parents:
diff changeset
   119
            String s = System.getProperty("saaj.mime.optimization");
474761f14bca Initial load
duke
parents:
diff changeset
   120
            if ((s != null) && s.equals("false")) {
474761f14bca Initial load
duke
parents:
diff changeset
   121
                switchOffBM = true;
474761f14bca Initial load
duke
parents:
diff changeset
   122
            }
474761f14bca Initial load
duke
parents:
diff changeset
   123
            s = System.getProperty("saaj.lazy.mime.optimization");
474761f14bca Initial load
duke
parents:
diff changeset
   124
            if ((s != null) && s.equals("false")) {
474761f14bca Initial load
duke
parents:
diff changeset
   125
                switchOffLazyAttachment = true;
474761f14bca Initial load
duke
parents:
diff changeset
   126
            }
474761f14bca Initial load
duke
parents:
diff changeset
   127
        } catch (SecurityException ex) {
474761f14bca Initial load
duke
parents:
diff changeset
   128
            // ignore it
474761f14bca Initial load
duke
parents:
diff changeset
   129
        }
474761f14bca Initial load
duke
parents:
diff changeset
   130
    }
474761f14bca Initial load
duke
parents:
diff changeset
   131
474761f14bca Initial load
duke
parents:
diff changeset
   132
    //property to indicate optimized serialization for lazy attachments
474761f14bca Initial load
duke
parents:
diff changeset
   133
    private boolean lazyAttachments = false;
474761f14bca Initial load
duke
parents:
diff changeset
   134
474761f14bca Initial load
duke
parents:
diff changeset
   135
    // most of the times, Content-Types are already all lower cased.
474761f14bca Initial load
duke
parents:
diff changeset
   136
    // String.toLowerCase() works faster in this case, so even if you
474761f14bca Initial load
duke
parents:
diff changeset
   137
    // are only doing one comparison, it pays off to use String.toLowerCase()
474761f14bca Initial load
duke
parents:
diff changeset
   138
    // than String.equalsIgnoreCase(). When you do more than one comparison,
474761f14bca Initial load
duke
parents:
diff changeset
   139
    // the benefits of String.toLowerCase() dominates.
474761f14bca Initial load
duke
parents:
diff changeset
   140
    //
474761f14bca Initial load
duke
parents:
diff changeset
   141
    //
474761f14bca Initial load
duke
parents:
diff changeset
   142
    // for FI,
474761f14bca Initial load
duke
parents:
diff changeset
   143
    //   use application/fastinfoset for SOAP 1.1
474761f14bca Initial load
duke
parents:
diff changeset
   144
    //   use application/soap+fastinfoset for SOAP 1.2
474761f14bca Initial load
duke
parents:
diff changeset
   145
    // to speed up comparisons, test methods always use lower cases.
474761f14bca Initial load
duke
parents:
diff changeset
   146
474761f14bca Initial load
duke
parents:
diff changeset
   147
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   148
     * @param primary
474761f14bca Initial load
duke
parents:
diff changeset
   149
     *      must be all lower case
474761f14bca Initial load
duke
parents:
diff changeset
   150
     * @param sub
474761f14bca Initial load
duke
parents:
diff changeset
   151
     *      must be all lower case
474761f14bca Initial load
duke
parents:
diff changeset
   152
     */
474761f14bca Initial load
duke
parents:
diff changeset
   153
    private static boolean isSoap1_1Type(String primary, String sub) {
474761f14bca Initial load
duke
parents:
diff changeset
   154
        return primary.equals("text") && sub.equals("xml")
474761f14bca Initial load
duke
parents:
diff changeset
   155
            || primary.equals("application")
474761f14bca Initial load
duke
parents:
diff changeset
   156
               && sub.equals("fastinfoset");
474761f14bca Initial load
duke
parents:
diff changeset
   157
    }
474761f14bca Initial load
duke
parents:
diff changeset
   158
474761f14bca Initial load
duke
parents:
diff changeset
   159
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   160
     * @param type
474761f14bca Initial load
duke
parents:
diff changeset
   161
     *      must be all lower case
474761f14bca Initial load
duke
parents:
diff changeset
   162
     */
474761f14bca Initial load
duke
parents:
diff changeset
   163
    private static boolean isEqualToSoap1_1Type(String type) {
474761f14bca Initial load
duke
parents:
diff changeset
   164
        return type.startsWith("text/xml") ||
474761f14bca Initial load
duke
parents:
diff changeset
   165
               type.startsWith("application/fastinfoset");
474761f14bca Initial load
duke
parents:
diff changeset
   166
    }
474761f14bca Initial load
duke
parents:
diff changeset
   167
474761f14bca Initial load
duke
parents:
diff changeset
   168
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   169
     * @param primary
474761f14bca Initial load
duke
parents:
diff changeset
   170
     *      must be all lower case
474761f14bca Initial load
duke
parents:
diff changeset
   171
     * @param sub
474761f14bca Initial load
duke
parents:
diff changeset
   172
     *      must be all lower case
474761f14bca Initial load
duke
parents:
diff changeset
   173
     */
474761f14bca Initial load
duke
parents:
diff changeset
   174
    private static boolean isSoap1_2Type(String primary, String sub) {
474761f14bca Initial load
duke
parents:
diff changeset
   175
        return primary.equals("application")
474761f14bca Initial load
duke
parents:
diff changeset
   176
               && (sub.equals("soap+xml")
474761f14bca Initial load
duke
parents:
diff changeset
   177
                   || sub.equals("soap+fastinfoset"));
474761f14bca Initial load
duke
parents:
diff changeset
   178
    }
474761f14bca Initial load
duke
parents:
diff changeset
   179
474761f14bca Initial load
duke
parents:
diff changeset
   180
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   181
     * @param type
474761f14bca Initial load
duke
parents:
diff changeset
   182
     *      must be all lower case
474761f14bca Initial load
duke
parents:
diff changeset
   183
     */
474761f14bca Initial load
duke
parents:
diff changeset
   184
    private static boolean isEqualToSoap1_2Type(String type) {
474761f14bca Initial load
duke
parents:
diff changeset
   185
        return type.startsWith("application/soap+xml") ||
474761f14bca Initial load
duke
parents:
diff changeset
   186
               type.startsWith("application/soap+fastinfoset");
474761f14bca Initial load
duke
parents:
diff changeset
   187
    }
474761f14bca Initial load
duke
parents:
diff changeset
   188
474761f14bca Initial load
duke
parents:
diff changeset
   189
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   190
      * Construct a new message. This will be invoked before message
474761f14bca Initial load
duke
parents:
diff changeset
   191
      * sends.
474761f14bca Initial load
duke
parents:
diff changeset
   192
      */
474761f14bca Initial load
duke
parents:
diff changeset
   193
    protected MessageImpl() {
474761f14bca Initial load
duke
parents:
diff changeset
   194
        this(false, false);
474761f14bca Initial load
duke
parents:
diff changeset
   195
        attachmentsInitialized = true;
474761f14bca Initial load
duke
parents:
diff changeset
   196
    }
474761f14bca Initial load
duke
parents:
diff changeset
   197
474761f14bca Initial load
duke
parents:
diff changeset
   198
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   199
      * Construct a new message. This will be invoked before message
474761f14bca Initial load
duke
parents:
diff changeset
   200
      * sends.
474761f14bca Initial load
duke
parents:
diff changeset
   201
      */
474761f14bca Initial load
duke
parents:
diff changeset
   202
    protected MessageImpl(boolean isFastInfoset, boolean acceptFastInfoset) {
474761f14bca Initial load
duke
parents:
diff changeset
   203
        this.isFastInfoset = isFastInfoset;
474761f14bca Initial load
duke
parents:
diff changeset
   204
        this.acceptFastInfoset = acceptFastInfoset;
474761f14bca Initial load
duke
parents:
diff changeset
   205
474761f14bca Initial load
duke
parents:
diff changeset
   206
        headers = new MimeHeaders();
474761f14bca Initial load
duke
parents:
diff changeset
   207
        headers.setHeader("Accept", getExpectedAcceptHeader());
474761f14bca Initial load
duke
parents:
diff changeset
   208
    }
474761f14bca Initial load
duke
parents:
diff changeset
   209
474761f14bca Initial load
duke
parents:
diff changeset
   210
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   211
     * Shallow copy.
474761f14bca Initial load
duke
parents:
diff changeset
   212
     */
474761f14bca Initial load
duke
parents:
diff changeset
   213
    protected MessageImpl(SOAPMessage msg) {
474761f14bca Initial load
duke
parents:
diff changeset
   214
        if (!(msg instanceof MessageImpl)) {
474761f14bca Initial load
duke
parents:
diff changeset
   215
            // don't know how to handle this.
474761f14bca Initial load
duke
parents:
diff changeset
   216
        }
474761f14bca Initial load
duke
parents:
diff changeset
   217
        MessageImpl src = (MessageImpl) msg;
474761f14bca Initial load
duke
parents:
diff changeset
   218
        this.headers = src.headers;
474761f14bca Initial load
duke
parents:
diff changeset
   219
        this.soapPart = src.soapPart;
474761f14bca Initial load
duke
parents:
diff changeset
   220
        this.attachments = src.attachments;
474761f14bca Initial load
duke
parents:
diff changeset
   221
        this.saved = src.saved;
474761f14bca Initial load
duke
parents:
diff changeset
   222
        this.messageBytes = src.messageBytes;
474761f14bca Initial load
duke
parents:
diff changeset
   223
        this.messageByteCount = src.messageByteCount;
474761f14bca Initial load
duke
parents:
diff changeset
   224
        this.properties = src.properties;
474761f14bca Initial load
duke
parents:
diff changeset
   225
    }
474761f14bca Initial load
duke
parents:
diff changeset
   226
474761f14bca Initial load
duke
parents:
diff changeset
   227
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   228
     * @param stat
474761f14bca Initial load
duke
parents:
diff changeset
   229
     *      the mask value obtained from {@link #identifyContentType(ContentType)}
474761f14bca Initial load
duke
parents:
diff changeset
   230
     */
474761f14bca Initial load
duke
parents:
diff changeset
   231
    protected static boolean isSoap1_1Content(int stat) {
474761f14bca Initial load
duke
parents:
diff changeset
   232
        return (stat & SOAP1_1_FLAG) != 0;
474761f14bca Initial load
duke
parents:
diff changeset
   233
    }
474761f14bca Initial load
duke
parents:
diff changeset
   234
474761f14bca Initial load
duke
parents:
diff changeset
   235
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   236
     * @param stat
474761f14bca Initial load
duke
parents:
diff changeset
   237
     *      the mask value obtained from {@link #identifyContentType(ContentType)}
474761f14bca Initial load
duke
parents:
diff changeset
   238
     */
474761f14bca Initial load
duke
parents:
diff changeset
   239
    protected static boolean isSoap1_2Content(int stat) {
474761f14bca Initial load
duke
parents:
diff changeset
   240
        return (stat & SOAP1_2_FLAG) != 0;
474761f14bca Initial load
duke
parents:
diff changeset
   241
    }
474761f14bca Initial load
duke
parents:
diff changeset
   242
474761f14bca Initial load
duke
parents:
diff changeset
   243
     private static boolean isMimeMultipartXOPPackage(ContentType contentType) {
474761f14bca Initial load
duke
parents:
diff changeset
   244
        String type = contentType.getParameter("type");
474761f14bca Initial load
duke
parents:
diff changeset
   245
        if(type==null)
474761f14bca Initial load
duke
parents:
diff changeset
   246
            return false;
474761f14bca Initial load
duke
parents:
diff changeset
   247
474761f14bca Initial load
duke
parents:
diff changeset
   248
        type = type.toLowerCase();
474761f14bca Initial load
duke
parents:
diff changeset
   249
        if(!type.startsWith("application/xop+xml"))
474761f14bca Initial load
duke
parents:
diff changeset
   250
            return false;
474761f14bca Initial load
duke
parents:
diff changeset
   251
474761f14bca Initial load
duke
parents:
diff changeset
   252
        String startinfo = contentType.getParameter("start-info");
474761f14bca Initial load
duke
parents:
diff changeset
   253
        if(startinfo == null)
474761f14bca Initial load
duke
parents:
diff changeset
   254
            return false;
474761f14bca Initial load
duke
parents:
diff changeset
   255
        startinfo = startinfo.toLowerCase();
474761f14bca Initial load
duke
parents:
diff changeset
   256
        return isEqualToSoap1_2Type(startinfo) || isEqualToSoap1_1Type(startinfo);
474761f14bca Initial load
duke
parents:
diff changeset
   257
    }
474761f14bca Initial load
duke
parents:
diff changeset
   258
474761f14bca Initial load
duke
parents:
diff changeset
   259
    private static boolean isSOAPBodyXOPPackage(ContentType contentType){
474761f14bca Initial load
duke
parents:
diff changeset
   260
        String primary = contentType.getPrimaryType();
474761f14bca Initial load
duke
parents:
diff changeset
   261
        String sub = contentType.getSubType();
474761f14bca Initial load
duke
parents:
diff changeset
   262
474761f14bca Initial load
duke
parents:
diff changeset
   263
        if (primary.equalsIgnoreCase("application")) {
474761f14bca Initial load
duke
parents:
diff changeset
   264
            if (sub.equalsIgnoreCase("xop+xml")) {
474761f14bca Initial load
duke
parents:
diff changeset
   265
                String type = getTypeParameter(contentType);
474761f14bca Initial load
duke
parents:
diff changeset
   266
                return isEqualToSoap1_2Type(type) || isEqualToSoap1_1Type(type);
474761f14bca Initial load
duke
parents:
diff changeset
   267
            }
474761f14bca Initial load
duke
parents:
diff changeset
   268
        }
474761f14bca Initial load
duke
parents:
diff changeset
   269
        return false;
474761f14bca Initial load
duke
parents:
diff changeset
   270
    }
474761f14bca Initial load
duke
parents:
diff changeset
   271
474761f14bca Initial load
duke
parents:
diff changeset
   272
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   273
     * Construct a message from an input stream. When messages are
474761f14bca Initial load
duke
parents:
diff changeset
   274
     * received, there's two parts -- the transport headers and the
474761f14bca Initial load
duke
parents:
diff changeset
   275
     * message content in a transport specific stream.
474761f14bca Initial load
duke
parents:
diff changeset
   276
     */
474761f14bca Initial load
duke
parents:
diff changeset
   277
    protected MessageImpl(MimeHeaders headers, final InputStream in)
474761f14bca Initial load
duke
parents:
diff changeset
   278
        throws SOAPExceptionImpl {
474761f14bca Initial load
duke
parents:
diff changeset
   279
        ContentType ct = parseContentType(headers);
474761f14bca Initial load
duke
parents:
diff changeset
   280
        init(headers,identifyContentType(ct),ct,in);
474761f14bca Initial load
duke
parents:
diff changeset
   281
    }
474761f14bca Initial load
duke
parents:
diff changeset
   282
474761f14bca Initial load
duke
parents:
diff changeset
   283
    private static ContentType parseContentType(MimeHeaders headers) throws SOAPExceptionImpl {
474761f14bca Initial load
duke
parents:
diff changeset
   284
        final String ct;
474761f14bca Initial load
duke
parents:
diff changeset
   285
        if (headers != null)
474761f14bca Initial load
duke
parents:
diff changeset
   286
            ct = getContentType(headers);
474761f14bca Initial load
duke
parents:
diff changeset
   287
        else {
474761f14bca Initial load
duke
parents:
diff changeset
   288
            log.severe("SAAJ0550.soap.null.headers");
474761f14bca Initial load
duke
parents:
diff changeset
   289
            throw new SOAPExceptionImpl("Cannot create message: " +
474761f14bca Initial load
duke
parents:
diff changeset
   290
                                        "Headers can't be null");
474761f14bca Initial load
duke
parents:
diff changeset
   291
        }
474761f14bca Initial load
duke
parents:
diff changeset
   292
474761f14bca Initial load
duke
parents:
diff changeset
   293
        if (ct == null) {
474761f14bca Initial load
duke
parents:
diff changeset
   294
            log.severe("SAAJ0532.soap.no.Content-Type");
474761f14bca Initial load
duke
parents:
diff changeset
   295
            throw new SOAPExceptionImpl("Absent Content-Type");
474761f14bca Initial load
duke
parents:
diff changeset
   296
        }
474761f14bca Initial load
duke
parents:
diff changeset
   297
        try {
474761f14bca Initial load
duke
parents:
diff changeset
   298
            return new ContentType(ct);
474761f14bca Initial load
duke
parents:
diff changeset
   299
        } catch (Throwable ex) {
474761f14bca Initial load
duke
parents:
diff changeset
   300
            log.severe("SAAJ0535.soap.cannot.internalize.message");
474761f14bca Initial load
duke
parents:
diff changeset
   301
            throw new SOAPExceptionImpl("Unable to internalize message", ex);
474761f14bca Initial load
duke
parents:
diff changeset
   302
        }
474761f14bca Initial load
duke
parents:
diff changeset
   303
    }
474761f14bca Initial load
duke
parents:
diff changeset
   304
474761f14bca Initial load
duke
parents:
diff changeset
   305
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   306
     * Construct a message from an input stream. When messages are
474761f14bca Initial load
duke
parents:
diff changeset
   307
     * received, there's two parts -- the transport headers and the
474761f14bca Initial load
duke
parents:
diff changeset
   308
     * message content in a transport specific stream.
474761f14bca Initial load
duke
parents:
diff changeset
   309
     *
474761f14bca Initial load
duke
parents:
diff changeset
   310
     * @param contentType
474761f14bca Initial load
duke
parents:
diff changeset
   311
     *      The parsed content type header from the headers variable.
474761f14bca Initial load
duke
parents:
diff changeset
   312
     *      This is redundant parameter, but it avoids reparsing this header again.
474761f14bca Initial load
duke
parents:
diff changeset
   313
     * @param stat
474761f14bca Initial load
duke
parents:
diff changeset
   314
     *      The result of {@link #identifyContentType(ContentType)} over
474761f14bca Initial load
duke
parents:
diff changeset
   315
     *      the contentType parameter. This redundant parameter, but it avoids
474761f14bca Initial load
duke
parents:
diff changeset
   316
     *      recomputing this information again.
474761f14bca Initial load
duke
parents:
diff changeset
   317
     */
474761f14bca Initial load
duke
parents:
diff changeset
   318
    protected MessageImpl(MimeHeaders headers, final ContentType contentType, int stat, final InputStream in) throws SOAPExceptionImpl {
474761f14bca Initial load
duke
parents:
diff changeset
   319
        init(headers, stat, contentType, in);
474761f14bca Initial load
duke
parents:
diff changeset
   320
474761f14bca Initial load
duke
parents:
diff changeset
   321
    }
474761f14bca Initial load
duke
parents:
diff changeset
   322
474761f14bca Initial load
duke
parents:
diff changeset
   323
    private void init(MimeHeaders headers, int stat, final ContentType contentType, final InputStream in) throws SOAPExceptionImpl {
474761f14bca Initial load
duke
parents:
diff changeset
   324
        this.headers = headers;
474761f14bca Initial load
duke
parents:
diff changeset
   325
474761f14bca Initial load
duke
parents:
diff changeset
   326
        try {
474761f14bca Initial load
duke
parents:
diff changeset
   327
474761f14bca Initial load
duke
parents:
diff changeset
   328
            // Set isFastInfoset/acceptFastInfoset flag based on MIME type
474761f14bca Initial load
duke
parents:
diff changeset
   329
            if ((stat & FI_ENCODED_FLAG) > 0) {
474761f14bca Initial load
duke
parents:
diff changeset
   330
                isFastInfoset = acceptFastInfoset = true;
474761f14bca Initial load
duke
parents:
diff changeset
   331
            }
474761f14bca Initial load
duke
parents:
diff changeset
   332
474761f14bca Initial load
duke
parents:
diff changeset
   333
            // If necessary, inspect Accept header to set acceptFastInfoset
474761f14bca Initial load
duke
parents:
diff changeset
   334
            if (!isFastInfoset) {
474761f14bca Initial load
duke
parents:
diff changeset
   335
                String[] values = headers.getHeader("Accept");
474761f14bca Initial load
duke
parents:
diff changeset
   336
                if (values != null) {
474761f14bca Initial load
duke
parents:
diff changeset
   337
                    for (int i = 0; i < values.length; i++) {
474761f14bca Initial load
duke
parents:
diff changeset
   338
                        StringTokenizer st = new StringTokenizer(values[i], ",");
474761f14bca Initial load
duke
parents:
diff changeset
   339
                        while (st.hasMoreTokens()) {
474761f14bca Initial load
duke
parents:
diff changeset
   340
                            final String token = st.nextToken().trim();
474761f14bca Initial load
duke
parents:
diff changeset
   341
                            if (token.equalsIgnoreCase("application/fastinfoset") ||
474761f14bca Initial load
duke
parents:
diff changeset
   342
                                token.equalsIgnoreCase("application/soap+fastinfoset")) {
474761f14bca Initial load
duke
parents:
diff changeset
   343
                                acceptFastInfoset = true;
474761f14bca Initial load
duke
parents:
diff changeset
   344
                                break;
474761f14bca Initial load
duke
parents:
diff changeset
   345
                            }
474761f14bca Initial load
duke
parents:
diff changeset
   346
                        }
474761f14bca Initial load
duke
parents:
diff changeset
   347
                    }
474761f14bca Initial load
duke
parents:
diff changeset
   348
                }
474761f14bca Initial load
duke
parents:
diff changeset
   349
            }
474761f14bca Initial load
duke
parents:
diff changeset
   350
474761f14bca Initial load
duke
parents:
diff changeset
   351
            if (!isCorrectSoapVersion(stat)) {
474761f14bca Initial load
duke
parents:
diff changeset
   352
                log.log(
474761f14bca Initial load
duke
parents:
diff changeset
   353
                    Level.SEVERE,
474761f14bca Initial load
duke
parents:
diff changeset
   354
                    "SAAJ0533.soap.incorrect.Content-Type",
474761f14bca Initial load
duke
parents:
diff changeset
   355
                    new String[] {
474761f14bca Initial load
duke
parents:
diff changeset
   356
                        contentType.toString(),
474761f14bca Initial load
duke
parents:
diff changeset
   357
                        getExpectedContentType()});
474761f14bca Initial load
duke
parents:
diff changeset
   358
                throw new SOAPVersionMismatchException(
474761f14bca Initial load
duke
parents:
diff changeset
   359
                    "Cannot create message: incorrect content-type for SOAP version. Got: "
474761f14bca Initial load
duke
parents:
diff changeset
   360
                        + contentType
474761f14bca Initial load
duke
parents:
diff changeset
   361
                        + " Expected: "
474761f14bca Initial load
duke
parents:
diff changeset
   362
                        + getExpectedContentType());
474761f14bca Initial load
duke
parents:
diff changeset
   363
            }
474761f14bca Initial load
duke
parents:
diff changeset
   364
474761f14bca Initial load
duke
parents:
diff changeset
   365
            if ((stat & PLAIN_XML_FLAG) != 0) {
474761f14bca Initial load
duke
parents:
diff changeset
   366
                if (isFastInfoset) {
474761f14bca Initial load
duke
parents:
diff changeset
   367
                    getSOAPPart().setContent(
474761f14bca Initial load
duke
parents:
diff changeset
   368
                        FastInfosetReflection.FastInfosetSource_new(in));
474761f14bca Initial load
duke
parents:
diff changeset
   369
                } else {
474761f14bca Initial load
duke
parents:
diff changeset
   370
                    initCharsetProperty(contentType);
474761f14bca Initial load
duke
parents:
diff changeset
   371
                    getSOAPPart().setContent(new StreamSource(in));
474761f14bca Initial load
duke
parents:
diff changeset
   372
                }
474761f14bca Initial load
duke
parents:
diff changeset
   373
            }
474761f14bca Initial load
duke
parents:
diff changeset
   374
            else if ((stat & MIME_MULTIPART_FLAG) != 0) {
474761f14bca Initial load
duke
parents:
diff changeset
   375
                DataSource ds = new DataSource() {
474761f14bca Initial load
duke
parents:
diff changeset
   376
                    public InputStream getInputStream() {
474761f14bca Initial load
duke
parents:
diff changeset
   377
                        return in;
474761f14bca Initial load
duke
parents:
diff changeset
   378
                    }
474761f14bca Initial load
duke
parents:
diff changeset
   379
474761f14bca Initial load
duke
parents:
diff changeset
   380
                    public OutputStream getOutputStream() {
474761f14bca Initial load
duke
parents:
diff changeset
   381
                        return null;
474761f14bca Initial load
duke
parents:
diff changeset
   382
                    }
474761f14bca Initial load
duke
parents:
diff changeset
   383
474761f14bca Initial load
duke
parents:
diff changeset
   384
                    public String getContentType() {
474761f14bca Initial load
duke
parents:
diff changeset
   385
                        return contentType.toString();
474761f14bca Initial load
duke
parents:
diff changeset
   386
                    }
474761f14bca Initial load
duke
parents:
diff changeset
   387
474761f14bca Initial load
duke
parents:
diff changeset
   388
                    public String getName() {
474761f14bca Initial load
duke
parents:
diff changeset
   389
                        return "";
474761f14bca Initial load
duke
parents:
diff changeset
   390
                    }
474761f14bca Initial load
duke
parents:
diff changeset
   391
                };
474761f14bca Initial load
duke
parents:
diff changeset
   392
474761f14bca Initial load
duke
parents:
diff changeset
   393
                multiPart = null;
474761f14bca Initial load
duke
parents:
diff changeset
   394
                if (switchOffBM) {
474761f14bca Initial load
duke
parents:
diff changeset
   395
                    multiPart = new MimeMultipart(ds,contentType);
474761f14bca Initial load
duke
parents:
diff changeset
   396
                } else {
474761f14bca Initial load
duke
parents:
diff changeset
   397
                    multiPart = new BMMimeMultipart(ds,contentType);
474761f14bca Initial load
duke
parents:
diff changeset
   398
                }
474761f14bca Initial load
duke
parents:
diff changeset
   399
474761f14bca Initial load
duke
parents:
diff changeset
   400
                String startParam = contentType.getParameter("start");
474761f14bca Initial load
duke
parents:
diff changeset
   401
                MimeBodyPart soapMessagePart = null;
474761f14bca Initial load
duke
parents:
diff changeset
   402
                String contentID = null;
474761f14bca Initial load
duke
parents:
diff changeset
   403
                if (switchOffBM || switchOffLazyAttachment) {
474761f14bca Initial load
duke
parents:
diff changeset
   404
                    if(startParam == null) {
474761f14bca Initial load
duke
parents:
diff changeset
   405
                        soapMessagePart = multiPart.getBodyPart(0);
474761f14bca Initial load
duke
parents:
diff changeset
   406
                        for (int i = 1; i < multiPart.getCount(); i++) {
474761f14bca Initial load
duke
parents:
diff changeset
   407
                            initializeAttachment(multiPart, i);
474761f14bca Initial load
duke
parents:
diff changeset
   408
                        }
474761f14bca Initial load
duke
parents:
diff changeset
   409
                    } else {
474761f14bca Initial load
duke
parents:
diff changeset
   410
                        soapMessagePart = multiPart.getBodyPart(startParam);
474761f14bca Initial load
duke
parents:
diff changeset
   411
                        for (int i = 0; i < multiPart.getCount(); i++) {
474761f14bca Initial load
duke
parents:
diff changeset
   412
                            contentID = multiPart.getBodyPart(i).getContentID();
474761f14bca Initial load
duke
parents:
diff changeset
   413
                            if(!contentID.equals(startParam))
474761f14bca Initial load
duke
parents:
diff changeset
   414
                                initializeAttachment(multiPart, i);
474761f14bca Initial load
duke
parents:
diff changeset
   415
                        }
474761f14bca Initial load
duke
parents:
diff changeset
   416
                    }
474761f14bca Initial load
duke
parents:
diff changeset
   417
                } else {
474761f14bca Initial load
duke
parents:
diff changeset
   418
                    BMMimeMultipart bmMultipart =
474761f14bca Initial load
duke
parents:
diff changeset
   419
                        (BMMimeMultipart)multiPart;
474761f14bca Initial load
duke
parents:
diff changeset
   420
                    InputStream stream = bmMultipart.initStream();
474761f14bca Initial load
duke
parents:
diff changeset
   421
474761f14bca Initial load
duke
parents:
diff changeset
   422
                    SharedInputStream sin = null;
474761f14bca Initial load
duke
parents:
diff changeset
   423
                    if (stream instanceof SharedInputStream) {
474761f14bca Initial load
duke
parents:
diff changeset
   424
                        sin = (SharedInputStream)stream;
474761f14bca Initial load
duke
parents:
diff changeset
   425
                    }
474761f14bca Initial load
duke
parents:
diff changeset
   426
474761f14bca Initial load
duke
parents:
diff changeset
   427
                    String boundary = "--" +
474761f14bca Initial load
duke
parents:
diff changeset
   428
                        contentType.getParameter("boundary");
474761f14bca Initial load
duke
parents:
diff changeset
   429
                    byte[] bndbytes = ASCIIUtility.getBytes(boundary);
474761f14bca Initial load
duke
parents:
diff changeset
   430
                    if (startParam == null) {
474761f14bca Initial load
duke
parents:
diff changeset
   431
                        soapMessagePart =
474761f14bca Initial load
duke
parents:
diff changeset
   432
                            bmMultipart.getNextPart(stream, bndbytes, sin);
474761f14bca Initial load
duke
parents:
diff changeset
   433
                        bmMultipart.removeBodyPart(soapMessagePart);
474761f14bca Initial load
duke
parents:
diff changeset
   434
                    } else {
474761f14bca Initial load
duke
parents:
diff changeset
   435
                        MimeBodyPart bp = null;
474761f14bca Initial load
duke
parents:
diff changeset
   436
                        try {
474761f14bca Initial load
duke
parents:
diff changeset
   437
                            while(!startParam.equals(contentID)) {
474761f14bca Initial load
duke
parents:
diff changeset
   438
                                bp = bmMultipart.getNextPart(
474761f14bca Initial load
duke
parents:
diff changeset
   439
                                    stream, bndbytes, sin);
474761f14bca Initial load
duke
parents:
diff changeset
   440
                                contentID = bp.getContentID();
474761f14bca Initial load
duke
parents:
diff changeset
   441
                            }
474761f14bca Initial load
duke
parents:
diff changeset
   442
                            soapMessagePart = bp;
474761f14bca Initial load
duke
parents:
diff changeset
   443
                            bmMultipart.removeBodyPart(bp);
474761f14bca Initial load
duke
parents:
diff changeset
   444
                        } catch (Exception e) {
474761f14bca Initial load
duke
parents:
diff changeset
   445
                            throw new SOAPExceptionImpl(e);
474761f14bca Initial load
duke
parents:
diff changeset
   446
                        }
474761f14bca Initial load
duke
parents:
diff changeset
   447
                    }
474761f14bca Initial load
duke
parents:
diff changeset
   448
                }
474761f14bca Initial load
duke
parents:
diff changeset
   449
474761f14bca Initial load
duke
parents:
diff changeset
   450
                ContentType soapPartCType = new ContentType(
474761f14bca Initial load
duke
parents:
diff changeset
   451
                                            soapMessagePart.getContentType());
474761f14bca Initial load
duke
parents:
diff changeset
   452
                initCharsetProperty(soapPartCType);
474761f14bca Initial load
duke
parents:
diff changeset
   453
                String baseType = soapPartCType.getBaseType().toLowerCase();
474761f14bca Initial load
duke
parents:
diff changeset
   454
                if(!(isEqualToSoap1_1Type(baseType)
474761f14bca Initial load
duke
parents:
diff changeset
   455
                  || isEqualToSoap1_2Type(baseType)
474761f14bca Initial load
duke
parents:
diff changeset
   456
                  || isSOAPBodyXOPPackage(soapPartCType))) {
474761f14bca Initial load
duke
parents:
diff changeset
   457
                    log.log(Level.SEVERE,
474761f14bca Initial load
duke
parents:
diff changeset
   458
                            "SAAJ0549.soap.part.invalid.Content-Type",
474761f14bca Initial load
duke
parents:
diff changeset
   459
                            new Object[] {baseType});
474761f14bca Initial load
duke
parents:
diff changeset
   460
                    throw new SOAPExceptionImpl(
474761f14bca Initial load
duke
parents:
diff changeset
   461
                            "Bad Content-Type for SOAP Part : " +
474761f14bca Initial load
duke
parents:
diff changeset
   462
                            baseType);
474761f14bca Initial load
duke
parents:
diff changeset
   463
                }
474761f14bca Initial load
duke
parents:
diff changeset
   464
474761f14bca Initial load
duke
parents:
diff changeset
   465
                SOAPPart soapPart = getSOAPPart();
474761f14bca Initial load
duke
parents:
diff changeset
   466
                setMimeHeaders(soapPart, soapMessagePart);
474761f14bca Initial load
duke
parents:
diff changeset
   467
                soapPart.setContent(isFastInfoset ?
474761f14bca Initial load
duke
parents:
diff changeset
   468
                     (Source) FastInfosetReflection.FastInfosetSource_new(
474761f14bca Initial load
duke
parents:
diff changeset
   469
                         soapMessagePart.getInputStream()) :
474761f14bca Initial load
duke
parents:
diff changeset
   470
                     (Source) new StreamSource(soapMessagePart.getInputStream()));
474761f14bca Initial load
duke
parents:
diff changeset
   471
            } else {
474761f14bca Initial load
duke
parents:
diff changeset
   472
                log.severe("SAAJ0534.soap.unknown.Content-Type");
474761f14bca Initial load
duke
parents:
diff changeset
   473
                throw new SOAPExceptionImpl("Unrecognized Content-Type");
474761f14bca Initial load
duke
parents:
diff changeset
   474
            }
474761f14bca Initial load
duke
parents:
diff changeset
   475
        } catch (Throwable ex) {
474761f14bca Initial load
duke
parents:
diff changeset
   476
            log.severe("SAAJ0535.soap.cannot.internalize.message");
474761f14bca Initial load
duke
parents:
diff changeset
   477
            throw new SOAPExceptionImpl("Unable to internalize message", ex);
474761f14bca Initial load
duke
parents:
diff changeset
   478
        }
474761f14bca Initial load
duke
parents:
diff changeset
   479
        needsSave();
474761f14bca Initial load
duke
parents:
diff changeset
   480
    }
474761f14bca Initial load
duke
parents:
diff changeset
   481
474761f14bca Initial load
duke
parents:
diff changeset
   482
    public boolean isFastInfoset() {
474761f14bca Initial load
duke
parents:
diff changeset
   483
        return isFastInfoset;
474761f14bca Initial load
duke
parents:
diff changeset
   484
    }
474761f14bca Initial load
duke
parents:
diff changeset
   485
474761f14bca Initial load
duke
parents:
diff changeset
   486
    public boolean acceptFastInfoset() {
474761f14bca Initial load
duke
parents:
diff changeset
   487
        return acceptFastInfoset;
474761f14bca Initial load
duke
parents:
diff changeset
   488
    }
474761f14bca Initial load
duke
parents:
diff changeset
   489
474761f14bca Initial load
duke
parents:
diff changeset
   490
    public void setIsFastInfoset(boolean value) {
474761f14bca Initial load
duke
parents:
diff changeset
   491
        if (value != isFastInfoset) {
474761f14bca Initial load
duke
parents:
diff changeset
   492
            isFastInfoset = value;
474761f14bca Initial load
duke
parents:
diff changeset
   493
            if (isFastInfoset) {
474761f14bca Initial load
duke
parents:
diff changeset
   494
                acceptFastInfoset = true;
474761f14bca Initial load
duke
parents:
diff changeset
   495
            }
474761f14bca Initial load
duke
parents:
diff changeset
   496
            saved = false;      // ensure transcoding if necessary
474761f14bca Initial load
duke
parents:
diff changeset
   497
        }
474761f14bca Initial load
duke
parents:
diff changeset
   498
    }
474761f14bca Initial load
duke
parents:
diff changeset
   499
474761f14bca Initial load
duke
parents:
diff changeset
   500
    public Object getProperty(String property) {
474761f14bca Initial load
duke
parents:
diff changeset
   501
        return (String) properties.get(property);
474761f14bca Initial load
duke
parents:
diff changeset
   502
    }
474761f14bca Initial load
duke
parents:
diff changeset
   503
474761f14bca Initial load
duke
parents:
diff changeset
   504
    public void setProperty(String property, Object value) {
474761f14bca Initial load
duke
parents:
diff changeset
   505
        verify(property, value);
474761f14bca Initial load
duke
parents:
diff changeset
   506
        properties.put(property, value);
474761f14bca Initial load
duke
parents:
diff changeset
   507
    }
474761f14bca Initial load
duke
parents:
diff changeset
   508
474761f14bca Initial load
duke
parents:
diff changeset
   509
    private void verify(String property, Object value) {
474761f14bca Initial load
duke
parents:
diff changeset
   510
        if (property.equalsIgnoreCase(SOAPMessage.WRITE_XML_DECLARATION)) {
474761f14bca Initial load
duke
parents:
diff changeset
   511
            if (!("true".equals(value) || "false".equals(value)))
474761f14bca Initial load
duke
parents:
diff changeset
   512
                throw new RuntimeException(
474761f14bca Initial load
duke
parents:
diff changeset
   513
                    property + " must have value false or true");
474761f14bca Initial load
duke
parents:
diff changeset
   514
474761f14bca Initial load
duke
parents:
diff changeset
   515
            try {
474761f14bca Initial load
duke
parents:
diff changeset
   516
                EnvelopeImpl env = (EnvelopeImpl) getSOAPPart().getEnvelope();
474761f14bca Initial load
duke
parents:
diff changeset
   517
                if ("true".equalsIgnoreCase((String)value)) {
474761f14bca Initial load
duke
parents:
diff changeset
   518
                    env.setOmitXmlDecl("no");
474761f14bca Initial load
duke
parents:
diff changeset
   519
                } else if ("false".equalsIgnoreCase((String)value)) {
474761f14bca Initial load
duke
parents:
diff changeset
   520
                    env.setOmitXmlDecl("yes");
474761f14bca Initial load
duke
parents:
diff changeset
   521
                }
474761f14bca Initial load
duke
parents:
diff changeset
   522
            } catch (Exception e) {
474761f14bca Initial load
duke
parents:
diff changeset
   523
                log.log(Level.SEVERE, "SAAJ0591.soap.exception.in.set.property",
474761f14bca Initial load
duke
parents:
diff changeset
   524
                    new Object[] {e.getMessage(), "javax.xml.soap.write-xml-declaration"});
474761f14bca Initial load
duke
parents:
diff changeset
   525
                throw new RuntimeException(e);
474761f14bca Initial load
duke
parents:
diff changeset
   526
            }
474761f14bca Initial load
duke
parents:
diff changeset
   527
            return;
474761f14bca Initial load
duke
parents:
diff changeset
   528
        }
474761f14bca Initial load
duke
parents:
diff changeset
   529
474761f14bca Initial load
duke
parents:
diff changeset
   530
        if (property.equalsIgnoreCase(SOAPMessage.CHARACTER_SET_ENCODING)) {
474761f14bca Initial load
duke
parents:
diff changeset
   531
            try {
474761f14bca Initial load
duke
parents:
diff changeset
   532
                ((EnvelopeImpl) getSOAPPart().getEnvelope()).setCharsetEncoding((String)value);
474761f14bca Initial load
duke
parents:
diff changeset
   533
            } catch (Exception e) {
474761f14bca Initial load
duke
parents:
diff changeset
   534
                log.log(Level.SEVERE, "SAAJ0591.soap.exception.in.set.property",
474761f14bca Initial load
duke
parents:
diff changeset
   535
                    new Object[] {e.getMessage(), "javax.xml.soap.character-set-encoding"});
474761f14bca Initial load
duke
parents:
diff changeset
   536
                throw new RuntimeException(e);
474761f14bca Initial load
duke
parents:
diff changeset
   537
            }
474761f14bca Initial load
duke
parents:
diff changeset
   538
        }
474761f14bca Initial load
duke
parents:
diff changeset
   539
    }
474761f14bca Initial load
duke
parents:
diff changeset
   540
474761f14bca Initial load
duke
parents:
diff changeset
   541
    protected abstract boolean isCorrectSoapVersion(int contentTypeId);
474761f14bca Initial load
duke
parents:
diff changeset
   542
474761f14bca Initial load
duke
parents:
diff changeset
   543
    protected abstract String getExpectedContentType();
474761f14bca Initial load
duke
parents:
diff changeset
   544
    protected abstract String getExpectedAcceptHeader();
474761f14bca Initial load
duke
parents:
diff changeset
   545
474761f14bca Initial load
duke
parents:
diff changeset
   546
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   547
     * Sniffs the Content-Type header so that we can determine how to process.
474761f14bca Initial load
duke
parents:
diff changeset
   548
     *
474761f14bca Initial load
duke
parents:
diff changeset
   549
     * <p>
474761f14bca Initial load
duke
parents:
diff changeset
   550
     * In the absence of type attribute we assume it to be text/xml.
474761f14bca Initial load
duke
parents:
diff changeset
   551
     * That would mean we're easy on accepting the message and
474761f14bca Initial load
duke
parents:
diff changeset
   552
     * generate the correct thing (as the SWA spec also specifies
474761f14bca Initial load
duke
parents:
diff changeset
   553
     * that the type parameter should always be text/xml)
474761f14bca Initial load
duke
parents:
diff changeset
   554
     *
474761f14bca Initial load
duke
parents:
diff changeset
   555
     * @return
474761f14bca Initial load
duke
parents:
diff changeset
   556
     *      combination of flags, such as PLAIN_XML_CODE and MIME_MULTIPART_CODE.
474761f14bca Initial load
duke
parents:
diff changeset
   557
     */
474761f14bca Initial load
duke
parents:
diff changeset
   558
    // SOAP1.2 allow SOAP1.2 content type
474761f14bca Initial load
duke
parents:
diff changeset
   559
    static int identifyContentType(ContentType contentType)
474761f14bca Initial load
duke
parents:
diff changeset
   560
        throws SOAPExceptionImpl {
474761f14bca Initial load
duke
parents:
diff changeset
   561
        // TBD
474761f14bca Initial load
duke
parents:
diff changeset
   562
        //    Is there anything else we need to verify here?
474761f14bca Initial load
duke
parents:
diff changeset
   563
474761f14bca Initial load
duke
parents:
diff changeset
   564
        String primary = contentType.getPrimaryType().toLowerCase();
474761f14bca Initial load
duke
parents:
diff changeset
   565
        String sub = contentType.getSubType().toLowerCase();
474761f14bca Initial load
duke
parents:
diff changeset
   566
474761f14bca Initial load
duke
parents:
diff changeset
   567
        if (primary.equals("multipart")) {
474761f14bca Initial load
duke
parents:
diff changeset
   568
            if (sub.equals("related")) {
474761f14bca Initial load
duke
parents:
diff changeset
   569
                String type = getTypeParameter(contentType);
474761f14bca Initial load
duke
parents:
diff changeset
   570
                if (isEqualToSoap1_1Type(type)) {
474761f14bca Initial load
duke
parents:
diff changeset
   571
                    return (type.equals("application/fastinfoset") ?
474761f14bca Initial load
duke
parents:
diff changeset
   572
                           FI_ENCODED_FLAG : 0) | MIME_MULTIPART_FLAG | SOAP1_1_FLAG;
474761f14bca Initial load
duke
parents:
diff changeset
   573
                }
474761f14bca Initial load
duke
parents:
diff changeset
   574
                else if (isEqualToSoap1_2Type(type)) {
474761f14bca Initial load
duke
parents:
diff changeset
   575
                    return (type.equals("application/soap+fastinfoset") ?
474761f14bca Initial load
duke
parents:
diff changeset
   576
                           FI_ENCODED_FLAG : 0) | MIME_MULTIPART_FLAG | SOAP1_2_FLAG;
474761f14bca Initial load
duke
parents:
diff changeset
   577
                } else if (isMimeMultipartXOPPackage(contentType)) {
474761f14bca Initial load
duke
parents:
diff changeset
   578
                    return MIME_MULTIPART_XOP_FLAG;
474761f14bca Initial load
duke
parents:
diff changeset
   579
                } else {
474761f14bca Initial load
duke
parents:
diff changeset
   580
                    log.severe("SAAJ0536.soap.content-type.mustbe.multipart");
474761f14bca Initial load
duke
parents:
diff changeset
   581
                    throw new SOAPExceptionImpl(
474761f14bca Initial load
duke
parents:
diff changeset
   582
                        "Content-Type needs to be Multipart/Related "
474761f14bca Initial load
duke
parents:
diff changeset
   583
                            + "and with \"type=text/xml\" "
474761f14bca Initial load
duke
parents:
diff changeset
   584
                            + "or \"type=application/soap+xml\"");
474761f14bca Initial load
duke
parents:
diff changeset
   585
                }
474761f14bca Initial load
duke
parents:
diff changeset
   586
            } else {
474761f14bca Initial load
duke
parents:
diff changeset
   587
                log.severe("SAAJ0537.soap.invalid.content-type");
474761f14bca Initial load
duke
parents:
diff changeset
   588
                throw new SOAPExceptionImpl(
474761f14bca Initial load
duke
parents:
diff changeset
   589
                    "Invalid Content-Type: " + primary + '/' + sub);
474761f14bca Initial load
duke
parents:
diff changeset
   590
            }
474761f14bca Initial load
duke
parents:
diff changeset
   591
        }
474761f14bca Initial load
duke
parents:
diff changeset
   592
        else if (isSoap1_1Type(primary, sub)) {
474761f14bca Initial load
duke
parents:
diff changeset
   593
            return (primary.equalsIgnoreCase("application")
474761f14bca Initial load
duke
parents:
diff changeset
   594
                    && sub.equalsIgnoreCase("fastinfoset") ?
474761f14bca Initial load
duke
parents:
diff changeset
   595
                        FI_ENCODED_FLAG : 0)
474761f14bca Initial load
duke
parents:
diff changeset
   596
                   | PLAIN_XML_FLAG | SOAP1_1_FLAG;
474761f14bca Initial load
duke
parents:
diff changeset
   597
        }
474761f14bca Initial load
duke
parents:
diff changeset
   598
        else if (isSoap1_2Type(primary, sub)) {
474761f14bca Initial load
duke
parents:
diff changeset
   599
            return (primary.equalsIgnoreCase("application")
474761f14bca Initial load
duke
parents:
diff changeset
   600
                    && sub.equalsIgnoreCase("soap+fastinfoset") ?
474761f14bca Initial load
duke
parents:
diff changeset
   601
                        FI_ENCODED_FLAG : 0)
474761f14bca Initial load
duke
parents:
diff changeset
   602
                   | PLAIN_XML_FLAG | SOAP1_2_FLAG;
474761f14bca Initial load
duke
parents:
diff changeset
   603
        } else if(isSOAPBodyXOPPackage(contentType)){
474761f14bca Initial load
duke
parents:
diff changeset
   604
            return XOP_FLAG;
474761f14bca Initial load
duke
parents:
diff changeset
   605
        } else {
474761f14bca Initial load
duke
parents:
diff changeset
   606
            log.severe("SAAJ0537.soap.invalid.content-type");
474761f14bca Initial load
duke
parents:
diff changeset
   607
            throw new SOAPExceptionImpl(
474761f14bca Initial load
duke
parents:
diff changeset
   608
                "Invalid Content-Type:"
474761f14bca Initial load
duke
parents:
diff changeset
   609
                    + primary
474761f14bca Initial load
duke
parents:
diff changeset
   610
                    + '/'
474761f14bca Initial load
duke
parents:
diff changeset
   611
                    + sub
474761f14bca Initial load
duke
parents:
diff changeset
   612
                    + ". Is this an error message instead of a SOAP response?");
474761f14bca Initial load
duke
parents:
diff changeset
   613
        }
474761f14bca Initial load
duke
parents:
diff changeset
   614
    }
474761f14bca Initial load
duke
parents:
diff changeset
   615
474761f14bca Initial load
duke
parents:
diff changeset
   616
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   617
     * Obtains the type parameter of the Content-Type header. Defaults to "text/xml".
474761f14bca Initial load
duke
parents:
diff changeset
   618
     */
474761f14bca Initial load
duke
parents:
diff changeset
   619
    private static String getTypeParameter(ContentType contentType) {
474761f14bca Initial load
duke
parents:
diff changeset
   620
        String p = contentType.getParameter("type");
474761f14bca Initial load
duke
parents:
diff changeset
   621
        if(p!=null)
474761f14bca Initial load
duke
parents:
diff changeset
   622
            return p.toLowerCase();
474761f14bca Initial load
duke
parents:
diff changeset
   623
        else
474761f14bca Initial load
duke
parents:
diff changeset
   624
            return "text/xml";
474761f14bca Initial load
duke
parents:
diff changeset
   625
    }
474761f14bca Initial load
duke
parents:
diff changeset
   626
474761f14bca Initial load
duke
parents:
diff changeset
   627
    public MimeHeaders getMimeHeaders() {
474761f14bca Initial load
duke
parents:
diff changeset
   628
        return this.headers;
474761f14bca Initial load
duke
parents:
diff changeset
   629
    }
474761f14bca Initial load
duke
parents:
diff changeset
   630
474761f14bca Initial load
duke
parents:
diff changeset
   631
    final static String getContentType(MimeHeaders headers) {
474761f14bca Initial load
duke
parents:
diff changeset
   632
        String[] values = headers.getHeader("Content-Type");
474761f14bca Initial load
duke
parents:
diff changeset
   633
        if (values == null)
474761f14bca Initial load
duke
parents:
diff changeset
   634
            return null;
474761f14bca Initial load
duke
parents:
diff changeset
   635
        else
474761f14bca Initial load
duke
parents:
diff changeset
   636
            return values[0];
474761f14bca Initial load
duke
parents:
diff changeset
   637
    }
474761f14bca Initial load
duke
parents:
diff changeset
   638
474761f14bca Initial load
duke
parents:
diff changeset
   639
    /*
474761f14bca Initial load
duke
parents:
diff changeset
   640
     * Get the complete ContentType value along with optional parameters.
474761f14bca Initial load
duke
parents:
diff changeset
   641
     */
474761f14bca Initial load
duke
parents:
diff changeset
   642
    public String getContentType() {
474761f14bca Initial load
duke
parents:
diff changeset
   643
        return getContentType(this.headers);
474761f14bca Initial load
duke
parents:
diff changeset
   644
    }
474761f14bca Initial load
duke
parents:
diff changeset
   645
474761f14bca Initial load
duke
parents:
diff changeset
   646
    public void setContentType(String type) {
474761f14bca Initial load
duke
parents:
diff changeset
   647
        headers.setHeader("Content-Type", type);
474761f14bca Initial load
duke
parents:
diff changeset
   648
        needsSave();
474761f14bca Initial load
duke
parents:
diff changeset
   649
    }
474761f14bca Initial load
duke
parents:
diff changeset
   650
474761f14bca Initial load
duke
parents:
diff changeset
   651
    private ContentType ContentType() {
474761f14bca Initial load
duke
parents:
diff changeset
   652
        ContentType ct = null;
474761f14bca Initial load
duke
parents:
diff changeset
   653
        try {
474761f14bca Initial load
duke
parents:
diff changeset
   654
            ct = new ContentType(getContentType());
474761f14bca Initial load
duke
parents:
diff changeset
   655
        } catch (Exception e) {
474761f14bca Initial load
duke
parents:
diff changeset
   656
            // what to do here?
474761f14bca Initial load
duke
parents:
diff changeset
   657
        }
474761f14bca Initial load
duke
parents:
diff changeset
   658
        return ct;
474761f14bca Initial load
duke
parents:
diff changeset
   659
    }
474761f14bca Initial load
duke
parents:
diff changeset
   660
474761f14bca Initial load
duke
parents:
diff changeset
   661
    /*
474761f14bca Initial load
duke
parents:
diff changeset
   662
     * Return the MIME type string, without the parameters.
474761f14bca Initial load
duke
parents:
diff changeset
   663
     */
474761f14bca Initial load
duke
parents:
diff changeset
   664
    public String getBaseType() {
474761f14bca Initial load
duke
parents:
diff changeset
   665
        return ContentType().getBaseType();
474761f14bca Initial load
duke
parents:
diff changeset
   666
    }
474761f14bca Initial load
duke
parents:
diff changeset
   667
474761f14bca Initial load
duke
parents:
diff changeset
   668
    public void setBaseType(String type) {
474761f14bca Initial load
duke
parents:
diff changeset
   669
        ContentType ct = ContentType();
474761f14bca Initial load
duke
parents:
diff changeset
   670
        ct.setParameter("type", type);
474761f14bca Initial load
duke
parents:
diff changeset
   671
        headers.setHeader("Content-Type", ct.toString());
474761f14bca Initial load
duke
parents:
diff changeset
   672
        needsSave();
474761f14bca Initial load
duke
parents:
diff changeset
   673
    }
474761f14bca Initial load
duke
parents:
diff changeset
   674
474761f14bca Initial load
duke
parents:
diff changeset
   675
    public String getAction() {
474761f14bca Initial load
duke
parents:
diff changeset
   676
        return ContentType().getParameter("action");
474761f14bca Initial load
duke
parents:
diff changeset
   677
    }
474761f14bca Initial load
duke
parents:
diff changeset
   678
474761f14bca Initial load
duke
parents:
diff changeset
   679
    public void setAction(String action) {
474761f14bca Initial load
duke
parents:
diff changeset
   680
        ContentType ct = ContentType();
474761f14bca Initial load
duke
parents:
diff changeset
   681
        ct.setParameter("action", action);
474761f14bca Initial load
duke
parents:
diff changeset
   682
        headers.setHeader("Content-Type", ct.toString());
474761f14bca Initial load
duke
parents:
diff changeset
   683
        needsSave();
474761f14bca Initial load
duke
parents:
diff changeset
   684
    }
474761f14bca Initial load
duke
parents:
diff changeset
   685
474761f14bca Initial load
duke
parents:
diff changeset
   686
    public String getCharset() {
474761f14bca Initial load
duke
parents:
diff changeset
   687
        return ContentType().getParameter("charset");
474761f14bca Initial load
duke
parents:
diff changeset
   688
    }
474761f14bca Initial load
duke
parents:
diff changeset
   689
474761f14bca Initial load
duke
parents:
diff changeset
   690
    public void setCharset(String charset) {
474761f14bca Initial load
duke
parents:
diff changeset
   691
        ContentType ct = ContentType();
474761f14bca Initial load
duke
parents:
diff changeset
   692
        ct.setParameter("charset", charset);
474761f14bca Initial load
duke
parents:
diff changeset
   693
        headers.setHeader("Content-Type", ct.toString());
474761f14bca Initial load
duke
parents:
diff changeset
   694
        needsSave();
474761f14bca Initial load
duke
parents:
diff changeset
   695
    }
474761f14bca Initial load
duke
parents:
diff changeset
   696
474761f14bca Initial load
duke
parents:
diff changeset
   697
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   698
     * All write methods (i.e setters) should call this method in
474761f14bca Initial load
duke
parents:
diff changeset
   699
     * order to make sure that a save is necessary since the state
474761f14bca Initial load
duke
parents:
diff changeset
   700
     * has been modified.
474761f14bca Initial load
duke
parents:
diff changeset
   701
     */
474761f14bca Initial load
duke
parents:
diff changeset
   702
    private final void needsSave() {
474761f14bca Initial load
duke
parents:
diff changeset
   703
        saved = false;
474761f14bca Initial load
duke
parents:
diff changeset
   704
    }
474761f14bca Initial load
duke
parents:
diff changeset
   705
474761f14bca Initial load
duke
parents:
diff changeset
   706
    public  boolean saveRequired() {
474761f14bca Initial load
duke
parents:
diff changeset
   707
        return saved != true;
474761f14bca Initial load
duke
parents:
diff changeset
   708
    }
474761f14bca Initial load
duke
parents:
diff changeset
   709
474761f14bca Initial load
duke
parents:
diff changeset
   710
    public String getContentDescription() {
474761f14bca Initial load
duke
parents:
diff changeset
   711
        String[] values = headers.getHeader("Content-Description");
474761f14bca Initial load
duke
parents:
diff changeset
   712
        if (values != null && values.length > 0)
474761f14bca Initial load
duke
parents:
diff changeset
   713
            return values[0];
474761f14bca Initial load
duke
parents:
diff changeset
   714
        return null;
474761f14bca Initial load
duke
parents:
diff changeset
   715
    }
474761f14bca Initial load
duke
parents:
diff changeset
   716
474761f14bca Initial load
duke
parents:
diff changeset
   717
    public void setContentDescription(String description) {
474761f14bca Initial load
duke
parents:
diff changeset
   718
        headers.setHeader("Content-Description", description);
474761f14bca Initial load
duke
parents:
diff changeset
   719
        needsSave();
474761f14bca Initial load
duke
parents:
diff changeset
   720
    }
474761f14bca Initial load
duke
parents:
diff changeset
   721
474761f14bca Initial load
duke
parents:
diff changeset
   722
    public abstract SOAPPart getSOAPPart();
474761f14bca Initial load
duke
parents:
diff changeset
   723
474761f14bca Initial load
duke
parents:
diff changeset
   724
    public void removeAllAttachments() {
474761f14bca Initial load
duke
parents:
diff changeset
   725
        try {
474761f14bca Initial load
duke
parents:
diff changeset
   726
            initializeAllAttachments();
474761f14bca Initial load
duke
parents:
diff changeset
   727
        } catch (Exception e) {
474761f14bca Initial load
duke
parents:
diff changeset
   728
            throw new RuntimeException(e);
474761f14bca Initial load
duke
parents:
diff changeset
   729
        }
474761f14bca Initial load
duke
parents:
diff changeset
   730
474761f14bca Initial load
duke
parents:
diff changeset
   731
        if (attachments != null) {
474761f14bca Initial load
duke
parents:
diff changeset
   732
            attachments.clear();
474761f14bca Initial load
duke
parents:
diff changeset
   733
            needsSave();
474761f14bca Initial load
duke
parents:
diff changeset
   734
        }
474761f14bca Initial load
duke
parents:
diff changeset
   735
    }
474761f14bca Initial load
duke
parents:
diff changeset
   736
474761f14bca Initial load
duke
parents:
diff changeset
   737
    public int countAttachments() {
474761f14bca Initial load
duke
parents:
diff changeset
   738
        try {
474761f14bca Initial load
duke
parents:
diff changeset
   739
            initializeAllAttachments();
474761f14bca Initial load
duke
parents:
diff changeset
   740
        } catch (Exception e) {
474761f14bca Initial load
duke
parents:
diff changeset
   741
            throw new RuntimeException(e);
474761f14bca Initial load
duke
parents:
diff changeset
   742
        }
474761f14bca Initial load
duke
parents:
diff changeset
   743
        if (attachments != null)
474761f14bca Initial load
duke
parents:
diff changeset
   744
            return attachments.size();
474761f14bca Initial load
duke
parents:
diff changeset
   745
        return 0;
474761f14bca Initial load
duke
parents:
diff changeset
   746
    }
474761f14bca Initial load
duke
parents:
diff changeset
   747
474761f14bca Initial load
duke
parents:
diff changeset
   748
    public void addAttachmentPart(AttachmentPart attachment) {
474761f14bca Initial load
duke
parents:
diff changeset
   749
        try {
474761f14bca Initial load
duke
parents:
diff changeset
   750
            initializeAllAttachments();
474761f14bca Initial load
duke
parents:
diff changeset
   751
        } catch (Exception e) {
474761f14bca Initial load
duke
parents:
diff changeset
   752
            throw new RuntimeException(e);
474761f14bca Initial load
duke
parents:
diff changeset
   753
        }
474761f14bca Initial load
duke
parents:
diff changeset
   754
        if (attachments == null)
474761f14bca Initial load
duke
parents:
diff changeset
   755
            attachments = new FinalArrayList();
474761f14bca Initial load
duke
parents:
diff changeset
   756
474761f14bca Initial load
duke
parents:
diff changeset
   757
        attachments.add(attachment);
474761f14bca Initial load
duke
parents:
diff changeset
   758
474761f14bca Initial load
duke
parents:
diff changeset
   759
        needsSave();
474761f14bca Initial load
duke
parents:
diff changeset
   760
    }
474761f14bca Initial load
duke
parents:
diff changeset
   761
474761f14bca Initial load
duke
parents:
diff changeset
   762
    static private final Iterator nullIter = Collections.EMPTY_LIST.iterator();
474761f14bca Initial load
duke
parents:
diff changeset
   763
474761f14bca Initial load
duke
parents:
diff changeset
   764
    public Iterator getAttachments() {
474761f14bca Initial load
duke
parents:
diff changeset
   765
        try {
474761f14bca Initial load
duke
parents:
diff changeset
   766
            initializeAllAttachments();
474761f14bca Initial load
duke
parents:
diff changeset
   767
        } catch (Exception e) {
474761f14bca Initial load
duke
parents:
diff changeset
   768
            throw new RuntimeException(e);
474761f14bca Initial load
duke
parents:
diff changeset
   769
        }
474761f14bca Initial load
duke
parents:
diff changeset
   770
        if (attachments == null)
474761f14bca Initial load
duke
parents:
diff changeset
   771
            return nullIter;
474761f14bca Initial load
duke
parents:
diff changeset
   772
        return attachments.iterator();
474761f14bca Initial load
duke
parents:
diff changeset
   773
    }
474761f14bca Initial load
duke
parents:
diff changeset
   774
474761f14bca Initial load
duke
parents:
diff changeset
   775
    private class MimeMatchingIterator implements Iterator {
474761f14bca Initial load
duke
parents:
diff changeset
   776
        public MimeMatchingIterator(MimeHeaders headers) {
474761f14bca Initial load
duke
parents:
diff changeset
   777
            this.headers = headers;
474761f14bca Initial load
duke
parents:
diff changeset
   778
            this.iter = attachments.iterator();
474761f14bca Initial load
duke
parents:
diff changeset
   779
        }
474761f14bca Initial load
duke
parents:
diff changeset
   780
474761f14bca Initial load
duke
parents:
diff changeset
   781
        private Iterator iter;
474761f14bca Initial load
duke
parents:
diff changeset
   782
        private MimeHeaders headers;
474761f14bca Initial load
duke
parents:
diff changeset
   783
        private Object nextAttachment;
474761f14bca Initial load
duke
parents:
diff changeset
   784
474761f14bca Initial load
duke
parents:
diff changeset
   785
        public boolean hasNext() {
474761f14bca Initial load
duke
parents:
diff changeset
   786
            if (nextAttachment == null)
474761f14bca Initial load
duke
parents:
diff changeset
   787
                nextAttachment = nextMatch();
474761f14bca Initial load
duke
parents:
diff changeset
   788
            return nextAttachment != null;
474761f14bca Initial load
duke
parents:
diff changeset
   789
        }
474761f14bca Initial load
duke
parents:
diff changeset
   790
474761f14bca Initial load
duke
parents:
diff changeset
   791
        public Object next() {
474761f14bca Initial load
duke
parents:
diff changeset
   792
            if (nextAttachment != null) {
474761f14bca Initial load
duke
parents:
diff changeset
   793
                Object ret = nextAttachment;
474761f14bca Initial load
duke
parents:
diff changeset
   794
                nextAttachment = null;
474761f14bca Initial load
duke
parents:
diff changeset
   795
                return ret;
474761f14bca Initial load
duke
parents:
diff changeset
   796
            }
474761f14bca Initial load
duke
parents:
diff changeset
   797
474761f14bca Initial load
duke
parents:
diff changeset
   798
            if (hasNext())
474761f14bca Initial load
duke
parents:
diff changeset
   799
                return nextAttachment;
474761f14bca Initial load
duke
parents:
diff changeset
   800
474761f14bca Initial load
duke
parents:
diff changeset
   801
            return null;
474761f14bca Initial load
duke
parents:
diff changeset
   802
        }
474761f14bca Initial load
duke
parents:
diff changeset
   803
474761f14bca Initial load
duke
parents:
diff changeset
   804
        Object nextMatch() {
474761f14bca Initial load
duke
parents:
diff changeset
   805
            while (iter.hasNext()) {
474761f14bca Initial load
duke
parents:
diff changeset
   806
                AttachmentPartImpl ap = (AttachmentPartImpl) iter.next();
474761f14bca Initial load
duke
parents:
diff changeset
   807
                if (ap.hasAllHeaders(headers))
474761f14bca Initial load
duke
parents:
diff changeset
   808
                    return ap;
474761f14bca Initial load
duke
parents:
diff changeset
   809
            }
474761f14bca Initial load
duke
parents:
diff changeset
   810
            return null;
474761f14bca Initial load
duke
parents:
diff changeset
   811
        }
474761f14bca Initial load
duke
parents:
diff changeset
   812
474761f14bca Initial load
duke
parents:
diff changeset
   813
        public void remove() {
474761f14bca Initial load
duke
parents:
diff changeset
   814
            iter.remove();
474761f14bca Initial load
duke
parents:
diff changeset
   815
        }
474761f14bca Initial load
duke
parents:
diff changeset
   816
    }
474761f14bca Initial load
duke
parents:
diff changeset
   817
474761f14bca Initial load
duke
parents:
diff changeset
   818
    public Iterator getAttachments(MimeHeaders headers) {
474761f14bca Initial load
duke
parents:
diff changeset
   819
        try {
474761f14bca Initial load
duke
parents:
diff changeset
   820
            initializeAllAttachments();
474761f14bca Initial load
duke
parents:
diff changeset
   821
        } catch (Exception e) {
474761f14bca Initial load
duke
parents:
diff changeset
   822
            throw new RuntimeException(e);
474761f14bca Initial load
duke
parents:
diff changeset
   823
        }
474761f14bca Initial load
duke
parents:
diff changeset
   824
        if (attachments == null)
474761f14bca Initial load
duke
parents:
diff changeset
   825
            return nullIter;
474761f14bca Initial load
duke
parents:
diff changeset
   826
474761f14bca Initial load
duke
parents:
diff changeset
   827
        return new MimeMatchingIterator(headers);
474761f14bca Initial load
duke
parents:
diff changeset
   828
    }
474761f14bca Initial load
duke
parents:
diff changeset
   829
474761f14bca Initial load
duke
parents:
diff changeset
   830
    public void removeAttachments(MimeHeaders headers) {
474761f14bca Initial load
duke
parents:
diff changeset
   831
        try {
474761f14bca Initial load
duke
parents:
diff changeset
   832
            initializeAllAttachments();
474761f14bca Initial load
duke
parents:
diff changeset
   833
        } catch (Exception e) {
474761f14bca Initial load
duke
parents:
diff changeset
   834
            throw new RuntimeException(e);
474761f14bca Initial load
duke
parents:
diff changeset
   835
        }
474761f14bca Initial load
duke
parents:
diff changeset
   836
        if (attachments == null)
474761f14bca Initial load
duke
parents:
diff changeset
   837
            return ;
474761f14bca Initial load
duke
parents:
diff changeset
   838
474761f14bca Initial load
duke
parents:
diff changeset
   839
        Iterator it =  new MimeMatchingIterator(headers);
474761f14bca Initial load
duke
parents:
diff changeset
   840
        while (it.hasNext()) {
474761f14bca Initial load
duke
parents:
diff changeset
   841
            int index = attachments.indexOf(it.next());
474761f14bca Initial load
duke
parents:
diff changeset
   842
            attachments.set(index, null);
474761f14bca Initial load
duke
parents:
diff changeset
   843
        }
474761f14bca Initial load
duke
parents:
diff changeset
   844
        FinalArrayList f = new FinalArrayList();
474761f14bca Initial load
duke
parents:
diff changeset
   845
        for (int i = 0; i < attachments.size(); i++) {
474761f14bca Initial load
duke
parents:
diff changeset
   846
            if (attachments.get(i) != null) {
474761f14bca Initial load
duke
parents:
diff changeset
   847
                f.add(attachments.get(i));
474761f14bca Initial load
duke
parents:
diff changeset
   848
            }
474761f14bca Initial load
duke
parents:
diff changeset
   849
        }
474761f14bca Initial load
duke
parents:
diff changeset
   850
        attachments = f;
474761f14bca Initial load
duke
parents:
diff changeset
   851
    }
474761f14bca Initial load
duke
parents:
diff changeset
   852
474761f14bca Initial load
duke
parents:
diff changeset
   853
    public AttachmentPart createAttachmentPart() {
474761f14bca Initial load
duke
parents:
diff changeset
   854
        return new AttachmentPartImpl();
474761f14bca Initial load
duke
parents:
diff changeset
   855
    }
474761f14bca Initial load
duke
parents:
diff changeset
   856
474761f14bca Initial load
duke
parents:
diff changeset
   857
    public  AttachmentPart getAttachment(SOAPElement element)
474761f14bca Initial load
duke
parents:
diff changeset
   858
        throws SOAPException {
474761f14bca Initial load
duke
parents:
diff changeset
   859
        try {
474761f14bca Initial load
duke
parents:
diff changeset
   860
            initializeAllAttachments();
474761f14bca Initial load
duke
parents:
diff changeset
   861
        } catch (Exception e) {
474761f14bca Initial load
duke
parents:
diff changeset
   862
            throw new RuntimeException(e);
474761f14bca Initial load
duke
parents:
diff changeset
   863
        }
474761f14bca Initial load
duke
parents:
diff changeset
   864
        String uri;
474761f14bca Initial load
duke
parents:
diff changeset
   865
        String hrefAttr = element.getAttribute("href");
474761f14bca Initial load
duke
parents:
diff changeset
   866
        if ("".equals(hrefAttr)) {
474761f14bca Initial load
duke
parents:
diff changeset
   867
            Node node = getValueNodeStrict(element);
474761f14bca Initial load
duke
parents:
diff changeset
   868
            String swaRef = null;
474761f14bca Initial load
duke
parents:
diff changeset
   869
            if (node != null) {
474761f14bca Initial load
duke
parents:
diff changeset
   870
                swaRef = node.getValue();
474761f14bca Initial load
duke
parents:
diff changeset
   871
            }
474761f14bca Initial load
duke
parents:
diff changeset
   872
            if (swaRef == null || "".equals(swaRef)) {
474761f14bca Initial load
duke
parents:
diff changeset
   873
                return null;
474761f14bca Initial load
duke
parents:
diff changeset
   874
            } else {
474761f14bca Initial load
duke
parents:
diff changeset
   875
                uri = swaRef;
474761f14bca Initial load
duke
parents:
diff changeset
   876
            }
474761f14bca Initial load
duke
parents:
diff changeset
   877
        } else {
474761f14bca Initial load
duke
parents:
diff changeset
   878
            uri = hrefAttr;
474761f14bca Initial load
duke
parents:
diff changeset
   879
        }
474761f14bca Initial load
duke
parents:
diff changeset
   880
        return getAttachmentPart(uri);
474761f14bca Initial load
duke
parents:
diff changeset
   881
    }
474761f14bca Initial load
duke
parents:
diff changeset
   882
474761f14bca Initial load
duke
parents:
diff changeset
   883
    private Node getValueNodeStrict(SOAPElement element) {
474761f14bca Initial load
duke
parents:
diff changeset
   884
        Node node = (Node)element.getFirstChild();
474761f14bca Initial load
duke
parents:
diff changeset
   885
        if (node != null) {
474761f14bca Initial load
duke
parents:
diff changeset
   886
            if (node.getNextSibling() == null
474761f14bca Initial load
duke
parents:
diff changeset
   887
                && node.getNodeType() == org.w3c.dom.Node.TEXT_NODE) {
474761f14bca Initial load
duke
parents:
diff changeset
   888
                return node;
474761f14bca Initial load
duke
parents:
diff changeset
   889
            } else {
474761f14bca Initial load
duke
parents:
diff changeset
   890
                return null;
474761f14bca Initial load
duke
parents:
diff changeset
   891
            }
474761f14bca Initial load
duke
parents:
diff changeset
   892
        }
474761f14bca Initial load
duke
parents:
diff changeset
   893
        return null;
474761f14bca Initial load
duke
parents:
diff changeset
   894
    }
474761f14bca Initial load
duke
parents:
diff changeset
   895
474761f14bca Initial load
duke
parents:
diff changeset
   896
474761f14bca Initial load
duke
parents:
diff changeset
   897
    private AttachmentPart getAttachmentPart(String uri) throws SOAPException {
474761f14bca Initial load
duke
parents:
diff changeset
   898
        AttachmentPart _part;
474761f14bca Initial load
duke
parents:
diff changeset
   899
        try {
474761f14bca Initial load
duke
parents:
diff changeset
   900
            if (uri.startsWith("cid:")) {
474761f14bca Initial load
duke
parents:
diff changeset
   901
                // rfc2392
474761f14bca Initial load
duke
parents:
diff changeset
   902
                uri = '<'+uri.substring("cid:".length())+'>';
474761f14bca Initial load
duke
parents:
diff changeset
   903
474761f14bca Initial load
duke
parents:
diff changeset
   904
                MimeHeaders headersToMatch = new MimeHeaders();
474761f14bca Initial load
duke
parents:
diff changeset
   905
                headersToMatch.addHeader(CONTENT_ID, uri);
474761f14bca Initial load
duke
parents:
diff changeset
   906
474761f14bca Initial load
duke
parents:
diff changeset
   907
                Iterator i = this.getAttachments(headersToMatch);
474761f14bca Initial load
duke
parents:
diff changeset
   908
                _part = (i == null) ? null : (AttachmentPart)i.next();
474761f14bca Initial load
duke
parents:
diff changeset
   909
            } else {
474761f14bca Initial load
duke
parents:
diff changeset
   910
                // try content-location
474761f14bca Initial load
duke
parents:
diff changeset
   911
                MimeHeaders headersToMatch = new MimeHeaders();
474761f14bca Initial load
duke
parents:
diff changeset
   912
                headersToMatch.addHeader(CONTENT_LOCATION, uri);
474761f14bca Initial load
duke
parents:
diff changeset
   913
474761f14bca Initial load
duke
parents:
diff changeset
   914
                Iterator i = this.getAttachments(headersToMatch);
474761f14bca Initial load
duke
parents:
diff changeset
   915
                _part = (i == null) ? null : (AttachmentPart)i.next();
474761f14bca Initial load
duke
parents:
diff changeset
   916
            }
474761f14bca Initial load
duke
parents:
diff changeset
   917
474761f14bca Initial load
duke
parents:
diff changeset
   918
            // try  auto-generated JAXRPC CID
474761f14bca Initial load
duke
parents:
diff changeset
   919
            if (_part == null) {
474761f14bca Initial load
duke
parents:
diff changeset
   920
                Iterator j = this.getAttachments();
474761f14bca Initial load
duke
parents:
diff changeset
   921
474761f14bca Initial load
duke
parents:
diff changeset
   922
                while (j.hasNext()) {
474761f14bca Initial load
duke
parents:
diff changeset
   923
                    AttachmentPart p = (AttachmentPart)j.next();
474761f14bca Initial load
duke
parents:
diff changeset
   924
                    String cl = p.getContentId();
474761f14bca Initial load
duke
parents:
diff changeset
   925
                    if (cl != null) {
474761f14bca Initial load
duke
parents:
diff changeset
   926
                        // obtain the partname
474761f14bca Initial load
duke
parents:
diff changeset
   927
                        int eqIndex = cl.indexOf("=");
474761f14bca Initial load
duke
parents:
diff changeset
   928
                        if (eqIndex > -1) {
474761f14bca Initial load
duke
parents:
diff changeset
   929
                            cl = cl.substring(1, eqIndex);
474761f14bca Initial load
duke
parents:
diff changeset
   930
                            if (cl.equalsIgnoreCase(uri)) {
474761f14bca Initial load
duke
parents:
diff changeset
   931
                                _part = p;
474761f14bca Initial load
duke
parents:
diff changeset
   932
                                 break;
474761f14bca Initial load
duke
parents:
diff changeset
   933
                            }
474761f14bca Initial load
duke
parents:
diff changeset
   934
                        }
474761f14bca Initial load
duke
parents:
diff changeset
   935
                    }
474761f14bca Initial load
duke
parents:
diff changeset
   936
                }
474761f14bca Initial load
duke
parents:
diff changeset
   937
            }
474761f14bca Initial load
duke
parents:
diff changeset
   938
474761f14bca Initial load
duke
parents:
diff changeset
   939
        } catch (Exception se) {
474761f14bca Initial load
duke
parents:
diff changeset
   940
            log.log(Level.SEVERE, "SAAJ0590.soap.unable.to.locate.attachment", new Object[] {uri});
474761f14bca Initial load
duke
parents:
diff changeset
   941
            throw new SOAPExceptionImpl(se);
474761f14bca Initial load
duke
parents:
diff changeset
   942
        }
474761f14bca Initial load
duke
parents:
diff changeset
   943
        return _part;
474761f14bca Initial load
duke
parents:
diff changeset
   944
    }
474761f14bca Initial load
duke
parents:
diff changeset
   945
474761f14bca Initial load
duke
parents:
diff changeset
   946
    private final ByteInputStream getHeaderBytes()
474761f14bca Initial load
duke
parents:
diff changeset
   947
        throws IOException {
474761f14bca Initial load
duke
parents:
diff changeset
   948
        SOAPPartImpl sp = (SOAPPartImpl) getSOAPPart();
474761f14bca Initial load
duke
parents:
diff changeset
   949
        return sp.getContentAsStream();
474761f14bca Initial load
duke
parents:
diff changeset
   950
    }
474761f14bca Initial load
duke
parents:
diff changeset
   951
474761f14bca Initial load
duke
parents:
diff changeset
   952
    private String convertToSingleLine(String contentType) {
474761f14bca Initial load
duke
parents:
diff changeset
   953
        StringBuffer buffer = new StringBuffer();
474761f14bca Initial load
duke
parents:
diff changeset
   954
        for (int i = 0; i < contentType.length(); i ++) {
474761f14bca Initial load
duke
parents:
diff changeset
   955
            char c = contentType.charAt(i);
474761f14bca Initial load
duke
parents:
diff changeset
   956
            if (c != '\r' && c != '\n' && c != '\t')
474761f14bca Initial load
duke
parents:
diff changeset
   957
                buffer.append(c);
474761f14bca Initial load
duke
parents:
diff changeset
   958
        }
474761f14bca Initial load
duke
parents:
diff changeset
   959
        return buffer.toString();
474761f14bca Initial load
duke
parents:
diff changeset
   960
    }
474761f14bca Initial load
duke
parents:
diff changeset
   961
474761f14bca Initial load
duke
parents:
diff changeset
   962
    private MimeMultipart getMimeMessage() throws SOAPException {
474761f14bca Initial load
duke
parents:
diff changeset
   963
        try {
474761f14bca Initial load
duke
parents:
diff changeset
   964
            SOAPPartImpl soapPart = (SOAPPartImpl) getSOAPPart();
474761f14bca Initial load
duke
parents:
diff changeset
   965
            MimeBodyPart mimeSoapPart = soapPart.getMimePart();
474761f14bca Initial load
duke
parents:
diff changeset
   966
474761f14bca Initial load
duke
parents:
diff changeset
   967
            /*
474761f14bca Initial load
duke
parents:
diff changeset
   968
             * Get content type from this message instead of soapPart
474761f14bca Initial load
duke
parents:
diff changeset
   969
             * to ensure agreement if soapPart is transcoded (XML <-> FI)
474761f14bca Initial load
duke
parents:
diff changeset
   970
             */
474761f14bca Initial load
duke
parents:
diff changeset
   971
            ContentType soapPartCtype = new ContentType(getExpectedContentType());
474761f14bca Initial load
duke
parents:
diff changeset
   972
474761f14bca Initial load
duke
parents:
diff changeset
   973
            if (!isFastInfoset) {
474761f14bca Initial load
duke
parents:
diff changeset
   974
                soapPartCtype.setParameter("charset", initCharset());
474761f14bca Initial load
duke
parents:
diff changeset
   975
            }
474761f14bca Initial load
duke
parents:
diff changeset
   976
            mimeSoapPart.setHeader("Content-Type", soapPartCtype.toString());
474761f14bca Initial load
duke
parents:
diff changeset
   977
474761f14bca Initial load
duke
parents:
diff changeset
   978
            MimeMultipart headerAndBody = null;
474761f14bca Initial load
duke
parents:
diff changeset
   979
474761f14bca Initial load
duke
parents:
diff changeset
   980
            if (!switchOffBM && !switchOffLazyAttachment &&
474761f14bca Initial load
duke
parents:
diff changeset
   981
                   (multiPart != null) && !attachmentsInitialized) {
474761f14bca Initial load
duke
parents:
diff changeset
   982
                headerAndBody = new BMMimeMultipart();
474761f14bca Initial load
duke
parents:
diff changeset
   983
                headerAndBody.addBodyPart(mimeSoapPart);
474761f14bca Initial load
duke
parents:
diff changeset
   984
                if (attachments != null) {
474761f14bca Initial load
duke
parents:
diff changeset
   985
                    for (Iterator eachAttachment = attachments.iterator();
474761f14bca Initial load
duke
parents:
diff changeset
   986
                         eachAttachment.hasNext();) {
474761f14bca Initial load
duke
parents:
diff changeset
   987
                        headerAndBody.addBodyPart(
474761f14bca Initial load
duke
parents:
diff changeset
   988
                            ((AttachmentPartImpl) eachAttachment.next())
474761f14bca Initial load
duke
parents:
diff changeset
   989
                                .getMimePart());
474761f14bca Initial load
duke
parents:
diff changeset
   990
                    }
474761f14bca Initial load
duke
parents:
diff changeset
   991
                }
474761f14bca Initial load
duke
parents:
diff changeset
   992
                InputStream in = ((BMMimeMultipart)multiPart).getInputStream();
474761f14bca Initial load
duke
parents:
diff changeset
   993
                if (!((BMMimeMultipart)multiPart).lastBodyPartFound() &&
474761f14bca Initial load
duke
parents:
diff changeset
   994
                    !((BMMimeMultipart)multiPart).isEndOfStream()) {
474761f14bca Initial load
duke
parents:
diff changeset
   995
                    ((BMMimeMultipart)headerAndBody).setInputStream(in);
474761f14bca Initial load
duke
parents:
diff changeset
   996
                    ((BMMimeMultipart)headerAndBody).setBoundary(
474761f14bca Initial load
duke
parents:
diff changeset
   997
                        ((BMMimeMultipart)multiPart).getBoundary());
474761f14bca Initial load
duke
parents:
diff changeset
   998
                    ((BMMimeMultipart)headerAndBody).
474761f14bca Initial load
duke
parents:
diff changeset
   999
                        setLazyAttachments(lazyAttachments);
474761f14bca Initial load
duke
parents:
diff changeset
  1000
                }
474761f14bca Initial load
duke
parents:
diff changeset
  1001
474761f14bca Initial load
duke
parents:
diff changeset
  1002
            } else {
474761f14bca Initial load
duke
parents:
diff changeset
  1003
                headerAndBody = new MimeMultipart();
474761f14bca Initial load
duke
parents:
diff changeset
  1004
                headerAndBody.addBodyPart(mimeSoapPart);
474761f14bca Initial load
duke
parents:
diff changeset
  1005
474761f14bca Initial load
duke
parents:
diff changeset
  1006
                for (Iterator eachAttachement = getAttachments();
474761f14bca Initial load
duke
parents:
diff changeset
  1007
                    eachAttachement.hasNext();
474761f14bca Initial load
duke
parents:
diff changeset
  1008
                    ) {
474761f14bca Initial load
duke
parents:
diff changeset
  1009
                    headerAndBody.addBodyPart(
474761f14bca Initial load
duke
parents:
diff changeset
  1010
                        ((AttachmentPartImpl) eachAttachement.next())
474761f14bca Initial load
duke
parents:
diff changeset
  1011
                            .getMimePart());
474761f14bca Initial load
duke
parents:
diff changeset
  1012
                }
474761f14bca Initial load
duke
parents:
diff changeset
  1013
            }
474761f14bca Initial load
duke
parents:
diff changeset
  1014
474761f14bca Initial load
duke
parents:
diff changeset
  1015
            ContentType contentType = headerAndBody.getContentType();
474761f14bca Initial load
duke
parents:
diff changeset
  1016
474761f14bca Initial load
duke
parents:
diff changeset
  1017
            ParameterList l = contentType.getParameterList();
474761f14bca Initial load
duke
parents:
diff changeset
  1018
474761f14bca Initial load
duke
parents:
diff changeset
  1019
            // set content type depending on SOAP version
474761f14bca Initial load
duke
parents:
diff changeset
  1020
            l.set("type", getExpectedContentType());
474761f14bca Initial load
duke
parents:
diff changeset
  1021
            l.set("boundary", contentType.getParameter("boundary"));
474761f14bca Initial load
duke
parents:
diff changeset
  1022
            ContentType nct = new ContentType("multipart", "related", l);
474761f14bca Initial load
duke
parents:
diff changeset
  1023
474761f14bca Initial load
duke
parents:
diff changeset
  1024
            headers.setHeader(
474761f14bca Initial load
duke
parents:
diff changeset
  1025
                "Content-Type",
474761f14bca Initial load
duke
parents:
diff changeset
  1026
                convertToSingleLine(nct.toString()));
474761f14bca Initial load
duke
parents:
diff changeset
  1027
          // TBD
474761f14bca Initial load
duke
parents:
diff changeset
  1028
          //    Set content length MIME header here.
474761f14bca Initial load
duke
parents:
diff changeset
  1029
474761f14bca Initial load
duke
parents:
diff changeset
  1030
            return headerAndBody;
474761f14bca Initial load
duke
parents:
diff changeset
  1031
        } catch (SOAPException ex) {
474761f14bca Initial load
duke
parents:
diff changeset
  1032
            throw ex;
474761f14bca Initial load
duke
parents:
diff changeset
  1033
        } catch (Throwable ex) {
474761f14bca Initial load
duke
parents:
diff changeset
  1034
            log.severe("SAAJ0538.soap.cannot.convert.msg.to.multipart.obj");
474761f14bca Initial load
duke
parents:
diff changeset
  1035
            throw new SOAPExceptionImpl(
474761f14bca Initial load
duke
parents:
diff changeset
  1036
                "Unable to convert SOAP message into "
474761f14bca Initial load
duke
parents:
diff changeset
  1037
                    + "a MimeMultipart object",
474761f14bca Initial load
duke
parents:
diff changeset
  1038
                ex);
474761f14bca Initial load
duke
parents:
diff changeset
  1039
        }
474761f14bca Initial load
duke
parents:
diff changeset
  1040
    }
474761f14bca Initial load
duke
parents:
diff changeset
  1041
474761f14bca Initial load
duke
parents:
diff changeset
  1042
    private String initCharset() {
474761f14bca Initial load
duke
parents:
diff changeset
  1043
474761f14bca Initial load
duke
parents:
diff changeset
  1044
        String charset = null;
474761f14bca Initial load
duke
parents:
diff changeset
  1045
474761f14bca Initial load
duke
parents:
diff changeset
  1046
        String[] cts = getMimeHeaders().getHeader("Content-Type");
474761f14bca Initial load
duke
parents:
diff changeset
  1047
        if ((cts != null) && (cts[0] != null)) {
474761f14bca Initial load
duke
parents:
diff changeset
  1048
            charset = getCharsetString(cts[0]);
474761f14bca Initial load
duke
parents:
diff changeset
  1049
        }
474761f14bca Initial load
duke
parents:
diff changeset
  1050
474761f14bca Initial load
duke
parents:
diff changeset
  1051
        if (charset == null) {
474761f14bca Initial load
duke
parents:
diff changeset
  1052
            charset = (String) getProperty(CHARACTER_SET_ENCODING);
474761f14bca Initial load
duke
parents:
diff changeset
  1053
        }
474761f14bca Initial load
duke
parents:
diff changeset
  1054
474761f14bca Initial load
duke
parents:
diff changeset
  1055
        if (charset != null) {
474761f14bca Initial load
duke
parents:
diff changeset
  1056
            return charset;
474761f14bca Initial load
duke
parents:
diff changeset
  1057
        }
474761f14bca Initial load
duke
parents:
diff changeset
  1058
474761f14bca Initial load
duke
parents:
diff changeset
  1059
        return "utf-8";
474761f14bca Initial load
duke
parents:
diff changeset
  1060
    }
474761f14bca Initial load
duke
parents:
diff changeset
  1061
474761f14bca Initial load
duke
parents:
diff changeset
  1062
    private String getCharsetString(String s) {
474761f14bca Initial load
duke
parents:
diff changeset
  1063
        try {
474761f14bca Initial load
duke
parents:
diff changeset
  1064
            int index = s.indexOf(";");
474761f14bca Initial load
duke
parents:
diff changeset
  1065
            if(index < 0)
474761f14bca Initial load
duke
parents:
diff changeset
  1066
                return null;
474761f14bca Initial load
duke
parents:
diff changeset
  1067
            ParameterList pl = new ParameterList(s.substring(index));
474761f14bca Initial load
duke
parents:
diff changeset
  1068
            return pl.get("charset");
474761f14bca Initial load
duke
parents:
diff changeset
  1069
        } catch(Exception e) {
474761f14bca Initial load
duke
parents:
diff changeset
  1070
            return null;
474761f14bca Initial load
duke
parents:
diff changeset
  1071
        }
474761f14bca Initial load
duke
parents:
diff changeset
  1072
    }
474761f14bca Initial load
duke
parents:
diff changeset
  1073
474761f14bca Initial load
duke
parents:
diff changeset
  1074
    public void saveChanges() throws SOAPException {
474761f14bca Initial load
duke
parents:
diff changeset
  1075
474761f14bca Initial load
duke
parents:
diff changeset
  1076
        // suck in all the data from the attachments and have it
474761f14bca Initial load
duke
parents:
diff changeset
  1077
        // ready for writing/sending etc.
474761f14bca Initial load
duke
parents:
diff changeset
  1078
474761f14bca Initial load
duke
parents:
diff changeset
  1079
        String charset = initCharset();
474761f14bca Initial load
duke
parents:
diff changeset
  1080
474761f14bca Initial load
duke
parents:
diff changeset
  1081
        /*if (countAttachments() == 0) {*/
474761f14bca Initial load
duke
parents:
diff changeset
  1082
        int attachmentCount = (attachments == null) ? 0 : attachments.size();
474761f14bca Initial load
duke
parents:
diff changeset
  1083
        if (attachmentCount == 0) {
474761f14bca Initial load
duke
parents:
diff changeset
  1084
            if (!switchOffBM && !switchOffLazyAttachment &&
474761f14bca Initial load
duke
parents:
diff changeset
  1085
                !attachmentsInitialized && (multiPart != null)) {
474761f14bca Initial load
duke
parents:
diff changeset
  1086
                // so there might be attachments
474761f14bca Initial load
duke
parents:
diff changeset
  1087
                attachmentCount = 1;
474761f14bca Initial load
duke
parents:
diff changeset
  1088
            }
474761f14bca Initial load
duke
parents:
diff changeset
  1089
        }
474761f14bca Initial load
duke
parents:
diff changeset
  1090
474761f14bca Initial load
duke
parents:
diff changeset
  1091
        try {
474761f14bca Initial load
duke
parents:
diff changeset
  1092
            if ((attachmentCount == 0) && !hasXOPContent()) {
474761f14bca Initial load
duke
parents:
diff changeset
  1093
                ByteInputStream in;
474761f14bca Initial load
duke
parents:
diff changeset
  1094
                try{
474761f14bca Initial load
duke
parents:
diff changeset
  1095
                /*
474761f14bca Initial load
duke
parents:
diff changeset
  1096
                 * Not sure why this is called getHeaderBytes(), but it actually
474761f14bca Initial load
duke
parents:
diff changeset
  1097
                 * returns the whole message as a byte stream. This stream could
474761f14bca Initial load
duke
parents:
diff changeset
  1098
                 * be either XML of Fast depending on the mode.
474761f14bca Initial load
duke
parents:
diff changeset
  1099
                 */
474761f14bca Initial load
duke
parents:
diff changeset
  1100
                    in = getHeaderBytes();
474761f14bca Initial load
duke
parents:
diff changeset
  1101
                    // no attachments, hence this property can be false
474761f14bca Initial load
duke
parents:
diff changeset
  1102
                    this.optimizeAttachmentProcessing = false;
474761f14bca Initial load
duke
parents:
diff changeset
  1103
                } catch (IOException ex) {
474761f14bca Initial load
duke
parents:
diff changeset
  1104
                    log.severe("SAAJ0539.soap.cannot.get.header.stream");
474761f14bca Initial load
duke
parents:
diff changeset
  1105
                    throw new SOAPExceptionImpl(
474761f14bca Initial load
duke
parents:
diff changeset
  1106
                            "Unable to get header stream in saveChanges: ",
474761f14bca Initial load
duke
parents:
diff changeset
  1107
                            ex);
474761f14bca Initial load
duke
parents:
diff changeset
  1108
                }
474761f14bca Initial load
duke
parents:
diff changeset
  1109
474761f14bca Initial load
duke
parents:
diff changeset
  1110
                messageBytes = in.getBytes();
474761f14bca Initial load
duke
parents:
diff changeset
  1111
                messageByteCount = in.getCount();
474761f14bca Initial load
duke
parents:
diff changeset
  1112
474761f14bca Initial load
duke
parents:
diff changeset
  1113
                headers.setHeader(
474761f14bca Initial load
duke
parents:
diff changeset
  1114
                        "Content-Type",
474761f14bca Initial load
duke
parents:
diff changeset
  1115
                        getExpectedContentType() +
474761f14bca Initial load
duke
parents:
diff changeset
  1116
                        (isFastInfoset ? "" : "; charset=" + charset));
474761f14bca Initial load
duke
parents:
diff changeset
  1117
                headers.setHeader(
474761f14bca Initial load
duke
parents:
diff changeset
  1118
                    "Content-Length",
474761f14bca Initial load
duke
parents:
diff changeset
  1119
                    Integer.toString(messageByteCount));
474761f14bca Initial load
duke
parents:
diff changeset
  1120
            } else {
474761f14bca Initial load
duke
parents:
diff changeset
  1121
                if(hasXOPContent())
474761f14bca Initial load
duke
parents:
diff changeset
  1122
                    mmp = getXOPMessage();
474761f14bca Initial load
duke
parents:
diff changeset
  1123
                else
474761f14bca Initial load
duke
parents:
diff changeset
  1124
                    mmp = getMimeMessage();
474761f14bca Initial load
duke
parents:
diff changeset
  1125
            }
474761f14bca Initial load
duke
parents:
diff changeset
  1126
        } catch (Throwable ex) {
474761f14bca Initial load
duke
parents:
diff changeset
  1127
            log.severe("SAAJ0540.soap.err.saving.multipart.msg");
474761f14bca Initial load
duke
parents:
diff changeset
  1128
            throw new SOAPExceptionImpl(
474761f14bca Initial load
duke
parents:
diff changeset
  1129
                    "Error during saving a multipart message",
474761f14bca Initial load
duke
parents:
diff changeset
  1130
                    ex);
474761f14bca Initial load
duke
parents:
diff changeset
  1131
        }
474761f14bca Initial load
duke
parents:
diff changeset
  1132
474761f14bca Initial load
duke
parents:
diff changeset
  1133
        // FIX ME -- SOAP Action replaced by Content-Type optional parameter action
474761f14bca Initial load
duke
parents:
diff changeset
  1134
        /*
474761f14bca Initial load
duke
parents:
diff changeset
  1135
        if(isCorrectSoapVersion(SOAP1_1_FLAG)) {
474761f14bca Initial load
duke
parents:
diff changeset
  1136
474761f14bca Initial load
duke
parents:
diff changeset
  1137
            String[] soapAction = headers.getHeader("SOAPAction");
474761f14bca Initial load
duke
parents:
diff changeset
  1138
474761f14bca Initial load
duke
parents:
diff changeset
  1139
            if (soapAction == null || soapAction.length == 0)
474761f14bca Initial load
duke
parents:
diff changeset
  1140
                headers.setHeader("SOAPAction", "\"\"");
474761f14bca Initial load
duke
parents:
diff changeset
  1141
474761f14bca Initial load
duke
parents:
diff changeset
  1142
        }
474761f14bca Initial load
duke
parents:
diff changeset
  1143
        */
474761f14bca Initial load
duke
parents:
diff changeset
  1144
474761f14bca Initial load
duke
parents:
diff changeset
  1145
        saved = true;
474761f14bca Initial load
duke
parents:
diff changeset
  1146
    }
474761f14bca Initial load
duke
parents:
diff changeset
  1147
474761f14bca Initial load
duke
parents:
diff changeset
  1148
    private MimeMultipart getXOPMessage() throws SOAPException {
474761f14bca Initial load
duke
parents:
diff changeset
  1149
        try {
474761f14bca Initial load
duke
parents:
diff changeset
  1150
            MimeMultipart headerAndBody = new MimeMultipart();
474761f14bca Initial load
duke
parents:
diff changeset
  1151
            SOAPPartImpl soapPart =  (SOAPPartImpl)getSOAPPart();
474761f14bca Initial load
duke
parents:
diff changeset
  1152
            MimeBodyPart mimeSoapPart = soapPart.getMimePart();
474761f14bca Initial load
duke
parents:
diff changeset
  1153
            ContentType soapPartCtype =
474761f14bca Initial load
duke
parents:
diff changeset
  1154
                new ContentType("application/xop+xml");
474761f14bca Initial load
duke
parents:
diff changeset
  1155
            soapPartCtype.setParameter("type", getExpectedContentType());
474761f14bca Initial load
duke
parents:
diff changeset
  1156
            String charset = initCharset();
474761f14bca Initial load
duke
parents:
diff changeset
  1157
            soapPartCtype.setParameter("charset", charset);
474761f14bca Initial load
duke
parents:
diff changeset
  1158
            mimeSoapPart.setHeader("Content-Type", soapPartCtype.toString());
474761f14bca Initial load
duke
parents:
diff changeset
  1159
            headerAndBody.addBodyPart(mimeSoapPart);
474761f14bca Initial load
duke
parents:
diff changeset
  1160
474761f14bca Initial load
duke
parents:
diff changeset
  1161
            for (Iterator eachAttachement = getAttachments();
474761f14bca Initial load
duke
parents:
diff changeset
  1162
                eachAttachement.hasNext();
474761f14bca Initial load
duke
parents:
diff changeset
  1163
                ) {
474761f14bca Initial load
duke
parents:
diff changeset
  1164
                headerAndBody.addBodyPart(
474761f14bca Initial load
duke
parents:
diff changeset
  1165
                    ((AttachmentPartImpl) eachAttachement.next())
474761f14bca Initial load
duke
parents:
diff changeset
  1166
                        .getMimePart());
474761f14bca Initial load
duke
parents:
diff changeset
  1167
            }
474761f14bca Initial load
duke
parents:
diff changeset
  1168
474761f14bca Initial load
duke
parents:
diff changeset
  1169
            ContentType contentType = headerAndBody.getContentType();
474761f14bca Initial load
duke
parents:
diff changeset
  1170
474761f14bca Initial load
duke
parents:
diff changeset
  1171
            ParameterList l = contentType.getParameterList();
474761f14bca Initial load
duke
parents:
diff changeset
  1172
474761f14bca Initial load
duke
parents:
diff changeset
  1173
            //lets not write start-info for now till we get servlet fix done
474761f14bca Initial load
duke
parents:
diff changeset
  1174
            l.set("start-info", getExpectedContentType());//+";charset="+initCharset());
474761f14bca Initial load
duke
parents:
diff changeset
  1175
474761f14bca Initial load
duke
parents:
diff changeset
  1176
            // set content type depending on SOAP version
474761f14bca Initial load
duke
parents:
diff changeset
  1177
            l.set("type", "application/xop+xml");
474761f14bca Initial load
duke
parents:
diff changeset
  1178
474761f14bca Initial load
duke
parents:
diff changeset
  1179
            if (isCorrectSoapVersion(SOAP1_2_FLAG)) {
474761f14bca Initial load
duke
parents:
diff changeset
  1180
                 String action = getAction();
474761f14bca Initial load
duke
parents:
diff changeset
  1181
                 if(action != null)
474761f14bca Initial load
duke
parents:
diff changeset
  1182
                     l.set("action", action);
474761f14bca Initial load
duke
parents:
diff changeset
  1183
            }
474761f14bca Initial load
duke
parents:
diff changeset
  1184
474761f14bca Initial load
duke
parents:
diff changeset
  1185
            l.set("boundary", contentType.getParameter("boundary"));
474761f14bca Initial load
duke
parents:
diff changeset
  1186
            ContentType nct = new ContentType("Multipart", "Related", l);
474761f14bca Initial load
duke
parents:
diff changeset
  1187
            headers.setHeader(
474761f14bca Initial load
duke
parents:
diff changeset
  1188
                "Content-Type",
474761f14bca Initial load
duke
parents:
diff changeset
  1189
                convertToSingleLine(nct.toString()));
474761f14bca Initial load
duke
parents:
diff changeset
  1190
            // TBD
474761f14bca Initial load
duke
parents:
diff changeset
  1191
            //    Set content length MIME header here.
474761f14bca Initial load
duke
parents:
diff changeset
  1192
474761f14bca Initial load
duke
parents:
diff changeset
  1193
            return headerAndBody;
474761f14bca Initial load
duke
parents:
diff changeset
  1194
        } catch (SOAPException ex) {
474761f14bca Initial load
duke
parents:
diff changeset
  1195
            throw ex;
474761f14bca Initial load
duke
parents:
diff changeset
  1196
        } catch (Throwable ex) {
474761f14bca Initial load
duke
parents:
diff changeset
  1197
            log.severe("SAAJ0538.soap.cannot.convert.msg.to.multipart.obj");
474761f14bca Initial load
duke
parents:
diff changeset
  1198
            throw new SOAPExceptionImpl(
474761f14bca Initial load
duke
parents:
diff changeset
  1199
                "Unable to convert SOAP message into "
474761f14bca Initial load
duke
parents:
diff changeset
  1200
                    + "a MimeMultipart object",
474761f14bca Initial load
duke
parents:
diff changeset
  1201
                ex);
474761f14bca Initial load
duke
parents:
diff changeset
  1202
        }
474761f14bca Initial load
duke
parents:
diff changeset
  1203
474761f14bca Initial load
duke
parents:
diff changeset
  1204
    }
474761f14bca Initial load
duke
parents:
diff changeset
  1205
474761f14bca Initial load
duke
parents:
diff changeset
  1206
    private boolean hasXOPContent() throws ParseException {
474761f14bca Initial load
duke
parents:
diff changeset
  1207
        String type = getContentType();
474761f14bca Initial load
duke
parents:
diff changeset
  1208
        if(type == null)
474761f14bca Initial load
duke
parents:
diff changeset
  1209
            return false;
474761f14bca Initial load
duke
parents:
diff changeset
  1210
        ContentType ct = new ContentType(type);
474761f14bca Initial load
duke
parents:
diff changeset
  1211
        return isMimeMultipartXOPPackage(ct) || isSOAPBodyXOPPackage(ct);
474761f14bca Initial load
duke
parents:
diff changeset
  1212
    }
474761f14bca Initial load
duke
parents:
diff changeset
  1213
474761f14bca Initial load
duke
parents:
diff changeset
  1214
    public void writeTo(OutputStream out) throws SOAPException, IOException {
474761f14bca Initial load
duke
parents:
diff changeset
  1215
        if (saveRequired()){
474761f14bca Initial load
duke
parents:
diff changeset
  1216
            this.optimizeAttachmentProcessing = true;
474761f14bca Initial load
duke
parents:
diff changeset
  1217
            saveChanges();
474761f14bca Initial load
duke
parents:
diff changeset
  1218
        }
474761f14bca Initial load
duke
parents:
diff changeset
  1219
474761f14bca Initial load
duke
parents:
diff changeset
  1220
        if(!optimizeAttachmentProcessing){
474761f14bca Initial load
duke
parents:
diff changeset
  1221
            out.write(messageBytes, 0, messageByteCount);
474761f14bca Initial load
duke
parents:
diff changeset
  1222
        }
474761f14bca Initial load
duke
parents:
diff changeset
  1223
        else{
474761f14bca Initial load
duke
parents:
diff changeset
  1224
            try{
474761f14bca Initial load
duke
parents:
diff changeset
  1225
                if(hasXOPContent()){
474761f14bca Initial load
duke
parents:
diff changeset
  1226
                    mmp.writeTo(out);
474761f14bca Initial load
duke
parents:
diff changeset
  1227
                }else{
474761f14bca Initial load
duke
parents:
diff changeset
  1228
                    mmp.writeTo(out);
474761f14bca Initial load
duke
parents:
diff changeset
  1229
                    if (!switchOffBM && !switchOffLazyAttachment &&
474761f14bca Initial load
duke
parents:
diff changeset
  1230
                            (multiPart != null) && !attachmentsInitialized) {
474761f14bca Initial load
duke
parents:
diff changeset
  1231
                        ((BMMimeMultipart)multiPart).setInputStream(
474761f14bca Initial load
duke
parents:
diff changeset
  1232
                                ((BMMimeMultipart)mmp).getInputStream());
474761f14bca Initial load
duke
parents:
diff changeset
  1233
                    }
474761f14bca Initial load
duke
parents:
diff changeset
  1234
                }
474761f14bca Initial load
duke
parents:
diff changeset
  1235
            } catch(Exception ex){
474761f14bca Initial load
duke
parents:
diff changeset
  1236
                log.severe("SAAJ0540.soap.err.saving.multipart.msg");
474761f14bca Initial load
duke
parents:
diff changeset
  1237
                throw new SOAPExceptionImpl(
474761f14bca Initial load
duke
parents:
diff changeset
  1238
                        "Error during saving a multipart message",
474761f14bca Initial load
duke
parents:
diff changeset
  1239
                        ex);
474761f14bca Initial load
duke
parents:
diff changeset
  1240
            }
474761f14bca Initial load
duke
parents:
diff changeset
  1241
        }
474761f14bca Initial load
duke
parents:
diff changeset
  1242
474761f14bca Initial load
duke
parents:
diff changeset
  1243
        if(isCorrectSoapVersion(SOAP1_1_FLAG)) {
474761f14bca Initial load
duke
parents:
diff changeset
  1244
474761f14bca Initial load
duke
parents:
diff changeset
  1245
            String[] soapAction = headers.getHeader("SOAPAction");
474761f14bca Initial load
duke
parents:
diff changeset
  1246
474761f14bca Initial load
duke
parents:
diff changeset
  1247
            if (soapAction == null || soapAction.length == 0)
474761f14bca Initial load
duke
parents:
diff changeset
  1248
                headers.setHeader("SOAPAction", "\"\"");
474761f14bca Initial load
duke
parents:
diff changeset
  1249
474761f14bca Initial load
duke
parents:
diff changeset
  1250
        }
474761f14bca Initial load
duke
parents:
diff changeset
  1251
474761f14bca Initial load
duke
parents:
diff changeset
  1252
        messageBytes = null;
474761f14bca Initial load
duke
parents:
diff changeset
  1253
        needsSave();
474761f14bca Initial load
duke
parents:
diff changeset
  1254
    }
474761f14bca Initial load
duke
parents:
diff changeset
  1255
474761f14bca Initial load
duke
parents:
diff changeset
  1256
    public SOAPBody getSOAPBody() throws SOAPException {
2678
57cf2a1c1a05 6831313: update jaxws in OpenJDK7 to 2.1 plus bug fixes from OpenJDK 6
tbell
parents: 8
diff changeset
  1257
        SOAPBody body = getSOAPPart().getEnvelope().getBody();
57cf2a1c1a05 6831313: update jaxws in OpenJDK7 to 2.1 plus bug fixes from OpenJDK 6
tbell
parents: 8
diff changeset
  1258
        /*if (body == null) {
57cf2a1c1a05 6831313: update jaxws in OpenJDK7 to 2.1 plus bug fixes from OpenJDK 6
tbell
parents: 8
diff changeset
  1259
             throw new SOAPException("No SOAP Body was found in the SOAP Message");
57cf2a1c1a05 6831313: update jaxws in OpenJDK7 to 2.1 plus bug fixes from OpenJDK 6
tbell
parents: 8
diff changeset
  1260
        }*/
57cf2a1c1a05 6831313: update jaxws in OpenJDK7 to 2.1 plus bug fixes from OpenJDK 6
tbell
parents: 8
diff changeset
  1261
        return body;
8
474761f14bca Initial load
duke
parents:
diff changeset
  1262
    }
474761f14bca Initial load
duke
parents:
diff changeset
  1263
474761f14bca Initial load
duke
parents:
diff changeset
  1264
    public SOAPHeader getSOAPHeader() throws SOAPException {
2678
57cf2a1c1a05 6831313: update jaxws in OpenJDK7 to 2.1 plus bug fixes from OpenJDK 6
tbell
parents: 8
diff changeset
  1265
        SOAPHeader hdr = getSOAPPart().getEnvelope().getHeader();
57cf2a1c1a05 6831313: update jaxws in OpenJDK7 to 2.1 plus bug fixes from OpenJDK 6
tbell
parents: 8
diff changeset
  1266
        /*if (hdr == null) {
57cf2a1c1a05 6831313: update jaxws in OpenJDK7 to 2.1 plus bug fixes from OpenJDK 6
tbell
parents: 8
diff changeset
  1267
            throw new SOAPException("No SOAP Header was found in the SOAP Message");
57cf2a1c1a05 6831313: update jaxws in OpenJDK7 to 2.1 plus bug fixes from OpenJDK 6
tbell
parents: 8
diff changeset
  1268
        }*/
57cf2a1c1a05 6831313: update jaxws in OpenJDK7 to 2.1 plus bug fixes from OpenJDK 6
tbell
parents: 8
diff changeset
  1269
        return hdr;
8
474761f14bca Initial load
duke
parents:
diff changeset
  1270
    }
474761f14bca Initial load
duke
parents:
diff changeset
  1271
474761f14bca Initial load
duke
parents:
diff changeset
  1272
    private void initializeAllAttachments ()
474761f14bca Initial load
duke
parents:
diff changeset
  1273
        throws MessagingException, SOAPException {
474761f14bca Initial load
duke
parents:
diff changeset
  1274
        if (switchOffBM || switchOffLazyAttachment) {
474761f14bca Initial load
duke
parents:
diff changeset
  1275
            return;
474761f14bca Initial load
duke
parents:
diff changeset
  1276
        }
474761f14bca Initial load
duke
parents:
diff changeset
  1277
474761f14bca Initial load
duke
parents:
diff changeset
  1278
        if (attachmentsInitialized || (multiPart == null)) {
474761f14bca Initial load
duke
parents:
diff changeset
  1279
            return;
474761f14bca Initial load
duke
parents:
diff changeset
  1280
        }
474761f14bca Initial load
duke
parents:
diff changeset
  1281
474761f14bca Initial load
duke
parents:
diff changeset
  1282
        if (attachments == null)
474761f14bca Initial load
duke
parents:
diff changeset
  1283
            attachments = new FinalArrayList();
474761f14bca Initial load
duke
parents:
diff changeset
  1284
474761f14bca Initial load
duke
parents:
diff changeset
  1285
        int count = multiPart.getCount();
474761f14bca Initial load
duke
parents:
diff changeset
  1286
        for (int i=0; i < count; i++ ) {
474761f14bca Initial load
duke
parents:
diff changeset
  1287
            initializeAttachment(multiPart.getBodyPart(i));
474761f14bca Initial load
duke
parents:
diff changeset
  1288
        }
474761f14bca Initial load
duke
parents:
diff changeset
  1289
        attachmentsInitialized = true;
474761f14bca Initial load
duke
parents:
diff changeset
  1290
        //multiPart = null;
474761f14bca Initial load
duke
parents:
diff changeset
  1291
        needsSave();
474761f14bca Initial load
duke
parents:
diff changeset
  1292
     }
474761f14bca Initial load
duke
parents:
diff changeset
  1293
474761f14bca Initial load
duke
parents:
diff changeset
  1294
    private void initializeAttachment(MimeBodyPart mbp) throws SOAPException {
474761f14bca Initial load
duke
parents:
diff changeset
  1295
        AttachmentPartImpl attachmentPart = new AttachmentPartImpl();
474761f14bca Initial load
duke
parents:
diff changeset
  1296
        DataHandler attachmentHandler = mbp.getDataHandler();
474761f14bca Initial load
duke
parents:
diff changeset
  1297
        attachmentPart.setDataHandler(attachmentHandler);
474761f14bca Initial load
duke
parents:
diff changeset
  1298
474761f14bca Initial load
duke
parents:
diff changeset
  1299
        AttachmentPartImpl.copyMimeHeaders(mbp, attachmentPart);
474761f14bca Initial load
duke
parents:
diff changeset
  1300
        attachments.add(attachmentPart);
474761f14bca Initial load
duke
parents:
diff changeset
  1301
    }
474761f14bca Initial load
duke
parents:
diff changeset
  1302
474761f14bca Initial load
duke
parents:
diff changeset
  1303
    private void initializeAttachment(MimeMultipart multiPart, int i)
474761f14bca Initial load
duke
parents:
diff changeset
  1304
        throws Exception {
474761f14bca Initial load
duke
parents:
diff changeset
  1305
        MimeBodyPart currentBodyPart = multiPart.getBodyPart(i);
474761f14bca Initial load
duke
parents:
diff changeset
  1306
        AttachmentPartImpl attachmentPart = new AttachmentPartImpl();
474761f14bca Initial load
duke
parents:
diff changeset
  1307
474761f14bca Initial load
duke
parents:
diff changeset
  1308
        DataHandler attachmentHandler = currentBodyPart.getDataHandler();
474761f14bca Initial load
duke
parents:
diff changeset
  1309
        attachmentPart.setDataHandler(attachmentHandler);
474761f14bca Initial load
duke
parents:
diff changeset
  1310
474761f14bca Initial load
duke
parents:
diff changeset
  1311
        AttachmentPartImpl.copyMimeHeaders(currentBodyPart, attachmentPart);
474761f14bca Initial load
duke
parents:
diff changeset
  1312
        addAttachmentPart(attachmentPart);
474761f14bca Initial load
duke
parents:
diff changeset
  1313
    }
474761f14bca Initial load
duke
parents:
diff changeset
  1314
474761f14bca Initial load
duke
parents:
diff changeset
  1315
    private void setMimeHeaders(SOAPPart soapPart,
474761f14bca Initial load
duke
parents:
diff changeset
  1316
            MimeBodyPart soapMessagePart) throws Exception {
474761f14bca Initial load
duke
parents:
diff changeset
  1317
474761f14bca Initial load
duke
parents:
diff changeset
  1318
        // first remove the existing content-type
474761f14bca Initial load
duke
parents:
diff changeset
  1319
        soapPart.removeAllMimeHeaders();
474761f14bca Initial load
duke
parents:
diff changeset
  1320
        // add everything present in soapMessagePart
474761f14bca Initial load
duke
parents:
diff changeset
  1321
        List headers = soapMessagePart.getAllHeaders();
474761f14bca Initial load
duke
parents:
diff changeset
  1322
        int sz = headers.size();
474761f14bca Initial load
duke
parents:
diff changeset
  1323
        for( int i=0; i<sz; i++ ) {
474761f14bca Initial load
duke
parents:
diff changeset
  1324
            Header h = (Header) headers.get(i);
474761f14bca Initial load
duke
parents:
diff changeset
  1325
            soapPart.addMimeHeader(h.getName(), h.getValue());
474761f14bca Initial load
duke
parents:
diff changeset
  1326
        }
474761f14bca Initial load
duke
parents:
diff changeset
  1327
    }
474761f14bca Initial load
duke
parents:
diff changeset
  1328
474761f14bca Initial load
duke
parents:
diff changeset
  1329
    private void initCharsetProperty(ContentType contentType) {
474761f14bca Initial load
duke
parents:
diff changeset
  1330
        String charset = contentType.getParameter("charset");
474761f14bca Initial load
duke
parents:
diff changeset
  1331
        if (charset != null) {
474761f14bca Initial load
duke
parents:
diff changeset
  1332
            ((SOAPPartImpl) getSOAPPart()).setSourceCharsetEncoding(charset);
474761f14bca Initial load
duke
parents:
diff changeset
  1333
            if(!charset.equalsIgnoreCase("utf-8"))
474761f14bca Initial load
duke
parents:
diff changeset
  1334
                setProperty(CHARACTER_SET_ENCODING, charset);
474761f14bca Initial load
duke
parents:
diff changeset
  1335
        }
474761f14bca Initial load
duke
parents:
diff changeset
  1336
    }
474761f14bca Initial load
duke
parents:
diff changeset
  1337
474761f14bca Initial load
duke
parents:
diff changeset
  1338
    public void setLazyAttachments(boolean flag) {
474761f14bca Initial load
duke
parents:
diff changeset
  1339
        lazyAttachments = flag;
474761f14bca Initial load
duke
parents:
diff changeset
  1340
    }
474761f14bca Initial load
duke
parents:
diff changeset
  1341
474761f14bca Initial load
duke
parents:
diff changeset
  1342
}