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.adapter;
17  
18  import static org.openehealth.ipf.platform.camel.core.util.Exchanges.prepareResult;
19  
20  import java.io.InputStream;
21  import java.io.Reader;
22  
23  import javax.xml.transform.Source;
24  
25  import org.apache.camel.Exchange;
26  import org.openehealth.ipf.commons.core.modules.api.Renderer;
27  
28  /**
29   * Adapts a {@link Renderer}. 
30   * 
31   * @author Martin Krasser
32   */
33  public class RendererAdapter extends ProcessorAdapter {
34  
35      private final Renderer renderer;
36      
37      /**
38       * Creates a new {@link RendererAdapter} and sets the delegate
39       * {@link Renderer}.
40       * 
41       * @param renderer
42       *            a renderer.
43       */
44      public RendererAdapter(Renderer renderer) {
45          this.renderer = renderer;
46      }
47      
48      /**
49       * Delegates processing of input data and input parameters to a
50       * {@link Renderer} object. The rendering result is written to body of the
51       * message returned by
52       * {@link org.openehealth.ipf.platform.camel.core.util.Exchanges#prepareResult(Exchange)}.
53       * 
54       * @param exchange
55       *            message exchange where to write processing results.
56       * @param inputData
57       *            input data.
58       * @param inputParams
59       *            input parameters.
60       * @throws Exception
61       *             if a processing error occurs.
62       */
63      @Override
64      protected void doProcess(Exchange exchange, Object inputData, 
65              Object... inputParams) throws Exception {
66          
67          if (inputData instanceof InputStream) {
68              throw new IllegalArgumentException(errorMessage(inputData));
69          } else if (inputData instanceof Reader) {
70              throw new IllegalArgumentException(errorMessage(inputData));
71          } else if (inputData instanceof Source) {
72              throw new IllegalArgumentException(errorMessage(inputData));
73          } else {
74              prepareResult(exchange).setBody(renderer.render(inputData, inputParams));
75          }
76          
77      }
78      
79      private static String errorMessage(Object inputData) {
80          return "invalid input data type for renderer: " + inputData.getClass()
81                  + ". Use a parser or converter instead.";
82      }
83      
84  }