soappartimpl.java

来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 1,226 行 · 第 1/5 页

JAVA
1,226
字号
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.axis2.saaj;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Iterator;

import javax.xml.soap.MimeHeader;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.apache.axiom.attachments.Attachments;
import org.apache.axiom.om.impl.MTOMConstants;
import org.apache.axiom.om.util.StAXUtils;
import org.apache.axiom.soap.SOAP11Constants;
import org.apache.axiom.soap.SOAP12Constants;
import org.apache.axiom.soap.SOAPFactory;
import org.apache.axiom.soap.impl.builder.MTOMStAXSOAPModelBuilder;
import org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder;
import org.apache.axiom.soap.impl.dom.soap11.SOAP11Factory;
import org.apache.axiom.soap.impl.dom.soap12.SOAP12Factory;
import org.apache.axis2.builder.BuilderUtil;
import org.apache.axis2.saaj.util.IDGenerator;
import org.apache.axis2.transport.http.HTTPConstants;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Attr;
import org.w3c.dom.CDATASection;
import org.w3c.dom.Comment;
import org.w3c.dom.DOMConfiguration;
import org.w3c.dom.DOMException;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentFragment;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Element;
import org.w3c.dom.EntityReference;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.ProcessingInstruction;
import org.w3c.dom.Text;
import org.w3c.dom.UserDataHandler;

public class SOAPPartImpl extends SOAPPart {

    private static final Log log = LogFactory.getLog(SOAPPartImpl.class);

    private Document document;
    private SOAPMessage soapMessage;
    private SOAPEnvelopeImpl envelope;
    private MimeHeadersEx mimeHeaders = new MimeHeadersEx();

    private Source source;

    public SOAPPartImpl(SOAPMessageImpl parentSoapMsg,
                        SOAPEnvelopeImpl soapEnvelope) {
        //setMimeHeader(HTTPConstants.HEADER_CONTENT_ID, IDGenerator.generateID());
        //setMimeHeader(HTTPConstants.HEADER_CONTENT_TYPE, "text/xml");
        this.mimeHeaders = (MimeHeadersEx)parentSoapMsg.getMimeHeaders();
        soapMessage = parentSoapMsg;
        envelope = soapEnvelope;
        document = soapEnvelope.getOwnerDocument();
    }

