Added
This commit is contained in:
parent
7cb3caa285
commit
5b4b7f96eb
|
|
@ -27,18 +27,23 @@ import org.graylog.plugins.pipelineprocessor.ast.functions.FunctionDescriptor;
|
|||
import org.graylog.plugins.pipelineprocessor.ast.functions.ParameterDescriptor;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.BufferedReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.zip.Inflater;
|
||||
import java.util.zip.InflaterInputStream;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
import java.net.URLDecoder;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
||||
|
||||
// @Function(name = "Base64Inflate", description = "Decodes a URL-safe Base64-encoded and GZIP-compressed string")
|
||||
public class Base64InflateFunction extends AbstractFunction<String> {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(Base64InflateFunction.class);
|
||||
|
||||
public static final String NAME = "urlsafe_base64_decode";
|
||||
|
||||
private final ParameterDescriptor<String, String> inputParam = ParameterDescriptor
|
||||
|
|
@ -57,30 +62,28 @@ public class Base64InflateFunction extends AbstractFunction<String> {
|
|||
.build();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String evaluate(FunctionArgs args, EvaluationContext context) {
|
||||
final String input = inputParam.required(args, context);
|
||||
|
||||
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 outputStream.toString(StandardCharsets.UTF_8.name());
|
||||
LOG.debug("Received input for Base64Inflate: {}", input);
|
||||
String cleanInput = URLDecoder.decode(input, StandardCharsets.UTF_8.name());
|
||||
byte[] decoded = Base64.getUrlDecoder().decode(cleanInput);
|
||||
try (GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(decoded))) {
|
||||
byte[] uncompressed = gis.readAllBytes();
|
||||
String result = new String(uncompressed, StandardCharsets.UTF_8);
|
||||
LOG.debug("Decompressed result: {}", result);
|
||||
return result;
|
||||
}
|
||||
} catch (IOException | IllegalArgumentException e) {
|
||||
e.printStackTrace();
|
||||
} catch (Exception e) {
|
||||
LOG.error("Base64Inflate failed: {}", e.getMessage(), e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
Binary file not shown.
Loading…
Reference in New Issue