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.iti65;
18  
19  import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
20  import org.apache.camel.Exchange;
21  import org.apache.camel.builder.RouteBuilder;
22  import org.apache.camel.support.ExpressionAdapter;
23  import org.hl7.fhir.dstu3.model.*;
24  import org.openehealth.ipf.platform.camel.core.adapter.ValidatorAdapter;
25  import org.openehealth.ipf.platform.camel.ihe.fhir.test.FhirTestContainer;
26  
27  import java.util.Date;
28  import java.util.UUID;
29  
30  import static org.openehealth.ipf.platform.camel.ihe.fhir.core.FhirCamelValidators.*;
31  
32  /**
33   *
34   */
35  public class Iti65TestRouteBuilder extends RouteBuilder {
36  
37      private final boolean returnError;
38  
39      public Iti65TestRouteBuilder(boolean returnError) {
40          this.returnError = returnError;
41      }
42  
43      @Override
44      public void configure() throws Exception {
45  
46          from("direct:input")
47                  .toF("mhd-iti65:localhost:%d", FhirTestContainer.DEMO_APP_PORT);
48  
49          from("mhd-iti65:stub?audit=true")
50                  .errorHandler(noErrorHandler())
51                  .setHeader(ValidatorAdapter.NEED_VALIDATION_HEADER_NAME, constant(true))
52                  .setHeader(VALIDATION_MODE, constant(SCHEMA | SCHEMATRON | MODEL))
53                  .process(itiRequestValidator())
54                  .transform(new Responder());
55      }
56  
57  
58      private class Responder extends ExpressionAdapter {
59  
60          @Override
61          public Object evaluate(Exchange exchange) {
62  
63              if (returnError) throw new InternalErrorException("Something went wrong");
64  
65              Bundle requestBundle = exchange.getIn().getBody(Bundle.class);
66  
67              Bundle responseBundle = new Bundle()
68                      .setType(Bundle.BundleType.TRANSACTIONRESPONSE)
69                      .setTotal(requestBundle.getTotal());
70  
71              for (Bundle.BundleEntryComponent requestEntry : requestBundle.getEntry()) {
72                  Bundle.BundleEntryResponseComponent response = new Bundle.BundleEntryResponseComponent()
73                          .setStatus("201 Created")
74                          .setLastModified(new Date())
75                          .setLocation(requestEntry.getResource().getClass().getSimpleName() + "/" + 4711);
76                  responseBundle.addEntry()
77                          .setResponse(response)
78                          .setResource(responseResource(requestEntry.getResource()));
79              }
80              return responseBundle;
81          }
82  
83      }
84  
85      private Resource responseResource(Resource request) {
86          if (request instanceof DocumentManifest) {
87              return new DocumentManifest().setId(UUID.randomUUID().toString());
88          } else if (request instanceof DocumentReference) {
89              return new DocumentReference().setId(UUID.randomUUID().toString());
90          } else if (request instanceof ListResource) {
91              return new ListResource().setId(UUID.randomUUID().toString());
92          } else if (request instanceof Binary) {
93              return new Binary().setId(UUID.randomUUID().toString());
94          } else {
95              throw new IllegalArgumentException(request + " is not allowed here");
96          }
97      }
98  
99  
100 }