From 3e21ea395e526adf0b740d55eb8e1755ee8324c4 Mon Sep 17 00:00:00 2001 From: Mark Nellemann Date: Tue, 6 Dec 2022 10:15:23 +0100 Subject: [PATCH] Update dependencies and build container. --- build.gradle | 12 +- gradle.properties | 2 +- .../biz/nellemann/syslogd/Application.java | 3 +- .../biz/nellemann/syslogd/SyslogPrinter.java | 2 +- .../biz/nellemann/syslogd/net/UdpClient.java | 14 +- .../nellemann/syslogd/parser/JsonUtil.java | 158 +++++++++--------- 6 files changed, 94 insertions(+), 97 deletions(-) diff --git a/build.gradle b/build.gradle index 6f7e2a0..b783c2b 100644 --- a/build.gradle +++ b/build.gradle @@ -12,14 +12,14 @@ repositories { } dependencies { - annotationProcessor 'info.picocli:picocli-codegen:4.6.3' - implementation 'info.picocli:picocli:4.6.3' - implementation 'org.slf4j:slf4j-api:2.0.3' - implementation 'org.slf4j:slf4j-simple:2.0.3' + annotationProcessor 'info.picocli:picocli-codegen:4.7.0' + implementation 'info.picocli:picocli:4.7.0' + implementation 'org.slf4j:slf4j-api:2.0.5' + implementation 'org.slf4j:slf4j-simple:2.0.5' testImplementation 'org.spockframework:spock-core:2.3-groovy-3.0' - testImplementation 'org.slf4j:slf4j-api:2.0.3' - testRuntimeOnly 'org.slf4j:slf4j-simple:2.0.3' + testImplementation 'org.slf4j:slf4j-api:2.0.5' + testRuntimeOnly 'org.slf4j:slf4j-simple:2.0.5' } application { diff --git a/gradle.properties b/gradle.properties index 27ee296..dfbba86 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,3 @@ id = syslogd group = biz.nellemann.syslogd -version = 1.2.7 +version = 1.2.8 diff --git a/src/main/java/biz/nellemann/syslogd/Application.java b/src/main/java/biz/nellemann/syslogd/Application.java index cab428a..c4c0619 100644 --- a/src/main/java/biz/nellemann/syslogd/Application.java +++ b/src/main/java/biz/nellemann/syslogd/Application.java @@ -168,8 +168,7 @@ public class Application implements Callable, LogReceiveListener { private InetSocketAddress getInetSocketAddress(URI input) { - InetSocketAddress inetSocketAddress = new InetSocketAddress(input.getHost(), input.getPort()); - return inetSocketAddress; + return new InetSocketAddress(input.getHost(), input.getPort()); } diff --git a/src/main/java/biz/nellemann/syslogd/SyslogPrinter.java b/src/main/java/biz/nellemann/syslogd/SyslogPrinter.java index f0cc824..631a4ce 100644 --- a/src/main/java/biz/nellemann/syslogd/SyslogPrinter.java +++ b/src/main/java/biz/nellemann/syslogd/SyslogPrinter.java @@ -133,7 +133,7 @@ public class SyslogPrinter { sb.append(String.format(" \"level\": \"%s\",", msg.severity)); sb.append(String.format(" \"application\": \"%s\"", msg.application)); sb.append("}, \"values\": [ "); - sb.append(String.format("[ \"%d\", \"%s\" ]", msg.timestamp.getEpochSecond() * 1000000000l, getMessageLine(msg))); + sb.append(String.format("[ \"%d\", \"%s\" ]", msg.timestamp.getEpochSecond() * 1000000000L, getMessageLine(msg))); sb.append(" ] } ] }"); log.debug(sb.toString()); return sb.toString(); diff --git a/src/main/java/biz/nellemann/syslogd/net/UdpClient.java b/src/main/java/biz/nellemann/syslogd/net/UdpClient.java index c99610e..ea6efa0 100644 --- a/src/main/java/biz/nellemann/syslogd/net/UdpClient.java +++ b/src/main/java/biz/nellemann/syslogd/net/UdpClient.java @@ -29,8 +29,8 @@ public class UdpClient implements LogForwardListener { private final static Logger log = LoggerFactory.getLogger(UdpClient.class); - private InetSocketAddress inetSocketAddress; - private DatagramSocket socket; + private final InetSocketAddress inetSocketAddress; + private final DatagramSocket socket; public UdpClient(InetSocketAddress inetSocketAddress) throws SocketException { this.inetSocketAddress = inetSocketAddress; @@ -40,12 +40,10 @@ public class UdpClient implements LogForwardListener { public void send(String msg) { byte[] buf = msg.getBytes(StandardCharsets.US_ASCII); DatagramPacket packet = new DatagramPacket(buf, buf.length, inetSocketAddress.getAddress(), inetSocketAddress.getPort()); - if(this.socket != null) { - try { - socket.send(packet); - } catch (IOException e) { - log.error("send() - Could not send packet: " + e.getMessage()); - } + try { + socket.send(packet); + } catch (IOException e) { + log.error("send() - Could not send packet: " + e.getMessage()); } } diff --git a/src/main/java/biz/nellemann/syslogd/parser/JsonUtil.java b/src/main/java/biz/nellemann/syslogd/parser/JsonUtil.java index 14882fa..71e86ef 100644 --- a/src/main/java/biz/nellemann/syslogd/parser/JsonUtil.java +++ b/src/main/java/biz/nellemann/syslogd/parser/JsonUtil.java @@ -6,92 +6,92 @@ package biz.nellemann.syslogd.parser; public class JsonUtil { - public static String encode(String input) { + public static String encode(String input) { - StringBuilder output = new StringBuilder(); + StringBuilder output = new StringBuilder(); - for(int i=0; i 127) { - output.append(String.format("\\u%04x", chx)); - } else { - output.append(ch); - } - } - - return output.toString(); - } - - - public static String decode(String input) { - - StringBuilder builder = new StringBuilder(); - - int i = 0; - while (i < input.length()) { - char delimiter = input.charAt(i); i++; // consume letter or backslash - - if(delimiter == '\\' && i < input.length()) { - - // consume first after backslash - char ch = input.charAt(i); i++; - - if(ch == '\\' || ch == '/' || ch == '"' || ch == '\'') { - builder.append(ch); - } - else if(ch == 'n') builder.append('\n'); - else if(ch == 'r') builder.append('\r'); - else if(ch == 't') builder.append('\t'); - else if(ch == 'b') builder.append('\b'); - else if(ch == 'f') builder.append('\f'); - else if(ch == 'u') { - - StringBuilder hex = new StringBuilder(); - - // expect 4 digits - if (i+4 > input.length()) { - throw new RuntimeException("Not enough unicode digits! "); - } - for (char x : input.substring(i, i + 4).toCharArray()) { - if(!Character.isLetterOrDigit(x)) { - throw new RuntimeException("Bad character in unicode escape."); + // let's not put any nulls in our strings + if ((int) ch == 0) { + continue; } - hex.append(Character.toLowerCase(x)); - } - i+=4; // consume those four digits. - int code = Integer.parseInt(hex.toString(), 16); - builder.append((char) code); - } else { - throw new RuntimeException("Illegal escape sequence: \\"+ch); + if (ch == '\n') { + output.append("\\n"); + } else if (ch == '\t') { + output.append("\\t"); + } else if (ch == '\r') { + output.append("\\r"); + } else if (ch == '\\') { + output.append("\\\\"); + } else if (ch == '"') { + output.append("\\\""); + } else if (ch == '\b') { + output.append("\\b"); + } else if (ch == '\f') { + output.append("\\f"); + } else if ((int) ch > 127) { + output.append(String.format("\\u%04x", (int) ch)); + } else { + output.append(ch); + } } - } else { // it's not a backslash, or it's the last character. - builder.append(delimiter); - } + + return output.toString(); } - return builder.toString(); - } + + public static String decode(String input) { + + StringBuilder builder = new StringBuilder(); + + int i = 0; + while (i < input.length()) { + char delimiter = input.charAt(i); + i++; // consume letter or backslash + + if (delimiter == '\\' && i < input.length()) { + + // consume first after backslash + char ch = input.charAt(i); + i++; + + if (ch == '\\' || ch == '/' || ch == '"' || ch == '\'') { + builder.append(ch); + } else if (ch == 'n') builder.append('\n'); + else if (ch == 'r') builder.append('\r'); + else if (ch == 't') builder.append('\t'); + else if (ch == 'b') builder.append('\b'); + else if (ch == 'f') builder.append('\f'); + else if (ch == 'u') { + + StringBuilder hex = new StringBuilder(); + + // expect 4 digits + if (i + 4 > input.length()) { + throw new RuntimeException("Not enough unicode digits! "); + } + for (char x : input.substring(i, i + 4).toCharArray()) { + if (!Character.isLetterOrDigit(x)) { + throw new RuntimeException("Bad character in unicode escape."); + } + hex.append(Character.toLowerCase(x)); + } + i += 4; // consume those four digits. + + int code = Integer.parseInt(hex.toString(), 16); + builder.append((char) code); + } else { + throw new RuntimeException("Illegal escape sequence: \\" + ch); + } + } else { // it's not a backslash, or it's the last character. + builder.append(delimiter); + } + } + + return builder.toString(); + } }