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 java.io.InputStream;
19  import java.io.OutputStream;
20  
21  import org.apache.camel.Exchange;
22  import org.apache.camel.spi.DataFormat;
23  import org.openehealth.ipf.commons.core.modules.api.Converter;
24  import org.openehealth.ipf.commons.core.modules.api.Parser;
25  import org.openehealth.ipf.commons.core.modules.api.Renderer;
26  
27  /**
28   * @author Martin Krasser
29   */
30  public class DataFormatAdapter extends AdapterSupport implements DataFormat {
31  
32      private final Parser parser;
33      private final Renderer renderer;
34      
35      public DataFormatAdapter(Parser parser) {
36          this(parser, null);
37      }
38      
39      public DataFormatAdapter(Renderer renderer) {
40          this(null, renderer);
41      }
42      
43      public DataFormatAdapter(Parser parser, Renderer renderer) {
44          this.parser = parser;
45          this.renderer = renderer;
46      }
47      
48      public DataFormatAdapter(Converter converter) {
49          parser = converter;
50          renderer = converter;
51      }
52      
53      @Override
54      public void marshal(Exchange exchange, Object graph, OutputStream stream) throws Exception {
55          Object input = adaptInput(exchange);
56          Object params = adaptParams(exchange);
57          renderer.render(input, stream, params);
58      }
59  
60      @Override
61      public Object unmarshal(Exchange exchange, InputStream stream) throws Exception {
62          Object params = adaptParams(exchange);
63          return parser.parse(stream, params);
64      }
65  
66  }