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: */package org.apache.cxf.jaxws.handler.soap;
019:
020: import java.net.URI;
021: import java.util.HashSet;
022: import java.util.Set;
023:
024: import javax.xml.namespace.QName;
025: import javax.xml.soap.SOAPMessage;
026: import javax.xml.ws.Binding;
027: import javax.xml.ws.handler.Handler;
028: import javax.xml.ws.handler.MessageContext;
029: import javax.xml.ws.handler.soap.SOAPHandler;
030: import javax.xml.ws.handler.soap.SOAPMessageContext;
031:
032: import org.apache.cxf.binding.soap.SoapMessage;
033: import org.apache.cxf.binding.soap.interceptor.SoapInterceptor;
034: import org.apache.cxf.endpoint.Endpoint;
035: import org.apache.cxf.helpers.CastUtils;
036: import org.apache.cxf.jaxws.handler.AbstractProtocolHandlerInterceptor;
037: import org.apache.cxf.jaxws.handler.HandlerChainInvoker;
038: import org.apache.cxf.jaxws.support.ContextPropertiesMapping;
039: import org.apache.cxf.message.Message;
040: import org.apache.cxf.phase.Phase;
041: import org.apache.cxf.phase.PhaseInterceptorChain;
042: import org.apache.cxf.transport.MessageObserver;
043:
044: public class DispatchSOAPHandlerInterceptor extends
045: AbstractProtocolHandlerInterceptor<SoapMessage> implements
046: SoapInterceptor {
047:
048: public DispatchSOAPHandlerInterceptor(Binding binding) {
049: super (binding, Phase.USER_PROTOCOL);
050: }
051:
052: public Set<URI> getRoles() {
053: return new HashSet<URI>();
054: }
055:
056: public Set<QName> getUnderstoodHeaders() {
057: Set<QName> understood = new HashSet<QName>();
058: for (Handler h : getBinding().getHandlerChain()) {
059: if (h instanceof SOAPHandler) {
060: Set<QName> headers = CastUtils.cast(((SOAPHandler) h)
061: .getHeaders());
062: if (headers != null) {
063: understood.addAll(headers);
064: }
065: }
066: }
067: return understood;
068: }
069:
070: public void handleMessage(SoapMessage message) {
071: if (getInvoker(message).getProtocolHandlers().isEmpty()) {
072: return;
073: }
074:
075: MessageContext context = createProtocolMessageContext(message);
076: HandlerChainInvoker invoker = getInvoker(message);
077: invoker.setProtocolMessageContext(context);
078:
079: if (!invoker.invokeProtocolHandlers(isRequestor(message),
080: context)) {
081: handleAbort(message, context);
082: }
083:
084: // If this is the outbound and end of MEP, call MEP completion
085: if (isRequestor(message)
086: && invoker.getLogicalHandlers().isEmpty()
087: && !isOutbound(message) && isMEPComlete(message)) {
088: onCompletion(message);
089: } else if (isOutbound(message) && isMEPComlete(message)) {
090: onCompletion(message);
091: }
092: }
093:
094: private void handleAbort(SoapMessage message, MessageContext context) {
095: if (isRequestor(message)) {
096: // client side outbound
097: if (getInvoker(message).isOutbound()) {
098: message.getInterceptorChain().abort();
099: Endpoint e = message.getExchange().get(Endpoint.class);
100: Message responseMsg = e.getBinding().createMessage();
101:
102: MessageObserver observer = (MessageObserver) message
103: .getExchange().get(MessageObserver.class);
104: if (observer != null) {
105: // the request message becomes the response message
106: message.getExchange().setInMessage(responseMsg);
107: SOAPMessage soapMessage = ((SOAPMessageContext) context)
108: .getMessage();
109:
110: if (soapMessage != null) {
111: responseMsg.setContent(SOAPMessage.class,
112: soapMessage);
113: }
114: responseMsg
115: .put(
116: PhaseInterceptorChain.STARTING_AT_INTERCEPTOR_ID,
117: SOAPHandlerInterceptor.class
118: .getName());
119: observer.onMessage(responseMsg);
120: }
121:
122: //We dont call onCompletion here, as onCompletion will be called by inbound
123: //LogicalHandlerInterceptor
124: } else {
125: // client side inbound - Normal handler message processing
126: // stops, but the inbound interceptor chain still continues, dispatch the message
127: //By onCompletion here, we can skip following Logical handlers
128: onCompletion(message);
129: }
130: }
131: }
132:
133: @Override
134: protected MessageContext createProtocolMessageContext(
135: SoapMessage message) {
136: SOAPMessageContextImpl sm = new SOAPMessageContextImpl(message);
137: boolean requestor = isRequestor(message);
138: ContextPropertiesMapping.mapCxf2Jaxws(message.getExchange(),
139: sm, requestor);
140: return sm;
141: }
142:
143: public void handleFault(SoapMessage message) {
144: }
145: }
|