From 228d9af8cb83452af2db5f947da44e266782b1ba Mon Sep 17 00:00:00 2001 From: Pierre-Yves Date: Thu, 16 Nov 2023 22:27:42 +0100 Subject: [PATCH] Java cleaning (#79) --- .../controller/DeepLinkController.java | 2 +- .../controller/RecommendationController.java | 2 +- .../playfff/controller/UpdateController.java | 8 +++--- .../controller/WhatIsNewController.java | 8 +++--- .../playfff/helper/BaseResourceString.java | 6 +---- .../playfff/model/DeepLinkJSContent.java | 8 +++--- .../ch/srgssr/playfff/model/Environment.java | 6 ++--- .../srgssr/playfff/model/RecommendedList.java | 4 +-- .../ch/srgssr/playfff/model/UpdateResult.java | 2 +- .../service/DeepLinkReportService.java | 4 +-- .../playfff/service/DeepLinkService.java | 9 +++---- .../service/RecommendationService.java | 26 +++++++++---------- .../java/ch/srgssr/playfff/utils/Sha1.java | 3 ++- 13 files changed, 41 insertions(+), 47 deletions(-) diff --git a/src/main/java/ch/srgssr/playfff/controller/DeepLinkController.java b/src/main/java/ch/srgssr/playfff/controller/DeepLinkController.java index 232276f..f178936 100644 --- a/src/main/java/ch/srgssr/playfff/controller/DeepLinkController.java +++ b/src/main/java/ch/srgssr/playfff/controller/DeepLinkController.java @@ -30,7 +30,7 @@ public class DeepLinkController { @Autowired private DeepLinkReportService deepLinkReportService; - private MediaType javascriptMediaType = new MediaType("application", "javascript", StandardCharsets.UTF_8); + private final MediaType javascriptMediaType = new MediaType("application", "javascript", StandardCharsets.UTF_8); @GetMapping(path = {"/api/v1/deeplink/report/{id}"}) public ResponseEntity findOne(@PathVariable("id") int id) { diff --git a/src/main/java/ch/srgssr/playfff/controller/RecommendationController.java b/src/main/java/ch/srgssr/playfff/controller/RecommendationController.java index b921e1b..19762e4 100644 --- a/src/main/java/ch/srgssr/playfff/controller/RecommendationController.java +++ b/src/main/java/ch/srgssr/playfff/controller/RecommendationController.java @@ -55,7 +55,7 @@ RecommendedList recommendationV2( private RecommendedList getRecommendationList(@PathVariable("purpose") String purpose, @PathVariable("urn") String urn, @RequestParam(value = "standalone", required = false, defaultValue = "false") boolean standalone) { RecommendedList recommendedList = service.getRecommendedUrns(purpose, urn, standalone); - if (recommendedList.getUrns().size() > 0) { + if (!recommendedList.getUrns().isEmpty()) { recommendedList.addUrn(0, urn); } return recommendedList; diff --git a/src/main/java/ch/srgssr/playfff/controller/UpdateController.java b/src/main/java/ch/srgssr/playfff/controller/UpdateController.java index 333ec8d..6524daa 100644 --- a/src/main/java/ch/srgssr/playfff/controller/UpdateController.java +++ b/src/main/java/ch/srgssr/playfff/controller/UpdateController.java @@ -19,7 +19,7 @@ @Controller public class UpdateController { - private Boolean updateCheckDisabled; + private final Boolean updateCheckDisabled; public UpdateController( @Value("${UPDATE_CHECK_DISABLED:false}") String updateCheckDisabledString) { @@ -47,9 +47,9 @@ public ResponseEntity> findAllDesc() { @PostMapping("/api/v1/update") public ResponseEntity create(@RequestBody Update update) { if (update == null - || update.packageName == null || update.packageName.length() == 0 - || update.version == null || update.version.length() == 0 - || update.text == null || update.text.length() == 0) { + || update.packageName == null || update.packageName.isEmpty() + || update.version == null || update.version.isEmpty() + || update.text == null || update.text.isEmpty()) { return new ResponseEntity<>(HttpStatus.NOT_ACCEPTABLE); } else { return new ResponseEntity<>(updateService.save(update), HttpStatus.CREATED); diff --git a/src/main/java/ch/srgssr/playfff/controller/WhatIsNewController.java b/src/main/java/ch/srgssr/playfff/controller/WhatIsNewController.java index 64d14f2..9ace536 100644 --- a/src/main/java/ch/srgssr/playfff/controller/WhatIsNewController.java +++ b/src/main/java/ch/srgssr/playfff/controller/WhatIsNewController.java @@ -22,14 +22,14 @@ public class WhatIsNewController { ReleaseNoteService releaseNoteService; @RequestMapping(value = "/whatisnew/admin", method = RequestMethod.GET) - public String whatisnewAdmin(@RequestParam(value = "name", required = false, defaultValue = "World") String name, Model model) { + public String whatIsNewAdmin(@RequestParam(value = "name", required = false, defaultValue = "World") String name, Model model) { model.addAttribute("name", name); return "whatisnew/entry"; } @RequestMapping(value = "/whatisnew/admin", method = RequestMethod.POST) @ResponseBody - public String whatisnewSave(@RequestParam(value = "package") String packageName, @RequestParam(value = "version") String version, @RequestParam(value = "text") String text) { + public String whatIsNewSave(@RequestParam(value = "package") String packageName, @RequestParam(value = "version") String version, @RequestParam(value = "text") String text) { ReleaseNote note = new ReleaseNote(); note.packageName = packageName; note.text = text; @@ -41,14 +41,14 @@ public String whatisnewSave(@RequestParam(value = "package") String packageName, // Public API @RequestMapping("/api/v1/whatisnew/text") @ResponseBody - public WhatIsNewResult whatisnewText(@RequestParam(value = "package") String packageName, @RequestParam(value = "version") String version) { + public WhatIsNewResult whatIsNewText(@RequestParam(value = "package") String packageName, @RequestParam(value = "version") String version) { return new WhatIsNewResult(releaseNoteService.getDisplayableText(packageName, version)); } // Public API @RequestMapping("/api/v1/whatisnew/html") @ResponseBody - public String whatisnewHtml(@RequestParam(value = "package") String packageName, @RequestParam(value = "version") String version) { + public String whatIsNewHtml(@RequestParam(value = "package") String packageName, @RequestParam(value = "version") String version) { return releaseNoteService.getDisplayableHtml(packageName, version); } diff --git a/src/main/java/ch/srgssr/playfff/helper/BaseResourceString.java b/src/main/java/ch/srgssr/playfff/helper/BaseResourceString.java index ae7fc10..8cb6536 100644 --- a/src/main/java/ch/srgssr/playfff/helper/BaseResourceString.java +++ b/src/main/java/ch/srgssr/playfff/helper/BaseResourceString.java @@ -16,11 +16,7 @@ public class BaseResourceString { public static String getString(ApplicationContext applicationContext, String name) { try { Resource resource = applicationContext.getResource("classpath:" + name); - if (resource == null) { - throw new RuntimeException("No resource: " + name); - } - String s = StreamUtils.copyToString(resource.getInputStream(), StandardCharsets.UTF_8); - return s; + return StreamUtils.copyToString(resource.getInputStream(), StandardCharsets.UTF_8); } catch (IOException e) { throw new RuntimeException("IO Exception for " + name, e); } diff --git a/src/main/java/ch/srgssr/playfff/model/DeepLinkJSContent.java b/src/main/java/ch/srgssr/playfff/model/DeepLinkJSContent.java index e46a55c..5b146db 100644 --- a/src/main/java/ch/srgssr/playfff/model/DeepLinkJSContent.java +++ b/src/main/java/ch/srgssr/playfff/model/DeepLinkJSContent.java @@ -7,11 +7,11 @@ */ public class DeepLinkJSContent { - private String contentV1; - private String hashV1; + private final String contentV1; + private final String hashV1; - private String contentV2; - private String hashV2; + private final String contentV2; + private final String hashV2; public DeepLinkJSContent(String contentV1, String hashV1, String contentV2, String hashV2) { this.contentV1 = contentV1; diff --git a/src/main/java/ch/srgssr/playfff/model/Environment.java b/src/main/java/ch/srgssr/playfff/model/Environment.java index adccc08..a0a03d9 100644 --- a/src/main/java/ch/srgssr/playfff/model/Environment.java +++ b/src/main/java/ch/srgssr/playfff/model/Environment.java @@ -19,9 +19,9 @@ public enum Environment { this.prettyName = prettyName; } - private String name; - private String baseUrl; - private String prettyName; + private final String name; + private final String baseUrl; + private final String prettyName; public static Environment fromValue(String v) { return valueOf(v.toUpperCase()); diff --git a/src/main/java/ch/srgssr/playfff/model/RecommendedList.java b/src/main/java/ch/srgssr/playfff/model/RecommendedList.java index 24365a7..fce5ea5 100644 --- a/src/main/java/ch/srgssr/playfff/model/RecommendedList.java +++ b/src/main/java/ch/srgssr/playfff/model/RecommendedList.java @@ -16,7 +16,7 @@ public class RecommendedList { private String recommendationId; - private List urns; + private final List urns; private String title; public RecommendedList(String title, String host, String recommendationId, List urns) { @@ -55,4 +55,4 @@ public String getTitle() { public void addUrn(int index, String urn) { this.urns.add(index, urn); } -} \ No newline at end of file +} diff --git a/src/main/java/ch/srgssr/playfff/model/UpdateResult.java b/src/main/java/ch/srgssr/playfff/model/UpdateResult.java index 7221ff9..0642b1c 100644 --- a/src/main/java/ch/srgssr/playfff/model/UpdateResult.java +++ b/src/main/java/ch/srgssr/playfff/model/UpdateResult.java @@ -14,7 +14,7 @@ public class UpdateResult { */ public String text; - enum Type { + public enum Type { None, Optional, Mandatory diff --git a/src/main/java/ch/srgssr/playfff/service/DeepLinkReportService.java b/src/main/java/ch/srgssr/playfff/service/DeepLinkReportService.java index 24d9a5d..152c7cb 100644 --- a/src/main/java/ch/srgssr/playfff/service/DeepLinkReportService.java +++ b/src/main/java/ch/srgssr/playfff/service/DeepLinkReportService.java @@ -19,12 +19,12 @@ public class DeepLinkReportService { @Autowired private DeepLinkReportRepository repository; - private int maxDeepLinkReports; + private final int maxDeepLinkReports; public DeepLinkReportService( @Value("${MAX_DEEP_LINK_REPORTS:2500}") String maxDeepLinkReportsString) { - this.maxDeepLinkReports = Integer.valueOf(maxDeepLinkReportsString); + this.maxDeepLinkReports = Integer.parseInt(maxDeepLinkReportsString); } @Transactional diff --git a/src/main/java/ch/srgssr/playfff/service/DeepLinkService.java b/src/main/java/ch/srgssr/playfff/service/DeepLinkService.java index e6b19c2..d7d8c33 100644 --- a/src/main/java/ch/srgssr/playfff/service/DeepLinkService.java +++ b/src/main/java/ch/srgssr/playfff/service/DeepLinkService.java @@ -38,9 +38,7 @@ public class DeepLinkService { private static final Logger logger = LoggerFactory.getLogger(DeepLinkService.class); - private RestTemplate restTemplate; - - private Set pullEnvironmentSet = new HashSet<>(); + private final Set pullEnvironmentSet = new HashSet<>(); @Autowired protected ApplicationContext applicationContext; @@ -50,7 +48,6 @@ public class DeepLinkService { public DeepLinkService(RestTemplateBuilder restTemplateBuilder, @Value("${DEEP_LINK_ENVIRONMENTS:PROD}") String environments) { - restTemplate = restTemplateBuilder.build(); String[] environmentStrings = environments.split(","); for (String environmentString : environmentStrings) { @@ -74,14 +71,14 @@ public synchronized DeepLinkJSContent refreshParsePlayUrlJSContent() { busMap.put(Environment.PROD, srgBUs); busMap.put(Environment.STAGE, srgBUs); busMap.put(Environment.TEST, srgBUs); - busMap.put(Environment.MMF, Arrays.asList()); + busMap.put(Environment.MMF, Collections.emptyList()); Map>> tvGlobalTopicMap = new HashMap<>(); for (Environment environment : pullEnvironmentSet) { Map> tvTopicMap = getTvTopicMap(busMap.get(environment), environment); - if (tvTopicMap.size() > 0) { + if (!tvTopicMap.isEmpty()) { tvGlobalTopicMap.put(environment.getPrettyName(), tvTopicMap); } } diff --git a/src/main/java/ch/srgssr/playfff/service/RecommendationService.java b/src/main/java/ch/srgssr/playfff/service/RecommendationService.java index 90dcfe0..ea5897a 100644 --- a/src/main/java/ch/srgssr/playfff/service/RecommendationService.java +++ b/src/main/java/ch/srgssr/playfff/service/RecommendationService.java @@ -32,11 +32,11 @@ public class RecommendationService { @Autowired private IntegrationLayerRequest integrationLayerRequest; - private RestTemplate restTemplate; + private final RestTemplate restTemplate; - private Boolean rtsRecommendationUsed; - private Boolean srfRecommendationUsed; - private int ascendingEpisodesMax; + private final Boolean rtsRecommendationUsed; + private final Boolean srfRecommendationUsed; + private final int ascendingEpisodesMax; public RecommendationService( @Value("${RTS_RECOMMENDATION_USED:true}") String rtsRecommendationUsedString, @@ -122,10 +122,10 @@ private RecommendedList pfffRecommendedList(String urn, MediaType mediaType, Boo List clipUrns = episodes.stream().flatMap(e -> e.getMediaList().stream().filter(m -> m.getMediaType() == mediaType)).map(Media::getUrn).collect(Collectors.toList()); clipUrns.removeAll(fullLengthUrns); - Boolean isFullLengthUrns = false; - List recommendationResult = null; + boolean isFullLengthUrns; + List recommendationResult; - List urns = null; + List urns; int index = -1; MediaComposition mediaComposition = null; @@ -142,13 +142,13 @@ private RecommendedList pfffRecommendedList(String urn, MediaType mediaType, Boo urns = clipUrns; } else { mediaComposition = integrationLayerRequest.getMediaComposition(urn, Environment.PROD); - isFullLengthUrns = (mediaComposition != null && mediaComposition.getSegmentUrn() != null) ? !urn.equals(mediaComposition.getSegmentUrn()) : true; + isFullLengthUrns = mediaComposition == null || mediaComposition.getSegmentUrn() == null || !urn.equals(mediaComposition.getSegmentUrn()); urns = isFullLengthUrns ? fullLengthUrns : clipUrns; } - // Take care of non standalone video. + // Take care of non-standalone video. String baseUrn = urn; - if (mediaType == MediaType.VIDEO && !standalone && !isFullLengthUrns && clipUrns.size() > 0) { + if (mediaType == MediaType.VIDEO && !standalone && !isFullLengthUrns && !clipUrns.isEmpty()) { if (index != -1) { EpisodeWithMedias episode = episodes.stream().filter(e -> e.getMediaList().stream().map(Media::getUrn).collect(Collectors.toList()).contains(urn)).findFirst().orElse(null); index = (episode != null) ? fullLengthUrns.indexOf(episode.getFullLengthUrn()) : -1; @@ -157,7 +157,7 @@ private RecommendedList pfffRecommendedList(String urn, MediaType mediaType, Boo urns = fullLengthUrns; } // Take care of full length and clip not in Episode composition, specially for RSI. - else if (index == -1 && clipUrns.size() == 0) { + else if (index == -1 && clipUrns.isEmpty()) { String chapterUrn = null; if (mediaComposition != null) { chapterUrn = mediaComposition.getChapterUrn(); @@ -263,7 +263,7 @@ private RecommendedList srfRecommendedList(String purpose, String urn, boolean s Environment environment = Environment.PROD; MediaList mediaList = integrationLayerRequest.getRecommendedMediaList(urn, environment); - if (mediaList == null || mediaList.getList().size() == 0) { + if (mediaList == null || mediaList.getList().isEmpty()) { return new RecommendedList(); } @@ -271,4 +271,4 @@ private RecommendedList srfRecommendedList(String purpose, String urn, boolean s return new RecommendedList(environment.getBaseUrl(), recommendationId, mediaList.getList().stream().map((i) -> i.getUrn()).collect(Collectors.toList())); } -} \ No newline at end of file +} diff --git a/src/main/java/ch/srgssr/playfff/utils/Sha1.java b/src/main/java/ch/srgssr/playfff/utils/Sha1.java index 9252249..2fbadbd 100644 --- a/src/main/java/ch/srgssr/playfff/utils/Sha1.java +++ b/src/main/java/ch/srgssr/playfff/utils/Sha1.java @@ -2,6 +2,7 @@ import javax.xml.bind.DatatypeConverter; import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @@ -13,7 +14,7 @@ public class Sha1 { public static String sha1(String input) throws NoSuchAlgorithmException, UnsupportedEncodingException { MessageDigest msdDigest = MessageDigest.getInstance("SHA-1"); - msdDigest.update(input.getBytes("UTF-8"), 0, input.length()); + msdDigest.update(input.getBytes(StandardCharsets.UTF_8), 0, input.length()); return DatatypeConverter.printHexBinary(msdDigest.digest()); } }