diff --git a/tasks/jira/pom.xml b/tasks/jira/pom.xml index 97a80d36..8b891bde 100644 --- a/tasks/jira/pom.xml +++ b/tasks/jira/pom.xml @@ -73,12 +73,6 @@ provided - - com.squareup.okhttp - okhttp - 2.7.5 - - org.junit.jupiter junit-jupiter-api diff --git a/tasks/jira/src/main/java/com/walmartlabs/concord/plugins/jira/JiraClient.java b/tasks/jira/src/main/java/com/walmartlabs/concord/plugins/jira/JiraClient.java deleted file mode 100644 index 1a8bf84d..00000000 --- a/tasks/jira/src/main/java/com/walmartlabs/concord/plugins/jira/JiraClient.java +++ /dev/null @@ -1,176 +0,0 @@ -package com.walmartlabs.concord.plugins.jira; - -/*- - * ***** - * Concord - * ----- - * Copyright (C) 2017 - 2019 Walmart Inc. - * ----- - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ===== - */ - -import com.fasterxml.jackson.databind.JavaType; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; -import com.squareup.okhttp.Call; -import com.squareup.okhttp.MediaType; -import com.squareup.okhttp.MultipartBuilder; -import com.squareup.okhttp.OkHttpClient; -import com.squareup.okhttp.Request; -import com.squareup.okhttp.RequestBody; -import com.squareup.okhttp.Response; -import com.squareup.okhttp.ResponseBody; - -import java.io.File; -import java.io.IOException; -import java.net.URI; -import java.nio.file.Files; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.concurrent.TimeUnit; - -public class JiraClient implements JiraHttpClient { - - private static final ObjectMapper MAPPER = new ObjectMapper() - .registerModule(new Jdk8Module()); - static final JavaType MAP_TYPE = MAPPER.getTypeFactory() - .constructMapType(HashMap.class, String.class, Object.class); - private static final JavaType LIST_OF_MAPS_TYPE = MAPPER.getTypeFactory() - .constructCollectionType(List.class, MAP_TYPE); - - private static final OkHttpClient client = new OkHttpClient(); - private final JiraClientCfg cfg; - private URI uri; - private int successCode; - private String auth; - - public JiraClient(JiraClientCfg cfg) { - this.cfg = cfg; - } - - @Override - public JiraHttpClient url(String url) { - this.uri = URI.create(url); - return this; - } - - @Override - public JiraHttpClient successCode(int successCode) { - this.successCode = successCode; - return this; - } - - @Override - public JiraHttpClient jiraAuth(String auth) { - this.auth = auth; - return this; - } - - @Override - public Map get() throws IOException { - Request request = requestBuilder(auth) - .url(uri.toURL()) - .get() - .build(); - - return call(request, MAP_TYPE); - } - - @Override - public Map post(Map data) throws IOException { - RequestBody body = RequestBody.create( - MediaType.parse("application/json; charset=utf-8"), MAPPER.writeValueAsString(data)); - Request request = requestBuilder(auth) - .url(uri.toURL()) - .post(body) - .build(); - - return call(request, MAP_TYPE); - } - - @Override - public void post(File file) throws IOException { - MultipartBuilder b = new MultipartBuilder(Constants.BOUNDARY).type(MultipartBuilder.FORM); - b.addFormDataPart("file", file.getName(), - RequestBody.create(MediaType.parse("application/octet-stream"), Files.readAllBytes(file.toPath()))); - - RequestBody body = b.build(); - Request request = requestBuilder(auth) - .header("X-Atlassian-Token", "nocheck") - .url(uri.toURL()) - .post(body) - .build(); - - call(request, LIST_OF_MAPS_TYPE); - } - - @Override - public void put(Map data) throws IOException { - RequestBody body = RequestBody.create( - MediaType.parse("application/json; charset=utf-8"), MAPPER.writeValueAsString(data)); - Request request = requestBuilder(auth) - .url(uri.toURL()) - .put(body) - .build(); - - call(request, MAP_TYPE); - } - - @Override - public void delete() throws IOException { - Request request = requestBuilder(auth) - .url(uri.toURL()) - .delete() - .build(); - - call(request, MAP_TYPE); - } - - private static Request.Builder requestBuilder(String auth) { - return new Request.Builder() - .addHeader("Authorization", auth) - .addHeader("Accept", "application/json"); - } - - - T call(Request request, JavaType returnType) throws IOException { - setClientTimeoutParams(cfg); - - Call call = client.newCall(request); - Response response = call.execute(); - - try (ResponseBody responseBody = response.body()) { - String results = null; - if (responseBody != null) { - results = responseBody.string(); - } - - int statusCode = response.code(); - JiraHttpClient.assertResponseCode(statusCode, results, successCode); - - if (results == null || statusCode == 204) { - return null; - } else { - return MAPPER.readValue(results, returnType); - } - } - } - - private static void setClientTimeoutParams(JiraClientCfg cfg) { - client.setConnectTimeout(cfg.connectTimeout(), TimeUnit.SECONDS); - client.setReadTimeout(cfg.readTimeout(), TimeUnit.SECONDS); - client.setWriteTimeout(cfg.writeTimeout(), TimeUnit.SECONDS); - } -} diff --git a/tasks/jira/src/main/java/com/walmartlabs/concord/plugins/jira/JiraHttpClientFactory.java b/tasks/jira/src/main/java/com/walmartlabs/concord/plugins/jira/JiraHttpClientFactory.java deleted file mode 100644 index 51d6d4c4..00000000 --- a/tasks/jira/src/main/java/com/walmartlabs/concord/plugins/jira/JiraHttpClientFactory.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.walmartlabs.concord.plugins.jira; - -/*- - * ***** - * Concord - * ----- - * Copyright (C) 2017 - 2024 Walmart Inc., Concord Authors - * ----- - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ===== - */ - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class JiraHttpClientFactory { - - private static final Logger log = LoggerFactory.getLogger(JiraHttpClientFactory.class); - - private JiraHttpClientFactory() { - throw new IllegalStateException("instantiation is not allowed"); - } - - public static JiraHttpClient create(JiraClientCfg cfg) { - try { - return new NativeJiraHttpClient(cfg); - } catch (NoClassDefFoundError e) { - // client2 may not exist - log.info("Falling back to okhttp client"); - } - - try { - return new JiraClient(cfg); - } catch (Exception e) { - // that's very unexpected as long as okhttp is still allowed - throw new IllegalStateException("No jira http client found"); - } - } - -} diff --git a/tasks/jira/src/main/java/com/walmartlabs/concord/plugins/jira/JiraTaskCommon.java b/tasks/jira/src/main/java/com/walmartlabs/concord/plugins/jira/JiraTaskCommon.java index abb3a78d..db8c9895 100644 --- a/tasks/jira/src/main/java/com/walmartlabs/concord/plugins/jira/JiraTaskCommon.java +++ b/tasks/jira/src/main/java/com/walmartlabs/concord/plugins/jira/JiraTaskCommon.java @@ -495,25 +495,17 @@ JiraHttpClient getClient(TaskParams in) { return getNativeClient(in); } catch (NoClassDefFoundError e) { // client2 may not exist - log.info("Falling back to okhttp client"); + log.info("Error while creating jira http client: {}", e.getMessage()); + log.info("Add com.walmartlabs.concord.client2 to classpath?"); } - try { - return getOkHttpClient(in); - } catch (Exception | NoClassDefFoundError e) { - // that's very unexpected as long as okhttp is still allowed - throw new IllegalStateException("No jira http client found"); - } + throw new IllegalStateException("Unexpected error while creating JiraHttpClient."); } JiraHttpClient getNativeClient(TaskParams in) { return new NativeJiraHttpClient(in); } - JiraHttpClient getOkHttpClient(TaskParams in) { - return new JiraClient(in); - } - JiraSecretService getSecretService() { return secretService; } diff --git a/tasks/jira/src/test/java/com/walmartlabs/concord/plugins/jira/CommonClientLoaderTest.java b/tasks/jira/src/test/java/com/walmartlabs/concord/plugins/jira/CommonClientLoaderTest.java index c3e146e4..47226dc3 100644 --- a/tasks/jira/src/test/java/com/walmartlabs/concord/plugins/jira/CommonClientLoaderTest.java +++ b/tasks/jira/src/test/java/com/walmartlabs/concord/plugins/jira/CommonClientLoaderTest.java @@ -56,27 +56,15 @@ void testLoadClient() { } @Test - void testLoadClientFallback() { + void testLoadNotFound() { Map input = new HashMap<>(); input.put("action", "getIssues"); doThrow(new NoClassDefFoundError()).when(delegate).getNativeClient(any()); - var client = delegate.getClient(TaskParams.of(new MapBackedVariables(input), Map.of())); - assertInstanceOf(JiraClient.class, client); - } - - @Test - void testNoClient() { - Map input = new HashMap<>(); - input.put("action", "getIssues"); - - doThrow(new NoClassDefFoundError()).when(delegate).getNativeClient(any()); - doThrow(new NoClassDefFoundError()).when(delegate).getOkHttpClient(any()); - - var params = TaskParams.of(new MapBackedVariables(input), Map.of()); + var ex = assertThrows(IllegalStateException.class, + () -> delegate.getClient(TaskParams.of(new MapBackedVariables(input), Map.of()))); - var expected = assertThrows(IllegalStateException.class, () -> delegate.getClient(params)); - assertTrue(expected.getMessage().contains("No jira http client found")); + assertTrue(ex.getMessage().contains("Unexpected error while creating JiraHttpClient")); } } diff --git a/tasks/jira/src/test/java/com/walmartlabs/concord/plugins/jira/CommonTest.java b/tasks/jira/src/test/java/com/walmartlabs/concord/plugins/jira/CommonTest.java index 3642a45c..aeec2505 100644 --- a/tasks/jira/src/test/java/com/walmartlabs/concord/plugins/jira/CommonTest.java +++ b/tasks/jira/src/test/java/com/walmartlabs/concord/plugins/jira/CommonTest.java @@ -53,7 +53,7 @@ class CommonTest { @Mock - JiraClient jiraClient; + JiraHttpClient jiraClient; @Mock JiraSecretService jiraSecretService; diff --git a/tasks/jira/src/test/java/com/walmartlabs/concord/plugins/jira/JiraClientTest.java b/tasks/jira/src/test/java/com/walmartlabs/concord/plugins/jira/JiraClientTest.java index 1e6056b9..e1b8f9c4 100644 --- a/tasks/jira/src/test/java/com/walmartlabs/concord/plugins/jira/JiraClientTest.java +++ b/tasks/jira/src/test/java/com/walmartlabs/concord/plugins/jira/JiraClientTest.java @@ -24,7 +24,7 @@ class JiraClientTest extends AbstractWiremockTest { @Override JiraHttpClient getClient(JiraClientCfg cfg) { - return new JiraClient(cfg); + return new NativeJiraHttpClient(cfg); } }