1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
32
33
34
35
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
52
53
54
55
56
57 public void setCamelContext(CamelContext camelContext) {
58 this.camelContext = camelContext;
59 }
60
61
62
63
64
65
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 }