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 org.openehealth.ipf.commons.audit.AuditContext;
20  import org.openehealth.ipf.commons.audit.utils.AuditUtils;
21  import org.slf4j.Logger;
22  import org.slf4j.LoggerFactory;
23  
24  import java.net.DatagramPacket;
25  import java.net.DatagramSocket;
26  import java.nio.charset.StandardCharsets;
27  
28  /**
29   * Simple UDP sender that opens a new DatagramSocket for every batch of AuditMessages
30   * being sent.
31   * <p>
32   * Note that this implementation disobeys the ATNA specification saying,
33   * that the Secure Application, Secure Node, or Audit Record Forwarder is unable to send the
34   * message to the Audit Record Repository, then the actor shall store the audit record
35   * locally and send it when it is able.
36   * </p>
37   *
38   * @author Christian Ohr
39   * @since 3.5
40   */
41  public class UDPSyslogSenderImpl extends RFC5424Protocol implements AuditTransmissionProtocol {
42  
43      private static final Logger LOG = LoggerFactory.getLogger(UDPSyslogSenderImpl.class);
44      private static final int MAX_DATAGRAM_PACKET_SIZE = 65479;
45  
46      public UDPSyslogSenderImpl() {
47          this(AuditUtils.getLocalHostName(), AuditUtils.getProcessId());
48      }
49  
50      public UDPSyslogSenderImpl(String sendingHost, String sendingProcess) {
51          super(sendingHost, sendingProcess);
52      }
53  
54      @Override
55      public String getTransportName() {
56          return "UDP";
57      }
58  
59      @Override
60      public void send(AuditContext auditContext, String... auditMessages) throws Exception {
61          if (auditMessages != null) {
62              try (DatagramSocket socket = new DatagramSocket()) {
63                  for (String auditMessage : auditMessages) {
64                      byte[] msgBytes = getTransportPayload(auditContext.getSendingApplication(), auditMessage);
65                      LOG.debug("Auditing to {}:{}",
66                              auditContext.getAuditRepositoryAddress().getHostAddress(),
67                              auditContext.getAuditRepositoryPort());
68                      LOG.trace("{}", new String(msgBytes, StandardCharsets.UTF_8));
69                      DatagramPacket packet = new DatagramPacket(
70                              msgBytes,
71                              Math.min(MAX_DATAGRAM_PACKET_SIZE, msgBytes.length),
72                              auditContext.getAuditRepositoryAddress(),
73                              auditContext.getAuditRepositoryPort());
74                      socket.send(packet);
75                  }
76              }
77          }
78      }
79  
80      @Override
81      public void shutdown() {
82  
83      }
84  }