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.jaxws.handler.AbstractJAXWSHandlerInterceptor;
036: import org.apache.cxf.jaxws.handler.HandlerChainInvoker;
037: import org.apache.cxf.jaxws.support.ContextPropertiesMapping;
038: import org.apache.cxf.message.Message;
039: import org.apache.cxf.phase.Phase;
040: import org.apache.cxf.phase.PhaseInterceptorChain;
041: import org.apache.cxf.staxutils.StaxUtils;
042: import org.apache.cxf.staxutils.W3CDOMStreamWriter;
043: import org.apache.cxf.transport.MessageObserver;
044:
045: public class LogicalHandlerOutInterceptor<T extends Message> extends
046: AbstractJAXWSHandlerInterceptor<T> {
047:
048: public static final String ORIGINAL_WRITER = "original_writer";
049: private LogicalHandlerOutEndingInterceptor<T> ending;
050:
051: public LogicalHandlerOutInterceptor(Binding binding) {
052: super (binding, Phase.PRE_MARSHAL);
053: ending = new LogicalHandlerOutEndingInterceptor<T>(binding);
054: }
055:
056: public void handleMessage(T message) throws Fault {
057: HandlerChainInvoker invoker = getInvoker(message);
058: if (invoker.getLogicalHandlers().isEmpty()) {
059: return;
060: }
061:
062: try {
063:
064: XMLStreamWriter origWriter = message
065: .getContent(XMLStreamWriter.class);
066: Document document = XMLUtils.newDocument();
067: message.setContent(Node.class, document);
068: W3CDOMStreamWriter writer = new W3CDOMStreamWriter(document);
069:
070: // Replace stax writer with DomStreamWriter
071: message.setContent(XMLStreamWriter.class, writer);
072: message.put(ORIGINAL_WRITER, origWriter);
073:
074: message.getInterceptorChain().add(ending);
075: } catch (ParserConfigurationException e) {
076: throw new Fault(e);
077: }
078: }
079:
080: private class LogicalHandlerOutEndingInterceptor<X extends Message>
081: extends AbstractJAXWSHandlerInterceptor<X> {
082:
083: public LogicalHandlerOutEndingInterceptor(Binding binding) {
084: super (binding, Phase.POST_MARSHAL);
085: }
086:
087: public void handleMessage(X message) throws Fault {
088: W3CDOMStreamWriter domWriter = (W3CDOMStreamWriter) message
089: .getContent(XMLStreamWriter.class);
090: XMLStreamWriter origWriter = (XMLStreamWriter) message
091: .get(LogicalHandlerOutInterceptor.ORIGINAL_WRITER);
092:
093: HandlerChainInvoker invoker = getInvoker(message);
094: LogicalMessageContextImpl lctx = new LogicalMessageContextImpl(
095: message);
096: invoker.setLogicalMessageContext(lctx);
097: boolean requestor = isRequestor(message);
098:
099: ContextPropertiesMapping.mapCxf2Jaxws(
100: message.getExchange(), lctx, requestor);
101:
102: XMLStreamReader reader = (XMLStreamReader) message
103: .get("LogicalHandlerInterceptor.INREADER");
104: SOAPMessage origMessage = null;
105: if (reader != null) {
106: origMessage = message.getContent(SOAPMessage.class);
107: message.setContent(XMLStreamReader.class, reader);
108: message.removeContent(SOAPMessage.class);
109: } else if (domWriter.getDocument().getDocumentElement() != null) {
110: Source source = new DOMSource(domWriter.getDocument());
111: message.setContent(Source.class, source);
112: message
113: .setContent(XMLStreamReader.class, StaxUtils
114: .createXMLStreamReader(domWriter
115: .getDocument()));
116: }
117:
118: if (!invoker.invokeLogicalHandlers(requestor, lctx)) {
119: if (requestor) {
120: // client side - abort
121: message.getInterceptorChain().abort();
122: Endpoint e = message.getExchange().get(
123: Endpoint.class);
124: Message responseMsg = e.getBinding()
125: .createMessage();
126:
127: MessageObserver observer = (MessageObserver) message
128: .getExchange().get(MessageObserver.class);
129: if (observer != null) {
130: //client side outbound, the request message becomes the response message
131: responseMsg
132: .setContent(
133: XMLStreamReader.class,
134: message
135: .getContent(XMLStreamReader.class));
136:
137: message.getExchange().setInMessage(responseMsg);
138: responseMsg
139: .put(
140: PhaseInterceptorChain.STARTING_AT_INTERCEPTOR_ID,
141: LogicalHandlerInInterceptor.class
142: .getName());
143: observer.onMessage(responseMsg);
144: }
145: } else {
146: // server side - abort
147: //System.out.println("Logical handler server side aborting");
148: }
149: return;
150: }
151: if (origMessage != null) {
152: message.setContent(SOAPMessage.class, origMessage);
153: }
154:
155: try {
156: reader = message.getContent(XMLStreamReader.class);
157: message.removeContent(XMLStreamReader.class);
158: if (reader != null) {
159: StaxUtils.copy(reader, origWriter);
160: } else if (domWriter.getDocument().getDocumentElement() != null) {
161: StaxUtils.copy(domWriter.getDocument(), origWriter);
162: }
163: message.setContent(XMLStreamWriter.class, origWriter);
164: } catch (XMLStreamException e) {
165: throw new Fault(e);
166: }
167: }
168:
169: }
170:
171: }
|