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 outgoing 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 ConsumerOutPayloadLoggerInterceptor extends InterceptorSupport<MllpEndpoint<?,?,?>> {
36      @Delegate private final MllpPayloadLoggerBase base = new MllpPayloadLoggerBase();
37  
38      /**
39       * Instantiation, implicitly using a {@link SpringExpressionResolver}
40       *
41       * @param fileNamePattern file name pattern
42       */
43      public ConsumerOutPayloadLoggerInterceptor(String fileNamePattern) {
44          this(new SpringExpressionResolver(fileNamePattern));
45      }
46  
47      /**
48       * Instantiation, explicitly using a ExpressionResolver instance
49       *
50       * @param resolver ExpressionResolver instance
51       * @since 3.1
52       */
53      public ConsumerOutPayloadLoggerInterceptor(ExpressionResolver resolver) {
54          super();
55          addBefore(ConsumerStringProcessingInterceptor.class.getName());
56          setExpressionResolver(resolver);
57      }
58  
59      @Override
60      public void process(Exchange exchange) throws Exception {
61          getWrappedProcessor().process(exchange);
62          if (canProcess()) {
63              logPayload(exchange, true);
64          }
65      }
66  
67      public static class Factory extends InterceptorFactorySupport<MllpEndpoint<?,?,?>, ConsumerOutPayloadLoggerInterceptor> {
68  
69          private final ExpressionResolver resolver;
70          private boolean locallyEnabled = true;
71  
72          public Factory(String fileNamePattern) {
73              this(new SpringExpressionResolver(fileNamePattern));
74          }
75  
76          public Factory(ExpressionResolver resolver) {
77              super(ConsumerOutPayloadLoggerInterceptor.class);
78              this.resolver = resolver;
79          }
80  
81          @Override
82          public ConsumerOutPayloadLoggerInterceptor getNewInstance() {
83              ConsumerOutPayloadLoggerInterceptor interceptor = new ConsumerOutPayloadLoggerInterceptor(resolver);
84              interceptor.setLocallyEnabled(locallyEnabled);
85              return interceptor;
86          }
87  
88          public void setLocallyEnabled(boolean locallyEnabled) {
89              this.locallyEnabled = locallyEnabled;
90          }
91      }
92  }