in collector/rest/src/main/java/org/apache/karaf/decanter/collector/rest/RestCollector.java [97:170]
public void run() {
LOGGER.debug("Karaf Decanter REST Collector starts harvesting from {} ...", url);
for (String path : paths) {
URL urlWithPath;
try {
urlWithPath = new URL(url, path);
} catch (MalformedURLException e) {
LOGGER.warn("Invalid URL. Stopping collector", e);
throw new IllegalArgumentException(e);
}
LOGGER.debug("\tpath {}", urlWithPath);
HttpURLConnection connection = null;
Map<String, Object> data = new HashMap<>();
try {
connection = (HttpURLConnection) urlWithPath.openConnection();
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);
}
connection.setRequestMethod(requestMethod);
if ((requestMethod.equalsIgnoreCase("POST") || requestMethod.equalsIgnoreCase("PUT")) && request != null) {
connection.setDoInput(true);
connection.setDoOutput(true);
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()))) {
writer.write(request);
}
}
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));
}
}
data.putAll(unmarshaller.unmarshal(connection.getInputStream()));
data.put("http.response.code", connection.getResponseCode());
data.put("http.response.message", connection.getResponseMessage());
data.put("type", "rest");
data.put("url", urlWithPath);
PropertiesPreparator.prepare(data, config);
data.put("service.hostName", url.getHost());
} catch (Exception e) {
LOGGER.warn("Can't request REST service", e);
if (exceptionAsHttpResponse) {
if (exceptionHttpResponseCode != null) {
data.put("http.response.code", exceptionHttpResponseCode);
} else {
try {
data.put("http.response.code", connection.getResponseCode());
} catch (Exception ie) {
// no-op
}
}
data.put("http.exception", e.getClass().getName() + ": " + e.getMessage());
data.put("type", "rest");
data.put("url", urlWithPath);
} else {
data.put("type", "rest");
data.put("url", urlWithPath);
data.put("error", e.getClass().getName() + ": " + e.getMessage());
}
} finally {
if (connection != null) {
connection.disconnect();
}
}
dispatcher.postEvent(new Event(topic, data));
}
LOGGER.debug("Karaf Decanter REST Collector harvesting done");
}