View Javadoc
1   /*
2    * Copyright 2009 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.builder;
17  
18  import org.apache.camel.Endpoint;
19  import org.apache.camel.Processor;
20  import org.apache.camel.builder.Builder;
21  import org.apache.camel.builder.RouteBuilder;
22  import org.apache.camel.processor.aggregate.AggregationStrategy;
23  import org.apache.camel.util.CamelContextHelper;
24  import org.openehealth.ipf.commons.core.modules.api.*;
25  import org.openehealth.ipf.commons.xml.SchematronValidator;
26  import org.openehealth.ipf.commons.xml.XsdValidator;
27  import org.openehealth.ipf.platform.camel.core.adapter.*;
28  import org.openehealth.ipf.platform.camel.core.process.Enricher;
29  import org.openehealth.ipf.platform.camel.core.process.Validation;
30  
31  import javax.xml.transform.stream.StreamSource;
32  
33  /**
34   * Helper class for creating IPF extensions in Java-based route definitions.
35   * 
36   * @author Martin Krasser
37   */
38  public class RouteHelper {
39      
40      private RouteBuilder routeBuilder;
41  
42      public RouteHelper(RouteBuilder routeBuilder) {
43          this.routeBuilder = routeBuilder;
44      }
45      
46      /**
47       * @param routeBuilder the routeBuilder to set
48       */
49      public void setRouteBuilder(RouteBuilder routeBuilder) {
50          this.routeBuilder = routeBuilder;
51      }
52   
53      // ----------------------------------------------------------------
54      //  Adapter
55      // ----------------------------------------------------------------
56      
57      public PredicateAdapter predicate(Predicate predicate) {
58          return new PredicateAdapter(predicate);
59      }
60      
61      /**
62       * Creates a new {@link PredicateAdapter} that adapts a {@link Predicate}
63       * Spring bean identified by name <code>predicateBeanName</code>.
64       * 
65       * @param predicateBeanName
66       *            name of the {@link Predicate} bean.
67       * @return an adapted {@link Predicate} bean.
68       */
69      public PredicateAdapter predicate(String predicateBeanName) {
70          return new PredicateAdapter(routeBuilder.getContext().getRegistry()
71                  .lookupByNameAndType(predicateBeanName, Predicate.class));
72      }
73      
74      /**
75       * Creates a new {@link ConverterAdapter} that adapts a
76       * {@link Converter} Spring bean identified by name
77       * <code>converterBeanName</code>.
78       * 
79       * @param converterBeanName
80       *            name of the {@link Converter} bean.
81       * @return an adapted {@link Converter} bean.
82       */
83      public ConverterAdapter converter(String converterBeanName) {
84          return new ConverterAdapter(routeBuilder.getContext().getRegistry()
85                  .lookupByNameAndType(converterBeanName, Converter.class));
86      }
87  
88      /**
89       * Creates a new {@link ParserAdapter} that adapts a
90       * {@link Parser} Spring bean identified by name
91       * <code>parserBeanName</code>.
92       * 
93       * @param parserBeanName
94       *            name of the {@link Parser} bean.
95       * @return an adapted {@link Parser} bean.
96       */
97      public ParserAdapter parser(String parserBeanName) {
98          return new ParserAdapter(routeBuilder.getContext().getRegistry()
99                  .lookupByNameAndType(parserBeanName, Parser.class));
100     }
101 
102     public ParserAdapter parser(Parser<?> parser) {
103         return new ParserAdapter(parser);
104     }
105 
106     /**
107      * Creates a new {@link RendererAdapter} that adapts a
108      * {@link Renderer} Spring bean identified by name
109      * <code>rendererBeanName</code>.
110      * 
111      * @param rendererBeanName
112      *            name of the {@link Renderer} bean.
113      * @return an adapted {@link Renderer} bean.
114      */
115     public RendererAdapter renderer(String rendererBeanName) {
116         return new RendererAdapter(routeBuilder.getContext().getRegistry()
117                 .lookupByNameAndType(rendererBeanName, Renderer.class));
118     }
119 
120     /**
121      * Creates a new {@link RendererAdapter} that adapts a {@link Renderer}.
122      * 
123      * @param renderer
124      *            a {@link Renderer}.
125      * @return an adapted {@link Renderer}.
126      */
127     public RendererAdapter renderer(Renderer<?> renderer) {
128         return new RendererAdapter(renderer);
129     }
130 
131     /**
132      * Creates a new {@link TransmogrifierAdapter} that adapts a
133      * {@link Transmogrifier} Spring bean identified by name
134      * <code>transmogrifierBeanName</code>.
135      * 
136      * @param transmogrifierBeanName
137      *            name of the {@link Transmogrifier} bean.
138      * @return an adapted {@link Transmogrifier} bean.
139      */
140     public TransmogrifierAdapter transmogrifier(String transmogrifierBeanName) {
141         return new TransmogrifierAdapter(routeBuilder.getContext().getRegistry()
142                 .lookupByNameAndType(transmogrifierBeanName, Transmogrifier.class));
143     }
144  
145     /**
146      * Creates a new {@link TransmogrifierAdapter} that adapts the given
147      * <code>transmogrifier</code>.
148      * 
149      * @param transmogrifier
150      *            a transmogrifier.
151      * @return an adapted transmogrifier.
152      */
153     public TransmogrifierAdapter transmogrifier(Transmogrifier<?, ?> transmogrifier) {
154         return new TransmogrifierAdapter(transmogrifier);
155     }
156  
157     /**
158      * Creates a new {@link ValidatorAdapter} that adapts a
159      * {@link Validator} Spring bean identified by name
160      * <code>validatorBeanName</code>.
161      * 
162      * @param validatorBeanName
163      *            name of the {@link Validator} bean.
164      * @return an adapted {@link Validator} bean.
165      */
166     public ValidatorAdapter validator(String validatorBeanName) {
167         return new ValidatorAdapter(routeBuilder.getContext().getRegistry()
168                 .lookupByNameAndType(validatorBeanName, Validator.class));
169     }
170 
171     /**
172      * Creates a new {@link ValidatorAdapter} that adapts the given
173      * <code>validator</code>.
174      * 
175      * @param validator
176      *            a validator.
177      * @return an adapted validator.
178      */
179     public ValidatorAdapter validator(Validator<?, ?> validator) {
180         return new ValidatorAdapter(validator);
181     }
182     
183     /**
184      * Creates a new {@link ValidatorAdapter} that adapts the given
185      * <code>XsdValidator</code>.
186      * 
187      * @return an adapted validator.
188      */
189     public ValidatorAdapter xsdValidator() {
190         ValidatorAdapter adapter = new ValidatorAdapter(new XsdValidator());
191         adapter.input(Builder.bodyAs(StreamSource.class));
192         return adapter;
193     }
194     
195     /**
196      * Creates a new {@link ValidatorAdapter} that adapts the given
197      * <code>SchematronValidator</code>.
198      * 
199      * @return an adapted validator.
200      */
201     public ValidatorAdapter schematronValidator() {
202         ValidatorAdapter adapter = new ValidatorAdapter(new SchematronValidator());
203         adapter.input(Builder.bodyAs(StreamSource.class));
204         return adapter;
205     }    
206 
207     /**
208      * Creates a new {@link AggregatorAdapter} that adapts the given
209      * <code>aggregator</code>.
210      * 
211      * @param aggregator
212      *            an aggregator.
213      * @return an adapted aggregator.
214      */
215     public AggregatorAdapter aggregationStrategy(Aggregator aggregator) {
216         return new AggregatorAdapter(aggregator);
217     }
218 
219     
220     /**
221      * Creates a new {@link AggregatorAdapter} that adapts a
222      * {@link Aggregator} Spring bean identified by name
223      * <code>aggregatorBeanName</code>.
224      * 
225      * @param aggregatorBeanName
226      *            name of the {@link Aggregator} bean.
227      * @return an adapted {@link Aggregator} bean.
228      */
229     public AggregatorAdapter aggregationStrategy(String aggregatorBeanName) {
230         return new AggregatorAdapter(routeBuilder.getContext().getRegistry()
231                 .lookupByNameAndType(aggregatorBeanName, Aggregator.class));
232     }
233     
234     public DataFormatAdapter dataFormatParser(Parser<?> parser) {
235         return new DataFormatAdapter(parser);
236     }
237     
238     public DataFormatAdapter dataFormatParser(String parserBeanName) {
239         return new DataFormatAdapter(routeBuilder.getContext().getRegistry()
240                 .lookupByNameAndType(parserBeanName, Parser.class));
241     }
242     
243     public DataFormatAdapter dataFormatRenderer(Renderer<?> renderer) {
244         return new DataFormatAdapter(renderer);
245     }
246     
247     public DataFormatAdapter dataFormatRenderer(String rendererBeanName) {
248         return new DataFormatAdapter(routeBuilder.getContext().getRegistry()
249                 .lookupByNameAndType(rendererBeanName, Renderer.class));
250     }
251     
252     // ----------------------------------------------------------------
253     //  Processor
254     // ----------------------------------------------------------------
255 
256     /**
257      * Creates a new {@link Enricher}.
258      * 
259      * @param aggregationStrategy
260      *            aggregation strategy to combine input data and additional
261      *            data.
262      * @param resourceUri
263      *            URI of resource endpoint for obtaining additional data.
264      * @return an enricher.
265      * @throws Exception
266      *             if a producer cannot be created for the endpoint represented
267      *             by <code>resourceUri</code>.
268      */
269     public Enricher enricher(AggregationStrategy aggregationStrategy, String resourceUri) throws Exception {
270         Endpoint endpoint = CamelContextHelper.getMandatoryEndpoint(routeBuilder.getContext(), resourceUri);
271         return new Enricher(aggregationStrategy, endpoint.createProducer());
272     }
273     
274     /**
275      * Creates a new {@link Enricher} using an {@link AggregatorAdapter} that
276      * adapts an {@link Aggregator} Spring bean identified by name
277      * <code>aggregatorBeanName</code>. The adapter is configured to obtain
278      * the enrichment data from the resource's response-message's body (i.e.
279      * from the out-message's body of the exchange used to communicate with the
280      * resource).
281      * 
282      * @param aggregatorBeanName
283      *            name of the {@link Aggregator} bean.
284      * @param resourceUri
285      *            URI of resource endpoint for obtaining additional data.
286      * @return an enricher.
287      * @throws Exception
288      *             if a producer cannot be created for the endpoint represented
289      *             by <code>resourceUri</code>.
290      */
291     public Enricher enricher(String aggregatorBeanName, String resourceUri) throws Exception {
292         Endpoint endpoint = CamelContextHelper.getMandatoryEndpoint(routeBuilder.getContext(), resourceUri);
293         return new Enricher(aggregationStrategy(aggregatorBeanName).aggregationInput(routeBuilder.body()), endpoint.createProducer());
294     }
295 
296     /**
297      * Creates a {@link Validation} process object.
298      * 
299      * @param validatorUri
300      *            URI of the endpoint that validates an exchange.
301      * @return a validation process object.
302      * @throws Exception
303      *             if a producer cannot be created for the endpoint represented
304      *             by <code>validatorUri</code>.
305      */
306     public Validation validation(String validatorUri) throws Exception {
307         Endpoint endpoint = CamelContextHelper.getMandatoryEndpoint(routeBuilder.getContext(), validatorUri);
308         return new Validation(endpoint.createProducer());
309     }
310     
311     /**
312      * Creates a {@link Validation} process object.
313      * 
314      * @param validator
315      *            processor that validates an exchange.
316      * @return a validation process object.
317      */
318     public Validation validation(Processor validator) {
319         return new Validation(validator);
320     }
321     
322 }