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.atna.util;
17  
18  import org.apache.camel.CamelContext;
19  import org.apache.camel.ProducerTemplate;
20  import org.openehealth.ipf.commons.audit.AuditContext;
21  import org.openehealth.ipf.commons.audit.model.AuditMessage;
22  import org.openehealth.ipf.commons.audit.queue.AuditMessageQueue;
23  
24  import java.net.InetAddress;
25  import java.net.URI;
26  import java.net.URISyntaxException;
27  import java.net.UnknownHostException;
28  import java.util.HashMap;
29  
30  /**
31   * An audit message sender that sends audit messages to a configured Camel
32   * endpoint. When configured in a Spring application context, it is attempted to
33   * auto-wire the required Camel context.
34   *
35   * @author Martin Krasser
36   */
37  public class CamelAuditMessageQueue implements AuditMessageQueue {
38  
39      public static final String HEADER_NAMESPACE = "org.openehealth.ipf.platform.camel.ihe.atna";
40  
41      private CamelContext camelContext;
42  
43      private ProducerTemplate producerTemplate;
44  
45      private String endpointUriString;
46      private URI endpointUriObject;
47      private InetAddress destinationAddress;
48      private int destinationPort;
49  
50      /**
51       * Sets the Camel context that contains the endpoint to send audit messages
52       * to. This method needs only be used for setting the Camel context if
53       * auto-wiring is not possible (e.g. in certain test environments).
54       *
55       * @param camelContext
56       */
57      public void setCamelContext(CamelContext camelContext) {
58          this.camelContext = camelContext;
59      }
60  
61      /**
62       * The endpoint URI to send audit messages to.
63       *
64       * @param endpointUri
65       * @throws URISyntaxException
66       */
67      public void setEndpointUri(String endpointUri) throws URISyntaxException, UnknownHostException {
68          endpointUriString = endpointUri;
69          endpointUriObject = new URI(endpointUri);
70          String host = endpointUriObject.getHost();
71          destinationAddress = InetAddress.getByName(host == null ? "0.0.0.0" : host);
72          destinationPort = endpointUriObject.getPort();
73      }
74  
75      public void init() throws Exception {
76          producerTemplate = camelContext.createProducerTemplate();
77          producerTemplate.start();
78      }
79  
80      public void destroy() throws Exception {
81          producerTemplate.stop();
82      }
83  
84  
85      @Override
86      public void audit(AuditContext auditContext, AuditMessage... auditMessages) {
87          for (AuditMessage m : auditMessages) {
88              HashMap<String, Object> headers = new HashMap<>();
89              headers.put(HEADER_NAMESPACE + ".destination.address", destinationAddress.getHostAddress());
90              headers.put(HEADER_NAMESPACE + ".destination.port", destinationPort);
91              producerTemplate.sendBodyAndHeaders(endpointUriString, m, headers);
92          }
93      }
94  
95      private InetAddress getDestinationAddress() throws UnknownHostException {
96          String host = endpointUriObject.getHost();
97          return InetAddress.getByName(host == null ? "0.0.0.0" : host);
98      }
99  
100 }