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.interceptor;
019:
020: import java.io.IOException;
021: import java.util.List;
022: import java.util.logging.Level;
023: import java.util.logging.Logger;
024:
025: import org.apache.cxf.Bus;
026: import org.apache.cxf.binding.Binding;
027: import org.apache.cxf.endpoint.ConduitSelector;
028: import org.apache.cxf.endpoint.Endpoint;
029: import org.apache.cxf.endpoint.PreexistingConduitSelector;
030: import org.apache.cxf.message.Exchange;
031: import org.apache.cxf.message.Message;
032: import org.apache.cxf.phase.AbstractPhaseInterceptor;
033: import org.apache.cxf.phase.Phase;
034: import org.apache.cxf.phase.PhaseChainCache;
035: import org.apache.cxf.phase.PhaseInterceptorChain;
036: import org.apache.cxf.phase.PhaseManager;
037: import org.apache.cxf.service.model.BindingMessageInfo;
038: import org.apache.cxf.service.model.BindingOperationInfo;
039: import org.apache.cxf.service.model.MessageInfo;
040: import org.apache.cxf.transport.Conduit;
041: import org.apache.cxf.ws.addressing.EndpointReferenceType;
042:
043: public class OutgoingChainInterceptor extends
044: AbstractPhaseInterceptor<Message> {
045: private static final Logger LOG = Logger
046: .getLogger(OutgoingChainInterceptor.class.getName());
047:
048: private PhaseChainCache chainCache = new PhaseChainCache();
049:
050: public OutgoingChainInterceptor() {
051: super (Phase.POST_INVOKE);
052: }
053:
054: public void handleMessage(Message message) {
055: Exchange ex = message.getExchange();
056: BindingOperationInfo bin = ex.get(BindingOperationInfo.class);
057: if (null != bin && null != bin.getOperationInfo()
058: && bin.getOperationInfo().isOneWay()) {
059: return;
060: }
061: Message out = ex.getOutMessage();
062: if (out != null) {
063: getBackChannelConduit(message);
064: if (bin != null) {
065: out.put(MessageInfo.class, bin.getOperationInfo()
066: .getOutput());
067: out.put(BindingMessageInfo.class, bin.getOutput());
068: }
069:
070: InterceptorChain outChain = out.getInterceptorChain();
071: if (outChain == null) {
072: outChain = getChain(ex);
073: out.setInterceptorChain(outChain);
074: }
075: outChain.doIntercept(out);
076: }
077: }
078:
079: protected static Conduit getBackChannelConduit(Message message) {
080: Conduit conduit = null;
081: Exchange ex = message.getExchange();
082: if (ex.getConduit(message) == null
083: && ex.getDestination() != null) {
084: try {
085: EndpointReferenceType target = ex
086: .get(EndpointReferenceType.class);
087: conduit = ex.getDestination().getBackChannel(
088: ex.getInMessage(), null, target);
089: ex.put(ConduitSelector.class,
090: new PreexistingConduitSelector(conduit, ex
091: .get(Endpoint.class)));
092: } catch (IOException e) {
093: // TODO Auto-generated catch block
094: e.printStackTrace();
095: }
096: }
097: return conduit;
098: }
099:
100: public static InterceptorChain getOutInterceptorChain(Exchange ex) {
101: Bus bus = ex.get(Bus.class);
102: Binding binding = ex.get(Binding.class);
103: PhaseManager pm = bus.getExtension(PhaseManager.class);
104: PhaseInterceptorChain chain = new PhaseInterceptorChain(pm
105: .getOutPhases());
106:
107: Endpoint ep = ex.get(Endpoint.class);
108: List<Interceptor> il = ep.getOutInterceptors();
109: if (LOG.isLoggable(Level.FINE)) {
110: LOG.fine("Interceptors contributed by endpoint: " + il);
111: }
112: chain.add(il);
113: il = ep.getService().getOutInterceptors();
114: if (LOG.isLoggable(Level.FINE)) {
115: LOG.fine("Interceptors contributed by service: " + il);
116: }
117: chain.add(il);
118: il = bus.getOutInterceptors();
119: if (LOG.isLoggable(Level.FINE)) {
120: LOG.fine("Interceptors contributed by bus: " + il);
121: }
122: chain.add(il);
123: if (binding != null) {
124: il = binding.getOutInterceptors();
125: if (LOG.isLoggable(Level.FINE)) {
126: LOG.fine("Interceptors contributed by binding: " + il);
127: }
128: chain.add(il);
129: }
130: chain.setFaultObserver(ep.getOutFaultObserver());
131: return chain;
132: }
133:
134: private PhaseInterceptorChain getChain(Exchange ex) {
135: Bus bus = ex.get(Bus.class);
136: Binding binding = ex.get(Binding.class);
137:
138: Endpoint ep = ex.get(Endpoint.class);
139:
140: List<Interceptor> i1 = ep.getOutInterceptors();
141: if (LOG.isLoggable(Level.FINE)) {
142: LOG.fine("Interceptors contributed by endpoint: " + i1);
143: }
144: List<Interceptor> i2 = ep.getService().getOutInterceptors();
145: if (LOG.isLoggable(Level.FINE)) {
146: LOG.fine("Interceptors contributed by service: " + i2);
147: }
148: List<Interceptor> i3 = bus.getOutInterceptors();
149: if (LOG.isLoggable(Level.FINE)) {
150: LOG.fine("Interceptors contributed by bus: " + i3);
151: }
152: List<Interceptor> i4 = null;
153: PhaseInterceptorChain chain;
154: if (binding != null) {
155: i4 = binding.getOutInterceptors();
156: if (LOG.isLoggable(Level.FINE)) {
157: LOG.fine("Interceptors contributed by binding: " + i4);
158: }
159: }
160: if (i4 != null) {
161: chain = chainCache.get(bus.getExtension(PhaseManager.class)
162: .getOutPhases(), i1, i2, i3, i4);
163: } else {
164: chain = chainCache.get(bus.getExtension(PhaseManager.class)
165: .getOutPhases(), i1, i2, i3);
166: }
167:
168: chain.setFaultObserver(ep.getOutFaultObserver());
169: return chain;
170: }
171:
172: }
|