endpointcontroller.java

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

JAVA
443
字号
/*
 * 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.server;

import org.apache.axiom.om.util.StAXUtils;
import org.apache.axis2.description.AxisOperation;
import org.apache.axis2.description.AxisService;
import org.apache.axis2.description.Parameter;
import org.apache.axis2.description.WSDL2Constants;
import org.apache.axis2.java.security.AccessController;
import org.apache.axis2.jaxws.ExceptionFactory;
import org.apache.axis2.jaxws.binding.SOAPBinding;
import org.apache.axis2.jaxws.core.InvocationContext;
import org.apache.axis2.jaxws.core.MessageContext;
import org.apache.axis2.jaxws.core.util.MessageContextUtils;
import org.apache.axis2.jaxws.description.DescriptionFactory;
import org.apache.axis2.jaxws.description.EndpointDescription;
import org.apache.axis2.jaxws.description.ServiceDescription;
import org.apache.axis2.jaxws.handler.HandlerChainProcessor;
import org.apache.axis2.jaxws.handler.HandlerInvokerUtils;
import org.apache.axis2.jaxws.handler.HandlerResolverImpl;
import org.apache.axis2.jaxws.handler.MEPContext;
import org.apache.axis2.jaxws.i18n.Messages;
import org.apache.axis2.jaxws.message.Message;
import org.apache.axis2.jaxws.message.Protocol;
import org.apache.axis2.jaxws.message.XMLFault;
import org.apache.axis2.jaxws.message.XMLFaultCode;
import org.apache.axis2.jaxws.message.XMLFaultReason;
import org.apache.axis2.jaxws.message.factory.MessageFactory;
import org.apache.axis2.jaxws.registry.FactoryRegistry;
import org.apache.axis2.jaxws.server.dispatcher.EndpointDispatcher;
import org.apache.axis2.jaxws.server.dispatcher.factory.EndpointDispatcherFactory;
import org.apache.axis2.jaxws.server.endpoint.lifecycle.EndpointLifecycleManager;
import org.apache.axis2.jaxws.server.endpoint.lifecycle.factory.EndpointLifecycleManagerFactory;
import org.apache.axis2.jaxws.spi.Constants;
import org.apache.axis2.wsdl.WSDLConstants.WSDL20_2004_Constants;
import org.apache.axis2.wsdl.WSDLConstants.WSDL20_2006Constants;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.ws.http.HTTPBinding;
import java.io.StringReader;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.Collection;

/**
 * The EndpointController is the server side equivalent to the InvocationController on the client
 * side.  It is an abstraction of the server side endpoint invocation that encapsulates all of the
 * Axis2 semantics.
 * <p/>
 * Like the InvocationController, this class is responsible for invoking the JAX-WS application
 * handler chain along with taking all of the provided information and setting up what's needed to
 * perform the actual invocation of the endpoint.
 */
public class EndpointController {

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

    private static final String PARAM_SERVICE_CLASS = "ServiceClass";

    public EndpointController() {
        //do nothing
    }

