builderutil.java

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

JAVA
709
字号
                    && attachmentSizeThresholdProperty instanceof String) {
                attachmentSizeThreshold = (String) attachmentSizeThresholdProperty;
            } else {
                Parameter attachmentSizeThresholdParameter = msgContext
                        .getParameter(Constants.Configuration.FILE_SIZE_THRESHOLD);
                attachmentSizeThreshold = attachmentSizeThresholdParameter
                        .getValue().toString();
            }
        }

        // Get the content-length if it is available
        int contentLength = 0;
        Map headers = (Map) msgContext.getProperty(MessageContext.TRANSPORT_HEADERS);
        if (headers != null) {
            String contentLengthValue = (String) headers.get(HTTPConstants.HEADER_CONTENT_LENGTH);
            if (contentLengthValue != null) {
                try {
                    contentLength = new Integer(contentLengthValue).intValue();
                } catch (NumberFormatException e) {
                    if (log.isDebugEnabled()) {
                        log.debug("Content-Length is not a valid number.  Will assume it is not set:" + e);
                    }
                }
            }
        }
        Attachments attachments = null;
        if (contentLength > 0) {
            if (log.isDebugEnabled()) {
                log.debug("Creating an Attachments map.  The content-length is" + contentLength);
            }
            attachments = 
                new Attachments(inStream,
                                contentTypeString,
                                fileCacheForAttachments,
                                attachmentRepoDir,
                                attachmentSizeThreshold,
                                contentLength);
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Creating an Attachments map.");
            }
            attachments = 
                new Attachments(inStream,
                                contentTypeString,
                                fileCacheForAttachments,
                                attachmentRepoDir,
                                attachmentSizeThreshold);
        }
            
        return attachments;
    }

    /**
     * @param in
     * @return
     * @throws XMLStreamException
     * @deprecated If some one really need this method, please shout.
     */
    public static StAXBuilder getBuilder(Reader in) throws XMLStreamException {
        XMLStreamReader xmlreader = StAXUtils.createXMLStreamReader(in);
        StAXBuilder builder = new StAXSOAPModelBuilder(xmlreader, null);
        return builder;
    }

    /**
     * Creates an OMBuilder for a plain XML message. Default character set encording is used.
     *
     * @param inStream InputStream for a XML message
     * @return Handler to a OMBuilder implementation instance
     * @throws XMLStreamException
     */
    public static StAXBuilder getBuilder(InputStream inStream) throws XMLStreamException {
        XMLStreamReader xmlReader = StAXUtils.createXMLStreamReader(inStream);
        return new StAXOMBuilder(xmlReader);
    }

    /**
     * Creates an OMBuilder for a plain XML message.
     *
     * @param inStream   InputStream for a XML message
     * @param charSetEnc Character set encoding to be used
     * @return Handler to a OMBuilder implementation instance
     * @throws XMLStreamException
     */
    public static StAXBuilder getBuilder(InputStream inStream, String charSetEnc)
            throws XMLStreamException {
        XMLStreamReader xmlReader = StAXUtils.createXMLStreamReader(inStream, charSetEnc);
        try {
            StAXBuilder builder =  new StAXSOAPModelBuilder(xmlReader);
            return builder;
        } catch (OMException e){
            log.info("OMException in getSOAPBuilder", e);
            try {
                log.info("Remaining input stream :[" + new String(IOUtils.getStreamAsByteArray(inStream), charSetEnc)+ "]");
            } catch (IOException e1) {
            }
            throw e;
        }
    }

    /**
     * Creates an OMBuilder for a SOAP message. Default character set encording is used.
     *
     * @param inStream InputStream for a SOAP message
     * @return Handler to a OMBuilder implementation instance
     * @throws XMLStreamException
     */
    public static StAXBuilder getSOAPBuilder(InputStream inStream) throws XMLStreamException {
        XMLStreamReader xmlReader = StAXUtils.createXMLStreamReader(inStream);
        try {
            StAXBuilder builder =  new StAXSOAPModelBuilder(xmlReader);
            return builder;
        } catch (OMException e){
            log.info("OMException in getSOAPBuilder", e);
            try {
                log.info("Remaining input stream :[" + new String(IOUtils.getStreamAsByteArray(inStream))+ "]");
            } catch (IOException e1) {
            }
            throw e;
        }
    }

    /**
     * Creates an OMBuilder for a SOAP message.
     *
     * @param inStream   InputStream for a SOAP message
     * @param charSetEnc Character set encoding to be used
     * @return Handler to a OMBuilder implementation instance
     * @throws XMLStreamException
     */
    public static StAXBuilder getSOAPBuilder(InputStream inStream, String charSetEnc)
            throws XMLStreamException {
        XMLStreamReader xmlReader = StAXUtils.createXMLStreamReader(inStream, charSetEnc);
        try {
            StAXBuilder builder =  new StAXSOAPModelBuilder(xmlReader);
            return builder;
        } catch (OMException e){
            log.info("OMException in getSOAPBuilder", e);
            try {
                log.info("Remaining input stream :[" + new String(IOUtils.getStreamAsByteArray(inStream), charSetEnc)+ "]");
            } catch (IOException e1) {
            }
            throw e;
        }
    }

    public static StAXBuilder getBuilder(SOAPFactory soapFactory, InputStream in, String charSetEnc)
            throws XMLStreamException {
        StAXBuilder builder;
        XMLStreamReader xmlreader = StAXUtils.createXMLStreamReader(in, charSetEnc);
        builder = new StAXOMBuilder(soapFactory, xmlreader);
        return builder;
    }

    /**
     * Initial work for a builder selector which selects the builder for a given
     * message format based on the the content type of the recieved message.
     * content-type to builder mapping can be specified through the Axis2.xml.
     *
     * @param type
     * @param msgContext
     * @return the builder registered against the given content-type
     * @throws AxisFault
     */
    public static Builder getBuilderFromSelector(String type, MessageContext msgContext)
            throws AxisFault {

        Builder builder = msgContext.getConfigurationContext().getAxisConfiguration()
                .getMessageBuilder(type);
        if (builder != null) {
            // Setting the received content-type as the messageType to make
            // sure that we respond using the received message serialisation
            // format.
            msgContext.setProperty(Constants.Configuration.MESSAGE_TYPE, type);
        }
        return builder;
    }

    public static void validateSOAPVersion(String soapNamespaceURIFromTransport,
                                           SOAPEnvelope envelope) {
        if (soapNamespaceURIFromTransport != null) {
            OMNamespace envelopeNamespace = envelope.getNamespace();
            String namespaceName = envelopeNamespace.getNamespaceURI();
            if (!(soapNamespaceURIFromTransport.equals(namespaceName))) {
                throw new SOAPProcessingException(
                        "Transport level information does not match with SOAP" +
                                " Message namespace URI", envelopeNamespace.getPrefix() + ":" +
                        SOAPConstants.FAULT_CODE_VERSION_MISMATCH);
            }
        }
    }

    public static void validateCharSetEncoding(String charsetEncodingFromTransport,
                                               String charsetEncodingFromXML,
                                               String soapNamespaceURI) throws AxisFault {
        if ((charsetEncodingFromXML != null)
                && !"".equals(charsetEncodingFromXML)
                && (charsetEncodingFromTransport != null)
                && !charsetEncodingFromXML.equalsIgnoreCase(charsetEncodingFromTransport)
                && !isValidPair(charsetEncodingFromXML, charsetEncodingFromTransport))
        {
            String faultCode;

            if (SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI.equals(soapNamespaceURI)) {
                faultCode = SOAP12Constants.FAULT_CODE_SENDER;
            } else {
                faultCode = SOAP11Constants.FAULT_CODE_SENDER;
            }

            throw new AxisFault("Character Set Encoding from "
                    + "transport information [" + charsetEncodingFromTransport + "] does not match with "
                    + "character set encoding in the received SOAP message [" + charsetEncodingFromXML + "]", faultCode);
        }
    }

    /**
     * check if the pair is [UTF-16,UTF-16LE] [UTF-32, UTF-32LE],[UTF-16,UTF-16BE] [UTF-32, UTF-32BE] etc.
     * 
     * @param enc1
     * @param enc2
     * @return
     */
    private static boolean isValidPair(String enc1, String enc2) {
        enc1 = enc1.toLowerCase();
        enc2 = enc2.toLowerCase();
        if (enc1.endsWith("be") || enc1.endsWith("le")) {
            enc1 = enc1.substring(0, enc1.length() - 2);
        }
        if (enc2.endsWith("be") || enc2.endsWith("le")) {
            enc2 = enc2.substring(0, enc2.length() - 2);
        }
        return enc1.equals(enc2);
    }
}

⌨️ 快捷键说明

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