-
-
Notifications
You must be signed in to change notification settings - Fork 106
Use mime type from config field in Elasticsearch indexer while posting HTTP request #41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
a359903
117ee91
d6e2d73
7576829
4146ffe
722137e
6a670a5
4fabced
e67b6a8
ed77e45
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,9 +42,15 @@ | |
| import java.io.PrintStream; | ||
| import java.net.URI; | ||
| import java.net.URISyntaxException; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.logging.Logger; | ||
|
|
||
| import com.google.common.collect.Range; | ||
|
|
||
| import jenkins.model.Jenkins; | ||
| import jenkins.plugins.logstash.LogstashInstallation; | ||
| import jenkins.plugins.logstash.LogstashInstallation.Descriptor; | ||
|
|
||
| /** | ||
| * Elastic Search Data Access Object. | ||
| * | ||
|
|
@@ -56,7 +62,8 @@ public class ElasticSearchDao extends AbstractLogstashIndexerDao { | |
| final URI uri; | ||
| final String auth; | ||
| final Range<Integer> successCodes = closedOpen(200,300); | ||
|
|
||
| private final static Logger log = Logger.getLogger(ElasticSearchDao.class.getName()); | ||
|
|
||
| //primary constructor used by indexer factory | ||
| public ElasticSearchDao(String host, int port, String key, String username, String password) { | ||
| this(null, host, port, key, username, password); | ||
|
|
@@ -94,9 +101,22 @@ public ElasticSearchDao(String host, int port, String key, String username, Stri | |
| } | ||
|
|
||
| HttpPost getHttpPost(String data) { | ||
| HttpPost postRequest; | ||
| postRequest = new HttpPost(uri); | ||
| StringEntity input = new StringEntity(data, ContentType.APPLICATION_JSON); | ||
| String mimeType = null; | ||
| try { | ||
| Descriptor logstashPluginConfig = (Descriptor) Jenkins.getInstance().getDescriptor(LogstashInstallation.class); | ||
| mimeType = logstashPluginConfig.mimeType; | ||
| } catch (NullPointerException e) { | ||
| log.warning("Unable to read mimetype from jenkins logstash plugin configuration"); | ||
| } | ||
| return getHttpPost(data, mimeType); | ||
| } | ||
| // Re-factored for unit-testing | ||
| HttpPost getHttpPost(String data, String mimeType) { | ||
| HttpPost postRequest = new HttpPost(uri); | ||
| // 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(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mimeType will never be null. If you save the global configuration with no content for the mimeType you will get an empty string. Only when you update the plugin without ever saving the configuration it will be null. |
||
| input.setContentType(mimeType); | ||
| postRequest.setEntity(input); | ||
| if (auth != null) { | ||
| postRequest.addHeader("Authorization", "Basic " + auth); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,10 @@ | |
| checkUrl="'${resURL}/descriptorByName/LogstashInstallation/checkString?value='+escape(this.value)" /> | ||
| </f:entry> | ||
| <f:advanced> | ||
| <f:entry title="${%MIME type}" field="mimeType"> | ||
| <f:textbox value="${descriptor.mimeType}" default="application/json" | ||
|
||
| checkUrl="'${resURL}/descriptorByName/LogstashInstallation/checkMimeType?value='+escape(this.value)" /> | ||
| </f:entry> | ||
| <f:entry title="${%Syslog format}" field="syslogFormat"> | ||
| <f:enum value="${descriptor.syslogFormat}">${it.name()}</f:enum> | ||
| </f:entry> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| <div> | ||
| <p> | ||
| MIME type of the request body that is sent to ELASTICSEARCH indexer. It should be at least of the form <b>type/subtype</b><br> | ||
| For e.g. <i>application/json</i> | ||
| </p> | ||
| </div> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's better to reuse https://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/entity/ContentType.html#parse(java.lang.String)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did think of this earlier. But I did not end up using it in my code because I realized it does not actually parse
Mime-Type. for e.g.ContentType.parse("application/json%%")(which is clearly not a valid mime-type) does not complain instead it returnsContentTypeobject with this invalid mimetype.For this reason I did not use it in the code and ended up validating by my own as per RFC-4288. This would potentially eliminate unwanted surprises that could cause with invalid mime-type.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I assumed
means it does validate it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It just validates Content-Type as a whole and does not look for mime-type validity.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could use javax.activation.MimeType
This does validate the mimetype. Though it seems
&is a valid character in a mimetype. Mimetype fails when you use§see RFC 2045