    /**
     * This method is used to start the JAX-WS invocation of a target endpoint. It takes an
     * InvocationContext, which must have a MessageContext specied for the request.  Once the
     * invocation is complete, the information will be stored
     */
    public InvocationContext invoke(InvocationContext ic) {
        MessageContext requestMsgCtx = ic.getRequestMessageContext();

        String implClassName = getServiceImplClassName(requestMsgCtx);

        Class implClass = loadServiceImplClass(implClassName,
                                               requestMsgCtx.getClassLoader());

        EndpointDescription endpointDesc = getEndpointDescription(requestMsgCtx, implClass);
        requestMsgCtx.setEndpointDescription(endpointDesc);

        /*
         * TODO: review: make sure the handlers are set on the InvocationContext
         * This implementation of the JAXWS runtime does not use Endpoint, which
         * would normally be the place to initialize and store the handler list.
         * In lieu of that, we will have to intialize and store them on the 
         * InvocationContext.  also see the InvocationContextFactory.  On the client
         * side, the binding is not yet set when we call into that factory, so the
         * handler list doesn't get set on the InvocationContext object there.  Thus
         * we gotta do it here.
         * 
         * Since we're on the server, and there apparently is no Binding object
         * anywhere to be found...
         */
        if (ic.getHandlers() == null) {
            ic.setHandlers(new HandlerResolverImpl(endpointDesc.getServiceDescription()).getHandlerChain(endpointDesc.getPortInfo()));
        }

        if (!bindingTypesMatch(requestMsgCtx, endpointDesc.getServiceDescription())) {
            Protocol protocol = requestMsgCtx.getMessage().getProtocol();
            // only if protocol is soap12 and MISmatches the endpoint do we halt processing
            if (protocol.equals(Protocol.soap12)) {
                ic.setResponseMessageContext(createMismatchFaultMsgCtx(requestMsgCtx,
                                                                       "Incoming SOAP message protocol is version 1.2, but endpoint is configured for SOAP 1.1"));
                return ic;
            } else if (protocol.equals(Protocol.soap11)) {
                // SOAP 1.1 message and SOAP 1.2 binding

                // The canSupport flag indicates that we can support this scenario.
                // Possible Examples of canSupport:  JAXB impl binding, JAXB Provider
                // Possible Example of !canSupport: Application handler usage, non-JAXB Provider
                // Initially I vote to hard code this as false.
                boolean canSupport = false;
                if (canSupport) {
                    // TODO: Okay, but we need to scrub the Message create code to make sure that the response message
                    // is always built from the receiver protocol...not the binding protocol
                } else {
                    ic.setResponseMessageContext(createMismatchFaultMsgCtx(requestMsgCtx,
                                                                           "Incoming SOAP message protocol is version 1.1, but endpoint is configured for SOAP 1.2.  This is not supported."));
                    return ic;
                }
            } else {
                ic.setResponseMessageContext(createMismatchFaultMsgCtx(requestMsgCtx,
                                                                       "Incoming message protocol does not match endpoint protocol."));
                return ic;
            }
        }

        MessageContext responseMsgContext = null;

        try {
            // Get the service instance.  This will run the @PostConstruct code.
            EndpointLifecycleManager elm = createEndpointlifecycleManager();
            Object serviceInstance = elm.createServiceInstance(requestMsgCtx, implClass);

            // The application handlers and dispatcher invoke will 
            // modify/destroy parts of the message.  Make sure to save
            // the request message if appropriate.
            saveRequestMessage(requestMsgCtx);

            // Invoke inbound application handlers.  It's safe to use the first object on the iterator because there is
            // always exactly one EndpointDescription on a server invoke
            boolean success =
                    HandlerInvokerUtils.invokeInboundHandlers(requestMsgCtx.getMEPContext(),
                                                              ic.getHandlers(),
                                                              HandlerChainProcessor.MEP.REQUEST,
                                                              isOneWay(requestMsgCtx.getAxisMessageContext()));

            if (success) {

                // Dispatch to the
                EndpointDispatcher dispatcher = getEndpointDispatcher(implClass, serviceInstance);
                try {
                    responseMsgContext = dispatcher.invoke(requestMsgCtx);
                } finally {
                    // Passed pivot point
                    requestMsgCtx.getMessage().setPostPivot();
                }

                // Invoke the outbound response handlers.
                // If the message is one way, we should not invoke the response handlers.  There is no response
                // MessageContext since a one way invocation is considered to have a "void" return.
                if (!isOneWay(requestMsgCtx.getAxisMessageContext())) {
                    responseMsgContext.setMEPContext(requestMsgCtx.getMEPContext());
                    
                    HandlerInvokerUtils.invokeOutboundHandlers(responseMsgContext.getMEPContext(),
                                                               ic.getHandlers(),
                                                               HandlerChainProcessor.MEP.RESPONSE,
                                                               false);
                }
            } else
            { // the inbound handler chain must have had a problem, and we've reversed directions
                responseMsgContext =
                        MessageContextUtils.createResponseMessageContext(requestMsgCtx);
                // since we've reversed directions, the message has "become a response message" (section 9.3.2.1, footnote superscript 2)
                responseMsgContext.setMessage(requestMsgCtx.getMessage());
            }

        } catch (Exception e) {
            // TODO for now, throw it.  We probably should try to make an XMLFault object and set it on the message
            throw ExceptionFactory.makeWebServiceException(e);
        } finally {
            restoreRequestMessage(requestMsgCtx);
        }

		// The response MessageContext should be set on the InvocationContext
		ic.setResponseMessageContext(responseMsgContext);

        return ic;
    }

    /*
      * Get the appropriate EndpointDispatcher for a given service endpoint.
      */
    protected EndpointDispatcher getEndpointDispatcher(Class serviceImplClass, Object serviceInstance)
            throws Exception {
        EndpointDispatcherFactory factory = 
            (EndpointDispatcherFactory)FactoryRegistry.getFactory(EndpointDispatcherFactory.class);        
        return factory.createEndpointDispatcher(serviceImplClass, serviceInstance);       
    }

    /*
      * Tries to load the implementation class that was specified for the
      * target endpoint

⌨️ 快捷键说明

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