diff --git a/pom.xml b/pom.xml
index a6d80937c..f75221331 100644
--- a/pom.xml
+++ b/pom.xml
@@ -69,6 +69,7 @@
1.1.3
1.5.2.Final
1.1.5.Final
+ 4.5.1
3.4
2.7
@@ -286,6 +287,14 @@
provided
+
+
+ org.apache.httpcomponents
+ httpclient
+ ${http-components.version}
+
+
+
junit
@@ -359,6 +368,7 @@
shrinkwrap-descriptors-impl-javaee
test
+
diff --git a/src/main/java/biz/paluch/logging/gelf/GelfUtil.java b/src/main/java/biz/paluch/logging/gelf/GelfUtil.java
index 70a7ffdd0..0c08a83d6 100644
--- a/src/main/java/biz/paluch/logging/gelf/GelfUtil.java
+++ b/src/main/java/biz/paluch/logging/gelf/GelfUtil.java
@@ -2,9 +2,11 @@
import biz.paluch.logging.gelf.intern.GelfMessage;
-import java.util.Date;
-import java.util.HashSet;
-import java.util.Set;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.*;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
/**
* @author Mark Paluch
@@ -93,4 +95,67 @@ public static Set getMatchingMdcNames(DynamicMdcMessageField field, Set<
}
return matchingMdcNames;
}
+
+ public static String addDefaultPortIfMissing(String urlString, String defaultPort) {
+ URL url;
+ try {
+ url = new URL(urlString);
+ } catch (MalformedURLException e) {
+ return urlString;
+ }
+ if (url.getPort() != -1) {
+ return urlString;
+ }
+ String regex = "http://([^/]+)";
+ String found = getFirstFound(urlString, regex);
+ String replacer = "http://" + found + ":" + defaultPort;
+
+ if (!isEmpty(found)) {
+ urlString = urlString.replaceFirst(regex, replacer);
+ }
+ return urlString;
+ }
+
+ public static String getFirstFound(String contents, String regex) {
+ List founds = getFound(contents, regex);
+ if (isEmpty(founds)) {
+ return null;
+ }
+ return founds.get(0);
+ }
+
+ public static List getFound(String contents, String regex) {
+ if (isEmpty(regex) || isEmpty(contents)) {
+ return null;
+ }
+ List results = new ArrayList();
+ Pattern pattern = Pattern.compile(regex, Pattern.UNICODE_CASE);
+ Matcher matcher = pattern.matcher(contents);
+
+ while (matcher.find()) {
+ if (matcher.groupCount() > 0) {
+ results.add(matcher.group(1));
+ } else {
+ results.add(matcher.group());
+ }
+ }
+ return results;
+ }
+
+ public static boolean isEmpty(List list) {
+ if (list == null || list.size() == 0) {
+ return true;
+ }
+ if (list.size() == 1 && isEmpty(list.get(0))) {
+ return true;
+ }
+ return false;
+ }
+
+ public static boolean isEmpty(String str) {
+ if (str != null && str.trim().length() > 0) {
+ return false;
+ }
+ return true;
+ }
}
diff --git a/src/main/java/biz/paluch/logging/gelf/intern/sender/DefaultGelfSenderProvider.java b/src/main/java/biz/paluch/logging/gelf/intern/sender/DefaultGelfSenderProvider.java
index 157e99460..983d296da 100644
--- a/src/main/java/biz/paluch/logging/gelf/intern/sender/DefaultGelfSenderProvider.java
+++ b/src/main/java/biz/paluch/logging/gelf/intern/sender/DefaultGelfSenderProvider.java
@@ -6,25 +6,24 @@
import java.io.IOException;
import java.net.URI;
+import java.net.URL;
import java.util.Map;
import java.util.concurrent.TimeUnit;
+import static biz.paluch.logging.gelf.GelfUtil.addDefaultPortIfMissing;
+
/**
- *
* (c) https://github.com/Batigoal/logstash-gelf.git
- *
*/
public class DefaultGelfSenderProvider implements GelfSenderProvider {
public static final int DEFAULT_PORT = 12201;
- @Override
- public boolean supports(String host) {
+ @Override public boolean supports(String host) {
return host != null;
}
- @Override
- public GelfSender create(GelfSenderConfiguration configuration) throws IOException {
+ @Override public GelfSender create(GelfSenderConfiguration configuration) throws IOException {
String graylogHost = configuration.getHost();
int port = configuration.getPort();
@@ -39,8 +38,7 @@ public GelfSender create(GelfSenderConfiguration configuration) throws IOExcepti
URI uri = URI.create(graylogHost);
Map params = UriParser.parse(uri);
- int connectionTimeMs = (int) UriParser.getTimeAsMs(params, GelfTCPSender.CONNECTION_TIMEOUT,
- defaultTimeoutMs);
+ int connectionTimeMs = (int) UriParser.getTimeAsMs(params, GelfTCPSender.CONNECTION_TIMEOUT, defaultTimeoutMs);
int readTimeMs = (int) UriParser.getTimeAsMs(params, GelfTCPSender.READ_TIMEOUT, defaultTimeoutMs);
int deliveryAttempts = UriParser.getInt(params, GelfTCPSender.RETRIES, 1);
boolean keepAlive = UriParser.getString(params, GelfTCPSender.KEEPALIVE, false);
@@ -52,6 +50,11 @@ public GelfSender create(GelfSenderConfiguration configuration) throws IOExcepti
URI uri = URI.create(graylogHost);
String udpGraylogHost = UriParser.getHost(uri);
return new GelfUDPSender(udpGraylogHost, port, configuration.getErrorReporter());
+ } else if (graylogHost.startsWith("http")) {
+ String graylogHostWithDefaultPort = addDefaultPortIfMissing(graylogHost, String.valueOf(port));
+ URL url = new URL(graylogHostWithDefaultPort);
+ return new GelfHTTPSender(url, configuration.getErrorReporter());
+
} else {
return new GelfUDPSender(graylogHost, port, configuration.getErrorReporter());
}
diff --git a/src/main/java/biz/paluch/logging/gelf/intern/sender/GelfHTTPSender.java b/src/main/java/biz/paluch/logging/gelf/intern/sender/GelfHTTPSender.java
new file mode 100644
index 000000000..709d23419
--- /dev/null
+++ b/src/main/java/biz/paluch/logging/gelf/intern/sender/GelfHTTPSender.java
@@ -0,0 +1,51 @@
+package biz.paluch.logging.gelf.intern.sender;
+
+import biz.paluch.logging.gelf.intern.ErrorReporter;
+import biz.paluch.logging.gelf.intern.GelfMessage;
+import biz.paluch.logging.gelf.intern.GelfSender;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
+
+/**
+ * Created by https://github.com/salex89
+ */
+public class GelfHTTPSender implements GelfSender {
+
+ final private ErrorReporter errorReporter;
+ final private URL url;
+ private HttpURLConnection connection = null;
+ private int HTTP_ACCEPTED_STATUS = 202;
+
+ public GelfHTTPSender(URL url, ErrorReporter errorReporter) {
+ this.errorReporter = errorReporter;
+ this.url = url;
+ }
+
+ @Override public boolean sendMessage(GelfMessage message) {
+ try {
+ connection = (HttpURLConnection) url.openConnection();
+ connection.setDoOutput(true);
+ OutputStream outputStream = connection.getOutputStream();
+ outputStream.write(message.toJson().getBytes());
+ outputStream.close();
+ int responseCode = connection.getResponseCode();
+ if (responseCode == HTTP_ACCEPTED_STATUS) {
+ return true;
+ } else {
+ errorReporter.reportError("Server responded with unexpected status code: ", null);
+ }
+ } catch (IOException e) {
+ errorReporter.reportError("Error when sending log message", e);
+ }
+ return false;
+ }
+
+ @Override public void close() {
+ //disconnecting HttpURLConnection here to avoid underlying premature underlying Socket being closed.
+ if (connection != null)
+ connection.disconnect();
+ }
+}
diff --git a/src/test/java/biz/paluch/logging/gelf/GelfInboundHTTPHandler.java b/src/test/java/biz/paluch/logging/gelf/GelfInboundHTTPHandler.java
new file mode 100644
index 000000000..a750ab615
--- /dev/null
+++ b/src/test/java/biz/paluch/logging/gelf/GelfInboundHTTPHandler.java
@@ -0,0 +1,81 @@
+package biz.paluch.logging.gelf;
+
+import io.netty.buffer.ByteBufInputStream;
+import io.netty.buffer.Unpooled;
+import io.netty.channel.ChannelFutureListener;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.channel.SimpleChannelInboundHandler;
+import io.netty.handler.codec.http.*;
+import io.netty.util.CharsetUtil;
+import org.json.simple.JSONValue;
+
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Created by aleksandar on 3/12/16.
+ */
+public class GelfInboundHTTPHandler extends SimpleChannelInboundHandler