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.iti68;
18  
19  import org.apache.camel.Exchange;
20  import org.apache.camel.builder.RouteBuilder;
21  import org.apache.camel.component.http4.HttpMethods;
22  import org.apache.camel.support.ExpressionAdapter;
23  import org.openehealth.ipf.commons.ihe.fhir.iti68.Iti68AuditDataset;
24  import org.openehealth.ipf.platform.camel.ihe.atna.interceptor.AuditInterceptor;
25  import org.openehealth.ipf.platform.camel.ihe.ws.StandardTestContainer;
26  
27  import java.io.ByteArrayInputStream;
28  import java.util.Arrays;
29  
30  /**
31   *
32   */
33  public class Iti68TestRouteBuilder extends RouteBuilder {
34  
35      static final String DOCUMENT_UNIQUE_ID = "documentUniqueId";
36      static final String HOME_COMMUNITY_ID = "homeCommunityId";
37      static final String REPOSITORY_ID = "repositoryId";
38  
39      static final byte[] DATA;
40  
41      static {
42          DATA = new byte[10000];
43          Arrays.fill(DATA, (byte) 'X');
44      }
45  
46      private final boolean returnError;
47  
48      public Iti68TestRouteBuilder(boolean returnError) {
49          this.returnError = returnError;
50      }
51  
52      @Override
53      public void configure() throws Exception {
54  
55          from("direct:input")
56                  .setHeader(Exchange.HTTP_METHOD, constant(HttpMethods.GET))
57                  .toF("http4:localhost:%d", StandardTestContainer.DEMO_APP_PORT);
58  
59          from("mhd-iti68:download?audit=true")
60                  .errorHandler(noErrorHandler())
61                  .transform(new Iti68Responder());
62      }
63  
64  
65      private class Iti68Responder extends ExpressionAdapter {
66  
67          @Override
68          public Object evaluate(Exchange exchange) {
69              Iti68AuditDataset auditDataset = exchange.getIn().getHeader(AuditInterceptor.AUDIT_DATASET_HEADER, Iti68AuditDataset.class);
70              auditDataset.setDocumentUniqueId(DOCUMENT_UNIQUE_ID);
71              auditDataset.setHomeCommunityId(HOME_COMMUNITY_ID);
72              auditDataset.setRepositoryUniqueId(REPOSITORY_ID);
73              if (!returnError) {
74                  return new ByteArrayInputStream(DATA);
75              } else {
76                  throw new RuntimeException("Something went wrong");
77              }
78          }
79      }
80  
81  }