View Javadoc
1   /*
2    * Copyright 2018 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  
17  package org.openehealth.ipf.commons.audit.protocol;
18  
19  import io.vertx.core.Vertx;
20  import io.vertx.core.buffer.Buffer;
21  import io.vertx.core.buffer.impl.BufferImpl;
22  import io.vertx.core.datagram.DatagramSocket;
23  import io.vertx.core.datagram.DatagramSocketOptions;
24  import org.openehealth.ipf.commons.audit.AuditContext;
25  import org.openehealth.ipf.commons.audit.utils.AuditUtils;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  import java.nio.charset.StandardCharsets;
30  
31  /**
32   * NIO implemention of a UDP Syslog sender by using an embedded Vert.x instance.
33   * There is no obvious performance improvement compared to {@link UDPSyslogSenderImpl}.
34   *
35   * @author Christian Ohr
36   * @since 3.5
37   */
38  public class VertxUDPSyslogSenderImpl extends RFC5424Protocol implements AuditTransmissionProtocol {
39  
40      private static final Logger LOG = LoggerFactory.getLogger(VertxUDPSyslogSenderImpl.class);
41  
42      private final Vertx vertx;
43  
44      public VertxUDPSyslogSenderImpl() {
45          this(Vertx.vertx());
46      }
47  
48      public VertxUDPSyslogSenderImpl(Vertx vertx) {
49          super(AuditUtils.getLocalHostName(), AuditUtils.getProcessId());
50          this.vertx = vertx;
51      }
52  
53      @Override
54      public void send(AuditContext auditContext, String... auditMessages) {
55          DatagramSocketOptions options = new DatagramSocketOptions()
56                  .setSendBufferSize(16384);
57          DatagramSocket socket = vertx.createDatagramSocket(options);
58          if (auditMessages != null) {
59              for (String auditMessage : auditMessages) {
60  
61                  // Could use a Vertx codec for this
62                  byte[] msgBytes = getTransportPayload(auditContext.getSendingApplication(), auditMessage);
63                  LOG.debug("Auditing to {}:{}",
64                          auditContext.getAuditRepositoryAddress().getHostAddress(),
65                          auditContext.getAuditRepositoryPort());
66                  LOG.trace("{}", new String(msgBytes, StandardCharsets.UTF_8));
67                  Buffer buffer = new BufferImpl().appendBytes(msgBytes);
68  
69                  // The net socket has registered itself on the Vertx EventBus
70                  socket.send(buffer,
71                          auditContext.getAuditRepositoryPort(),
72                          auditContext.getAuditRepositoryAddress().getHostAddress(),
73                          event -> {
74                              if (event.failed()) {
75                                  LOG.warn("Sending Audit Event via UDP failed");
76                              }
77                          });
78              }
79          }
80      }
81  
82      @Override
83      public String getTransportName() {
84          return "NIO-UDP";
85      }
86  
87      @Override
88      public void shutdown() {
89          vertx.close();
90      }
91  
92  
93  }