/*
 * Copyright 2017 the original author or authors.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.openehealth.ipf.commons.audit.protocol;


import java.nio.charset.StandardCharsets;
import java.time.Instant;

import static java.util.Objects.requireNonNull;

/**
 * Base client implementation of RFC 5424 syslog for sending audit messages to an Audit Record Repository
 * that implements RFC 5424 SYSLOG.
 *
 * @author Christian Ohr
 * @since 3.5
 */
public class RFC5424Protocol {

    /**
     * Default syslog priority for this transport, according to
     * http://dicom.nema.org/medical/dicom/current/output/html/part15.html#sect_A.6
     */
    private static final int TRANSPORT_PRI = 10 * 8 + 5;

    /**
     * Default syslog MSGID for this transport, according to
     * http://dicom.nema.org/medical/dicom/current/output/html/part15.html#sect_A.6
     */
    private static final String TRANSPORT_MSGID = "IHE+RFC-3881";

    private final String senderHostName;
    private final String senderProcessId;

    public RFC5424Protocol(String senderHostName, String senderProcessId) {
        this.senderHostName = requireNonNull(senderHostName);
        this.senderProcessId = requireNonNull(senderProcessId);
    }

    /**
     * Serialize the syslog message payload body for sending by this transport
     *
     * @param auditMessage Message to prepare
     * @return serialized message
     */
    protected byte[] getTransportPayload(String sendingApplication, String auditMessage) {
        String msg = String.format("<%s>1 %s %s %s %s %s - \uFEFF<?xml version=\"1.0\" encoding=\"UTF-8\"?>%s",
                TRANSPORT_PRI,
                Instant.now(),
                senderHostName,
                sendingApplication.replace(' ', '_'),
                senderProcessId,
                TRANSPORT_MSGID,
                auditMessage);
        return msg.trim().getBytes(StandardCharsets.UTF_8);
    }

}

@@ -741,16 +741,94 @@
 Instant;
+%0Aimport java.time.temporal.ChronoUnit;%0Aimport java.time.temporal.TemporalUnit;
 %0A%0Aimport
@@ -1544,16 +1544,231 @@
 3881%22;%0A%0A
+    /**%0A     * Precision of the timestamp. Note that RFC5424 only allows 6 subsecond digits%0A     * which corresponds to %7B@link ChronoUnit#MICROS%7D%0A     */%0A    public static ChronoUnit precision = ChronoUnit.MILLIS;%0A%0A
     priv
@@ -2468,16 +2468,16 @@
 RT_PRI,%0A
-
         
@@ -2497,16 +2497,39 @@
 nt.now()
+.truncatedTo(precision)
 ,%0A      

Set timestamp precision of RFC5424 audit to milliseconds (configurable) See https://mail.google.com/mail/u/0/#category/forums/FMfcgxwHMGMcqdMTTglqBnflVDNHphxK for details