Skip to content

Commit e230287

Browse files
committed
Switch to OkHttp client; Add ignoreCertErrors options
1 parent e9d3c35 commit e230287

File tree

5 files changed

+205
-133
lines changed

5 files changed

+205
-133
lines changed

pom.xml

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>com.assertthat.plugins</groupId>
77
<artifactId>assertthat-bdd-standalone</artifactId>
8-
<version>1.9.4</version>
8+
<version>1.9.5</version>
99
<name>assertthat-bdd-standalone</name>
1010
<description>AssertThat BDD Jira plugin client standalone</description>
1111
<url>http://www.assertthat.com</url>
@@ -25,7 +25,7 @@
2525
<url>https://github.com/assertthat/assertthat-bdd-standalone</url>
2626
<connection>scm:git:git@github.com:assertthat/assertthat-bdd-standalone.git</connection>
2727
<developerConnection>scm:git:git@github.com:assertthat/assertthat-bdd-standalone.git</developerConnection>
28-
<tag>v1.9.4</tag>
28+
<tag>v1.9.5</tag>
2929
</scm>
3030
<developers>
3131
<developer>
@@ -51,21 +51,11 @@
5151
<artifactId>commons-cli</artifactId>
5252
<version>1.4</version>
5353
</dependency>
54-
<dependency>
55-
<groupId>com.sun.jersey.contribs</groupId>
56-
<artifactId>jersey-apache-client4</artifactId>
57-
<version>1.19.4</version>
58-
</dependency>
5954
<dependency>
6055
<groupId>org.apache.maven.plugin-tools</groupId>
6156
<artifactId>maven-plugin-tools-ant</artifactId>
6257
<version>3.6.0</version>
6358
</dependency>
64-
<dependency>
65-
<groupId>com.sun.jersey.contribs</groupId>
66-
<artifactId>jersey-multipart</artifactId>
67-
<version>1.19.4</version>
68-
</dependency>
6959
<dependency>
7060
<groupId>org.codehaus.jettison</groupId>
7161
<artifactId>jettison</artifactId>
@@ -76,6 +66,11 @@
7666
<artifactId>guava</artifactId>
7767
<version>23.0</version>
7868
</dependency>
69+
<dependency>
70+
<groupId>com.squareup.okhttp3</groupId>
71+
<artifactId>okhttp</artifactId>
72+
<version>3.3.1</version>
73+
</dependency>
7974
</dependencies>
8075

8176
<build>

