11package com .assertthat .plugins .internal ;
22
33import 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 .*;
195import org .codehaus .jettison .json .JSONException ;
206import org .codehaus .jettison .json .JSONObject ;
217
22- import javax .ws .rs .core .MediaType ;
23- import javax .ws .rs .core .MultivaluedMap ;
248import java .io .*;
25- import java .net .URLEncoder ;
26- import java .nio . charset . StandardCharsets ;
9+ import java .net .MalformedURLException ;
10+ import java .net . URL ;
2711import 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