View Javadoc
1   /*
2    * Copyright 2012 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.hl7v2.intercept.consumer;
17  
18  import org.apache.camel.Exchange;
19  import org.apache.commons.lang3.Validate;
20  import org.openehealth.ipf.platform.camel.core.util.Exchanges;
21  import org.openehealth.ipf.platform.camel.ihe.core.InterceptorSupport;
22  import org.openehealth.ipf.platform.camel.ihe.hl7v2.HL7v2Endpoint;
23  
24  /**
25   * Consumer-side HL7v2 interceptor which echoes one segment from
26   * the request message in the corresponding response message.
27   * When multiple segments with the same name exist in the request
28   * and/or response message, the first occurrence will be processed.
29   *
30   * @author Dmytro Rud
31   */
32  public class ConsumerSegmentEchoingInterceptor extends InterceptorSupport<HL7v2Endpoint> {
33      private final String segmentName;
34  
35  
36      /**
37       * @param segmentName
38       *      name of the segment to be echoed.
39       */
40      public ConsumerSegmentEchoingInterceptor(String segmentName) {
41          this.segmentName = Validate.notEmpty(segmentName);
42          addBefore(ConsumerMarshalInterceptor.class.getName());
43      }
44  
45  
46      @Override
47      public void process(Exchange exchange) throws Exception {
48          // determine segment boundaries in the request message
49          String request = exchange.getIn().getBody(String.class);
50          int[] requestQpdBoundaries = getQpdBoundaries(request);
51  
52          // run the route
53          getWrappedProcessor().process(exchange);
54  
55          // replace the segment in the response message by the one from the request
56          if (requestQpdBoundaries != null) {
57              String response = Exchanges.resultMessage(exchange).getBody(String.class);
58              int[] responseQpdBoundaries = getQpdBoundaries(response);
59              if (responseQpdBoundaries != null) {
60                  Exchanges.resultMessage(exchange).setBody(new StringBuilder()
61                          .append(response, 0, responseQpdBoundaries[0])
62                          .append(request, requestQpdBoundaries[0], requestQpdBoundaries[1])
63                          .append(response, responseQpdBoundaries[1], response.length())
64                          .toString());
65              }
66          }
67      }
68  
69  
70      private int[] getQpdBoundaries(String s) {
71          int pos1 = s.indexOf("\r" + segmentName + s.charAt(3));
72          int pos2 = (pos1 > 0) ? s.indexOf("\r", pos1 + 4) : -1;
73          return (pos2 > 0) ? new int[] {pos1, pos2} : null;
74      }
75  }