001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements. See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership. The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License. You may obtain a copy of the License at
009     *
010     * http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied. See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    package org.apache.geronimo.axis2.ejb;
021    
022    import javax.interceptor.AroundInvoke;
023    import javax.xml.ws.Binding;
024    
025    import org.apache.axis2.AxisFault;
026    import org.apache.axis2.context.OperationContext;
027    import org.apache.axis2.description.AxisOperation;
028    import org.apache.axis2.description.WSDL2Constants;
029    import org.apache.axis2.engine.AxisEngine;
030    import org.apache.axis2.jaxws.core.InvocationContext;
031    import org.apache.axis2.jaxws.core.InvocationContextFactory;
032    import org.apache.axis2.jaxws.core.MessageContext;
033    import org.apache.axis2.jaxws.message.util.MessageUtils;
034    import org.apache.axis2.jaxws.server.JAXWSMessageReceiver;
035    import org.apache.axis2.wsdl.WSDLConstants.WSDL20_2004_Constants;
036    import org.apache.axis2.wsdl.WSDLConstants.WSDL20_2006Constants;
037    import org.apache.commons.logging.Log;
038    import org.apache.commons.logging.LogFactory;
039    
040    public class EJBInterceptor {
041    
042        private static final Log LOG = LogFactory.getLog(EJBInterceptor.class);
043    
044        private MessageContext requestMsgCtx;
045        private EJBWebServiceContainer container;
046    
047        public EJBInterceptor(EJBWebServiceContainer container, MessageContext requestCtx) {
048            this.container = container;
049            this.requestMsgCtx = requestCtx;
050        }
051    
052        @AroundInvoke
053        public Object intercept(javax.interceptor.InvocationContext invContext) throws Exception {
054                    
055            this.container.injectHandlers();
056            
057            AxisOperation operation = this.requestMsgCtx.getAxisMessageContext().getAxisOperation();
058            String mep = operation.getMessageExchangePattern();
059            
060            EJBEndpointController controller = new EJBEndpointController(invContext);
061                   
062            Binding binding = (Binding)this.requestMsgCtx.getAxisMessageContext().getProperty(JAXWSMessageReceiver.PARAM_BINDING);
063            InvocationContext ic = InvocationContextFactory.createInvocationContext(binding);
064            ic.setRequestMessageContext(this.requestMsgCtx);
065            
066            controller.invoke(ic);
067            
068            MessageContext responseMsgCtx = ic.getResponseMessageContext();
069    
070            //If there is a fault it could be Robust In-Only
071            if (!isMepInOnly(mep) || hasFault(responseMsgCtx)) {
072                // If this is a two-way exchange, there should already be a
073                // JAX-WS MessageContext for the response.  We need to pull 
074                // the Message data out of there and set it on the Axis2 
075                // MessageContext.
076                org.apache.axis2.context.MessageContext axisResponseMsgCtx =
077                        responseMsgCtx.getAxisMessageContext();
078    
079                MessageUtils.putMessageOnMessageContext(responseMsgCtx.getMessage(),
080                                                        axisResponseMsgCtx);
081    
082                OperationContext opCtx = axisResponseMsgCtx.getOperationContext();
083                opCtx.addMessageContext(axisResponseMsgCtx);
084    
085                // If this is a fault message, we want to throw it as an
086                // exception so that the transport can do the appropriate things
087                if (responseMsgCtx.getMessage().isFault()) {
088                    throw new AxisFault("An error was detected during JAXWS processing",
089                                        axisResponseMsgCtx);
090                } else {
091                    //Create the AxisEngine for the reponse and send it.
092                    AxisEngine engine =
093                            new AxisEngine(axisResponseMsgCtx.getConfigurationContext());
094                    engine.send(axisResponseMsgCtx);
095                }
096            }    
097            
098            // TODO: convert response into object?
099            return null;
100        }
101        
102        private boolean hasFault(MessageContext responseMsgCtx) {
103            if (responseMsgCtx == null || responseMsgCtx.getMessage() == null) {
104                return false;
105            }
106            return responseMsgCtx.getMessage().isFault();
107        }
108    
109        private boolean isMepInOnly(String mep) {
110            boolean inOnly = mep.equals(WSDL20_2004_Constants.MEP_URI_ROBUST_IN_ONLY) ||
111                    mep.equals(WSDL20_2004_Constants.MEP_URI_IN_ONLY) ||
112                    mep.equals(WSDL2Constants.MEP_URI_IN_ONLY) ||
113                    mep.equals(WSDL2Constants.MEP_URI_ROBUST_IN_ONLY) ||
114                    mep.equals(WSDL20_2006Constants.MEP_URI_ROBUST_IN_ONLY) ||
115                    mep.equals(WSDL20_2006Constants.MEP_URI_IN_ONLY);
116            return inOnly;
117        }
118            
119    }