CPD Results
The following document contains the results of PMD's CPD 7.7.0.
Duplications
File |
Project |
Line |
org/petify/feed/exception/GlobalExceptionHandler.java |
feed |
19 |
org/petify/image/exception/GlobalExceptionHandler.java |
image |
19 |
@RestControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(AccessDeniedException.class)
ProblemDetail handleAccessDeniedException(WebRequest request) {
ProblemDetail problemDetail = ProblemDetail.forStatus(HttpStatus.FORBIDDEN);
problemDetail.setTitle("Access Denied");
problemDetail.setDetail(request.getDescription(false));
problemDetail.setProperty("timestamp", Instant.now());
return problemDetail;
}
@ExceptionHandler(Exception.class)
ProblemDetail handleGenericException(Exception ex) {
ProblemDetail problemDetail = ProblemDetail.forStatus(HttpStatus.INTERNAL_SERVER_ERROR);
problemDetail.setTitle("Internal Server Error");
problemDetail.setDetail("An unexpected error occurred: " + ex.getMessage());
problemDetail.setProperty("timestamp", Instant.now());
return problemDetail;
}
@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(
MethodArgumentNotValidException ex,
HttpHeaders headers,
HttpStatusCode status,
WebRequest request) {
ProblemDetail problemDetail = ProblemDetail.forStatus(status);
problemDetail.setTitle("Validation Failed");
Map<String, String> errors = new HashMap<>();
ex.getBindingResult().getFieldErrors().forEach(error ->
errors.put(error.getField(), error.getDefaultMessage())
);
problemDetail.setProperty("errors", errors);
return new ResponseEntity<>(problemDetail, headers, status);
}
@ExceptionHandler(AlreadyParticipatingException.class) |
File |
Project |
Line |
org/petify/image/service/StorageService.java |
image |
17 |
org/petify/shelter/service/StorageService.java |
shelter |
17 |
@RequiredArgsConstructor
@Service
public class StorageService {
private final AmazonS3 space;
private static final String bucketName = "petify";
public List<String> getImageFileNames() {
ListObjectsV2Result result = space.listObjectsV2(bucketName);
List<S3ObjectSummary> objects = result.getObjectSummaries();
return objects.stream()
.map(S3ObjectSummary::getKey).toList();
}
public String uploadImage(MultipartFile file) {
try {
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentType(file.getContentType());
String fileName = generateImageName();
space.putObject(new PutObjectRequest(bucketName, fileName, file.getInputStream(), objectMetadata)
.withCannedAcl(CannedAccessControlList.PublicRead));
return fileName;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public boolean deleteImage(String imageName) {
try {
if (space.doesObjectExist(bucketName, imageName)) {
space.deleteObject(bucketName, imageName);
return true;
}
return false;
} catch (Exception e) {
throw new RuntimeException("Failed to delete image " + imageName, e);
}
}
private String generateImageName() {
return UUID.randomUUID().toString();
}
} |
File |
Project |
Line |
org/petify/feed/exception/GlobalExceptionHandler.java |
feed |
35 |
org/petify/shelter/exception/GlobalExceptionHandler.java |
shelter |
26 |
problemDetail.setDetail("An unexpected error occurred: " + ex.getMessage());
problemDetail.setProperty("timestamp", Instant.now());
return problemDetail;
}
@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(
MethodArgumentNotValidException ex,
HttpHeaders headers,
HttpStatusCode status,
WebRequest request) {
ProblemDetail problemDetail = ProblemDetail.forStatus(status);
problemDetail.setTitle("Validation Failed");
Map<String, String> errors = new HashMap<>();
ex.getBindingResult().getFieldErrors().forEach(error ->
errors.put(error.getField(), error.getDefaultMessage())
);
problemDetail.setProperty("errors", errors);
return new ResponseEntity<>(problemDetail, headers, status);
}
@ExceptionHandler(AlreadyParticipatingException.class) |
File |
Project |
Line |
org/petify/image/exception/GlobalExceptionHandler.java |
image |
35 |
org/petify/shelter/exception/GlobalExceptionHandler.java |
shelter |
26 |
problemDetail.setDetail("An unexpected error occurred: " + ex.getMessage());
problemDetail.setProperty("timestamp", Instant.now());
return problemDetail;
}
@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(
MethodArgumentNotValidException ex,
HttpHeaders headers,
HttpStatusCode status,
WebRequest request) {
ProblemDetail problemDetail = ProblemDetail.forStatus(status);
problemDetail.setTitle("Validation Failed");
Map<String, String> errors = new HashMap<>();
ex.getBindingResult().getFieldErrors().forEach(error ->
errors.put(error.getField(), error.getDefaultMessage())
);
problemDetail.setProperty("errors", errors);
return new ResponseEntity<>(problemDetail, headers, status);
}
@ExceptionHandler(ImageNotFoundException.class) |
File |
Project |
Line |
org/petify/feed/config/JwtSecurityConfig.java |
feed |
21 |
org/petify/image/config/JwtSecurityConfig.java |
image |
20 |
org/petify/shelter/config/JwtSecurityConfig.java |
shelter |
22 |
.requestMatchers("/events/**").permitAll()
.requestMatchers(
"/swagger-ui.html",
"/swagger-ui/**",
"/v3/api-docs",
"/v3/api-docs/**",
"/actuator/health"
).permitAll()
)
.sessionManagement(session -> session
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
)
.oauth2ResourceServer(oauth2 -> oauth2
.jwt(jwt -> jwt
.jwtAuthenticationConverter(jwtAuthenticationConverter())
)
);
return http.build();
}
@Bean
public JwtAuthenticationConverter jwtAuthenticationConverter() {
JwtGrantedAuthoritiesConverter grantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
grantedAuthoritiesConverter.setAuthorityPrefix("ROLE_");
grantedAuthoritiesConverter.setAuthoritiesClaimName("roles");
JwtAuthenticationConverter jwtAuthenticationConverter = new JwtAuthenticationConverter();
jwtAuthenticationConverter.setJwtGrantedAuthoritiesConverter(grantedAuthoritiesConverter);
return jwtAuthenticationConverter;
}
} |