#78 fixed some smells

This commit is contained in:
Fabio Formosa
2022-11-08 22:59:07 +01:00
parent 6eededed2c
commit a4b0a1bafb
6 changed files with 27 additions and 30 deletions

View File

@@ -2,6 +2,9 @@ package it.fabioformosa.quartzmanager.api.common.config;
public class QuartzManagerPaths {
private QuartzManagerPaths(){
}
public static final String QUARTZ_MANAGER_BASE_CONTEXT_PATH = "/quartz-manager";
public static final String WEBJAR_PATH = "/quartz-manager-ui";

View File

@@ -8,15 +8,18 @@ import java.util.Date;
public class DateUtils {
static public Date fromLocalDateTimeToDate(LocalDateTime localDateTime){
private DateUtils(){
}
public static Date fromLocalDateTimeToDate(LocalDateTime localDateTime){
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant().truncatedTo(ChronoUnit.MILLIS));
}
static public LocalDateTime fromDateToLocalDateTime(Date date) {
public static LocalDateTime fromDateToLocalDateTime(Date date) {
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime().truncatedTo(ChronoUnit.MILLIS);
}
static public Date addHoursToNow(long hours){
public static Date addHoursToNow(long hours){
return DateUtils.fromLocalDateTimeToDate(LocalDateTime.now().plus(Duration.ofHours(hours)));
}

View File

@@ -16,11 +16,11 @@ public class Try<R> {
return success;
}
public static <R> Try success(R r){
public static <R> Try<R> success(R r){
return new Try<>(null, r);
}
public static <ExceptionType> Try failure(Throwable e){
public static <R> Try<R> failure(Throwable e){
return new Try<>(e, null);
}

View File

@@ -22,7 +22,7 @@ class SimpleTriggerServiceIntegrationTest {
private SimpleTriggerService simpleTriggerService;
@Test
public void givenASimpleTriggerCommandDTOWithAllData_whenANewSimpleTriggerIsScheduled_thenShouldGetATriggertDTO() throws SchedulerException, ClassNotFoundException {
void givenASimpleTriggerCommandDTOWithAllData_whenANewSimpleTriggerIsScheduled_thenShouldGetATriggertDTO() throws SchedulerException, ClassNotFoundException {
String simpleTriggerTestName = "simpleTriggerWithAllData";
String jobClass = "it.fabioformosa.quartzmanager.api.jobs.SampleJob";
Date startDate = new Date();
@@ -59,7 +59,7 @@ class SimpleTriggerServiceIntegrationTest {
}
@Test
public void givenASimpleTriggerCommandDTOWithMissingOptionalField_whenANewSimpleTriggerIsScheduled_thenShouldGetATriggertDTO() throws SchedulerException, ClassNotFoundException {
void givenASimpleTriggerCommandDTOWithMissingOptionalField_whenANewSimpleTriggerIsScheduled_thenShouldGetATriggertDTO() throws SchedulerException, ClassNotFoundException {
String simpleTriggerTestName = "simpleTriggerWithoutOptionalData";
String jobClass = "it.fabioformosa.quartzmanager.api.jobs.SampleJob";

View File

@@ -41,7 +41,7 @@ public class JwtTokenAuthenticationFilter extends OncePerRequestFilter {
private static final String LOGIN_MATCHER = "/api/login";
private static final String LOGOUT_MATCHER = "/api/logout";
private static List<String> PATH_TO_SKIP = Arrays.asList(
private final static List<String> PATH_TO_SKIP = Arrays.asList(
ROOT_MATCHER,
HTML_MATCHER,
FAVICON_MATCHER,
@@ -77,9 +77,6 @@ public class JwtTokenAuthenticationFilter extends OncePerRequestFilter {
SecurityContextHolder.getContext().setAuthentication(authentication);
} catch (Exception e) {
log.error("Authentication failed! an expected error occurred authenticating the request {} due to {}", request.getRequestURL(), e.getMessage(), e);
// SecurityContextHolder.getContext().setAuthentication(new AnonAuthentication());
// log.error("Switched to Anonymous Authentication, "
// + "because an error occurred setting authentication in security context holder due to " + e.getMessage(), e);
}
}
else if(skipPathRequest(request, PATH_TO_SKIP)) {
@@ -94,7 +91,7 @@ public class JwtTokenAuthenticationFilter extends OncePerRequestFilter {
private boolean skipPathRequest(HttpServletRequest request, List<String> pathsToSkip ) {
if(pathsToSkip == null)
pathsToSkip = new ArrayList<String>();
pathsToSkip = new ArrayList<>();
List<RequestMatcher> matchers = pathsToSkip.stream().map(path -> new AntPathRequestMatcher(path)).collect(Collectors.toList());
OrRequestMatcher compositeMatchers = new OrRequestMatcher(matchers);
return compositeMatchers.matches(request);

View File

@@ -33,7 +33,7 @@ public class JwtTokenHelper {
private final JwtSecurityProperties jwtSecurityProps;
private SignatureAlgorithm SIGNATURE_ALGORITHM = SignatureAlgorithm.HS512;
private static final SignatureAlgorithm SIGNATURE_ALGORITHM = SignatureAlgorithm.HS512;
public JwtTokenHelper(String appName, JwtSecurityProperties jwtSecurityProps) {
super();
@@ -41,16 +41,15 @@ public class JwtTokenHelper {
this.jwtSecurityProps = jwtSecurityProps;
}
public Boolean canTokenBeRefreshed(String token) {
try {
final Date expirationDate = verifyAndGetClaimsFromToken(token).getExpiration();
// String username = getUsernameFromToken(token);
// UserDetails userDetails = userDetailsService.loadUserByUsername(username);
return expirationDate.compareTo(generateCurrentDate()) > 0;
} catch (Exception e) {
return false;
}
public Boolean canTokenBeRefreshed(String token) {
try {
final Date expirationDate = verifyAndGetClaimsFromToken(token).getExpiration();
return expirationDate.compareTo(generateCurrentDate()) > 0;
} catch (Exception e) {
log.error("Error getting claims from jwt token due to " + e.getMessage(), e);
return false;
}
}
private Date generateCurrentDate() {
return new Date(getCurrentTimeMillis());
@@ -73,14 +72,9 @@ public class JwtTokenHelper {
private Claims verifyAndGetClaimsFromToken(String token) {
Claims claims;
try {
claims = Jwts.parser().setSigningKey(base64EncodeSecretKey(jwtSecurityProps.getSecret()))
.parseClaimsJws(token).getBody();
} catch (Exception e) {
log.error("Error getting claims from jwt token due to " + e.getMessage(), e);
throw e;
}
if(claims == null)
claims = Jwts.parser().setSigningKey(base64EncodeSecretKey(jwtSecurityProps.getSecret()))
.parseClaimsJws(token).getBody();
if (claims == null)
throw new IllegalStateException("Not found any claims into the JWT token!");
return claims;
}