addressingouthandler.java

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

JAVA
546
字号
/*
 * 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.handlers.addressing;

import org.apache.axiom.om.OMAttribute;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.util.AttributeHelper;
import org.apache.axiom.om.util.ElementHelper;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axiom.soap.SOAPFactory;
import org.apache.axiom.soap.SOAPFault;
import org.apache.axiom.soap.SOAPHeader;
import org.apache.axiom.soap.SOAPHeaderBlock;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.AddressingConstants;
import org.apache.axis2.addressing.AddressingFaultsHelper;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.addressing.EndpointReferenceHelper;
import org.apache.axis2.addressing.RelatesTo;
import org.apache.axis2.addressing.i18n.AddressingMessages;
import org.apache.axis2.client.Options;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.description.HandlerDescription;
import org.apache.axis2.description.Parameter;
import org.apache.axis2.handlers.AbstractHandler;
import org.apache.axis2.util.JavaUtils;
import org.apache.axis2.util.Utils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.xml.namespace.QName;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class AddressingOutHandler extends AbstractHandler implements AddressingConstants {

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

    /** This variable should only be updated inside the {@link #init(HandlerDescription)} method. */
    private boolean includeOptionalHeaders = false;

    /** Initialize the addressing out handler. */
    public void init(HandlerDescription arg0) {
        super.init(arg0);

        //Determine whether to include optional addressing headers in the output message.
        //The default is not to include any headers that can be safely omitted.
        Parameter param = arg0.getParameter(INCLUDE_OPTIONAL_HEADERS);
        String value = Utils.getParameterValue(param);
        includeOptionalHeaders = JavaUtils.isTrueExplicitly(value);
    }

    public InvocationResponse invoke(MessageContext msgContext) throws AxisFault {
        // it should be able to disable addressing by some one.
        if (msgContext.isPropertyTrue(DISABLE_ADDRESSING_FOR_OUT_MESSAGES)) {
            if (log.isTraceEnabled()) {
                log.trace(msgContext.getLogIDString() +
                        " Addressing is disabled. Not adding WS-Addressing headers.");
            }
            return InvocationResponse.CONTINUE;
        }

        // Determine the addressin namespace in effect.
        Object addressingVersionFromCurrentMsgCtxt = msgContext.getProperty(WS_ADDRESSING_VERSION);
        if (log.isTraceEnabled()) {
            log.trace("Addressing version string from messageContext=" +
                    addressingVersionFromCurrentMsgCtxt);
        }
        boolean isSubmissionNamespace =
                Submission.WSA_NAMESPACE.equals(addressingVersionFromCurrentMsgCtxt);

        // Determine whether to include optional addressing headers in the output.
        boolean includeOptionalHeaders = this.includeOptionalHeaders ||
                                            msgContext.isPropertyTrue(INCLUDE_OPTIONAL_HEADERS);

        // Determine if a MustUnderstand attribute will be added to all headers in the
        // addressing namespace.
        boolean addMustUnderstandAttribute =
                msgContext.isPropertyTrue(ADD_MUST_UNDERSTAND_TO_ADDRESSING_HEADERS);

        // what if there are addressing headers already in the message. Do you replace that or not?
        // Lets have a parameter to control that. The default behavior is you won't replace addressing
        // headers if there are any (this was the case so far).
        boolean replaceHeaders = msgContext.isPropertyTrue(REPLACE_ADDRESSING_HEADERS);

        WSAHeaderWriter writer = new WSAHeaderWriter(msgContext, isSubmissionNamespace,
                                                     addMustUnderstandAttribute, replaceHeaders,
                                                     includeOptionalHeaders);
        writer.writeHeaders();

        return InvocationResponse.CONTINUE;
    }

    private class WSAHeaderWriter {

        private MessageContext messageContext;
        private SOAPEnvelope envelope;
        private SOAPHeader header;
        private SOAPFactory factory;
        private Options messageContextOptions;
        private OMNamespace addressingNamespaceObject;
        private String addressingNamespace;

        private boolean isFinalAddressingNamespace;
        private boolean addMustUnderstandAttribute;
        private boolean replaceHeaders;  // determines whether we replace the existing headers or not, if they present
        private boolean includeOptionalHeaders;

        public WSAHeaderWriter(MessageContext mc, boolean isSubmissionNamespace, boolean addMU,
                               boolean replace, boolean includeOptional) {
            if (log.isDebugEnabled()) {
                log.debug("WSAHeaderWriter: isFinal=" + isSubmissionNamespace + " addMU=" + addMU +
                        " replace=" + replace + " includeOptional=" + includeOptional);
            }

            messageContext = mc;
            envelope = mc.getEnvelope();
            factory = (SOAPFactory)envelope.getOMFactory();

            header = envelope.getHeader();

            // if there is no soap header in the envelope being processed, add one.
            if (header == null) {
                header = factory.createSOAPHeader(envelope);
            }

            messageContextOptions = messageContext.getOptions();

            addressingNamespace =
                    (isSubmissionNamespace ? Submission.WSA_NAMESPACE : Final.WSA_NAMESPACE);
            addressingNamespaceObject =
                    factory.createOMNamespace(addressingNamespace, WSA_DEFAULT_PREFIX);

            isFinalAddressingNamespace = !isSubmissionNamespace;
            addMustUnderstandAttribute = addMU;
            replaceHeaders = replace;
            includeOptionalHeaders = includeOptional;
        }

        public void writeHeaders() throws AxisFault {

            // by this time, we definitely have some addressing information to be sent. This is because,
            // we have tested at the start of this whether messageInformationHeaders are null or not.
            // So rather than declaring addressing namespace in each and every addressing header, lets
            // define that in the Header itself.
            envelope.declareNamespace(addressingNamespaceObject);

            // processing WSA To
            processToEPR();

            // processing WSA replyTo
            processReplyTo();

            // processing WSA From
            processFromEPR();

            // processing WSA FaultTo
            processFaultToEPR();

            // processing WSA MessageID
            processMessageID();

            // processing WSA Action
            processWSAAction();

            // processing WSA RelatesTo
            processRelatesTo();

            // process fault headers, if present
            processFaultsInfoIfPresent();

            // process mustUnderstand attribute, if required.
            processMustUnderstandProperty();
        }


        private void processMessageID() {
            String messageID = messageContextOptions.getMessageId();
            if (messageID != null && !isAddressingHeaderAlreadyAvailable(WSA_MESSAGE_ID, false))
            {//optional
                OMElement oe = processStringInfo(messageID, WSA_MESSAGE_ID);
                ArrayList attributes = (ArrayList)messageContext.getProperty(
                        AddressingConstants.MESSAGEID_ATTRIBUTES);
                if (attributes != null && !attributes.isEmpty()) {
                    Iterator attrIterator = attributes.iterator();
                    while (attrIterator.hasNext()) {
                        AttributeHelper.importOMAttribute((OMAttribute)attrIterator.next(), oe);
                    }
                }
            }
        }

        private void processWSAAction() throws AxisFault {
            String action = messageContextOptions.getAction();

            if (log.isTraceEnabled()) {
                log.trace(messageContext.getLogIDString() +
                        " processWSAAction: action from messageContext: " + action);
            }
            if (action == null || "".equals(action)) {
                if (messageContext.getAxisOperation() != null) {
                    action = messageContext.getAxisOperation().getOutputAction();
                    if (log.isTraceEnabled()) {
                        log.trace(messageContext.getLogIDString() +
                                " processWSAAction: action from AxisOperation: " + action);
                    }
                }
            }

            // Use the correct fault action for the selected namespace
            if (Final.WSA_FAULT_ACTION.equals(action) || Submission.WSA_FAULT_ACTION.equals(action))
            {
                action = isFinalAddressingNamespace ? Final.WSA_FAULT_ACTION :
                        Submission.WSA_FAULT_ACTION;
                messageContextOptions.setAction(action);
            } else if (!isFinalAddressingNamespace && Final.WSA_SOAP_FAULT_ACTION.equals(action)) {
                action = Submission.WSA_FAULT_ACTION;
                messageContextOptions.setAction(action);
            }

            // If we need to add a wsa:Action header
            if (!isAddressingHeaderAlreadyAvailable(WSA_ACTION, false)) {
                if (log.isTraceEnabled()) {
                    log.trace(messageContext.getLogIDString() +
                            " processWSAAction: No existing wsa:Action header found");
                }
                // If we don't have an action to add,
                if (action == null || "".equals(action)) {
                    if (log.isTraceEnabled()) {
                        log.trace(messageContext.getLogIDString() +
                                " processWSAAction: No action to add to header");
                    }
                    // Fault unless validation has been explictily turned off
                    if (!messageContext.isPropertyTrue(
                            AddressingConstants.DISABLE_OUTBOUND_ADDRESSING_VALIDATION))
                    {
                        throw new AxisFault(AddressingMessages.getMessage("outboundNoAction"));
                    }
                } else {
                    if (log.isTraceEnabled()) {
                        log.trace(messageContext.getLogIDString() +
                                " processWSAAction: Adding action to header: " + action);
                    }
                    // Otherwise just add the header
                    OMElement oe = processStringInfo(action, WSA_ACTION);
                    ArrayList attributes = (ArrayList)messageContext.getProperty(
                            AddressingConstants.ACTION_ATTRIBUTES);
                    if (attributes != null && !attributes.isEmpty()) {
                        Iterator attrIterator = attributes.iterator();
                        while (attrIterator.hasNext()) {
                            AttributeHelper
                                    .importOMAttribute((OMAttribute)attrIterator.next(), oe);
                        }

⌨️ 快捷键说明

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