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.IOException;
21  import java.io.InputStream;
22  import java.io.OutputStream;
23  import java.io.Reader;
24  import java.io.Writer;
25  
26  import javax.xml.transform.Result;
27  import javax.xml.transform.Source;
28  
29  import org.apache.camel.Exchange;
30  import org.openehealth.ipf.commons.core.modules.api.Converter;
31  import org.openehealth.ipf.platform.camel.core.io.ReadableByteArrayOutputStream;
32  import org.openehealth.ipf.platform.camel.core.io.ReadableStreamResult;
33  import org.openehealth.ipf.platform.camel.core.io.ReadableStringWriter;
34  
35  
36  /**
37   * Adapts a {@link Converter}. 
38   * 
39   * @author Martin Krasser
40   */
41  public class ConverterAdapter extends ProcessorAdapter {
42  
43      private final Converter converter;
44      
45      /**
46       * Creates a new {@link ConverterAdapter} and sets the delegate
47       * {@link Converter}.
48       * 
49       * @param converter
50       *            a converter.
51       */
52      public ConverterAdapter(Converter converter) {
53          this.converter = converter;
54      }
55  
56      /**
57       * Processes input data and populates the output message. This method
58       * delegates message processing to more specialized <code>doProcess</code>
59       * implementations.
60       * 
61       * @param exchange
62       *            message exchange where to write processing results.
63       * @param inputData
64       *            input data.
65       * @param inputParams
66       *            input parameters.
67       * @throws Exception
68       *             if a processing error occurs.
69       * 
70       * @see #doProcess(InputStream, OutputStream, Object...)
71       * @see #doProcess(Reader, Writer, Object...)
72       * @see #doProcess(Source, Result, Object...)
73       * @see #doProcess(String, Object...)
74       */
75      @Override
76      protected void doProcess(Exchange exchange, Object inputData, 
77              Object... inputParams) throws Exception {
78          
79          if (inputData instanceof InputStream) {
80              ReadableByteArrayOutputStream outputData = new ReadableByteArrayOutputStream();
81              doProcess((InputStream)inputData, outputData, inputParams);
82              prepareResult(exchange).setBody(outputData.inputStream());
83          } else if (inputData instanceof Reader) {
84              ReadableStringWriter outputData = new ReadableStringWriter();
85              doProcess((Reader)inputData, outputData, inputParams);
86              prepareResult(exchange).setBody(outputData.reader());
87          } else if (inputData instanceof Source) {
88              ReadableStreamResult outputData = new ReadableStreamResult();
89              doProcess((Source)inputData, outputData, inputParams);
90              prepareResult(exchange).setBody(outputData.source());
91          } else if (inputData instanceof String) {
92              prepareResult(exchange).setBody(doProcess((String)inputData, inputParams));
93          } else {
94              throw new IllegalArgumentException(
95                      "input data class not supported: " + inputData.getClass());
96          }
97          
98      }
99      
100     /**
101      * Processes <code>inputData</code> and <code>inputParams</code> and
102      * writes converted result to <code>outputData</code>. Conversion is
103      * delegated to the {@link Converter} set at construction time.
104      * 
105      * @param inputData
106      *            input data
107      * @param outputData
108      *            output data
109      * @param inputParams
110      *            input parameters
111      * @throws IOException
112      *             if a system-level problem occurs
113      */
114     protected void doProcess(InputStream inputData, OutputStream outputData, Object... inputParams) throws IOException {
115         converter.convert(inputData, outputData, inputParams);
116     }
117     
118     /**
119      * Processes <code>inputData</code> and <code>inputParams</code> and
120      * writes converted result to <code>outputData</code>. Conversion is
121      * delegated to the {@link Converter} set at construction time.
122      * 
123      * @param inputData
124      *            input data
125      * @param outputData
126      *            output data
127      * @param inputParams
128      *            input parameters
129      * @throws IOException
130      *             if a system-level problem occurs
131      */
132     protected void doProcess(Reader inputData, Writer outputData, Object... inputParams) throws IOException {
133         converter.convert(inputData, outputData, inputParams);
134     }
135     
136     /**
137      * Processes <code>inputData</code> and <code>inputParams</code> and
138      * writes converted result to <code>outputData</code>. Conversion is
139      * delegated to the {@link Converter} set at construction time.
140      * 
141      * @param inputData
142      *            input data
143      * @param outputData
144      *            output data
145      * @param inputParams
146      *            input parameters
147      * @throws IOException
148      *             if a system-level problem occurs
149      */
150     protected void doProcess(Source inputData, Result outputData, Object... inputParams) throws IOException {
151         converter.convert(inputData, outputData, inputParams);
152     }
153     
154     /**
155      * Processes <code>inputData</code> and <code>inputParams</code> and
156      * returns the converted result. Conversion is delegated to the
157      * {@link Converter} set at construction time.
158      * 
159      * @param inputData
160      *            input data
161      * @param inputParams
162      *            input parameters
163      * @return converted result.
164      */
165     protected String doProcess(String inputData, Object... inputParams) {
166         return converter.convert(inputData, inputParams);
167     }
168     
169 }