Skip to content

Commit

Permalink
Updates required due to code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Vaman Kulkarni authored and Vaman Kulkarni committed Feb 18, 2018
1 parent 7576829 commit 5e71bcf
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public static Descriptor getLogstashDescriptor() {

@Extension
public static final class Descriptor extends ToolDescriptor<LogstashInstallation> {

private transient IndexerType type;
private transient SyslogFormat syslogFormat;
private transient SyslogProtocol syslogProtocol;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,6 +24,7 @@ public class ElasticSearch extends LogstashIndexer<ElasticSearchDao>
private String username;
private Secret password;
private URI uri;
private String mimeType;

@DataBoundConstructor
public ElasticSearch()
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -111,7 +112,6 @@ public URI getUri()
{
return uri;
}

public String getHost()
{
return uri.getHost();
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">

<!-- We need to keep this file in order to prevent that in the UI we get the option to configure a Logstash Installation -->
<f:section title="Logstash Plugin" name="logstash">
<f:entry>
Logstash configuration has moved to the <a href="${rootURL}/configure">Global Configuration</a>
</f:entry>
</f:section>

</j:jelly>

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@
<f:entry title="${%Password}" field="password">
<f:password/>
</f:entry>
<f:entry title="${%Mime Type}" field="mimeType">
<f:textbox default="application/json"/>
</f:entry>
</j:jelly>
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 of the form <b>type/subtype</b> e.g. <i>application/json</i><br>
Since this is a field for MIME Type and not Content-Type we do not support additional content-type paramters like <b>charset</b> or <b>boundry</b>
</p>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 5e71bcf

Please sign in to comment.