This commit is contained in:
Vadim Shulkin 2025-05-08 15:11:26 -04:00
parent feeb4daf6a
commit 973d66a051
4 changed files with 22 additions and 15 deletions

View File

@ -27,11 +27,14 @@ import org.graylog.plugins.pipelineprocessor.ast.functions.FunctionDescriptor;
import org.graylog.plugins.pipelineprocessor.ast.functions.ParameterDescriptor;
import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.zip.GZIPInputStream;
import java.util.zip.Inflater;
import java.util.zip.InflaterInputStream;
import java.net.URLDecoder;
// @Function(name = "Base64Inflate", description = "Decodes a URL-safe Base64-encoded and GZIP-compressed string")
public class Base64InflateFunction extends AbstractFunction<String> {
@ -57,21 +60,25 @@ public class Base64InflateFunction extends AbstractFunction<String> {
@Override
public String evaluate(FunctionArgs args, EvaluationContext context) {
final String input = inputParam.required(args, context);
try {
byte[] decoded = Base64.getUrlDecoder().decode(input);
try (GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(decoded));
InputStreamReader isr = new InputStreamReader(gis, StandardCharsets.UTF_8);
BufferedReader br = new BufferedReader(isr)) {
StringBuilder out = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
out.append(line);
try {
String urlDecoded = URLDecoder.decode(input, StandardCharsets.UTF_8.name());
byte[] base64Decoded = Base64.getDecoder().decode(urlDecoded);
try (InflaterInputStream inflater = new InflaterInputStream(
new ByteArrayInputStream(base64Decoded), new Inflater(true));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
byte[] buffer = new byte[1024];
int len;
while ((len = inflater.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
return out.toString();
return outputStream.toString(StandardCharsets.UTF_8.name());
}
} catch (Exception e) {
// You may choose to log this or return null
} catch (IOException | IllegalArgumentException e) {
e.printStackTrace();
return null;
}
}