Skip to content

Commit

Permalink
restructured constants file
Browse files Browse the repository at this point in the history
Signed-off-by: Santhosh Gandhe <[email protected]>
  • Loading branch information
san81 committed Nov 1, 2024
1 parent d3c3a33 commit 1590886
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,25 @@
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.BAD_REQUEST_EXCEPTION;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.CLOSING_ROUND_BRACKET;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.CONTENT_TYPE;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.CREATED;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.DELIMITER;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.GREATER_THAN_EQUALS;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.ISSUE_KEY;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.ISSUE_TYPE_IN;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.KEY;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.LIVE;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.NAME;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.PREFIX;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.PROJECT;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.PROJECT_IN;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.PROJECT_KEY;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.PROJECT_NAME;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.STATUS_IN;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.SUFFIX;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.UPDATED;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants._ISSUE;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants._PROJECT;
import static org.opensearch.dataprepper.plugins.source.jira.utils.JqlConstants.CLOSING_ROUND_BRACKET;
import static org.opensearch.dataprepper.plugins.source.jira.utils.JqlConstants.DELIMITER;
import static org.opensearch.dataprepper.plugins.source.jira.utils.JqlConstants.GREATER_THAN_EQUALS;
import static org.opensearch.dataprepper.plugins.source.jira.utils.JqlConstants.ISSUE_TYPE_IN;
import static org.opensearch.dataprepper.plugins.source.jira.utils.JqlConstants.PREFIX;
import static org.opensearch.dataprepper.plugins.source.jira.utils.JqlConstants.PROJECT_IN;
import static org.opensearch.dataprepper.plugins.source.jira.utils.JqlConstants.STATUS_IN;
import static org.opensearch.dataprepper.plugins.source.jira.utils.JqlConstants.SUFFIX;


/**
Expand All @@ -59,6 +57,7 @@
public class JiraService {


public static final String CONTENT_TYPE = "ContentType";
private static final String SEARCH_RESULTS_FOUND = "searchResultsFound";
private static final Map<String, String> jiraProjectCache = new ConcurrentHashMap<>();

Expand Down Expand Up @@ -225,7 +224,8 @@ private void validateProjectFilters(JiraSourceConfig configuration) {
if (!badFilters.isEmpty()) {
String filters = String.join("\"" + badFilters + "\"", ", ");
log.error("One or more invalid project keys found in filter configuration: {}", badFilters);
throw new BadRequestException(BAD_REQUEST_EXCEPTION
throw new BadRequestException("Bad request exception occurred " +
"Invalid project key found in filter configuration for "
+ filters);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,33 @@
import org.opensearch.dataprepper.plugins.source.jira.exception.UnAuthorizedException;
import org.opensearch.dataprepper.plugins.source.jira.models.SearchResults;
import org.opensearch.dataprepper.plugins.source.jira.rest.auth.JiraAuthConfig;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import javax.inject.Named;
import java.net.URI;
import java.util.List;

import static org.opensearch.dataprepper.logging.DataPrepperMarkers.NOISY;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.AUTHORIZATION_ERROR_CODE;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.EXPAND_FIELD;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.EXPAND_VALUE;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.FIFTY;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.JQL_FIELD;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.MAX_RESULT;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.OAUTH2;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.RATE_LIMIT;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.REST_API_FETCH_ISSUE;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.REST_API_SEARCH;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.RETRY_ATTEMPT;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.RETRY_ATTEMPT_SLEEP_TIME;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.START_AT;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.TOKEN_EXPIRED;
import static org.opensearch.dataprepper.plugins.source.jira.utils.JqlConstants.EXPAND_FIELD;
import static org.opensearch.dataprepper.plugins.source.jira.utils.JqlConstants.EXPAND_VALUE;
import static org.opensearch.dataprepper.plugins.source.jira.utils.JqlConstants.JQL_FIELD;

@Slf4j
@Named
public class JiraRestClient {

public static final String REST_API_SEARCH = "rest/api/3/search";
public static final String REST_API_FETCH_ISSUE = "rest/api/3/issue";
public static final String FIFTY = "50";
public static final String START_AT = "startAt";
public static final String MAX_RESULT = "maxResults";
public static final List<Integer> RETRY_ATTEMPT_SLEEP_TIME = List.of(1, 2, 5, 10, 20, 40);
private static final String TICKET_FETCH_LATENCY_TIMER = "ticketFetchLatency";
private static final String SEARCH_CALL_LATENCY_TIMER = "searchCallLatency";
private static final String ISSUES_REQUESTED = "issuesRequested";
Expand Down Expand Up @@ -105,15 +104,15 @@ private <T> ResponseEntity<T> invokeRestApi(URI uri, Class<T> responseType) {
try {
return restTemplate.getForEntity(uri, responseType);
} catch (HttpClientErrorException ex) {
int statusCode = ex.getRawStatusCode();
HttpStatus statusCode = ex.getStatusCode();
String statusMessage = ex.getMessage();
log.error("An exception has occurred while getting response from Jira search API {}", ex.getMessage(), ex);
if (statusCode == AUTHORIZATION_ERROR_CODE) {
if (statusCode == HttpStatus.FORBIDDEN) {
throw new UnAuthorizedException(statusMessage);
} else if (statusCode == TOKEN_EXPIRED) {
} else if (statusCode == HttpStatus.UNAUTHORIZED) {
log.error(NOISY, "Token expired. We will try to renew the tokens now", ex);
authConfig.renewCredentials();
} else if (statusCode == RATE_LIMIT) {
} else if (statusCode == HttpStatus.TOO_MANY_REQUESTS) {
log.error(NOISY, "Hitting API rate limit. Backing off with sleep timer.", ex);
}
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import lombok.Getter;
import org.opensearch.dataprepper.plugins.source.jira.JiraSourceConfig;
import org.opensearch.dataprepper.plugins.source.jira.exception.UnAuthorizedException;
import org.opensearch.dataprepper.plugins.source.jira.utils.Constants;
import org.slf4j.Logger;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.HttpClientErrorException;
Expand All @@ -17,29 +17,31 @@
import java.util.List;
import java.util.Map;

import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.ACCESSIBLE_RESOURCES;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.OAuth2_URL;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.RETRY_ATTEMPT;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.SLASH;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.TOKEN_EXPIRED;
import static org.opensearch.dataprepper.plugins.source.jira.utils.JqlConstants.SLASH;

/**
* The type Jira service.
*/

