View Javadoc
1   /*
2    * Copyright 2016 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  
17  package org.openehealth.ipf.platform.camel.ihe.fhir.datamodel;
18  
19  import ca.uhn.fhir.context.FhirContext;
20  import ca.uhn.fhir.parser.IParser;
21  import org.apache.camel.Exchange;
22  import org.apache.camel.spi.DataFormat;
23  import org.apache.camel.util.ExchangeHelper;
24  import org.hl7.fhir.instance.model.api.IBaseResource;
25  import org.openehealth.ipf.commons.ihe.fhir.Constants;
26  
27  import java.io.*;
28  import java.nio.charset.Charset;
29  import java.nio.charset.StandardCharsets;
30  
31  /**
32   *
33   */
34  abstract class FhirDataFormat implements DataFormat {
35  
36      private FhirContext defaultFhirContext = FhirContext.forDstu3();
37      private Charset defaultCharset = StandardCharsets.UTF_8;
38  
39      public void setDefaultFhirContext(FhirContext context) {
40          defaultFhirContext = context;
41      }
42  
43      public void setDefaultCharset(String charset) {
44          defaultCharset = Charset.forName(charset);
45      }
46  
47      @Override
48      public void marshal(Exchange exchange, Object body, OutputStream stream) throws Exception {
49          IBaseResource resource = ExchangeHelper.convertToMandatoryType(exchange, IBaseResource.class, body);
50          Writer writer = new OutputStreamWriter(stream, getCharset(exchange));
51          getParser(getFhirContext(exchange))
52                  .setPrettyPrint(true)
53                  .encodeResourceToWriter(resource, writer);
54      }
55  
56      @Override
57      public Object unmarshal(Exchange exchange, InputStream stream) throws Exception {
58          Reader reader = new InputStreamReader(stream, getCharset(exchange));
59          return getParser(getFhirContext(exchange)).parseResource(reader);
60      }
61  
62      protected FhirContext getFhirContext(Exchange exchange) {
63          FhirContext context = exchange.getIn().getHeader(Constants.FHIR_CONTEXT, FhirContext.class);
64          if (context == null) context = defaultFhirContext;
65          return context;
66      }
67  
68      protected Charset getCharset(Exchange exchange) {
69          Charset charset = defaultCharset;
70          String charsetName = exchange.getProperty(Exchange.CHARSET_NAME, String.class);
71          if (charsetName != null) charset = Charset.forName(charsetName);
72          return charset;
73      }
74  
75      protected abstract IParser getParser(FhirContext context);
76  }