View Javadoc
1   /*
2    * Copyright 2018 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.pcc44;
18  
19  import ca.uhn.fhir.context.FhirContext;
20  import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
21  import org.apache.camel.Exchange;
22  import org.apache.camel.builder.RouteBuilder;
23  import org.apache.camel.support.ExpressionAdapter;
24  import org.hl7.fhir.dstu3.model.Bundle;
25  import org.openehealth.ipf.platform.camel.ihe.fhir.test.FhirTestContainer;
26  
27  import java.io.InputStreamReader;
28  import java.util.stream.Collectors;
29  
30  /**
31   *
32   */
33  public class Pcc44TestRouteBuilder extends RouteBuilder {
34  
35      private final boolean returnError;
36  
37      public Pcc44TestRouteBuilder(boolean returnError) {
38          this.returnError = returnError;
39      }
40  
41      @Override
42      public void configure() {
43  
44          from("direct:input")
45                  .toF("qedm-pcc44:localhost:%d", FhirTestContainer.DEMO_APP_PORT);
46  
47          from("qedm-pcc44:translation?audit=true&options=OBSERVATIONS")
48                  .errorHandler(noErrorHandler())
49                  .transform(new Pcc44Responder());
50      }
51  
52  
53      private class Pcc44Responder extends ExpressionAdapter {
54  
55          @Override
56          public Object evaluate(Exchange exchange) {
57              if (!returnError) {
58                  Bundle resource = FhirContext.forDstu3().newXmlParser()
59                          .parseResource(Bundle.class, new InputStreamReader(getClass().getResourceAsStream("/ObservationResponse.xml")));
60                  // The endpoint expects a list of resources rather than a bundle
61                  return resource.getEntry().stream()
62                          .map(Bundle.BundleEntryComponent::getResource)
63                          .collect(Collectors.toList());
64              } else {
65                  throw new InternalErrorException("Something went wrong");
66              }
67          }
68      }
69  
70  }