public class JiraOauthConfig implements JiraAuthConfig {

public static final String OAuth2_URL = "https://api.atlassian.com/ex/jira/";
public static final String ACCESSIBLE_RESOURCES = "https://api.atlassian.com/oauth/token/accessible-resources";
public static final String TOKEN_LOCATION = "https://auth.atlassian.com/oauth/token";

public static final String EXPIRES_IN = "expires_in";
public static final String REFRESH_TOKEN = "refresh_token";
public static final String ACCESS_TOKEN = "access_token";
private static final Logger log =
org.slf4j.LoggerFactory.getLogger(JiraOauthConfig.class);

private final String clientId;
private final String clientSecret;
private final JiraSourceConfig jiraSourceConfig;
private final Object cloudIdFetchLock = new Object();
private final Object tokenRenewLock = new Object();
RestTemplate restTemplate = new RestTemplate();
private String url;

@Getter
private int expiresInSeconds = 0;
@Getter
Expand Down Expand Up @@ -81,7 +83,7 @@ private String getJiraAccountCloudId() {
Map<String, Object> response = listResponse.get(0);
return (String) response.get("id");
} catch (HttpClientErrorException e) {
if (e.getRawStatusCode() == TOKEN_EXPIRED) {
if (e.getRawStatusCode() == HttpStatus.UNAUTHORIZED.value()) {
renewCredentials();
}
log.error("Error occurred while accessing resources: ", e);
Expand Down Expand Up @@ -112,11 +114,11 @@ public void renewCredentials() {
HttpEntity<String> entity = new HttpEntity<>(payload, headers);

try {
ResponseEntity<Map> responseEntity = restTemplate.postForEntity(Constants.TOKEN_LOCATION, entity, Map.class);
ResponseEntity<Map> responseEntity = restTemplate.postForEntity(TOKEN_LOCATION, entity, Map.class);
Map<String, Object> oauthClientResponse = responseEntity.getBody();
this.accessToken = (String) oauthClientResponse.get(Constants.ACCESS_TOKEN);
this.refreshToken = (String) oauthClientResponse.get(Constants.REFRESH_TOKEN);
this.expiresInSeconds = (int) oauthClientResponse.get(Constants.EXPIRES_IN);
this.accessToken = (String) oauthClientResponse.get(ACCESS_TOKEN);
this.refreshToken = (String) oauthClientResponse.get(REFRESH_TOKEN);
this.expiresInSeconds = (int) oauthClientResponse.get(EXPIRES_IN);
this.expireTime = Instant.ofEpochMilli(System.currentTimeMillis() + (expiresInSeconds * 1000L));
} catch (HttpClientErrorException ex) {
this.expireTime = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import java.net.URL;
import java.net.UnknownHostException;

import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.INVALID_URL;


/**
* This is the AddressValidation Class.
Expand All @@ -19,6 +17,8 @@
@Slf4j
public class AddressValidation {

public static final String INVALID_URL = "URL is not valid ";

/**
* Method for getInetAddress.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
package org.opensearch.dataprepper.plugins.source.jira.utils;

import java.util.List;

/**
* The type Constants.
*/
public class Constants {

public static final String TOKEN_LOCATION = "https://auth.atlassian.com/oauth/token";
public static final int TOKEN_EXPIRED = 401;
public static final int RATE_LIMIT = 429;
public static final int AUTHORIZATION_ERROR_CODE = 403;
public static final int RETRY_ATTEMPT = 6;
public static final List<Integer> RETRY_ATTEMPT_SLEEP_TIME = List.of(1, 2, 5, 10, 20, 40);
public static final String OAuth2_URL = "https://api.atlassian.com/ex/jira/";
public static final String ACCESSIBLE_RESOURCES = "https://api.atlassian.com/oauth/token/accessible-resources";
public static final String CONTENT_TYPE = "ContentType";

public static final String KEY = "key";
public static final String NAME = "name";
public static final String PROJECT = "project";
Expand All @@ -29,35 +20,5 @@ public class Constants {
public static final String CREATED = "created";
public static final String BASIC = "Basic";
public static final String LIVE = "live";
public static final String ACCESS_TOKEN = "access_token";
public static final String REFRESH_TOKEN = "refresh_token";
public static final String EXPIRES_IN = "expires_in";
public static final String PLUGIN_NAME = "jira";

public static final String BAD_REQUEST_EXCEPTION = "Bad request exception occurred "
+ "Invalid project key found in filter configuration for ";


public static final String GREATER_THAN_EQUALS = ">=";
public static final String CLOSING_ROUND_BRACKET = ")";

public static final String SLASH = "/";
public static final String PROJECT_IN = " AND project in (";
public static final String STATUS_IN = " AND status in (";
public static final String DELIMITER = "\",\"";
public static final String PREFIX = "\"";
public static final String SUFFIX = "\"";
public static final String REST_API_SEARCH = "rest/api/3/search";
public static final String REST_API_FETCH_ISSUE = "rest/api/3/issue";
public static final String MAX_RESULT = "maxResults";
public static final String ISSUE_TYPE_IN = " AND issueType in (";
public static final String INVALID_URL = "URL is not valid ";


public static final String FIFTY = "50";
public static final String START_AT = "startAt";
public static final String JQL_FIELD = "jql";
public static final String EXPAND_FIELD = "expand";
public static final String EXPAND_VALUE = "all";

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.opensearch.dataprepper.plugins.source.jira.utils;

public class JqlConstants {
public static final String GREATER_THAN_EQUALS = ">=";
public static final String CLOSING_ROUND_BRACKET = ")";

public static final String SLASH = "/";
public static final String PROJECT_IN = " AND project in (";
public static final String STATUS_IN = " AND status in (";
public static final String DELIMITER = "\",\"";
public static final String PREFIX = "\"";
public static final String SUFFIX = "\"";
public static final String ISSUE_TYPE_IN = " AND issueType in (";
public static final String JQL_FIELD = "jql";
public static final String EXPAND_FIELD = "expand";
public static final String EXPAND_VALUE = "all";
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.ACCESSIBLE_RESOURCES;
import static org.opensearch.dataprepper.plugins.source.jira.rest.auth.JiraOauthConfig.ACCESSIBLE_RESOURCES;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.BASIC;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.CREATED;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.KEY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.AUTHORIZATION_ERROR_CODE;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.BASIC;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.OAUTH2;
import static org.opensearch.dataprepper.plugins.source.jira.utils.Constants.TOKEN_EXPIRED;

@ExtendWith(MockitoExtension.class)
public class JiraRestClientTest {
Expand All @@ -51,8 +49,8 @@ public class JiraRestClientTest {

private static Stream<Arguments> provideHttpStatusCodesWithExceptionClass() {
return Stream.of(
Arguments.of(HttpStatus.valueOf(AUTHORIZATION_ERROR_CODE), UnAuthorizedException.class),
Arguments.of(HttpStatus.valueOf(TOKEN_EXPIRED), RuntimeException.class),
Arguments.of(HttpStatus.FORBIDDEN, UnAuthorizedException.class),
Arguments.of(HttpStatus.UNAUTHORIZED, RuntimeException.class),
Arguments.of(HttpStatus.TOO_MANY_REQUESTS, RuntimeException.class),
Arguments.of(HttpStatus.INSUFFICIENT_STORAGE, RuntimeException.class)
);
Expand Down

0 comments on commit 1590886

Please sign in to comment.