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.SOAPException;
026: import javax.xml.soap.SOAPMessage;
027: import javax.xml.stream.XMLStreamException;
028: import javax.xml.stream.XMLStreamReader;
029: import javax.xml.transform.dom.DOMSource;
030: import javax.xml.ws.Binding;
031: import javax.xml.ws.handler.Handler;
032: import javax.xml.ws.handler.MessageContext;
033: import javax.xml.ws.handler.soap.SOAPHandler;
034:
035: import org.apache.cxf.binding.soap.SoapMessage;
036: import org.apache.cxf.binding.soap.interceptor.SoapInterceptor;
037: import org.apache.cxf.helpers.CastUtils;
038: import org.apache.cxf.jaxws.handler.AbstractProtocolHandlerInterceptor;
039: import org.apache.cxf.jaxws.handler.HandlerChainInvoker;
040: import org.apache.cxf.phase.Phase;
041: import org.apache.cxf.staxutils.StaxUtils;
042:
043: public class SOAPHandlerFaultInInterceptor extends
044: AbstractProtocolHandlerInterceptor<SoapMessage> implements
045: SoapInterceptor {
046:
047: public SOAPHandlerFaultInInterceptor(Binding binding) {
048: super (binding, Phase.PRE_PROTOCOL);
049: }
050:
051: public Set<URI> getRoles() {
052: Set<URI> roles = new HashSet<URI>();
053: // TODO
054: return roles;
055: }
056:
057: public Set<QName> getUnderstoodHeaders() {
058: Set<QName> understood = new HashSet<QName>();
059: for (Handler h : getBinding().getHandlerChain()) {
060: if (h instanceof SOAPHandler) {
061: Set<QName> headers = CastUtils.cast(((SOAPHandler) h)
062: .getHeaders());
063: if (headers != null) {
064: understood.addAll(headers);
065: }
066: }
067: }
068: return understood;
069: }
070:
071: public void handleMessage(SoapMessage message) {
072: if (getInvoker(message).getProtocolHandlers().isEmpty()) {
073: return;
074: }
075:
076: MessageContext context = createProtocolMessageContext(message);
077: HandlerChainInvoker invoker = getInvoker(message);
078: invoker.setProtocolMessageContext(context);
079:
080: if (!invoker.invokeProtocolHandlersHandleFault(
081: isRequestor(message), context)) {
082: handleAbort(message, context);
083: }
084:
085: SOAPMessage msg = message.getContent(SOAPMessage.class);
086: if (msg != null) {
087: XMLStreamReader xmlReader = createXMLStreamReaderFromSOAPMessage(msg);
088: message.setContent(XMLStreamReader.class, xmlReader);
089: }
090:
091: }
092:
093: private void handleAbort(SoapMessage message, MessageContext context) {
094: if (isRequestor(message)) {
095:
096: if (getInvoker(message).isOutbound()) {
097: // client side outbound
098: // wont get here
099: } else {
100: // client side inbound - Normal handler message processing
101: // stops, but the inbound interceptor chain still continues, dispatch the message
102: //By onCompletion here, we can skip rest Logical handlers
103: onCompletion(message);
104: }
105: } else {
106: if (!getInvoker(message).isOutbound()) {
107: // server side inbound
108: // wont get here
109: } else {
110: // server side outbound
111: // wont get here
112: }
113: }
114: }
115:
116: @Override
117: protected MessageContext createProtocolMessageContext(
118: SoapMessage message) {
119: return new SOAPMessageContextImpl(message);
120: }
121:
122: private XMLStreamReader createXMLStreamReaderFromSOAPMessage(
123: SOAPMessage soapMessage) {
124: // responseMsg.setContent(SOAPMessage.class, soapMessage);
125: XMLStreamReader xmlReader = null;
126: try {
127: DOMSource bodySource = new DOMSource(soapMessage
128: .getSOAPPart().getEnvelope().getBody());
129: xmlReader = StaxUtils.createXMLStreamReader(bodySource);
130: xmlReader.nextTag();
131: xmlReader.nextTag(); // move past body tag
132: } catch (SOAPException e) {
133: e.printStackTrace();
134: } catch (XMLStreamException e) {
135: e.printStackTrace();
136: }
137: return xmlReader;
138: }
139:
140: public void handleFault(SoapMessage message) {
141: }
142: }
|