src/main/java/org/embulk/input/jira/client/JiraClient.java in embulk-input-jira-0.2.9 vs src/main/java/org/embulk/input/jira/client/JiraClient.java in embulk-input-jira-0.2.10

- old
+ new

@@ -1,9 +1,8 @@ package org.embulk.input.jira.client; import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Optional; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.google.gson.JsonPrimitive; @@ -22,19 +21,20 @@ import org.embulk.config.ConfigException; import org.embulk.input.jira.Issue; import org.embulk.input.jira.JiraInputPlugin.PluginTask; import org.embulk.input.jira.util.JiraException; import org.embulk.input.jira.util.JiraUtil; -import org.embulk.spi.Exec; import org.embulk.spi.util.RetryExecutor.RetryGiveupException; import org.embulk.spi.util.RetryExecutor.Retryable; import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Map.Entry; +import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; import java.util.stream.StreamSupport; import static java.util.Base64.getEncoder; @@ -47,40 +47,40 @@ public class JiraClient { public JiraClient() {} - private static final Logger LOGGER = Exec.getLogger(JiraClient.class); + private static final Logger LOGGER = LoggerFactory.getLogger(JiraClient.class); public void checkUserCredentials(final PluginTask task) { try { authorizeAndRequest(task, JiraUtil.buildPermissionUrl(task.getUri()), null); } - catch (JiraException e) { + catch (final JiraException e) { LOGGER.error(String.format("JIRA return status (%s), reason (%s)", e.getStatusCode(), e.getMessage())); if (e.getStatusCode() == HttpStatus.SC_UNAUTHORIZED) { throw new ConfigException("Could not authorize with your credential."); } else { throw new ConfigException("Could not authorize with your credential due to problems when contacting JIRA API."); } } } - public List<Issue> searchIssues(final PluginTask task, int startAt, int maxResults) + public List<Issue> searchIssues(final PluginTask task, final int startAt, final int maxResults) { - String response = searchJiraAPI(task, startAt, maxResults); - JsonObject result = new JsonParser().parse(response).getAsJsonObject(); + final String response = searchJiraAPI(task, startAt, maxResults); + final JsonObject result = new JsonParser().parse(response).getAsJsonObject(); return StreamSupport.stream(result.get("issues").getAsJsonArray().spliterator(), false) .map(jsonElement -> { - JsonObject json = jsonElement.getAsJsonObject(); - JsonObject fields = json.get("fields").getAsJsonObject(); - Set<Entry<String, JsonElement>> entries = fields.entrySet(); + final JsonObject json = jsonElement.getAsJsonObject(); + final JsonObject fields = json.get("fields").getAsJsonObject(); + final Set<Entry<String, JsonElement>> entries = fields.entrySet(); json.remove("fields"); // Merged all properties in fields to the object - for (Entry<String, JsonElement> entry : entries) { + for (final Entry<String, JsonElement> entry : entries) { json.add(entry.getKey(), entry.getValue()); } return new Issue(json); }) .collect(Collectors.toList()); @@ -89,11 +89,11 @@ public int getTotalCount(final PluginTask task) { return new JsonParser().parse(searchJiraAPI(task, 0, MIN_RESULTS)).getAsJsonObject().get("total").getAsInt(); } - private String searchJiraAPI(final PluginTask task, int startAt, int maxResults) + private String searchJiraAPI(final PluginTask task, final int startAt, final int maxResults) { try { return retryExecutor().withRetryLimit(task.getRetryLimit()) .withInitialRetryWait(task.getInitialRetryIntervalMillis()) .withMaxRetryWait(task.getMaximumRetryIntervalMillis()) @@ -104,46 +104,46 @@ { return authorizeAndRequest(task, JiraUtil.buildSearchUrl(task.getUri()), createSearchIssuesBody(task, startAt, maxResults)); } @Override - public boolean isRetryableException(Exception exception) + public boolean isRetryableException(final Exception exception) { if (exception instanceof JiraException) { - int statusCode = ((JiraException) exception).getStatusCode(); + final int statusCode = ((JiraException) exception).getStatusCode(); // When overloading JIRA APIs (i.e 100 requests per second) the API will return 401 although the credential is correct. So add retry for this // 429 is stand for "Too many requests" // Other 4xx considered errors return statusCode / 100 != 4 || statusCode == HttpStatus.SC_UNAUTHORIZED || statusCode == 429; } return false; } @Override - public void onRetry(Exception exception, int retryCount, int retryLimit, int retryWait) + public void onRetry(final Exception exception, final int retryCount, final int retryLimit, final int retryWait) throws RetryGiveupException { if (exception instanceof JiraException) { - String message = String + final String message = String .format("Retrying %d/%d after %d seconds. HTTP status code: %s", retryCount, retryLimit, retryWait / 1000, ((JiraException) exception).getStatusCode()); LOGGER.warn(message); } else { - String message = String + final String message = String .format("Retrying %d/%d after %d seconds. Message: %s", retryCount, retryLimit, retryWait / 1000, exception.getMessage()); LOGGER.warn(message, exception); } } @Override - public void onGiveup(Exception firstException, Exception lastException) throws RetryGiveupException + public void onGiveup(final Exception firstException, final Exception lastException) throws RetryGiveupException { LOGGER.warn("Retry Limit Exceeded"); } }); } @@ -153,11 +153,11 @@ } throw new ConfigException(e); } } - private String authorizeAndRequest(final PluginTask task, String url, String body) throws JiraException + private String authorizeAndRequest(final PluginTask task, final String url, final String body) throws JiraException { try (CloseableHttpClient client = createHttpClient()) { HttpRequestBase request; if (body == null) { request = createGetRequest(task, url); @@ -165,32 +165,32 @@ else { request = createPostRequest(task, url, body); } try (CloseableHttpResponse response = client.execute(request)) { // Check for HTTP response code : 200 : SUCCESS - int statusCode = response.getStatusLine().getStatusCode(); + final int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != HttpStatus.SC_OK) { throw new JiraException(statusCode, extractErrorMessages(EntityUtils.toString(response.getEntity()))); } return EntityUtils.toString(response.getEntity()); } } - catch (IOException e) { + catch (final IOException e) { throw new JiraException(-1, e.getMessage()); } } - private String extractErrorMessages(String errorResponse) + private String extractErrorMessages(final String errorResponse) { - List<String> messages = new ArrayList<>(); + final List<String> messages = new ArrayList<>(); try { - JsonObject errorObject = new JsonParser().parse(errorResponse).getAsJsonObject(); - for (JsonElement element : errorObject.get("errorMessages").getAsJsonArray()) { + final JsonObject errorObject = new JsonParser().parse(errorResponse).getAsJsonObject(); + for (final JsonElement element : errorObject.get("errorMessages").getAsJsonArray()) { messages.add(element.getAsString()); } } - catch (Exception e) { + catch (final Exception e) { messages.add(errorResponse); } return String.join(" , ", messages); } @@ -205,13 +205,13 @@ .setCookieSpec(CookieSpecs.STANDARD) .build()) .build(); } - private HttpRequestBase createPostRequest(PluginTask task, String url, String body) throws IOException + private HttpRequestBase createPostRequest(final PluginTask task, final String url, final String body) throws IOException { - HttpPost request = new HttpPost(url); + final HttpPost request = new HttpPost(url); switch (task.getAuthMethod()) { default: request.setHeader( AUTHORIZATION, String.format("Basic %s", @@ -224,13 +224,13 @@ } request.setEntity(new StringEntity(body)); return request; } - private HttpRequestBase createGetRequest(PluginTask task, String url) + private HttpRequestBase createGetRequest(final PluginTask task, final String url) { - HttpGet request = new HttpGet(url); + final HttpGet request = new HttpGet(url); switch (task.getAuthMethod()) { default: request.setHeader( AUTHORIZATION, String.format("Basic %s", @@ -242,17 +242,17 @@ break; } return request; } - private String createSearchIssuesBody(PluginTask task, int startAt, int maxResults) + private String createSearchIssuesBody(final PluginTask task, final int startAt, final int maxResults) { - JsonObject body = new JsonObject(); - Optional<String> jql = task.getJQL(); - body.add("jql", new JsonPrimitive(jql.or(""))); + final JsonObject body = new JsonObject(); + final Optional<String> jql = task.getJQL(); + body.add("jql", new JsonPrimitive(jql.orElse(""))); body.add("startAt", new JsonPrimitive(startAt)); body.add("maxResults", new JsonPrimitive(maxResults)); - JsonArray fields = new JsonArray(); + final JsonArray fields = new JsonArray(); fields.add("*all"); body.add("fields", fields); return body.toString(); } }