From 3546aba9611203553242dcf0756f17e87f8fe3f7 Mon Sep 17 00:00:00 2001 From: aleksandar Date: Fri, 19 Feb 2016 23:02:19 +0100 Subject: [PATCH 1/4] Added GelfHTTPSender. Added methods to GelfUtil that add a port to a URI if not present. --- pom.xml | 10 +++ .../biz/paluch/logging/gelf/GelfUtil.java | 71 +++++++++++++++- .../sender/DefaultGelfSenderProvider.java | 7 +- .../gelf/intern/sender/GelfHTTPSender.java | 75 ++++++++++++++++ .../biz/paluch/logging/gelf/GelfUtilTest.java | 7 ++ .../intern/sender/GelfHTTPSenderTest.java | 85 +++++++++++++++++++ 6 files changed, 249 insertions(+), 6 deletions(-) create mode 100644 src/main/java/biz/paluch/logging/gelf/intern/sender/GelfHTTPSender.java create mode 100644 src/test/java/biz/paluch/logging/gelf/intern/sender/GelfHTTPSenderTest.java diff --git a/pom.xml b/pom.xml index 543c34424..885b53687 100644 --- a/pom.xml +++ b/pom.xml @@ -70,6 +70,7 @@ 1.1.3 1.5.2.Final 1.1.5.Final + 4.5.1 3.4 2.7 @@ -287,6 +288,14 @@ provided + + + org.apache.httpcomponents + httpclient + ${http-components.version} + + + junit @@ -360,6 +369,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 5dde69451..c5593d1cf 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 @@ -3,14 +3,13 @@ import biz.paluch.logging.gelf.intern.GelfSender; import biz.paluch.logging.gelf.intern.GelfSenderConfiguration; import biz.paluch.logging.gelf.intern.GelfSenderProvider; +import org.apache.http.impl.client.HttpClientBuilder; import java.io.IOException; import java.util.concurrent.TimeUnit; /** - * * (c) https://github.com/Batigoal/logstash-gelf.git - * */ public class DefaultGelfSenderProvider implements GelfSenderProvider { @@ -31,13 +30,15 @@ public GelfSender create(GelfSenderConfiguration configuration) throws IOExcepti } if (graylogHost.startsWith("tcp:")) { - int timeoutMs = (int) TimeUnit.MILLISECONDS.convert(2, TimeUnit.SECONDS); String tcpGraylogHost = graylogHost.substring(4, graylogHost.length()); return new GelfTCPSender(tcpGraylogHost, port, timeoutMs, timeoutMs, configuration.getErrorReporter()); } else if (graylogHost.startsWith("udp:")) { String udpGraylogHost = graylogHost.substring(4, graylogHost.length()); return new GelfUDPSender(udpGraylogHost, port, configuration.getErrorReporter()); + } else if (graylogHost.startsWith("http")) { + return new GelfHTTPSender(graylogHost, port, configuration.getErrorReporter(), HttpClientBuilder.create()); + } 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..45c3534f2 --- /dev/null +++ b/src/main/java/biz/paluch/logging/gelf/intern/sender/GelfHTTPSender.java @@ -0,0 +1,75 @@ +package biz.paluch.logging.gelf.intern.sender; + +import biz.paluch.logging.gelf.intern.Closer; +import biz.paluch.logging.gelf.intern.ErrorReporter; +import biz.paluch.logging.gelf.intern.GelfMessage; +import biz.paluch.logging.gelf.intern.GelfSender; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.URI; +import java.net.URISyntaxException; + +import static biz.paluch.logging.gelf.GelfUtil.addDefaultPortIfMissing; + +/** + * Created by https://github.com/salex89 + */ +public class GelfHTTPSender implements GelfSender { + + final private String uri; + final private int port; + final private ErrorReporter errorReporter; + final private CloseableHttpClient httpClient; + + + public GelfHTTPSender(String uri, int port, ErrorReporter errorReporter, HttpClientBuilder builder) { + + this.uri = addDefaultPortIfMissing(uri, String.valueOf(port)); + this.port = port; + this.errorReporter = errorReporter; + + + httpClient = builder.build(); + } + + @Override + public boolean sendMessage(GelfMessage message) { + + CloseableHttpResponse httpResponse = null; + try { + HttpPost post = new HttpPost(uri); + String messageJson = message.toJson(); + post.setEntity(new StringEntity(messageJson)); + httpResponse = httpClient.execute(post); + int responseStatusCode = httpResponse.getStatusLine().getStatusCode(); + if (responseStatusCode == 202) { + return true; + } else { + errorReporter.reportError("HTTP responded with non-202 status code: " + + responseStatusCode, new IOException("Cannot send data to " + uri + ":" + port)); + } + } catch (UnsupportedEncodingException e) { + errorReporter.reportError(e.getMessage(), new IOException("Cannot create HTTP GELF message to ", e)); + } catch (ClientProtocolException e) { + errorReporter.reportError(e.getMessage(), new IOException("Cannot send data to " + uri + ":" + port, e)); + } catch (IOException e) { + errorReporter.reportError(e.getMessage(), new IOException("Cannot send data to " + uri + ":" + port, e)); + } finally { + if (httpResponse != null) + Closer.close(httpResponse); + } + return false; + } + + @Override + public void close() { + Closer.close(httpClient); + } +} diff --git a/src/test/java/biz/paluch/logging/gelf/GelfUtilTest.java b/src/test/java/biz/paluch/logging/gelf/GelfUtilTest.java index f71c63e5a..42016a314 100644 --- a/src/test/java/biz/paluch/logging/gelf/GelfUtilTest.java +++ b/src/test/java/biz/paluch/logging/gelf/GelfUtilTest.java @@ -1,6 +1,7 @@ package biz.paluch.logging.gelf; import static org.junit.Assert.assertEquals; + import biz.paluch.logging.gelf.intern.GelfMessage; import biz.paluch.logging.gelf.jboss7.JBoss7JulLogEvent; import org.jboss.logmanager.ExtLogRecord; @@ -41,4 +42,10 @@ public void testProfilingLong() throws Exception { assertEquals("12sec", message.getAdditonalFields().get(GelfUtil.MDC_REQUEST_DURATION)); } + + @Test + public void addDefaultPortIfMissing() { + String url = GelfUtil.addDefaultPortIfMissing("http://example.com/foo", String.valueOf(1234)); + assertEquals("http://example.com:1234/foo", url); + } } diff --git a/src/test/java/biz/paluch/logging/gelf/intern/sender/GelfHTTPSenderTest.java b/src/test/java/biz/paluch/logging/gelf/intern/sender/GelfHTTPSenderTest.java new file mode 100644 index 000000000..97a89bd1b --- /dev/null +++ b/src/test/java/biz/paluch/logging/gelf/intern/sender/GelfHTTPSenderTest.java @@ -0,0 +1,85 @@ +package biz.paluch.logging.gelf.intern.sender; + +import biz.paluch.logging.gelf.intern.ErrorReporter; +import biz.paluch.logging.gelf.intern.GelfMessage; +import org.apache.http.ProtocolVersion; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpUriRequest; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.message.BasicStatusLine; +import org.apache.http.util.EntityUtils; +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.io.IOException; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.*; + +/** + * (c) Alekandar - https://github.com/salex89 + */ + +public class GelfHTTPSenderTest { + + + @Mock + HttpClientBuilder builder; + @Mock + CloseableHttpClient closeableHttpClient; + @Mock + CloseableHttpResponse closeableHttpResponse; + @Mock + ErrorReporter errorReporter; + + @Before + public void prepareMocks() throws IOException { + MockitoAnnotations.initMocks(this); +// when(closeableHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 202, "Accepted")); +// when(builder.build()).thenReturn(closeableHttpClient); +// when(closeableHttpClient.execute((HttpUriRequest) any())).thenReturn(closeableHttpResponse); + } + + @Test + public void sendMessageTest() throws IOException { + when(closeableHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 202, "Accepted")); + when(builder.build()).thenReturn(closeableHttpClient); + when(closeableHttpClient.execute((HttpUriRequest) any())).thenReturn(closeableHttpResponse); + + String uri = "http://192.168.0.100/gelf"; + GelfHTTPSender sender = new GelfHTTPSender(uri, 12201, errorReporter, builder); + GelfMessage gelfMessage = new GelfMessage(); + boolean success = sender.sendMessage(gelfMessage); + verify(builder, times(1)).build(); + assertTrue(success); + verifyZeroInteractions(errorReporter); + ArgumentCaptor postCaptor = ArgumentCaptor.forClass(HttpPost.class); + verify(closeableHttpClient).execute(postCaptor.capture()); + HttpPost executedPost = postCaptor.getValue(); + assertEquals("http://192.168.0.100:12201/gelf", executedPost.getURI().toString()); + assertEquals(gelfMessage.toJson(), EntityUtils.toString(executedPost.getEntity())); + } + + @Test + public void sendMessageTestIncorrectUrl() throws IOException { + String uri = "adda"; + GelfHTTPSender sender = new GelfHTTPSender(uri, 12201, errorReporter, HttpClientBuilder.create()); + GelfMessage gelfMessage = new GelfMessage(); + boolean success = sender.sendMessage(gelfMessage); + assertFalse(success); +// verifyZeroInteractions(errorReporter); +// ArgumentCaptor postCaptor = ArgumentCaptor.forClass(HttpPost.class); +// verify(closeableHttpClient).execute(postCaptor.capture()); +// HttpPost executedPost = postCaptor.getValue(); +// assertEquals("http://192.168.0.100:12201/gelf", executedPost.getURI().toString()); +// assertEquals(gelfMessage.toJson(), EntityUtils.toString(executedPost.getEntity())); + } +} From 0ecfc39a812b432a75421e42a86412df0a7ed6c3 Mon Sep 17 00:00:00 2001 From: aleksandar Date: Fri, 19 Feb 2016 23:02:19 +0100 Subject: [PATCH 2/4] Added GelfHTTPSender. Added methods to GelfUtil that add a port to a URI if not present. --- pom.xml | 10 +++ .../biz/paluch/logging/gelf/GelfUtil.java | 71 +++++++++++++++- .../sender/DefaultGelfSenderProvider.java | 6 +- .../gelf/intern/sender/GelfHTTPSender.java | 75 ++++++++++++++++ .../biz/paluch/logging/gelf/GelfUtilTest.java | 7 ++ .../intern/sender/GelfHTTPSenderTest.java | 85 +++++++++++++++++++ 6 files changed, 249 insertions(+), 5 deletions(-) create mode 100644 src/main/java/biz/paluch/logging/gelf/intern/sender/GelfHTTPSender.java create mode 100644 src/test/java/biz/paluch/logging/gelf/intern/sender/GelfHTTPSenderTest.java 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..9b25fa677 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 @@ -3,6 +3,7 @@ import biz.paluch.logging.gelf.intern.GelfSender; import biz.paluch.logging.gelf.intern.GelfSenderConfiguration; import biz.paluch.logging.gelf.intern.GelfSenderProvider; +import org.apache.http.impl.client.HttpClientBuilder; import java.io.IOException; import java.net.URI; @@ -10,9 +11,7 @@ import java.util.concurrent.TimeUnit; /** - * * (c) https://github.com/Batigoal/logstash-gelf.git - * */ public class DefaultGelfSenderProvider implements GelfSenderProvider { @@ -52,6 +51,9 @@ 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")) { + return new GelfHTTPSender(graylogHost, port, configuration.getErrorReporter(), HttpClientBuilder.create()); + } 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..45c3534f2 --- /dev/null +++ b/src/main/java/biz/paluch/logging/gelf/intern/sender/GelfHTTPSender.java @@ -0,0 +1,75 @@ +package biz.paluch.logging.gelf.intern.sender; + +import biz.paluch.logging.gelf.intern.Closer; +import biz.paluch.logging.gelf.intern.ErrorReporter; +import biz.paluch.logging.gelf.intern.GelfMessage; +import biz.paluch.logging.gelf.intern.GelfSender; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.URI; +import java.net.URISyntaxException; + +import static biz.paluch.logging.gelf.GelfUtil.addDefaultPortIfMissing; + +/** + * Created by https://github.com/salex89 + */ +public class GelfHTTPSender implements GelfSender { + + final private String uri; + final private int port; + final private ErrorReporter errorReporter; + final private CloseableHttpClient httpClient; + + + public GelfHTTPSender(String uri, int port, ErrorReporter errorReporter, HttpClientBuilder builder) { + + this.uri = addDefaultPortIfMissing(uri, String.valueOf(port)); + this.port = port; + this.errorReporter = errorReporter; + + + httpClient = builder.build(); + } + + @Override + public boolean sendMessage(GelfMessage message) { + + CloseableHttpResponse httpResponse = null; + try { + HttpPost post = new HttpPost(uri); + String messageJson = message.toJson(); + post.setEntity(new StringEntity(messageJson)); + httpResponse = httpClient.execute(post); + int responseStatusCode = httpResponse.getStatusLine().getStatusCode(); + if (responseStatusCode == 202) { + return true; + } else { + errorReporter.reportError("HTTP responded with non-202 status code: " + + responseStatusCode, new IOException("Cannot send data to " + uri + ":" + port)); + } + } catch (UnsupportedEncodingException e) { + errorReporter.reportError(e.getMessage(), new IOException("Cannot create HTTP GELF message to ", e)); + } catch (ClientProtocolException e) { + errorReporter.reportError(e.getMessage(), new IOException("Cannot send data to " + uri + ":" + port, e)); + } catch (IOException e) { + errorReporter.reportError(e.getMessage(), new IOException("Cannot send data to " + uri + ":" + port, e)); + } finally { + if (httpResponse != null) + Closer.close(httpResponse); + } + return false; + } + + @Override + public void close() { + Closer.close(httpClient); + } +} diff --git a/src/test/java/biz/paluch/logging/gelf/GelfUtilTest.java b/src/test/java/biz/paluch/logging/gelf/GelfUtilTest.java index f71c63e5a..42016a314 100644 --- a/src/test/java/biz/paluch/logging/gelf/GelfUtilTest.java +++ b/src/test/java/biz/paluch/logging/gelf/GelfUtilTest.java @@ -1,6 +1,7 @@ package biz.paluch.logging.gelf; import static org.junit.Assert.assertEquals; + import biz.paluch.logging.gelf.intern.GelfMessage; import biz.paluch.logging.gelf.jboss7.JBoss7JulLogEvent; import org.jboss.logmanager.ExtLogRecord; @@ -41,4 +42,10 @@ public void testProfilingLong() throws Exception { assertEquals("12sec", message.getAdditonalFields().get(GelfUtil.MDC_REQUEST_DURATION)); } + + @Test + public void addDefaultPortIfMissing() { + String url = GelfUtil.addDefaultPortIfMissing("http://example.com/foo", String.valueOf(1234)); + assertEquals("http://example.com:1234/foo", url); + } } diff --git a/src/test/java/biz/paluch/logging/gelf/intern/sender/GelfHTTPSenderTest.java b/src/test/java/biz/paluch/logging/gelf/intern/sender/GelfHTTPSenderTest.java new file mode 100644 index 000000000..97a89bd1b --- /dev/null +++ b/src/test/java/biz/paluch/logging/gelf/intern/sender/GelfHTTPSenderTest.java @@ -0,0 +1,85 @@ +package biz.paluch.logging.gelf.intern.sender; + +import biz.paluch.logging.gelf.intern.ErrorReporter; +import biz.paluch.logging.gelf.intern.GelfMessage; +import org.apache.http.ProtocolVersion; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpUriRequest; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.message.BasicStatusLine; +import org.apache.http.util.EntityUtils; +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.io.IOException; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.*; + +/** + * (c) Alekandar - https://github.com/salex89 + */ + +public class GelfHTTPSenderTest { + + + @Mock + HttpClientBuilder builder; + @Mock + CloseableHttpClient closeableHttpClient; + @Mock + CloseableHttpResponse closeableHttpResponse; + @Mock + ErrorReporter errorReporter; + + @Before + public void prepareMocks() throws IOException { + MockitoAnnotations.initMocks(this); +// when(closeableHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 202, "Accepted")); +// when(builder.build()).thenReturn(closeableHttpClient); +// when(closeableHttpClient.execute((HttpUriRequest) any())).thenReturn(closeableHttpResponse); + } + + @Test + public void sendMessageTest() throws IOException { + when(closeableHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 202, "Accepted")); + when(builder.build()).thenReturn(closeableHttpClient); + when(closeableHttpClient.execute((HttpUriRequest) any())).thenReturn(closeableHttpResponse); + + String uri = "http://192.168.0.100/gelf"; + GelfHTTPSender sender = new GelfHTTPSender(uri, 12201, errorReporter, builder); + GelfMessage gelfMessage = new GelfMessage(); + boolean success = sender.sendMessage(gelfMessage); + verify(builder, times(1)).build(); + assertTrue(success); + verifyZeroInteractions(errorReporter); + ArgumentCaptor postCaptor = ArgumentCaptor.forClass(HttpPost.class); + verify(closeableHttpClient).execute(postCaptor.capture()); + HttpPost executedPost = postCaptor.getValue(); + assertEquals("http://192.168.0.100:12201/gelf", executedPost.getURI().toString()); + assertEquals(gelfMessage.toJson(), EntityUtils.toString(executedPost.getEntity())); + } + + @Test + public void sendMessageTestIncorrectUrl() throws IOException { + String uri = "adda"; + GelfHTTPSender sender = new GelfHTTPSender(uri, 12201, errorReporter, HttpClientBuilder.create()); + GelfMessage gelfMessage = new GelfMessage(); + boolean success = sender.sendMessage(gelfMessage); + assertFalse(success); +// verifyZeroInteractions(errorReporter); +// ArgumentCaptor postCaptor = ArgumentCaptor.forClass(HttpPost.class); +// verify(closeableHttpClient).execute(postCaptor.capture()); +// HttpPost executedPost = postCaptor.getValue(); +// assertEquals("http://192.168.0.100:12201/gelf", executedPost.getURI().toString()); +// assertEquals(gelfMessage.toJson(), EntityUtils.toString(executedPost.getEntity())); + } +} From 802df37108d54e0d14e3e16075faa4bce2d7c19f Mon Sep 17 00:00:00 2001 From: aleksandar Date: Wed, 16 Mar 2016 23:40:10 +0100 Subject: [PATCH 3/4] Refactored HTTP seder to use URLConnection. Added tests with Netty. --- .../sender/DefaultGelfSenderProvider.java | 17 +-- .../gelf/intern/sender/GelfHTTPSender.java | 68 ++++------- .../logging/gelf/GelfInboundHTTPHandler.java | 81 +++++++++++++ .../gelf/GelfInboundHTTPInitializer.java | 30 +++++ .../biz/paluch/logging/gelf/GelfUtilTest.java | 11 +- .../logging/gelf/NettyLocalHTTPServer.java | 52 ++++++++ .../paluch/logging/gelf/NettyLocalServer.java | 7 +- .../intern/sender/GelfHTTPSenderTest.java | 112 ++++++++++-------- src/test/resources/logback-gelf-with-http.xml | 28 +++++ 9 files changed, 291 insertions(+), 115 deletions(-) create mode 100644 src/test/java/biz/paluch/logging/gelf/GelfInboundHTTPHandler.java create mode 100644 src/test/java/biz/paluch/logging/gelf/GelfInboundHTTPInitializer.java create mode 100644 src/test/java/biz/paluch/logging/gelf/NettyLocalHTTPServer.java create mode 100644 src/test/resources/logback-gelf-with-http.xml 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 9b25fa677..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 @@ -3,13 +3,15 @@ import biz.paluch.logging.gelf.intern.GelfSender; import biz.paluch.logging.gelf.intern.GelfSenderConfiguration; import biz.paluch.logging.gelf.intern.GelfSenderProvider; -import org.apache.http.impl.client.HttpClientBuilder; 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 */ @@ -17,13 +19,11 @@ 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(); @@ -38,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,7 +51,9 @@ public GelfSender create(GelfSenderConfiguration configuration) throws IOExcepti String udpGraylogHost = UriParser.getHost(uri); return new GelfUDPSender(udpGraylogHost, port, configuration.getErrorReporter()); } else if (graylogHost.startsWith("http")) { - return new GelfHTTPSender(graylogHost, port, configuration.getErrorReporter(), HttpClientBuilder.create()); + 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 index 45c3534f2..709d23419 100644 --- a/src/main/java/biz/paluch/logging/gelf/intern/sender/GelfHTTPSender.java +++ b/src/main/java/biz/paluch/logging/gelf/intern/sender/GelfHTTPSender.java @@ -1,75 +1,51 @@ package biz.paluch.logging.gelf.intern.sender; -import biz.paluch.logging.gelf.intern.Closer; import biz.paluch.logging.gelf.intern.ErrorReporter; import biz.paluch.logging.gelf.intern.GelfMessage; import biz.paluch.logging.gelf.intern.GelfSender; -import org.apache.http.client.ClientProtocolException; -import org.apache.http.client.methods.CloseableHttpResponse; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.entity.StringEntity; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClientBuilder; import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.net.URI; -import java.net.URISyntaxException; - -import static biz.paluch.logging.gelf.GelfUtil.addDefaultPortIfMissing; +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 String uri; - final private int port; final private ErrorReporter errorReporter; - final private CloseableHttpClient httpClient; - - - public GelfHTTPSender(String uri, int port, ErrorReporter errorReporter, HttpClientBuilder builder) { + final private URL url; + private HttpURLConnection connection = null; + private int HTTP_ACCEPTED_STATUS = 202; - this.uri = addDefaultPortIfMissing(uri, String.valueOf(port)); - this.port = port; + public GelfHTTPSender(URL url, ErrorReporter errorReporter) { this.errorReporter = errorReporter; - - - httpClient = builder.build(); + this.url = url; } - @Override - public boolean sendMessage(GelfMessage message) { - - CloseableHttpResponse httpResponse = null; + @Override public boolean sendMessage(GelfMessage message) { try { - HttpPost post = new HttpPost(uri); - String messageJson = message.toJson(); - post.setEntity(new StringEntity(messageJson)); - httpResponse = httpClient.execute(post); - int responseStatusCode = httpResponse.getStatusLine().getStatusCode(); - if (responseStatusCode == 202) { + 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("HTTP responded with non-202 status code: " + - responseStatusCode, new IOException("Cannot send data to " + uri + ":" + port)); + errorReporter.reportError("Server responded with unexpected status code: ", null); } - } catch (UnsupportedEncodingException e) { - errorReporter.reportError(e.getMessage(), new IOException("Cannot create HTTP GELF message to ", e)); - } catch (ClientProtocolException e) { - errorReporter.reportError(e.getMessage(), new IOException("Cannot send data to " + uri + ":" + port, e)); } catch (IOException e) { - errorReporter.reportError(e.getMessage(), new IOException("Cannot send data to " + uri + ":" + port, e)); - } finally { - if (httpResponse != null) - Closer.close(httpResponse); + errorReporter.reportError("Error when sending log message", e); } return false; } - @Override - public void close() { - Closer.close(httpClient); + @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 { + private List values = new ArrayList(); + private HttpRequest httpRequest; + private HttpContent httpContent; + private HttpResponseStatus responseStatus = HttpResponseStatus.ACCEPTED; + + private final StringBuilder contentBuffer = new StringBuilder(); + + @Override protected void channelRead0(ChannelHandlerContext channelHandlerContext, Object message) throws Exception { + + resetState(); + if (message instanceof HttpRequest) { + httpRequest = (HttpRequest) message; + } + if (message instanceof HttpContent) { + httpContent = (HttpContent) message; + contentBuffer.append(httpContent.content().toString(CharsetUtil.UTF_8)); + if (message instanceof LastHttpContent) { + Object parsedContent = JSONValue.parse(contentBuffer.toString()); + synchronized (values) { + values.add(parsedContent); + } + writeResponse(channelHandlerContext); + closeConnection(channelHandlerContext); + } + } + + } + + private void resetState() { + this.values.clear(); + this.httpContent = null; + this.httpRequest = null; + } + + private void closeConnection(ChannelHandlerContext channelHandlerContext) { + channelHandlerContext.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); + } + + private void writeResponse(ChannelHandlerContext ctx) { + FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, responseStatus); + ctx.write(response); + } + + public List getValues() { + return values; + } + + public String getUri() { + return httpRequest.getUri(); + } + + public HttpMethod getMethod() { + return httpRequest.getMethod(); + } + + public void setReturnStatus(HttpResponseStatus status) { + this.responseStatus = status; + } +} diff --git a/src/test/java/biz/paluch/logging/gelf/GelfInboundHTTPInitializer.java b/src/test/java/biz/paluch/logging/gelf/GelfInboundHTTPInitializer.java new file mode 100644 index 000000000..8b27e0257 --- /dev/null +++ b/src/test/java/biz/paluch/logging/gelf/GelfInboundHTTPInitializer.java @@ -0,0 +1,30 @@ +package biz.paluch.logging.gelf; + +import io.netty.channel.socket.SocketChannel; +import io.netty.channel.*; +import io.netty.handler.codec.http.HttpObjectAggregator; +import io.netty.handler.codec.http.HttpRequestDecoder; +import io.netty.handler.codec.http.HttpResponseEncoder; + +/** + * Created by aleksandar on 3/12/16. + */ +public class GelfInboundHTTPInitializer extends ChannelInitializer { + protected GelfInboundHTTPHandler handler; + + public GelfInboundHTTPInitializer() { + handler = new GelfInboundHTTPHandler(); + } + + @Override protected void initChannel(SocketChannel socketChannel) throws Exception { + ChannelPipeline pipeline = socketChannel.pipeline(); + pipeline.addLast(new HttpRequestDecoder()); + pipeline.addLast(new HttpObjectAggregator(1048576)); + pipeline.addLast(new HttpResponseEncoder()); + pipeline.addLast(handler); + } + + public GelfInboundHTTPHandler getHandler() { + return handler; + } +} diff --git a/src/test/java/biz/paluch/logging/gelf/GelfUtilTest.java b/src/test/java/biz/paluch/logging/gelf/GelfUtilTest.java index 42016a314..0a1949ecb 100644 --- a/src/test/java/biz/paluch/logging/gelf/GelfUtilTest.java +++ b/src/test/java/biz/paluch/logging/gelf/GelfUtilTest.java @@ -13,8 +13,7 @@ public class GelfUtilTest { - @Test - public void testProfilingString() throws Exception { + @Test public void testProfilingString() throws Exception { Map mdcMap = new HashMap(); mdcMap.put(GelfUtil.MDC_REQUEST_START_MS, "" + (System.currentTimeMillis() - 12000)); @@ -28,8 +27,7 @@ public void testProfilingString() throws Exception { } - @Test - public void testProfilingLong() throws Exception { + @Test public void testProfilingLong() throws Exception { Map mdcMap = new HashMap(); mdcMap.put(GelfUtil.MDC_REQUEST_START_MS, (System.currentTimeMillis() - 12000)); @@ -43,9 +41,10 @@ public void testProfilingLong() throws Exception { } - @Test - public void addDefaultPortIfMissing() { + @Test public void addDefaultPortIfMissing() { String url = GelfUtil.addDefaultPortIfMissing("http://example.com/foo", String.valueOf(1234)); assertEquals("http://example.com:1234/foo", url); + String url2 = GelfUtil.addDefaultPortIfMissing("http://example.com:8080/foo", String.valueOf(1234)); + assertEquals("http://example.com:8080/foo", url2); } } diff --git a/src/test/java/biz/paluch/logging/gelf/NettyLocalHTTPServer.java b/src/test/java/biz/paluch/logging/gelf/NettyLocalHTTPServer.java new file mode 100644 index 000000000..71dbbc1b7 --- /dev/null +++ b/src/test/java/biz/paluch/logging/gelf/NettyLocalHTTPServer.java @@ -0,0 +1,52 @@ +package biz.paluch.logging.gelf; + +import io.netty.bootstrap.Bootstrap; +import io.netty.bootstrap.ServerBootstrap; +import io.netty.channel.*; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.handler.codec.http.HttpResponseStatus; + +import java.util.List; + +/** + * @author Mark Paluch + * @since 10.11.13 10:30 + */ +public class NettyLocalHTTPServer { + + private int port = 19393; + private EventLoopGroup group = new NioEventLoopGroup(); + private GelfInboundHTTPInitializer handlerInitializer = new GelfInboundHTTPInitializer(); + private Class channelClass = NioServerSocketChannel.class; + + private ChannelFuture f; + + public NettyLocalHTTPServer() { + } + + public void run() throws Exception { + + ServerBootstrap b = new ServerBootstrap(); + b.group(group); + b.channel((Class) channelClass).childHandler(handlerInitializer); + f = b.bind(port).sync(); + } + + public void close() { + f.channel().close(); + f = null; + } + + public List getJsonValues() { + return handlerInitializer.getHandler().getValues(); + } + + public void setReturnStatus(HttpResponseStatus status) { + handlerInitializer.getHandler().setReturnStatus(status); + } + + public GelfInboundHTTPInitializer getHandlerInitializer() { + return handlerInitializer; + } +} diff --git a/src/test/java/biz/paluch/logging/gelf/NettyLocalServer.java b/src/test/java/biz/paluch/logging/gelf/NettyLocalServer.java index 8af88f9a7..9b97735f5 100644 --- a/src/test/java/biz/paluch/logging/gelf/NettyLocalServer.java +++ b/src/test/java/biz/paluch/logging/gelf/NettyLocalServer.java @@ -2,12 +2,7 @@ import io.netty.bootstrap.Bootstrap; import io.netty.bootstrap.ServerBootstrap; -import io.netty.channel.AdaptiveRecvByteBufAllocator; -import io.netty.channel.Channel; -import io.netty.channel.ChannelFuture; -import io.netty.channel.ChannelOption; -import io.netty.channel.EventLoopGroup; -import io.netty.channel.ServerChannel; +import io.netty.channel.*; import io.netty.channel.nio.NioEventLoopGroup; import java.util.List; diff --git a/src/test/java/biz/paluch/logging/gelf/intern/sender/GelfHTTPSenderTest.java b/src/test/java/biz/paluch/logging/gelf/intern/sender/GelfHTTPSenderTest.java index 97a89bd1b..842f7f788 100644 --- a/src/test/java/biz/paluch/logging/gelf/intern/sender/GelfHTTPSenderTest.java +++ b/src/test/java/biz/paluch/logging/gelf/intern/sender/GelfHTTPSenderTest.java @@ -1,27 +1,29 @@ package biz.paluch.logging.gelf.intern.sender; +import biz.paluch.logging.gelf.GelfTestSender; +import biz.paluch.logging.gelf.NettyLocalHTTPServer; import biz.paluch.logging.gelf.intern.ErrorReporter; import biz.paluch.logging.gelf.intern.GelfMessage; -import org.apache.http.ProtocolVersion; -import org.apache.http.client.methods.CloseableHttpResponse; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.client.methods.HttpUriRequest; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClientBuilder; -import org.apache.http.message.BasicStatusLine; -import org.apache.http.util.EntityUtils; -import org.junit.Before; -import org.junit.Test; -import org.mockito.ArgumentCaptor; +import ch.qos.logback.classic.Logger; +import ch.qos.logback.classic.LoggerContext; +import ch.qos.logback.classic.joran.JoranConfigurator; +import ch.qos.logback.core.joran.spi.JoranException; +import io.netty.handler.codec.http.HttpResponseStatus; +import org.json.simple.JSONObject; +import org.junit.*; import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import org.slf4j.MDC; import java.io.IOException; +import java.net.URL; +import java.util.List; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.*; /** @@ -30,56 +32,68 @@ public class GelfHTTPSenderTest { + private NettyLocalHTTPServer server; - @Mock - HttpClientBuilder builder; - @Mock - CloseableHttpClient closeableHttpClient; - @Mock - CloseableHttpResponse closeableHttpResponse; - @Mock - ErrorReporter errorReporter; + @Mock ErrorReporter errorReporter; - @Before - public void prepareMocks() throws IOException { + @Before public void setUpClass() throws Exception { + server = new NettyLocalHTTPServer(); + server.run(); + + } + + @After public void tearDownClass() { + server.close(); + } + + @Before public void setUp() throws IOException { MockitoAnnotations.initMocks(this); -// when(closeableHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 202, "Accepted")); -// when(builder.build()).thenReturn(closeableHttpClient); -// when(closeableHttpClient.execute((HttpUriRequest) any())).thenReturn(closeableHttpResponse); } - @Test - public void sendMessageTest() throws IOException { - when(closeableHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 202, "Accepted")); - when(builder.build()).thenReturn(closeableHttpClient); - when(closeableHttpClient.execute((HttpUriRequest) any())).thenReturn(closeableHttpResponse); + @Test public void sendMessageTest() throws IOException { - String uri = "http://192.168.0.100/gelf"; - GelfHTTPSender sender = new GelfHTTPSender(uri, 12201, errorReporter, builder); - GelfMessage gelfMessage = new GelfMessage(); + server.setReturnStatus(HttpResponseStatus.ACCEPTED); + String uri = "http://127.0.0.1:19393"; + GelfHTTPSender sender = new GelfHTTPSender(new URL(uri), errorReporter); + GelfMessage gelfMessage = new GelfMessage("shortMessage", "fullMessage", 12121l, "WARNING"); boolean success = sender.sendMessage(gelfMessage); - verify(builder, times(1)).build(); assertTrue(success); verifyZeroInteractions(errorReporter); - ArgumentCaptor postCaptor = ArgumentCaptor.forClass(HttpPost.class); - verify(closeableHttpClient).execute(postCaptor.capture()); - HttpPost executedPost = postCaptor.getValue(); - assertEquals("http://192.168.0.100:12201/gelf", executedPost.getURI().toString()); - assertEquals(gelfMessage.toJson(), EntityUtils.toString(executedPost.getEntity())); + List jsonValues = server.getJsonValues(); + assertEquals(1, jsonValues.size()); + JSONObject messageJson = (JSONObject) jsonValues.get(0); + assertEquals(gelfMessage.getShortMessage(), messageJson.get("short_message")); + assertEquals(gelfMessage.getFullMessage(), messageJson.get("full_message")); + assertEquals(gelfMessage.getTimestamp(), messageJson.get("timestamp")); + assertEquals(gelfMessage.getLevel(), messageJson.get("level")); } - @Test - public void sendMessageTestIncorrectUrl() throws IOException { - String uri = "adda"; - GelfHTTPSender sender = new GelfHTTPSender(uri, 12201, errorReporter, HttpClientBuilder.create()); - GelfMessage gelfMessage = new GelfMessage(); + @Test public void sendMessageFailureTest() throws IOException { + + server.setReturnStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR); + String uri = "http://127.0.0.1:19393"; + GelfHTTPSender sender = new GelfHTTPSender(new URL(uri), errorReporter); + GelfMessage gelfMessage = new GelfMessage("shortMessage", "fullMessage", 12121l, "WARNING"); boolean success = sender.sendMessage(gelfMessage); assertFalse(success); -// verifyZeroInteractions(errorReporter); -// ArgumentCaptor postCaptor = ArgumentCaptor.forClass(HttpPost.class); -// verify(closeableHttpClient).execute(postCaptor.capture()); -// HttpPost executedPost = postCaptor.getValue(); -// assertEquals("http://192.168.0.100:12201/gelf", executedPost.getURI().toString()); -// assertEquals(gelfMessage.toJson(), EntityUtils.toString(executedPost.getEntity())); + verify(errorReporter, times(1)).reportError(anyString(), any(Exception.class)); } + + @Test public void testWithLoggingContext() throws JoranException { + LoggerContext lc = new LoggerContext(); + JoranConfigurator configurator = new JoranConfigurator(); + configurator.setContext(lc); + URL xmlConfigFile = getClass().getResource("/logback-gelf-with-http.xml"); + configurator.doConfigure(xmlConfigFile); + Logger testLogger = lc.getLogger("testLogger"); + testLogger.error("Hi there"); + List jsonValues = server.getJsonValues(); + String uri = server.getHandlerInitializer().getHandler().getUri(); + assertEquals("/foo/bar", uri); + assertEquals(1, jsonValues.size()); + JSONObject jsonObject = (JSONObject) jsonValues.get(0); + assertEquals("Hi there", jsonObject.get("short_message")); + + } + } diff --git a/src/test/resources/logback-gelf-with-http.xml b/src/test/resources/logback-gelf-with-http.xml new file mode 100644 index 000000000..708fe4838 --- /dev/null +++ b/src/test/resources/logback-gelf-with-http.xml @@ -0,0 +1,28 @@ + + + + + test + + + + http://localhost:19393/foo/bar + 19393 + java-test + true + true + true + yyyy-MM-dd HH:mm:ss,SSSS + 8192 + fieldName1=fieldValue1,fieldName2=fieldValue2,myOriginHost=${HOSTNAME} + mdcField1,mdcField2 + 1.2.3.4 + + INFO + + + + + + + From fb16a1bc8fce595dcecfdd68d8d5997657e2369f Mon Sep 17 00:00:00 2001 From: aleksandar Date: Wed, 16 Mar 2016 23:56:50 +0100 Subject: [PATCH 4/4] Refactored HTTP Sender tests. --- .../gelf/intern/sender/GelfHTTPSenderTest.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/test/java/biz/paluch/logging/gelf/intern/sender/GelfHTTPSenderTest.java b/src/test/java/biz/paluch/logging/gelf/intern/sender/GelfHTTPSenderTest.java index 842f7f788..d29fc916c 100644 --- a/src/test/java/biz/paluch/logging/gelf/intern/sender/GelfHTTPSenderTest.java +++ b/src/test/java/biz/paluch/logging/gelf/intern/sender/GelfHTTPSenderTest.java @@ -33,28 +33,27 @@ public class GelfHTTPSenderTest { private NettyLocalHTTPServer server; - + private GelfHTTPSender sender; @Mock ErrorReporter errorReporter; - @Before public void setUpClass() throws Exception { + @Before public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); server = new NettyLocalHTTPServer(); server.run(); + String uri = "http://127.0.0.1:19393"; + sender = new GelfHTTPSender(new URL(uri), errorReporter); } - @After public void tearDownClass() { + @After public void tearDown() { server.close(); - } - - @Before public void setUp() throws IOException { - MockitoAnnotations.initMocks(this); + sender.close(); } @Test public void sendMessageTest() throws IOException { server.setReturnStatus(HttpResponseStatus.ACCEPTED); - String uri = "http://127.0.0.1:19393"; - GelfHTTPSender sender = new GelfHTTPSender(new URL(uri), errorReporter); + GelfMessage gelfMessage = new GelfMessage("shortMessage", "fullMessage", 12121l, "WARNING"); boolean success = sender.sendMessage(gelfMessage); assertTrue(success); @@ -66,6 +65,7 @@ public class GelfHTTPSenderTest { assertEquals(gelfMessage.getFullMessage(), messageJson.get("full_message")); assertEquals(gelfMessage.getTimestamp(), messageJson.get("timestamp")); assertEquals(gelfMessage.getLevel(), messageJson.get("level")); + } @Test public void sendMessageFailureTest() throws IOException {