View Javadoc
1   /*
2    * Copyright 2009 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 org.apache.camel.Exchange;
19  import org.apache.camel.component.mina2.Mina2Constants;
20  import org.openehealth.ipf.commons.audit.AuditContext;
21  import org.openehealth.ipf.commons.audit.model.AuditMessage;
22  import org.openehealth.ipf.commons.ihe.hl7v2.audit.MllpAuditUtils;
23  import org.openehealth.ipf.platform.camel.ihe.core.InterceptorSupport;
24  import org.openehealth.ipf.platform.camel.ihe.mllp.core.MllpAuthenticationFailure;
25  import org.openehealth.ipf.platform.camel.ihe.mllp.core.MllpTransactionEndpoint;
26  
27  import java.net.InetSocketAddress;
28  
29  import static java.util.Objects.requireNonNull;
30  
31  /**
32   * Interceptor that handles any {@link MllpAuthenticationFailure} that occurred while
33   * processing an exchange.
34   */
35  public class ConsumerAuthenticationFailureInterceptor extends InterceptorSupport<MllpTransactionEndpoint<?>> {
36  
37      private final AuditContext auditContext;
38  
39      public ConsumerAuthenticationFailureInterceptor(AuditContext auditContext) {
40          this.auditContext = requireNonNull(auditContext);
41      }
42  
43      @Override
44      public void process(Exchange exchange) throws Exception {
45          try {
46              getWrappedProcessor().process(exchange);
47          } catch (MllpAuthenticationFailure e) {
48              AuditMessage auditMessage =
49                      MllpAuditUtils.auditAuthenticationNodeFailure(
50                              auditContext, e.getMessage(), getRemoteAddress(exchange));
51              auditContext.audit(auditMessage);
52              throw e;
53          }
54      }
55  
56      private String getRemoteAddress(Exchange exchange) {
57          InetSocketAddress address = (InetSocketAddress) exchange.getIn().getHeader(Mina2Constants.MINA_REMOTE_ADDRESS);
58          return address != null ? address.getAddress().getHostAddress() : "unknown";
59      }
60  
61  }