doclitwrappedplusmethodmarshaller.java

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

JAVA
743
字号
/*
 * 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.jaxws.marshaller.impl.alt;

import org.apache.axis2.jaxws.ExceptionFactory;
import org.apache.axis2.jaxws.description.EndpointDescription;
import org.apache.axis2.jaxws.description.EndpointInterfaceDescription;
import org.apache.axis2.jaxws.description.OperationDescription;
import org.apache.axis2.jaxws.description.ParameterDescription;
import org.apache.axis2.jaxws.i18n.Messages;
import org.apache.axis2.jaxws.marshaller.MethodMarshaller;
import org.apache.axis2.jaxws.message.Block;
import org.apache.axis2.jaxws.message.Message;
import org.apache.axis2.jaxws.message.Protocol;
import org.apache.axis2.jaxws.message.databinding.JAXBBlockContext;
import org.apache.axis2.jaxws.message.databinding.JAXBUtils;
import org.apache.axis2.jaxws.message.factory.JAXBBlockFactory;
import org.apache.axis2.jaxws.message.factory.MessageFactory;
import org.apache.axis2.jaxws.registry.FactoryRegistry;
import org.apache.axis2.jaxws.runtime.description.marshal.MarshalServiceRuntimeDescription;
import org.apache.axis2.jaxws.utility.ConvertUtils;
import org.apache.axis2.jaxws.wrapper.JAXBWrapperTool;
import org.apache.axis2.jaxws.wrapper.impl.JAXBWrapperToolImpl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.jws.WebParam.Mode;
import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;
import javax.xml.ws.WebServiceException;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;

/**
 * The DocLitWrapped "plus" marshaller is used when the java method is doc/lit wrapped, but it does
 * not exactly comply with the doc/lit rules.  This can result from the customer adding annotations,
 * but it can also be the result of WSGEN generation
 * <p/>
 * Here is an example:
 *
 * @WebMethod(action = "http://addheaders.sample.test.org/echoStringWSGEN1")
 * @RequestWrapper(localName = "echoStringWSGEN1", targetNamespace = "http://wrap.sample.test.org",
 * className = "org.test.sample.wrap.EchoStringWSGEN1")
 * @ResponseWrapper(localName = "echoStringWSGEN1Response", targetNamespace =
 * "http://wrap.sample.test.org", className = "org.test.sample.wrap.EchoStringWSGEN1Response")
 * public String echoStringWSGEN1(
 * @WebParam(name = "headerValue", targetNamespace = "http://wrap.sample.test.org", header = true)
 * String headerValue )
 * <p/>
 * In this case the method is doc/lit, but the headerValue parameter is passed in the header.  The
 * wrapper class EchoStringWSGEN1 will not have any child elements.
 * <p/>
 * A similar example is:
 * @WebMethod(action = "http://addheaders.sample.test.org/echoStringWSGEN2")
 * @RequestWrapper(localName = "echoStringWSGEN2", targetNamespace = "http://wrap.sample.test.org",
 * className = "org.test.sample.wrap.EchoStringWSGEN2")
 * @ResponseWrapper(localName = "echoStringWSGEN2Response", targetNamespace =
 * "http://wrap.sample.test.org", className = "org.test.sample.wrap.EchoStringWSGEN2Response")
 * @WebResult(name = "headerValue", targetNamespace = "http://wrap.sample.test.org", header = true)
 * public String echoStringWSGEN2(
 * @WebParam(name = "data", targetNamespace = "") String data )
 * <p/>
 * In this second case, the return is passed in a header (as defined by @WebResult)
 * <p/>
 * At the time of this writing, the "plus" marshaller is only used if a doc/lit wrapped method has
 * "header" parameters.  If other deviations are found, this class will be updated.  The advantage
 * of using DocLitWrapperPlus for these deviations is that it does not impact the normal
 * DocLitWrapper marshalling (which is used for 99% of the cases).  Thus these deviations will not
 * polute or slow down the normal flow.
 * <p/>
 * Scheu
 */
public class DocLitWrappedPlusMethodMarshaller implements MethodMarshaller {

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


    public DocLitWrappedPlusMethodMarshaller() {
        super();
    }

