methodmarshallerutils.java

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

JAVA
1,273
字号
            context.setIsxmlList(isList);
        }
        Block block = null;
        if (isHeader) {
            block = message.getHeaderBlock(headerNS, headerLocalPart, context, factory);
        } else {
            if (hasOutputBodyParams) {
                block = message.getBodyBlock(0, context, factory);
            } else {
                // If there is only 1 block, we can use the get body block method
                // that streams the whole block content.
                block = message.getBodyBlock(context, factory);
            }
        }

        // Get the business object.  We want to return the object that represents the type.
        Element returnElement = new Element(block.getBusinessObject(true), block.getQName());
        return returnElement;
    }

    /**
     * Marshaling a fault is essentially the same for rpc/lit and doc/lit. This method is used by
     * all of the MethodMarshallers
     *
     * @param throwable     Throwable to marshal
     * @param operationDesc OperationDescription
     * @param packages      Packages needed to marshal the object
     * @param message       Message
     */
    static void marshalFaultResponse(Throwable throwable,
                                     MarshalServiceRuntimeDescription marshalDesc,
                                     OperationDescription operationDesc,
                                     Message message) {
        // Get the root cause of the throwable object
        Throwable t = ClassUtils.getRootCause(throwable);
        if (log.isDebugEnabled()) {
            log.debug("Marshal Throwable =" + throwable.getClass().getName());
            log.debug("  rootCause =" + t.getClass().getName());
            log.debug("  exception=" + t.toString());
            log.debug("  stack=" + stackToString(t));
        }

        XMLFault xmlfault = null;

        try {

            // There are 5 different categories of exceptions.  
            // Each category has a little different marshaling code.
            // A) Service Exception that matches the JAX-WS 
            //    specification (chapter 2.5 of the spec)
            // B) Service Exception that matches the JAX-WS "legacy" 
            //    exception (chapter 3.7 of the spec)
            // C) SOAPFaultException
            // D) WebServiceException
            // E) Other runtime exceptions (i.e. NullPointerException)

            // Get the FaultDescriptor matching this Exception.
            // If FaultDescriptor is found, this is a JAX-B Service Exception.
            // If not found, this is a System Exception
            FaultDescription fd =
                    operationDesc.resolveFaultByExceptionName(t.getClass().getCanonicalName());

            if (fd != null) {
                if (log.isErrorEnabled()) {
                    log.debug("Marshal as a Service Exception");
                }
                // Create the JAXB Context
                JAXBBlockContext context = new JAXBBlockContext(marshalDesc.getPackages());

                // The exception is a Service Exception.  
                // It may be (A) JAX-WS compliant exception or 
                // (B) JAX-WS legacy exception

                // The faultBeanObject is a JAXB object that represents the data of the exception.
                // It is marshalled in the detail section of the soap fault.  
                // The faultBeanObject is obtained direction from the exception (A) or via 
                // the legacy exception rules (B).
                Object faultBeanObject = null;

                FaultBeanDesc faultBeanDesc = marshalDesc.getFaultBeanDesc(fd);
                String faultInfo = fd.getFaultInfo();
                if (faultInfo == null || faultInfo.length() == 0) {
                    // Legacy Exception case
                    faultBeanObject = LegacyExceptionUtil.createFaultBean(t, fd, marshalDesc);
                } else {
                    // Normal case
                    // Get the fault bean object.  
                    Method getFaultInfo = t.getClass().getMethod("getFaultInfo", null);
                    faultBeanObject = getFaultInfo.invoke(t, null);
                }

                if (log.isErrorEnabled()) {
                    log.debug("The faultBean type is" + faultBeanObject.getClass().getName());
                }

                // Use "by java type" marshalling if necessary
                if (faultBeanObject == t ||
                        (context.getConstructionType() != JAXBUtils.CONSTRUCTION_TYPE
                                .BY_CONTEXT_PATH &&
                                isNotJAXBRootElement(faultBeanObject.getClass(), marshalDesc))) {
                    context.setProcessType(faultBeanObject.getClass());
                }

                QName faultBeanQName = new QName(faultBeanDesc.getFaultBeanNamespace(),
                                                 faultBeanDesc.getFaultBeanLocalName());
                // Make sure the faultBeanObject can be marshalled as an element
                if (!marshalDesc.getAnnotationDesc(faultBeanObject.getClass()).
                        hasXmlRootElement())
                {
                    faultBeanObject = new JAXBElement(faultBeanQName, faultBeanObject.getClass(),
                                                      faultBeanObject);
                }

                // Create a detailblock representing the faultBeanObject
                Block[] detailBlocks = new Block[1];
                detailBlocks[0] = factory.createFrom(faultBeanObject, context, faultBeanQName);

                if (log.isErrorEnabled()) {
                    log.debug("Create the xmlFault for the Service Exception");
                }
                // Get the fault text using algorithm defined in JAX-WS 10.2.2.3
                String text = t.getMessage();
                if (text == null || text.length() == 0) {
                    text = t.toString();
                }
                // Now make a XMLFault containing the detailblock
                xmlfault = new XMLFault(null, new XMLFaultReason(text), detailBlocks);
            } else {
                xmlfault = createXMLFaultFromSystemException(t);
            }
        } catch (Throwable e) {
            // If an exception occurs while demarshalling an exception, 
            // then rinse and repeat with a system exception
            if (log.isDebugEnabled()) {
                log.debug("An exception (" + e + ") occurred while marshalling exception (" + t +
                        ")");
            }
            WebServiceException wse = ExceptionFactory.makeWebServiceException(e);
            xmlfault = createXMLFaultFromSystemException(wse);
        }

        // Add the fault to the message
        message.setXMLFault(xmlfault);
    }

    /**
     * This method is used by WebService Impl and Provider to create an XMLFault (for marshalling)
     * from an exception that is a non-service exception
     *
     * @param t Throwable that represents a Service Exception
     * @return XMLFault
     */
    public static XMLFault createXMLFaultFromSystemException(Throwable t) {

        try {
            XMLFault xmlfault = null;
            if (t instanceof SOAPFaultException) {
                if (log.isErrorEnabled()) {
                    log.debug("Marshal SOAPFaultException");
                }
                // Category C: SOAPFaultException 
                // Construct the xmlFault from the SOAPFaultException's Fault
                SOAPFaultException sfe = (SOAPFaultException)t;
                SOAPFault soapFault = sfe.getFault();
                if (soapFault == null) {
                    // No fault ?  I will treat this like category E
                    xmlfault = 
                        new XMLFault(null,       // Use the default XMLFaultCode
                                     new XMLFaultReason(
                                     t.toString()));  // Assumes text lang of current Locale
                } else {
                    xmlfault = XMLFaultUtils.createXMLFault(soapFault);
                }

            } else if (t instanceof WebServiceException) {
                if (log.isErrorEnabled()) {
                    log.debug("Marshal as a WebServiceException");
                }
                // Category D: WebServiceException
                // The reason is constructed with the getMessage of the exception.  
                // There is no detail
                WebServiceException wse = (WebServiceException)t;

                // Get the fault text using algorithm defined in JAX-WS 10.2.2.3
                String text = wse.getMessage();
                if (text == null || text.length() == 0) {
                    text = wse.toString();
                }
                xmlfault = new XMLFault(null,       // Use the default XMLFaultCode
                                        new XMLFaultReason(
                                             text));  // Assumes text lang of current Locale
            } else {
                if (log.isErrorEnabled()) {
                    log.debug("Marshal as a unchecked System Exception");
                }
                // Category E: Other System Exception
                // The reason is constructed with the toString of the exception.  
                // This places the class name of the exception in the reason
                // There is no detail.
                // Get the fault text using algorithm defined in JAX-WS 10.2.2.3
                String text = t.getMessage();
                if (text == null || text.length() == 0) {
                    text = t.toString();
                }
                xmlfault = new XMLFault(null,       // Use the default XMLFaultCode
                                        new XMLFaultReason(
                                                text));  // Assumes text lang of current Locale
            }
            return xmlfault;
        } catch (Throwable e) {
            try {
                // If an exception occurs while demarshalling an exception, 
                // then rinse and repeat with a webservice exception
                if (log.isDebugEnabled()) {
                    log.debug("An exception (" + e + ") occurred while marshalling exception (" +
                            t + ")");
                }
                // Get the fault text using algorithm defined in JAX-WS 10.2.2.3
                String text = e.getMessage();
                if (text == null || text.length() == 0) {
                    text = e.toString();
                }
                WebServiceException wse = ExceptionFactory.makeWebServiceException(e);

                return new XMLFault(null,       // Use the default XMLFaultCode
                                    new XMLFaultReason(
                                            text));  // Assumes text lang of current Locale
            } catch (Exception e2) {
                // Exception while creating Exception for Exception
                throw ExceptionFactory.makeWebServiceException(e2);
            }
        }
    }

    /**
     * Unmarshal the service/system exception from a Message. This is used by all of the
     * marshallers
     *
     * @param operationDesc
     * @param marshalDesc
     * @param message
     * @return Throwable
     * @throws WebServiceException
     * @throws ClassNotFoundException
     * @throws IllegalAccessException
     * @throws InstantiationException
     * @throws XMLStreamException
     * @throws InvocationTargetException
     * @throws NoSuchMethodException
     */
    static Throwable demarshalFaultResponse(OperationDescription operationDesc,
                                            MarshalServiceRuntimeDescription marshalDesc,
                                            Message message)
            throws WebServiceException, ClassNotFoundException, IllegalAccessException,
            InstantiationException, XMLStreamException, InvocationTargetException,
            NoSuchMethodException {

        Throwable exception = null;
        // Get the fault from the message and get the detail blocks (probably one)
        XMLFault xmlfault = message.getXMLFault();
        Block[] detailBlocks = xmlfault.getDetailBlocks();

        // If there is only one block, get the element name of that block.
        QName elementQName = null;
        if (detailBlocks != null && detailBlocks.length == 1) {
            elementQName = detailBlocks[0].getQName();
        }

        // Use the element name to find the matching FaultDescriptor
        FaultDescription faultDesc = null;
        if (elementQName != null) {
            for (int i = 0; i < operationDesc.getFaultDescriptions().length && faultDesc == null;
                 i++) {
                FaultDescription fd = operationDesc.getFaultDescriptions()[i];
                FaultBeanDesc faultBeanDesc = marshalDesc.getFaultBeanDesc(fd);
                QName tryQName = new QName(faultBeanDesc.getFaultBeanNamespace(),
                                           faultBeanDesc.getFaultBeanLocalName());
                if (log.isErrorEnabled()) {
                    log.debug("  FaultDescription qname is (" + tryQName +
                            ") and detail element qname is (" + elementQName + ")");
                }
                if (elementQName.equals(tryQName)) {
                    faultDesc = fd;
                }
            }
        }

        if (faultDesc == null && elementQName != null) {
            // If not found, retry the search using just the local name
            for (int i = 0; i < operationDesc.getFaultDescriptions().length && faultDesc == null;
                 i++) {
                FaultDescription fd = operationDesc.getFaultDescriptions()[i];
                FaultBeanDesc faultBeanDesc = marshalDesc.getFaultBeanDesc(fd);
                String tryName = faultBeanDesc.getFaultBeanLocalName();
                if (elementQName.getLocalPart().equals(tryName)) {
                    faultDesc = fd;
                }
            }
        }


        if (faultDesc == null) {
            // This is a system exception if the method does not throw a checked exception or if
            // the detail block is missing or contains multiple items.
            exception = createSystemException(xmlfault, message);
        } else {
            if (log.isErrorEnabled()) {
                log.debug("Ready to demarshal service exception.  The detail entry name is " +
                        elementQName);
            }
            FaultBeanDesc faultBeanDesc = marshalDesc.getFaultBeanDesc(faultDesc);
            boolean isLegacy =
                    (faultDesc.getFaultInfo() == null || faultDesc.getFaultInfo().length() == 0);

            // Get the JAXB object from the block
            JAXBBlockContext blockContext = new JAXBBlockContext(marshalDesc.getPackages());

            // Note that faultBean may not be a bean, it could be a primitive 
            Class faultBeanFormalClass = loadClass(faultBeanDesc.getFaultBeanClassName());

⌨️ 快捷键说明

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