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.util.SortedSet;
021: import java.util.logging.Level;
022: import java.util.logging.Logger;
023:
024: import org.apache.cxf.Bus;
025: import org.apache.cxf.common.logging.LogUtils;
026: import org.apache.cxf.endpoint.Endpoint;
027: import org.apache.cxf.message.Exchange;
028: import org.apache.cxf.message.FaultMode;
029: import org.apache.cxf.message.Message;
030: import org.apache.cxf.phase.Phase;
031: import org.apache.cxf.phase.PhaseInterceptorChain;
032: import org.apache.cxf.service.model.BindingFaultInfo;
033: import org.apache.cxf.transport.MessageObserver;
034:
035: public abstract class AbstractFaultChainInitiatorObserver implements
036: MessageObserver {
037:
038: private static final Logger LOG = Logger
039: .getLogger(AbstractFaultChainInitiatorObserver.class
040: .getName());
041:
042: private Bus bus;
043:
044: public AbstractFaultChainInitiatorObserver(Bus bus) {
045: this .bus = bus;
046: }
047:
048: public void onMessage(Message message) {
049:
050: assert null != message;
051: Exchange exchange = message.getExchange();
052:
053: Message faultMessage = null;
054:
055: // now that we have switched over to the fault chain,
056: // prevent any further operations on the in/out message
057:
058: if (isOutboundObserver()) {
059: Exception ex = message.getContent(Exception.class);
060: if (!(ex instanceof Fault)) {
061: ex = new Fault(ex);
062: }
063: FaultMode mode = (FaultMode) message.get(FaultMode.class);
064:
065: faultMessage = exchange.getOutMessage();
066: if (null == faultMessage) {
067: faultMessage = exchange.get(Endpoint.class)
068: .getBinding().createMessage();
069: }
070: faultMessage.setContent(Exception.class, ex);
071: if (null != mode) {
072: faultMessage.put(FaultMode.class, mode);
073: }
074: exchange.setOutMessage(null);
075: exchange.setOutFaultMessage(faultMessage);
076: if (message.get(BindingFaultInfo.class) != null) {
077: faultMessage.put(BindingFaultInfo.class, message
078: .get(BindingFaultInfo.class));
079: }
080: } else {
081: faultMessage = message;
082: exchange.setInMessage(null);
083: exchange.setInFaultMessage(faultMessage);
084: }
085:
086: // setup chain
087: PhaseInterceptorChain chain = new PhaseInterceptorChain(
088: getPhases());
089: initializeInterceptors(faultMessage.getExchange(), chain);
090:
091: faultMessage.setInterceptorChain(chain);
092: try {
093: chain.doIntercept(faultMessage);
094: } catch (Exception exc) {
095: LogUtils.log(LOG, Level.SEVERE,
096: "Error occurred during error handling, give up!",
097: exc);
098: throw new RuntimeException(exc.getCause());
099: }
100: }
101:
102: protected abstract boolean isOutboundObserver();
103:
104: protected abstract SortedSet<Phase> getPhases();
105:
106: protected void initializeInterceptors(Exchange ex,
107: PhaseInterceptorChain chain) {
108:
109: }
110:
111: public Bus getBus() {
112: return bus;
113: }
114:
115: }
|