Skip to content

Commit

Permalink
removed apache stuff for http client
Browse files Browse the repository at this point in the history
  • Loading branch information
markuswehrle committed Nov 18, 2015
1 parent 2b090ff commit e03dd06
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 37 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
# Mobile Tools for Java (J2ME)
.mtj.tmp/

# idea folders and files
.idea
*.iml

# Package Files #
*.war
*.ear
Expand Down
6 changes: 0 additions & 6 deletions plugin-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,6 @@
<artifactId>mockito-core</artifactId>
<version>1.10.19</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.6</version>
</dependency>


</dependencies>

Expand Down
36 changes: 11 additions & 25 deletions plugin-common/src/com/tw/go/plugin/common/ApiRequestBase.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
package com.tw.go.plugin.common;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

Expand All @@ -23,7 +15,6 @@
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpClientConnection;


/**
Expand Down Expand Up @@ -73,6 +64,10 @@ protected String requestPost(String requestUrlString, String jsonData) throws Ex
return request(requestUrlString, jsonData, "POST");
}

protected String requestPostFormUrlEncoded(String requestUrlString, String data) throws Exception{
return request(requestUrlString, data, "POST", "application/x-www-form-urlencoded");
}

private String request(String requestUrlString, String requestMethod) throws Exception{
String result = "";

Expand Down Expand Up @@ -120,39 +115,30 @@ private String request(String requestUrlString, String requestMethod) throws Exc
}


protected int requestPostFormUrlEncodedValue(String requestUrlString, String value) throws Exception{

HttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost(requestUrlString);

// Request parameters and other properties.
List<NameValuePair> params = new ArrayList<NameValuePair>(2);
params.add(new BasicNameValuePair("value", value));
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

//Execute and get the response.
HttpResponse response = httpclient.execute(httppost);
return response.getStatusLine().getStatusCode();

private String request(String requestUrlString, String jsonData, String requestMethod) throws Exception {
return request(requestUrlString, jsonData, requestMethod, "application/json");
}

private String request(String requestUrlString, String jsonData, String requestMethod) throws Exception{
private String request(String requestUrlString, String data, String requestMethod, String contentType) throws Exception{

String result = "";

URL url = new URL(requestUrlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setRequestMethod(requestMethod);
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Content-Type", contentType);
conn.setRequestProperty("Content-Length", "" + Integer.toString(data.getBytes().length));

if(_apiKeysAvailable) {
conn.setRequestProperty("X-ApiKeys", getXApiKeys()); // API keys for secure access
}
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
conn.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
out.write(jsonData);
out.write(data);
out.close();

int responseCode = conn.getResponseCode();
Expand Down
16 changes: 11 additions & 5 deletions plugin-common/src/com/tw/go/plugin/common/GoApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import org.json.CDL;
import org.json.JSONObject;

import java.io.IOException;
import java.net.URLEncoder;

/**
* Created by MarkusW on 17.11.2015.
*/
Expand All @@ -31,15 +34,18 @@ public String getJobProperty(String pipelineName, String pipelineCounter, String

public String setJobProperty(String pipelineName, String pipelineCounter, String stageName, String stageCounter, String jobName, String propertyName, String propertyValue) throws Exception {
String uri = getJobPropertyRequestUri(pipelineName, pipelineCounter, stageName, stageCounter, jobName, propertyName);
int response = requestPostFormUrlEncodedValue(uri, propertyValue);
if(response == 201){
return propertyValue;
String urlParameters = "value=" + URLEncoder.encode(propertyValue, "UTF-8");

try {
requestPostFormUrlEncoded(uri, urlParameters);
}
catch (IOException e) {

}
else if (response == 409) {
finally {
return getJobProperty(pipelineName, pipelineCounter, stageName, stageCounter, jobName, propertyName);
}

throw new Exception("An error occured: Http response = " + response);
}

public JSONObject getJobProperties(String pipelineName, String pipelineCounter, String stageName, String stageCounter, String jobName) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class JobPropParamParserTest {

private String testPropertyValue1;
private String testPropertyValue2;
private String testPropertyValue3;

@Before
public void init() throws Exception{
Expand All @@ -33,6 +34,7 @@ public void init() throws Exception{
// initialize test properties
testPropertyValue1 = setJobProperty("test_property_1", "Test Value 1");
testPropertyValue2 = setJobProperty("test_property_2", "Test Value 2");
testPropertyValue3 = setJobProperty("test_property_TestPluginStage", "Test Value 3");

}

Expand Down Expand Up @@ -64,6 +66,17 @@ public void testPropVarAndEnvVarInParameter() throws Exception {
EnvVarParamParser envParser = new EnvVarParamParser(context.getEnvironmentVariables(), mockConsole);
JobPropParamParser propParser = new JobPropParamParser(context.getEnvironmentVariables(), mockConsole);

Assert.assertEquals("Test Value 3", propParser.Parse(envParser.Parse("%{test_property_${GO_STAGE_NAME}}")));
Assert.assertEquals("abcTest Value 3", propParser.Parse(envParser.Parse("abc%{test_property_${GO_STAGE_NAME}}")));
Assert.assertEquals("Test Value 3abc", propParser.Parse(envParser.Parse("%{test_property_${GO_STAGE_NAME}}abc")));
Assert.assertEquals("abcTest Value 3abc", propParser.Parse(envParser.Parse("abc%{test_property_${GO_STAGE_NAME}}abc")));
}

@Test
public void testEnvVarAsPropVarNameInParameter() throws Exception {
EnvVarParamParser envParser = new EnvVarParamParser(context.getEnvironmentVariables(), mockConsole);
JobPropParamParser propParser = new JobPropParamParser(context.getEnvironmentVariables(), mockConsole);

Assert.assertEquals("Test Value 1TestPluginStage", propParser.Parse(envParser.Parse("%{test_property_1}${GO_STAGE_NAME}")));
Assert.assertEquals("Test Value 1_TestPluginStage", propParser.Parse(envParser.Parse("%{test_property_1}_${GO_STAGE_NAME}")));
Assert.assertEquals("abcTest Value 1_TestPluginStage", propParser.Parse(envParser.Parse("abc%{test_property_1}_${GO_STAGE_NAME}")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static Context getDefaultContext() {

public static Map getDefaultEnvVarMap(){
Map envVars = new HashMap();
envVars.put("GO_SERVER_URL","http://localhost:8153/go/" );
envVars.put("GO_SERVER_URL","https://localhost:8154/go/" );
envVars.put("GO_PIPELINE_NAME", "TestPluginsPipeline");
envVars.put("GO_PIPELINE_COUNTER", "1");
envVars.put("GO_STAGE_NAME", "TestPluginStage");
Expand Down

0 comments on commit e03dd06

Please sign in to comment.