diff --git a/src/main/java/jenkins/plugins/logstash/LogstashInstallation.java b/src/main/java/jenkins/plugins/logstash/LogstashInstallation.java index 4f8ed7e0..337e0776 100644 --- a/src/main/java/jenkins/plugins/logstash/LogstashInstallation.java +++ b/src/main/java/jenkins/plugins/logstash/LogstashInstallation.java @@ -60,7 +60,6 @@ public static Descriptor getLogstashDescriptor() { @Extension public static final class Descriptor extends ToolDescriptor { - private transient IndexerType type; private transient SyslogFormat syslogFormat; private transient SyslogProtocol syslogProtocol; diff --git a/src/main/java/jenkins/plugins/logstash/configuration/ElasticSearch.java b/src/main/java/jenkins/plugins/logstash/configuration/ElasticSearch.java index f5f1a7db..47f2be7f 100644 --- a/src/main/java/jenkins/plugins/logstash/configuration/ElasticSearch.java +++ b/src/main/java/jenkins/plugins/logstash/configuration/ElasticSearch.java @@ -5,6 +5,9 @@ import java.net.URISyntaxException; import java.net.URL; +import javax.activation.MimeType; +import javax.activation.MimeTypeParseException; + import org.apache.commons.lang.StringUtils; import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.DataBoundSetter; @@ -21,6 +24,7 @@ public class ElasticSearch extends LogstashIndexer private String username; private Secret password; private URI uri; + private String mimeType; @DataBoundConstructor public ElasticSearch() @@ -68,7 +72,16 @@ public void setPassword(String password) { this.password = Secret.fromString(password); } - + + @DataBoundSetter + public void setMimeType(String mimeType) { + this.mimeType = mimeType; + } + + public String getMimeType() { + return mimeType; + } + @Override public boolean equals(Object obj) { @@ -118,7 +131,9 @@ public int hashCode() @Override public ElasticSearchDao createIndexerInstance() { - return new ElasticSearchDao(getUri(), username, Secret.toString(password)); + ElasticSearchDao esDao = new ElasticSearchDao(getUri(), username, Secret.toString(password)); + esDao.setMimeType(getMimeType()); + return esDao; } @Extension @@ -163,5 +178,17 @@ public FormValidation doCheckUrl(@QueryParameter("value") String value) } return FormValidation.ok(); } + public FormValidation doCheckMimeType(@QueryParameter("value") String value) { + if (StringUtils.isBlank(value)) { + return FormValidation.error(Messages.ValueIsRequired()); + } + try { + //This is simply to check validity of the given mimeType + new MimeType(value); + } catch (MimeTypeParseException e) { + return FormValidation.error(Messages.ProvideValidMimeType()); + } + return FormValidation.ok(); + } } } diff --git a/src/main/java/jenkins/plugins/logstash/persistence/ElasticSearchDao.java b/src/main/java/jenkins/plugins/logstash/persistence/ElasticSearchDao.java index e215989b..d96e077c 100644 --- a/src/main/java/jenkins/plugins/logstash/persistence/ElasticSearchDao.java +++ b/src/main/java/jenkins/plugins/logstash/persistence/ElasticSearchDao.java @@ -68,7 +68,8 @@ public class ElasticSearchDao extends AbstractLogstashIndexerDao { private String username; private String password; - + private String mimeType; + //primary constructor used by indexer factory public ElasticSearchDao(URI uri, String username, String password) { @@ -111,7 +112,6 @@ public URI getUri() { return uri; } - public String getHost() { return uri.getHost(); @@ -141,17 +141,27 @@ public String getKey() { return uri.getPath(); } - + + public String getMimeType() { + return this.mimeType; + } + + public void setMimeType(String mimeType) { + this.mimeType = mimeType; + } + String getAuth() { return auth; } - protected HttpPost getHttpPost(String data) { - HttpPost postRequest; - postRequest = new HttpPost(uri); - StringEntity input = new StringEntity(data, ContentType.APPLICATION_JSON); - + HttpPost getHttpPost(String data) { + HttpPost postRequest = new HttpPost(uri); + String mimeType = this.getMimeType(); + // char encoding is set to UTF_8 since this request posts a JSON string + StringEntity input = new StringEntity(data, StandardCharsets.UTF_8); + mimeType = (mimeType != null) ? mimeType : ContentType.APPLICATION_JSON.toString(); + input.setContentType(mimeType); postRequest.setEntity(input); if (auth != null) { postRequest.addHeader("Authorization", "Basic " + auth); diff --git a/src/main/resources/jenkins/plugins/logstash/LogstashInstallation/global.jelly b/src/main/resources/jenkins/plugins/logstash/LogstashInstallation/global.jelly index 705a1d68..f0055c8a 100644 --- a/src/main/resources/jenkins/plugins/logstash/LogstashInstallation/global.jelly +++ b/src/main/resources/jenkins/plugins/logstash/LogstashInstallation/global.jelly @@ -1,10 +1,8 @@ - Logstash configuration has moved to the Global Configuration - diff --git a/src/main/resources/jenkins/plugins/logstash/LogstashInstallation/help-mimeType.html b/src/main/resources/jenkins/plugins/logstash/LogstashInstallation/help-mimeType.html deleted file mode 100644 index 70251a4c..00000000 --- a/src/main/resources/jenkins/plugins/logstash/LogstashInstallation/help-mimeType.html +++ /dev/null @@ -1,6 +0,0 @@ -
-

- MIME type of the request body that is sent to ELASTICSEARCH indexer. It should be at least of the form type/subtype
- For e.g. application/json -

-
diff --git a/src/main/resources/jenkins/plugins/logstash/Messages.properties b/src/main/resources/jenkins/plugins/logstash/Messages.properties index f2659941..14956598 100644 --- a/src/main/resources/jenkins/plugins/logstash/Messages.properties +++ b/src/main/resources/jenkins/plugins/logstash/Messages.properties @@ -24,3 +24,4 @@ DisplayName = Send console log to Logstash ValueIsInt = Value must be an integer ValueIsRequired = Value is required PleaseProvideHost = Please set a valid host name +ProvideValidMimeType = Please provide a valid mime type \ No newline at end of file diff --git a/src/main/resources/jenkins/plugins/logstash/configuration/ElasticSearch/config.jelly b/src/main/resources/jenkins/plugins/logstash/configuration/ElasticSearch/config.jelly index a01fab63..fc1ec8f9 100644 --- a/src/main/resources/jenkins/plugins/logstash/configuration/ElasticSearch/config.jelly +++ b/src/main/resources/jenkins/plugins/logstash/configuration/ElasticSearch/config.jelly @@ -9,4 +9,7 @@ + + + \ No newline at end of file diff --git a/src/main/resources/jenkins/plugins/logstash/configuration/ElasticSearch/help-mimeType.html b/src/main/resources/jenkins/plugins/logstash/configuration/ElasticSearch/help-mimeType.html new file mode 100644 index 00000000..20dba0a5 --- /dev/null +++ b/src/main/resources/jenkins/plugins/logstash/configuration/ElasticSearch/help-mimeType.html @@ -0,0 +1,6 @@ +
+

+ MIME type of the request body that is sent to ELASTICSEARCH indexer. It should be of the form type/subtype e.g. application/json
+ Since this is a field for MIME Type and not Content-Type we do not support additional content-type paramters like charset or boundry +

+
diff --git a/src/test/java/jenkins/plugins/logstash/persistence/ElasticSearchDaoTest.java b/src/test/java/jenkins/plugins/logstash/persistence/ElasticSearchDaoTest.java index 63a02fa9..dc514341 100644 --- a/src/test/java/jenkins/plugins/logstash/persistence/ElasticSearchDaoTest.java +++ b/src/test/java/jenkins/plugins/logstash/persistence/ElasticSearchDaoTest.java @@ -49,6 +49,7 @@ ElasticSearchDao createDao(String url, String username, String password) throws public void before() throws Exception { int port = (int) (Math.random() * 1000); dao = createDao("http://localhost:8200/logstash", "username", "password"); + when(mockClientBuilder.build()).thenReturn(mockHttpClient); when(mockHttpClient.execute(any(HttpPost.class))).thenReturn(mockResponse); when(mockResponse.getStatusLine()).thenReturn(mockStatusLine); @@ -195,20 +196,22 @@ public void pushFailStatusCode() throws Exception { e.getMessage().contains("Something bad happened.") && e.getMessage().contains("HTTP error code: 500")); throw e; } + } @Test - public void getHttpPostSuccessWithUserInput() { + public void getHttpPostSuccessWithUserInput() throws Exception { String json = "{ 'foo': 'bar' }"; - dao = createDao("http://localhost", 8200, "/jenkins/logstash", "", ""); String mimeType = "application/json"; - HttpPost post = dao.getHttpPost(json, mimeType); + dao = createDao("http://localhost:8200/jenkins/logstash", "username", "password"); + dao.setMimeType(mimeType); + HttpPost post = dao.getHttpPost(json); HttpEntity entity = post.getEntity(); assertEquals("Content type do not match", mimeType, entity.getContentType().getValue()); } @Test - public void getHttpPostWithFallbackInput() { + public void getHttpPostWithFallbackInput() throws Exception { String json = "{ 'foo': 'bar' }"; - dao = createDao("http://localhost", 8200, "/jenkins/logstash", "", ""); + dao = createDao("http://localhost:8200/jenkins/logstash", "username", "password"); HttpPost post = dao.getHttpPost(json); HttpEntity entity = post.getEntity(); assertEquals("Content type do not match", ContentType.APPLICATION_JSON.toString(), entity.getContentType().getValue());