in appender/rest/src/main/java/org/apache/karaf/decanter/appender/rest/RestAppender.java [85:130]
public void handleEvent(Event event) {
if (EventFilter.match(event, config)) {
try {
HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection();
connection.setDoOutput(true);
connection.setInstanceFollowRedirects(false);
String user = config.get("user") != null ? (String) config.get("user") : null;
String password = config.get("password") != null ? (String) config.get("password") : null;
if (user != null) {
String authentication = user + ":" + password;
byte[] encodedAuthentication = Base64.getEncoder().encode(authentication.getBytes(StandardCharsets.UTF_8));
String authenticationHeader = "Basic " + new String(encodedAuthentication);
connection.setRequestProperty("Authorization", authenticationHeader);
}
String requestMethod = config.get("request.method") != null ? (String) config.get("request.method") : "POST";
connection.setRequestMethod(requestMethod);
String contentType = config.get("content.type") != null ? (String) config.get("content.type") : "application/json";
connection.setRequestProperty("Content-Type", contentType);
String charset = config.get("charset") != null ? (String) config.get("charset") : "utf-8";
connection.setRequestProperty("charset", charset);
Enumeration<String> keys = config.keys();
while (keys.hasMoreElements()) {
String key = keys.nextElement();
if (key.startsWith("header.")) {
connection.setRequestProperty(key.substring("header.".length()), (String) config.get(key));
}
}
String payloadHeader = config.get("payload.header") != null ? (String) config.get("payload.header") : null;
if (payloadHeader != null) {
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
marshaller.marshal(event, out);
connection.setRequestProperty(payloadHeader, out.toString());
}
} else {
try (OutputStream out = connection.getOutputStream()) {
marshaller.marshal(event, out);
}
}
InputStream is = connection.getInputStream();
is.read();
is.close();
} catch (Exception e) {
LOGGER.warn("Error sending event to rest service", e);
}
}
}