    public SOAPPartImpl(SOAPMessageImpl parentSoapMsg,
                        InputStream inputStream, javax.xml.soap.MimeHeaders mimeHeaders
    ) throws SOAPException {
        String contentType = "";
        String fullContentTypeStr = "";
        if (mimeHeaders == null) {
            //TODO : read string from constants
            mimeHeaders = new MimeHeaders();
            mimeHeaders.addHeader("Content-ID", IDGenerator.generateID());
            mimeHeaders.addHeader("content-type", HTTPConstants.MEDIA_TYPE_APPLICATION_SOAP_XML);
        } else {
            String contentTypes[] = mimeHeaders.getHeader(HTTPConstants.CONTENT_TYPE);
            if (contentTypes != null && contentTypes.length > 0) {
                fullContentTypeStr = contentTypes[0];
                contentType = extractFirstPart(fullContentTypeStr);
            }
        }

        Iterator mimeHeaderIterator = mimeHeaders.getAllHeaders();
        while (mimeHeaderIterator.hasNext()) {
            MimeHeader mimeHeader = (MimeHeader)mimeHeaderIterator.next();
            String value = mimeHeader.getValue();
            setMimeHeader(mimeHeader.getName(), value);
        }
        soapMessage = parentSoapMsg;

        String knownEncoding = (String) soapMessage.getProperty(SOAPMessage.CHARACTER_SET_ENCODING);
        XMLStreamReader xmlReader = null;
      
        
        InputStream modifiedInputStream = null;
        StAXSOAPModelBuilder builder = null;
        InputStreamReader isReader = null;

        if (contentType.indexOf("multipart/related") == 0) {
            //This contains attachements
            try {
                Attachments attachments =
                        new Attachments(inputStream, fullContentTypeStr, false, "", "");
                modifiedInputStream = attachments.getSOAPPartInputStream();
               	isReader = new InputStreamReader(modifiedInputStream);
               

                String soapEnvelopeNamespaceURI =
                        BuilderUtil.getEnvelopeNamespace(fullContentTypeStr);

                String charSetEncoding =
                        BuilderUtil.getCharSetEncoding(attachments.getSOAPPartContentType());

                XMLStreamReader streamReader =
                        StAXUtils.createXMLStreamReader(BuilderUtil.getReader(
                                attachments.getSOAPPartInputStream(), charSetEncoding));

                SOAPFactory factory;
                if (SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI.equals(soapEnvelopeNamespaceURI)) {
                    factory = new SOAP11Factory();
                } else {
                    factory = new SOAP12Factory();
                }
                if (attachments.getAttachmentSpecType().equals(
                        MTOMConstants.MTOM_TYPE)) {
                    //Creates the MTOM specific MTOMStAXSOAPModelBuilder
                    builder = new MTOMStAXSOAPModelBuilder(streamReader,
                                                           factory,
                                                           attachments,
                                                           soapEnvelopeNamespaceURI);
                } else if (attachments.getAttachmentSpecType().equals(
                        MTOMConstants.SWA_TYPE)) {
                    builder = new StAXSOAPModelBuilder(streamReader,
                                                       factory,
                                                       soapEnvelopeNamespaceURI);
                } else if (attachments.getAttachmentSpecType().equals(
                        MTOMConstants.SWA_TYPE_12)) {
                    builder = new StAXSOAPModelBuilder(streamReader,
                                                       factory,
                                                       soapEnvelopeNamespaceURI);
                }

            } catch (Exception e) {
                throw new SOAPException(e);
            }
        } else {
            modifiedInputStream = inputStream;
            try {
                isReader = new InputStreamReader(modifiedInputStream);
                XMLStreamReader streamReader = null;
                
                if(knownEncoding != null){
                	streamReader = StAXUtils.createXMLStreamReader(modifiedInputStream, knownEncoding);
                }else{
                	streamReader = StAXUtils.createXMLStreamReader(modifiedInputStream);                	
                }

                if (HTTPConstants.MEDIA_TYPE_TEXT_XML.equals(contentType)) {
                    builder = new StAXSOAPModelBuilder(streamReader,
                                                       new SOAP11Factory(),
                                                       SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);

                } else if (HTTPConstants.MEDIA_TYPE_APPLICATION_SOAP_XML.equals(contentType)) {
                    builder = new StAXSOAPModelBuilder(streamReader,
                                                       new SOAP12Factory(),
                                                       SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI);

                } else if (HTTPConstants.MEDIA_TYPE_MULTIPART_RELATED.equals(contentType)) {
                    builder = new StAXSOAPModelBuilder(streamReader,
                                                       new SOAP11Factory(),
                                                       null);
                } else {
                    builder = new StAXSOAPModelBuilder(streamReader,
                                                       new SOAP11Factory(),
                                                       null);
                }
            } catch (XMLStreamException e) {
                throw new SOAPException(e);
            }
        }
        try {
            org.apache.axiom.soap.SOAPEnvelope soapEnvelope = builder.getSOAPEnvelope();
            envelope = new SOAPEnvelopeImpl(
                    (org.apache.axiom.soap.impl.dom.SOAPEnvelopeImpl)soapEnvelope);
            envelope.element.build();
            this.document = envelope.getOwnerDocument();
            javax.xml.transform.Source xmlSource =
                    new javax.xml.transform.stream.StreamSource( isReader);
            this.source = xmlSource;
        } catch (Exception e) {
            throw new SOAPException(e);
        }
    }

    private String extractFirstPart(String fullContentTypeStr) {
        String contentType;//tmpContentType can be like 'application/soap+xml; charset=UTF-8;'
        //Only the first part is important
        if (fullContentTypeStr.indexOf(";") > -1) {
            contentType = fullContentTypeStr.substring(0, fullContentTypeStr.indexOf(";"));
        } else {
            contentType = fullContentTypeStr;
        }
        return contentType;
    }


    public SOAPPartImpl(SOAPMessageImpl parentSoapMsg,
                        InputStream inputStream) throws SOAPException {
        this(parentSoapMsg, inputStream, null);
    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?