src/main/java/com/assertthat/plugins/api/Main.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ public static void main(String[] args) throws IOException, JSONException {
128128
numberedOption.setArgName("true|false");
129129
options.addOption(numberedOption);
130130

131+
Option ignoreCertErrors = new Option("e", "ignoreCertErrors", true, "Ignore ssl certificate eerors (default is false)");
132+
numberedOption.setRequired(false);
133+
numberedOption.setArgName("true|false");
134+
options.addOption(ignoreCertErrors);
135+
131136
Option reportOption = new Option("r", "report", false, "Upload report");
132137
reportOption.setRequired(false);
133138

@@ -159,9 +164,13 @@ public static void main(String[] args) throws IOException, JSONException {
159164
System.exit(0);
160165
}
161166
boolean isNumbered = true;
162-
if(cmd.hasOption("numbered") && cmd.getOptionValue("numbered") !=null && cmd.getOptionValue("numbered").equals("false")){
167+
if (cmd.hasOption("numbered") && cmd.getOptionValue("numbered") != null && cmd.getOptionValue("numbered").equals("false")) {
163168
isNumbered = false;
164169
}
170+
boolean ignoreCertErrorsVal = false;
171+
if (cmd.hasOption("ignoreCertErrors") && cmd.getOptionValue("ignoreCertErrors") != null && cmd.getOptionValue("ignoreCertErrors").equals("true")) {
172+
ignoreCertErrorsVal = true;
173+
}
165174
Arguments arguments = new Arguments(
166175
cmd.getOptionValue("accessKey"),
167176
cmd.getOptionValue("secretKey"),
@@ -178,15 +187,16 @@ public static void main(String[] args) throws IOException, JSONException {
178187
cmd.getOptionValue("tags"),
179188
cmd.getOptionValue("type"),
180189
cmd.getOptionValue("jiraServerUrl"),
190+
ignoreCertErrorsVal,
181191
isNumbered
182192
);
183193

184-
APIUtil apiUtil = new APIUtil(arguments.getProjectId(), arguments.getAccessKey(), arguments.getSecretKey(), arguments.getProxyURI(), arguments.getProxyUsername(), arguments.getProxyPassword(), arguments.getJiraServerUrl());
194+
APIUtil apiUtil = new APIUtil(arguments.getProjectId(), arguments.getAccessKey(), arguments.getSecretKey(), arguments.getProxyURI(), arguments.getProxyUsername(), arguments.getProxyPassword(), arguments.getJiraServerUrl(), ignoreCertErrorsVal);
185195

186196
if (cmd.hasOption("features")) {
187197
File inZip =
188198
apiUtil.download(new File(arguments.getOutputFolder()),
189-
arguments.getMode(), arguments.getJql() ,
199+
arguments.getMode(), arguments.getJql(),
190200
arguments.getTags(), arguments.isNumbered());
191201
File zip = new FileUtil().unpackArchive(inZip, new File(arguments.getOutputFolder()));
192202
zip.delete();
@@ -195,7 +205,7 @@ public static void main(String[] args) throws IOException, JSONException {
195205
String[] files = new FileUtil().findJsonFiles(new File(arguments.getJsonReportFolder()), arguments.getJsonReportIncludePattern(), null);
196206
Long runid = -1L;
197207
for (String f : files) {
198-
runid = apiUtil.upload(runid, arguments.getRunName(), arguments.getJsonReportFolder() + f, arguments.getType(),null, arguments.getJql());
208+
runid = apiUtil.upload(runid, arguments.getRunName(), arguments.getJsonReportFolder() + f, arguments.getType(), null, arguments.getJql());
199209
}
200210
}
201211
}
Lines changed: 64 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,15 @@
11
package com.assertthat.plugins.internal;
22

33
import com.google.common.net.UrlEscapers;
4-
import com.sun.jersey.api.client.*;
5-
import com.sun.jersey.api.client.config.DefaultClientConfig;
6-
import com.sun.jersey.api.client.filter.ClientFilter;
7-
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
8-
import com.sun.jersey.client.apache4.ApacheHttpClient4;
9-
import com.sun.jersey.core.header.FormDataContentDisposition;
10-
import com.sun.jersey.core.util.MultivaluedMapImpl;
11-
import com.sun.jersey.multipart.Boundary;
12-
import com.sun.jersey.multipart.FormDataMultiPart;
13-
import com.sun.jersey.multipart.MultiPart;
14-
import com.sun.jersey.multipart.file.FileDataBodyPart;
15-
import com.sun.jersey.multipart.impl.MultiPartWriter;
16-
import org.apache.commons.io.FileUtils;
17-
import org.apache.commons.io.IOUtils;
18-
import org.apache.http.HttpHeaders;
4+
import okhttp3.*;
195
import org.codehaus.jettison.json.JSONException;
206
import org.codehaus.jettison.json.JSONObject;
217

22-
import javax.ws.rs.core.MediaType;
23-
import javax.ws.rs.core.MultivaluedMap;
248
import java.io.*;
25-
import java.net.URLEncoder;
26-
import java.nio.charset.StandardCharsets;
9+
import java.net.MalformedURLException;
10+
import java.net.URL;
2711
import java.util.logging.Logger;
2812

29-
import static javax.ws.rs.core.MediaType.MULTIPART_FORM_DATA_TYPE;
30-
3113
/**
3214
* Copyright (c) 2018 AssertThat
3315
* <p>
@@ -55,33 +37,37 @@ public class APIUtil {
5537

5638
private final static Logger LOGGER = Logger.getLogger(APIUtil.class.getName());
5739
private final static String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0";
58-
private String accessKey;
59-
private String secretKey;
60-
private String projectId;
6140
private String featuresUrl;
6241
private String reportUrl;
63-
private DefaultClientConfig config = new DefaultClientConfig();
42+
private OkHttpClient client;
6443

65-
public APIUtil(String projectId, String accessKey, String secretKey, String proxyURI, String proxyUsername, String proxyPassword, String jiraServerURL) {
66-
this.accessKey = accessKey;
67-
this.secretKey = secretKey;
68-
this.projectId = projectId;
69-
if (proxyURI != null && !proxyURI.trim().isEmpty()) {
70-
this.config.getProperties().put("com.sun.jersey.impl.client.httpclient.proxyURI", proxyURI);
44+
public APIUtil(String projectId, String accessKey, String secretKey, String proxyURI, String proxyUsername, String proxyPassword, String jiraServerURL, boolean ignoreCertErrors) {
45+
if (jiraServerURL != null) {
46+
this.featuresUrl = jiraServerURL + "/rest/assertthat/latest/project/" + projectId + "/client/features";
47+
this.reportUrl = jiraServerURL + "/rest/assertthat/latest/project/" + projectId + "/client/report";
48+
} else {
49+
this.featuresUrl = "https://bdd.assertthat.app/rest/api/1/project/" + projectId + "/features";
50+
this.reportUrl = "https://bdd.assertthat.app/rest/api/1/project/" + projectId + "/report";
7151
}
72-
if (proxyUsername != null && !proxyUsername.trim().isEmpty()) {
73-
this.config.getProperties().put("com.sun.jersey.impl.client.httpclient.proxyUsername", proxyUsername);
52+
OkHttpClientBuilder builder = new OkHttpClientBuilder();
53+
builder.authenticated(accessKey, secretKey);
54+
if (ignoreCertErrors) {
55+
builder.ignoringCertificate();
7456
}
75-
if (proxyPassword != null && !proxyPassword.trim().isEmpty()) {
76-
this.config.getProperties().put("com.sun.jersey.impl.client.httpclient.proxyPassword", proxyPassword);
57+
if (proxyURI != null && !proxyURI.trim().isEmpty()) {
58+
URL url;
59+
try {
60+
url = new URL(proxyURI);
61+
} catch (MalformedURLException e) {
62+
throw new RuntimeException("[ERROR] Parsing proxy URL: " + e.getMessage());
63+
}
64+
builder.withProxy(url.getHost(), url.getPort());
7765
}
78-
if(jiraServerURL!=null) {
79-
this.featuresUrl = jiraServerURL+"/rest/assertthat/latest/project/"+projectId+"/client/features";
80-
this.reportUrl = jiraServerURL+"/rest/assertthat/latest/project/"+projectId+"/client/report";
81-
}else{
82-
this.featuresUrl = "https://bdd.assertthat.app/rest/api/1/project/" + projectId + "/features";
83-
this.reportUrl = "https://bdd.assertthat.app/rest/api/1/project/" + projectId + "/report";
66+
if (proxyUsername != null && !proxyUsername.trim().isEmpty() && proxyPassword != null
67+
&& !proxyPassword.trim().isEmpty()) {
68+
builder.withProxyAuth(proxyUsername, proxyPassword);
8469
}
70+
client = builder.build();
8571
}
8672

8773
public static void copyInputStream(InputStream in, OutputStream out) throws IOException {
@@ -103,97 +89,64 @@ public File download(File targetDir, String mode, String jql,
10389
f.delete();
10490
}
10591
}
106-
}else{
92+
} else {
10793
targetDir.mkdirs();
10894
}
109-
Client client = ApacheHttpClient4.create(config);
110-
client.addFilter(new HTTPBasicAuthFilter(this.accessKey, this.secretKey));
111-
WebResource webResource = client.resource(this.featuresUrl);
112-
MultivaluedMap queryParams = new MultivaluedMapImpl();
95+
HttpUrl.Builder httpBuilder = HttpUrl.parse(this.featuresUrl).newBuilder();
11396
if (mode != null) {
114-
queryParams.add("mode", mode.trim());
97+
httpBuilder.addQueryParameter("mode", mode.trim());
11598
}
11699
if (tags != null) {
117-
queryParams.add("tags", tags.trim());
100+
httpBuilder.addQueryParameter("tags", tags.trim());
118101
}
119102
if (jql != null) {
120-
queryParams.add("jql", jql.trim());
103+
httpBuilder.addQueryParameter("jql", jql.trim());
121104
}
122-
queryParams.add("numbered", String.valueOf(isNumbered));
123-
client.addFilter(new ClientFilter() {
124-
@Override
125-
public ClientResponse handle(ClientRequest
126-
request)
127-
throws ClientHandlerException {
128-
request.getHeaders().add(
129-
HttpHeaders.USER_AGENT,
130-
USER_AGENT);
131-
return getNext().handle(request);
132-
}
133-
});
134-
ClientResponse response = webResource.queryParams(queryParams).get(ClientResponse.class);
135-
if (response.getStatus() != 200) {
136-
throw new RuntimeException(response.getStatusInfo().getReasonPhrase());
105+
httpBuilder.addQueryParameter("numbered", String.valueOf(isNumbered));
106+
Request.Builder request = new Request.Builder().url(httpBuilder.build()).addHeader("User-Agent",
107+
USER_AGENT);
108+
Response response = client.newCall(request.build()).execute();
109+
if (!response.isSuccessful()) {
110+
throw new IOException("Failed to download file: " + response);
137111
}
138-
InputStream in = new BufferedInputStream(response.getEntity(InputStream.class));
139112
File zip = File.createTempFile("arc", ".zip", targetDir);
140-
OutputStream out = new BufferedOutputStream(new FileOutputStream(zip));
141-
copyInputStream(in, out);
142-
out.close();
113+
FileOutputStream fos = new FileOutputStream(zip);
114+
fos.write(response.body().bytes());
115+
fos.close();
143116
return zip;
144117
}
145118

146119
public Long upload(Long runId, String runName, String filePath, String type, String metadata, String jql) throws IOException, JSONException {
147-
config.getClasses().add(FormDataMultiPart.class);
148-
config.getClasses().add(MultiPartWriter.class);
149-
Client client = ApacheHttpClient4.create(config);
150-
client.addFilter(new HTTPBasicAuthFilter(this.accessKey, this.secretKey));
151-
WebResource webResource = client.resource(this.reportUrl);
152-
MultivaluedMap queryParams = new MultivaluedMapImpl();
153-
queryParams.add("runName", runName);
154-
queryParams.add("runId", runId.toString());
155-
queryParams.add("type", type);
120+
File fileToUpload = new File(filePath);
121+
RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM)
122+
.addFormDataPart("file", fileToUpload.getName(),
123+
RequestBody.create(MediaType.parse("application/json"), fileToUpload))
124+
.addFormDataPart("some-field", "some-value")
125+
.build();
126+
HttpUrl.Builder httpBuilder = HttpUrl.parse(this.reportUrl).newBuilder();
127+
httpBuilder.addQueryParameter("runName", runName);
128+
httpBuilder.addQueryParameter("runId", runId.toString());
129+
httpBuilder.addQueryParameter("type", type);
156130
if (jql != null) {
157-
queryParams.add("jql", jql.trim());
131+
httpBuilder.addQueryParameter("jql", jql.trim());
158132
}
159-
if(metadata!=null) {
160-
queryParams.add("metadata", UrlEscapers.urlFragmentEscaper().escape(metadata));
133+
if (metadata != null) {
134+
httpBuilder.addQueryParameter("metadata", UrlEscapers.urlFragmentEscaper().escape(metadata));
161135
}
162-
client.addFilter(new ClientFilter() {
163-
@Override
164-
public ClientResponse handle(ClientRequest
165-
request)
166-
throws ClientHandlerException {
167-
request.getHeaders().add(
168-
HttpHeaders.USER_AGENT,
169-
USER_AGENT);
170-
return getNext().handle(request);
171-
}
172-
});
173-
File fileToUpload = new File(filePath);
174-
FileDataBodyPart fileDataBodyPart = new FileDataBodyPart("file",
175-
fileToUpload,
176-
MediaType.APPLICATION_OCTET_STREAM_TYPE);
177-
fileDataBodyPart.setContentDisposition(
178-
FormDataContentDisposition.name("file")
179-
.fileName(fileToUpload.getName()).build());
180-
final MultiPart multiPart = new FormDataMultiPart()
181-
.bodyPart(fileDataBodyPart);
182-
multiPart.setMediaType(MULTIPART_FORM_DATA_TYPE);
183-
ClientResponse response = webResource
184-
.queryParams(queryParams)
185-
.type(MULTIPART_FORM_DATA_TYPE)
186-
.type(Boundary.addBoundary(MULTIPART_FORM_DATA_TYPE))
187-
.post(ClientResponse.class, multiPart);
188-
189-
if (response.getStatus() == 200) {
190-
String responseBody = IOUtils.toString(response.getEntityInputStream(), "UTF-8");
191-
JSONObject responseJson = new JSONObject(responseBody);
136+
Request request = new Request.Builder()
137+
.url(httpBuilder.build())
138+
.post(requestBody)
139+
.addHeader("User-Agent", USER_AGENT)
140+
.build();
141+
Response response = client.newCall(request).execute();
142+
if (response.isSuccessful()) {
143+
JSONObject responseJson = new JSONObject(response.body().string());
192144
return Long.valueOf(responseJson.getString("runId"));
193145
} else {
194-
LOGGER.warning(IOUtils.toString(response.getEntityInputStream(), "UTF-8"));
146+
LOGGER.warning(response.body().string());
195147
LOGGER.warning("Failed to process " + filePath);
196148
return runId;
197149
}
198150
}
151+
199152
}

0 commit comments

Comments
 (0)