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.mllp.core.intercept.consumer;
17  
18  import lombok.experimental.Delegate;
19  import org.apache.camel.Exchange;
20  import org.openehealth.ipf.commons.ihe.core.payload.ExpressionResolver;
21  import org.openehealth.ipf.commons.ihe.core.payload.SpringExpressionResolver;
22  import org.openehealth.ipf.platform.camel.ihe.core.InterceptorFactorySupport;
23  import org.openehealth.ipf.platform.camel.ihe.core.InterceptorSupport;
24  import org.openehealth.ipf.platform.camel.ihe.mllp.core.MllpEndpoint;
25  import org.openehealth.ipf.platform.camel.ihe.mllp.core.intercept.MllpPayloadLoggerBase;
26  
27  /**
28   * Consumer-side MLLP interceptor which stores incoming payload
29   * into files with user-defined name patterns.
30   * <p/>
31   * Members of {@link MllpPayloadLoggerBase} are mixed into this class.
32   *
33   * @author Dmytro Rud
34   */
35  public class ConsumerInPayloadLoggerInterceptor extends InterceptorSupport<MllpEndpoint<?,?,?>> {
36      @Delegate
37      private final MllpPayloadLoggerBase base = new MllpPayloadLoggerBase();
38  
39      /**
40       * Instantiation, implicitly using a {@link SpringExpressionResolver}
41       *
42       * @param fileNamePattern file name pattern
43       */
44      public ConsumerInPayloadLoggerInterceptor(String fileNamePattern) {
45          this(new SpringExpressionResolver(fileNamePattern));
46      }
47  
48      /**
49       * Instantiation, explicitly using a ExpressionResolver instance
50       *
51       * @param resolver ExpressionResolver instance
52       * @since 3.1
53       */
54      public ConsumerInPayloadLoggerInterceptor(ExpressionResolver resolver) {
55          super();
56          addBefore(ConsumerStringProcessingInterceptor.class.getName());
57          setExpressionResolver(resolver);
58      }
59  
60      @Override
61      public void process(Exchange exchange) throws Exception {
62          if (canProcess()) {
63              logPayload(exchange, false);
64          }
65          getWrappedProcessor().process(exchange);
66      }
67  
68      public static class Factory extends InterceptorFactorySupport<MllpEndpoint<?,?,?>, ConsumerInPayloadLoggerInterceptor> {
69  
70          private final ExpressionResolver resolver;
71          private boolean locallyEnabled = true;
72  
73          public Factory(String fileNamePattern) {
74              this(new SpringExpressionResolver(fileNamePattern));
75          }
76  
77          public Factory(ExpressionResolver resolver) {
78              super(ConsumerInPayloadLoggerInterceptor.class);
79              this.resolver = resolver;
80          }
81  
82          @Override
83          public ConsumerInPayloadLoggerInterceptor getNewInstance() {
84              ConsumerInPayloadLoggerInterceptor interceptor = new ConsumerInPayloadLoggerInterceptor(resolver);
85              interceptor.setLocallyEnabled(locallyEnabled);
86              return interceptor;
87          }
88  
89          public void setLocallyEnabled(boolean locallyEnabled) {
90              this.locallyEnabled = locallyEnabled;
91          }
92      }
93  
94  }