View Javadoc
1   /*
2    * Copyright 2011 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.ihe.hl7v2ws.pcd01;
17  
18  import ca.uhn.hl7v2.HL7Exception;
19  import ca.uhn.hl7v2.HapiContext;
20  import ca.uhn.hl7v2.model.Message;
21  import org.apache.camel.builder.RouteBuilder;
22  import org.openehealth.ipf.commons.core.modules.api.ValidationException;
23  import org.openehealth.ipf.commons.ihe.hl7v2.definitions.HapiContextFactory;
24  import org.openehealth.ipf.gazelle.validation.profile.pcd.PcdTransactions;
25  
26  import java.util.Scanner;
27  
28  import static org.openehealth.ipf.platform.camel.ihe.hl7v2ws.Hl7v2WsCamelValidators.pcdValidator;
29  
30  /**
31   * @author Mitko Kolev
32   */
33  public class Pcd01RouteBuilder extends RouteBuilder {
34  
35      public static final Message PCD_01_SPEC_RESPONSE = load(
36              HapiContextFactory.createHapiContext(PcdTransactions.PCD1),
37              "pcd01/pcd01-response.hl7");
38      public static final Message PCD_01_SPEC_RESPONSE_INVALID = load(
39              HapiContextFactory.createHapiContext(PcdTransactions.PCD1),
40              "pcd01/pcd01-response-invalid.hl7");
41  
42      /* (non-Javadoc)
43       * @see org.apache.camel.builder.RouteBuilder#configure()
44       */
45      @Override
46      public void configure() throws Exception {
47  
48          from("pcd-pcd01:devicedata?rejectionHandlingStrategy=#rejectionHandlingStrategy")
49                  .onException(Exception.class)
50                  .maximumRedeliveries(0)
51                  .end()
52                  .transform(constant(PCD_01_SPEC_RESPONSE));
53  
54          from("pcd-pcd01:route_throws_exception?rejectionHandlingStrategy=#rejectionHandlingStrategy")
55                  .throwException(new RuntimeException())
56                  .transform(constant(PCD_01_SPEC_RESPONSE));
57  
58          from("pcd-pcd01:route_unacceptable_response?rejectionHandlingStrategy=#rejectionHandlingStrategy")
59                  .transform(constant(PCD_01_SPEC_RESPONSE_INVALID));
60  
61          from("pcd-pcd01:route_inbound_validation")
62                  .onException(ValidationException.class)
63                  .maximumRedeliveries(0)
64                  .end()
65                  .process(pcdValidator())
66                  .transform(constant(PCD_01_SPEC_RESPONSE));
67  
68          from("pcd-pcd01:route_inbound_and_outbound_validation")
69                  .onException(ValidationException.class)
70                  .maximumRedeliveries(0)
71                  .end()
72                  .process(pcdValidator())
73                  .transform(constant(PCD_01_SPEC_RESPONSE))
74                  .process(pcdValidator());
75      }
76  
77      private static <T extends Message> T load(HapiContext context, String fileName) {
78          try {
79              return (T) context.getPipeParser().parse(
80                      new Scanner(Pcd01RouteBuilder.class.getResourceAsStream("/" + fileName)).useDelimiter("\\A").next());
81          } catch (HL7Exception e) {
82              return null;
83          }
84      }
85  
86  }