01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */package org.apache.cxf.interceptor;
19:
20: import java.io.IOException;
21: import java.util.ResourceBundle;
22:
23: import org.apache.cxf.common.i18n.BundleUtils;
24: import org.apache.cxf.message.Exchange;
25: import org.apache.cxf.message.Message;
26: import org.apache.cxf.phase.AbstractPhaseInterceptor;
27: import org.apache.cxf.phase.Phase;
28: import org.apache.cxf.transport.Conduit;
29:
30: /**
31: * Takes the Conduit from the exchange and sends the message through it.
32: */
33: public class MessageSenderInterceptor extends
34: AbstractPhaseInterceptor<Message> {
35: private static final ResourceBundle BUNDLE = BundleUtils
36: .getBundle(MessageSenderInterceptor.class);
37: private MessageSenderEndingInterceptor ending = new MessageSenderEndingInterceptor();
38:
39: public MessageSenderInterceptor() {
40: super (Phase.PREPARE_SEND);
41: }
42:
43: public void handleMessage(Message message) {
44: try {
45: getConduit(message).prepare(message);
46: } catch (IOException ex) {
47: throw new Fault(new org.apache.cxf.common.i18n.Message(
48: "COULD_NOT_SEND", BUNDLE), ex);
49: }
50:
51: // Add a final interceptor to close the conduit
52: message.getInterceptorChain().add(ending);
53: }
54:
55: public class MessageSenderEndingInterceptor extends
56: AbstractPhaseInterceptor<Message> {
57: public MessageSenderEndingInterceptor() {
58: super (Phase.PREPARE_SEND_ENDING);
59: }
60:
61: public void handleMessage(Message message) throws Fault {
62: try {
63: getConduit(message).close(message);
64: } catch (IOException e) {
65: throw new Fault(new org.apache.cxf.common.i18n.Message(
66: "COULD_NOT_SEND", BUNDLE), e);
67: }
68: }
69: }
70:
71: private Conduit getConduit(Message message) {
72: Exchange exchange = message.getExchange();
73: Conduit conduit = exchange.getConduit(message);
74: if (conduit == null
75: && (exchange.getOutMessage() != null || exchange
76: .getOutFaultMessage() != null)) {
77: conduit = OutgoingChainInterceptor
78: .getBackChannelConduit(message);
79: }
80: return conduit;
81: }
82:
83: }
|