    public Object demarshalResponse(Message message, Object[] signatureArgs,
                                    OperationDescription operationDesc)
            throws WebServiceException {
        if (log.isDebugEnabled()) {
            log.debug("Calling DocLitWrapperPlusMethodMarshaller.demarshalResponse");
            log.debug(
                    "  The DocLitWrapped Plus marshaller is used when the web service method deviates from the normal doc/lit rules.");
        }
        // Note all exceptions are caught and rethrown with a WebServiceException

        EndpointInterfaceDescription ed = operationDesc.getEndpointInterfaceDescription();
        EndpointDescription endpointDesc = ed.getEndpointDescription();

        try {
            // Sample Document message
            // ..
            // <soapenv:body>
            //    <m:operationResponse ... >
            //       <param>hello</param>
            //    </m:operationResponse>
            // </soapenv:body>
            //
            // Important points.
            //   1) There is no operation element in the message
            //   2) The data blocks are located underneath the body element. 
            //   3) The name of the data block (m:operationResponse) is defined by the schema.
            //      It matches the operation name + "Response", and it has a corresponding JAXB element.
            //      This element is called the wrapper element
            //   4) The parameters are (param) are child elements of the wrapper element.
            ParameterDescription[] pds = operationDesc.getParameterDescriptions();
            MarshalServiceRuntimeDescription marshalDesc =
                    MethodMarshallerUtils.getMarshalDesc(endpointDesc);
            TreeSet<String> packages = marshalDesc.getPackages();
            String packagesKey = marshalDesc.getPackagesKey();

            // Determine if a returnValue is expected.
            // The return value may be an child element
            // The wrapper element 
            // or null
            Object returnValue = null;
            Class returnType = operationDesc.getResultActualType();
            boolean isChildReturn = !operationDesc.isJAXWSAsyncClientMethod() &&
                    (operationDesc.getResultPartName() != null);
            boolean isNoReturn = (returnType == void.class);

            // In usage=WRAPPED, there will be a single JAXB block inside the body.
            // Get this block
            JAXBBlockContext blockContext = new JAXBBlockContext(packages, packagesKey);
            JAXBBlockFactory factory =
                    (JAXBBlockFactory)FactoryRegistry.getFactory(JAXBBlockFactory.class);
            Block block = message.getBodyBlock(blockContext, factory);
            Object wrapperObject = block.getBusinessObject(true);

            // The child elements are within the object that 
            // represents the type
            if (wrapperObject instanceof JAXBElement) {
                wrapperObject = ((JAXBElement)wrapperObject).getValue();
            }

            // Use the wrapper tool to get the child objects.
            JAXBWrapperTool wrapperTool = new JAXBWrapperToolImpl();

            // Get the list of names for the output parameters
            List<String> names = new ArrayList<String>();
            List<ParameterDescription> pdList = new ArrayList<ParameterDescription>();
            for (int i = 0; i < pds.length; i++) {
                ParameterDescription pd = pds[i];
                if (pd.getMode() == Mode.OUT ||
                        pd.getMode() == Mode.INOUT) {
                    if (!pd.isHeader()) {
                        // Header names are not in the wrapped element
                        names.add(pd.getParameterName());
                    }
                    pdList.add(pd);
                }
            }


            if (!operationDesc.isResultHeader()) {
                // Normal case (Body Result) The return name is in the wrapped object
                if (isChildReturn && !isNoReturn) {
                    names.add(operationDesc.getResultPartName());
                }
            }

            // Get the child objects
            Object[] objects = wrapperTool.unWrap(wrapperObject, names,
                                                  marshalDesc.getPropertyDescriptorMap(
                                                          wrapperObject.getClass()));

            // Now create a list of paramValues so that we can populate the signature
            List<PDElement> pvList = new ArrayList<PDElement>();
            int bodyIndex = 0;
            for (int i = 0; i < pdList.size(); i++) {
                ParameterDescription pd = pdList.get(i);
                if (!pd.isHeader()) {
                    // Body elements are obtained from the unwrapped array of objects
                    Object value = objects[bodyIndex];
                    // The object in the PDElement must be an element
                    QName qName = new QName(pd.getTargetNamespace(),
                                            pd.getPartName());
                    Element element = null;
                    if (!marshalDesc.getAnnotationDesc(pd.getParameterActualType())
                            .hasXmlRootElement()) {
                        element = new Element(value, qName, pd.getParameterActualType());
                    } else {
                        element = new Element(value, qName);
                    }
                    pvList.add(new PDElement(pd, element, null));
                    bodyIndex++;
                } else {
                    // Header
                    // Get the Block from the header
                    String localName = pd.getParameterName();
                    JAXBBlockContext blkContext = new JAXBBlockContext(packages, packagesKey);

                    // Set up "by java type" unmarshalling if necessary
                    if (blkContext.getConstructionType() != JAXBUtils.CONSTRUCTION_TYPE
                            .BY_CONTEXT_PATH) {
                        Class actualType = pd.getParameterActualType();
                        if (MethodMarshallerUtils.isNotJAXBRootElement(actualType, marshalDesc)) {
                            blkContext.setProcessType(actualType);
                            blkContext.setIsxmlList(pd.isListType());
                        }
                    }
                    block = message.getHeaderBlock(pd.getTargetNamespace(), localName, blkContext,
                                                   factory);
                    Object value = block.getBusinessObject(true);
                    Element element =
                            new Element(value, new QName(pd.getTargetNamespace(), localName));
                    pvList.add(new PDElement(pd, element, blkContext.getProcessType()));
                }
            }

            // Populate the response Holders in the signature
            MethodMarshallerUtils.updateResponseSignatureArgs(pds, pvList, signatureArgs);

            // Now get the return value

            if (isNoReturn) {
                returnValue = null;
            } else if (isChildReturn) {
                if (!operationDesc.isResultHeader()) {
                    // Normal: Get the return from the jaxb object
                    returnValue = objects[objects.length - 1];
                } else {

⌨️ 快捷键说明

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