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.logical;
019:
020: import javax.xml.parsers.ParserConfigurationException;
021: import javax.xml.soap.SOAPMessage;
022: import javax.xml.stream.XMLStreamException;
023: import javax.xml.stream.XMLStreamReader;
024: import javax.xml.stream.XMLStreamWriter;
025: import javax.xml.transform.Source;
026: import javax.xml.transform.dom.DOMSource;
027: import javax.xml.ws.Binding;
028:
029: import org.w3c.dom.Document;
030: import org.w3c.dom.Node;
031:
032: import org.apache.cxf.endpoint.Endpoint;
033: import org.apache.cxf.helpers.XMLUtils;
034: import org.apache.cxf.interceptor.Fault;
035: import org.apache.cxf.interceptor.InterceptorChain;
036: import org.apache.cxf.jaxws.handler.AbstractJAXWSHandlerInterceptor;
037: import org.apache.cxf.jaxws.handler.HandlerChainInvoker;
038: import org.apache.cxf.message.Exchange;
039: import org.apache.cxf.message.FaultMode;
040: import org.apache.cxf.message.Message;
041: import org.apache.cxf.phase.Phase;
042: import org.apache.cxf.staxutils.StaxUtils;
043: import org.apache.cxf.staxutils.W3CDOMStreamWriter;
044:
045: public class LogicalHandlerFaultOutInterceptor<T extends Message>
046: extends AbstractJAXWSHandlerInterceptor<T> {
047:
048: public static final String ORIGINAL_WRITER = "original_writer";
049:
050: LogicalHandlerFaultOutEndingInterceptor<T> ending;
051:
052: public LogicalHandlerFaultOutInterceptor(Binding binding) {
053: super (binding, Phase.PRE_MARSHAL);
054: ending = new LogicalHandlerFaultOutEndingInterceptor<T>(binding);
055: }
056:
057: public void handleMessage(T message) throws Fault {
058: HandlerChainInvoker invoker = getInvoker(message);
059: if (invoker.getLogicalHandlers().isEmpty()) {
060: return;
061: }
062:
063: try {
064:
065: XMLStreamWriter origWriter = message
066: .getContent(XMLStreamWriter.class);
067: Document doc = XMLUtils.newDocument();
068: message.setContent(Node.class, doc);
069: W3CDOMStreamWriter writer = new W3CDOMStreamWriter(doc);
070:
071: // Replace stax writer with DomStreamWriter
072: message.setContent(XMLStreamWriter.class, writer);
073: message.put(ORIGINAL_WRITER, origWriter);
074:
075: message.getInterceptorChain().add(ending);
076: } catch (ParserConfigurationException e) {
077: throw new Fault(e);
078: }
079: }
080:
081: private class LogicalHandlerFaultOutEndingInterceptor<X extends Message>
082: extends AbstractJAXWSHandlerInterceptor<X> {
083:
084: public LogicalHandlerFaultOutEndingInterceptor(Binding binding) {
085: super (binding, Phase.POST_MARSHAL);
086: }
087:
088: public void handleMessage(X message) throws Fault {
089: W3CDOMStreamWriter domWriter = (W3CDOMStreamWriter) message
090: .getContent(XMLStreamWriter.class);
091: XMLStreamWriter origWriter = (XMLStreamWriter) message
092: .get(LogicalHandlerFaultOutInterceptor.ORIGINAL_WRITER);
093:
094: HandlerChainInvoker invoker = getInvoker(message);
095: LogicalMessageContextImpl lctx = new LogicalMessageContextImpl(
096: message);
097: invoker.setLogicalMessageContext(lctx);
098: boolean requestor = isRequestor(message);
099:
100: XMLStreamReader reader = (XMLStreamReader) message
101: .get("LogicalHandlerInterceptor.INREADER");
102: SOAPMessage origMessage = null;
103: if (reader != null) {
104: origMessage = message.getContent(SOAPMessage.class);
105: message.setContent(XMLStreamReader.class, reader);
106: message.removeContent(SOAPMessage.class);
107: } else if (domWriter.getDocument().getDocumentElement() != null) {
108: Source source = new DOMSource(domWriter.getDocument());
109: message.setContent(Source.class, source);
110: message.setContent(Node.class, domWriter.getDocument());
111: message
112: .setContent(XMLStreamReader.class, StaxUtils
113: .createXMLStreamReader(domWriter
114: .getDocument()));
115: }
116:
117: try {
118: if (!invoker.invokeLogicalHandlersHandleFault(
119: requestor, lctx)) {
120: // handleAbort(message, context);
121: }
122: } catch (RuntimeException exception) {
123: Exchange exchange = message.getExchange();
124:
125: Exception ex = new Fault(exception);
126:
127: FaultMode mode = (FaultMode) message
128: .get(FaultMode.class);
129:
130: Message faultMessage = exchange.getOutMessage();
131: if (null == faultMessage) {
132: faultMessage = exchange.get(Endpoint.class)
133: .getBinding().createMessage();
134: }
135: faultMessage.setContent(Exception.class, ex);
136: if (null != mode) {
137: faultMessage.put(FaultMode.class, mode);
138: }
139: exchange.setOutMessage(null);
140: exchange.setOutFaultMessage(faultMessage);
141:
142: InterceptorChain ic = message.getInterceptorChain();
143: ic.reset();
144:
145: onCompletion(message);
146:
147: faultMessage.setInterceptorChain(ic);
148: ic.doIntercept(faultMessage);
149:
150: return;
151: }
152:
153: if (origMessage != null) {
154: message.setContent(SOAPMessage.class, origMessage);
155: }
156:
157: try {
158: reader = message.getContent(XMLStreamReader.class);
159: message.removeContent(XMLStreamReader.class);
160: if (reader != null) {
161: StaxUtils.copy(reader, origWriter);
162: } else if (domWriter.getDocument().getDocumentElement() != null) {
163: StaxUtils.copy(domWriter.getDocument(), origWriter);
164: }
165: message.setContent(XMLStreamWriter.class, origWriter);
166: } catch (XMLStreamException e) {
167: throw new Fault(e);
168: }
169: }
170: }
171:
172: }
|