View Javadoc
1   /*
2    * Copyright 2008 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *     
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.openehealth.ipf.platform.camel.core.util;
17  
18  import org.apache.camel.CamelContext;
19  import org.apache.camel.Exchange;
20  import org.apache.camel.ExchangePattern;
21  import org.apache.camel.Message;
22  import org.apache.camel.ProducerTemplate;
23  import org.apache.camel.impl.DefaultExchange;
24  
25  /**
26   * Utility related to Camel {@link Exchange}s.
27   * 
28   * @author Martin Krasser
29   */
30  public class Exchanges {
31  
32      /**
33       * Creates a {@link ProducerTemplate} from the given {@link Exchange}
34       * 
35       * @param exchange message exchange.
36       * @return a producer template.
37       */
38      public static ProducerTemplate producerTemplate(Exchange exchange) {
39          return exchange.getContext().createProducerTemplate();
40      }
41      
42      /**
43       * Returns the message where to write results.
44       * 
45       * @param exchange message exchange.
46       * @return result message.
47       */
48      public static Message resultMessage(Exchange exchange) {
49          if (exchange.getPattern().isOutCapable()) {
50              return exchange.getOut();
51          } else {
52              return exchange.getIn();
53          }
54      }
55  
56      /**
57       * Returns the message where to write results. This method copies the
58       * in-message to the out-message if the exchange is out-capable.
59       * 
60       * @param exchange message exchange.
61       * @return result message.
62       */
63      public static Message prepareResult(Exchange exchange) {
64          Message result = resultMessage(exchange);
65          if (exchange.getPattern().isOutCapable()) {
66              result.copyFrom(exchange.getIn());
67          }
68          return result;
69      }
70      
71      /**
72       * Creates a new {@link Exchange} instance from the given
73       * <code>exchange</code>. The resulting exchange's pattern is defined by
74       * <code>pattern</code>.
75       * 
76       * @param source
77       *            exchange to copy from.
78       * @param pattern
79       *            exchange pattern to set.
80       * @return created exchange.
81       */
82      public static Exchange createExchange(Exchange source, ExchangePattern pattern) {
83          DefaultExchange target = new DefaultExchange(source.getContext());
84          copyExchange(source, target);
85          target.setPattern(pattern);
86          return target;
87      }
88      
89      /**
90       * Creates a new {@link Exchange} instance using <code>context</code>.
91       * The resulting exchange's pattern is defined by <code>pattern</code>.
92       * 
93       * @param context
94       *            Camel context.
95       * @param pattern
96       *            exchange pattern.
97       * @return created exchange.
98       */
99      public static Exchange createExchange(CamelContext context, ExchangePattern pattern) {
100         DefaultExchange target = new DefaultExchange(context);
101         target.setPattern(pattern);
102         return target;
103     }
104 
105     /**
106      * Copies the exchange's in-message to the out-message if the exchange
107      * pattern is {@link ExchangePattern#InOut}.
108      * 
109      * @param exchange
110      *            message exchange.
111      */
112     public static void copyInput(Exchange exchange) {
113         prepareResult(exchange);
114     }
115     
116     /**
117      * Copies the <code>source</code> exchange to <code>target</code> exchange
118      * preserving the {@link ExchangePattern} of <code>target</code>.  
119      * 
120      * @param source source exchange.
121      * @param target target exchange.
122      * 
123      * @see #resultMessage(Exchange)
124      */
125     public static void copyExchange(Exchange source, Exchange target) {
126         if (source == target) {
127             // no need to copy
128             return;
129         }
130         
131         // copy in message
132         target.getIn().copyFrom(source.getIn());
133     
134         // copy out message
135         if (source.hasOut()) {
136             resultMessage(target).copyFrom(source.getOut());
137         }
138         
139         // copy exception
140         target.setException(source.getException());
141 
142         // copy properties
143         target.getProperties().putAll(source.getProperties());
144         
145     }
146 
147 
148     /**
149      * Extracts the exception handled while processing the given
150      * exchange (if any), and optionally marks the exchange as non-failed.
151      *
152      * @param exchange
153      *      Camel exchange, should be not <code>null</code>.
154      * @param cleanup
155      *      <code>true</code> iff the information about the occurred exception
156      *      should be removed from the given exchange.
157      * @return
158      *      an {@link Exception} instance, or <code>null</code> when no exception was handled.
159      */
160     public static Exception extractException(Exchange exchange, boolean cleanup) {
161         Exception exception = exchange.getException();
162         if (exception != null) {
163             if (cleanup) {
164                 exchange.setException(null);
165             }
166         }
167         else if (exchange.getProperty(Exchange.EXCEPTION_CAUGHT) != null) {
168             exception = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class);
169             if (cleanup) {
170                 exchange.removeProperty(Exchange.EXCEPTION_CAUGHT);
171                 exchange.removeProperty(Exchange.ERRORHANDLER_HANDLED);
172             }
173         }
174         return exception;
175     }
176 
177 
178     /**
179      * Extracts the exception handled while processing the given
180      * exchange (if any), and marks the exchange as non-failed.
181      * <p>
182      * This method corresponds to {@link #extractException(org.apache.camel.Exchange, boolean)}
183      * with the second parameter set to <code>true</code>.
184      * </p>
185      *
186      * @param exchange
187      *      Camel exchange, should be not <code>null</code>.
188      * @return
189      *      an {@link Exception} instance, or <code>null</code> when no exception was handled.
190      */
191     public static Exception extractException(Exchange exchange) {
192         return extractException(exchange, true);
193     }
194 
195 }