Skip to content

Commit

Permalink
Java cleaning (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
pyby authored Nov 16, 2023
1 parent d8cee09 commit 228d9af
Show file tree
Hide file tree
Showing 13 changed files with 41 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<DeepLinkReport> findOne(@PathVariable("id") int id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
@Controller
public class UpdateController {

private Boolean updateCheckDisabled;
private final Boolean updateCheckDisabled;

public UpdateController(
@Value("${UPDATE_CHECK_DISABLED:false}") String updateCheckDisabledString) {
Expand Down Expand Up @@ -47,9 +47,9 @@ public ResponseEntity<Iterable<Update>> findAllDesc() {
@PostMapping("/api/v1/update")
public ResponseEntity<Update> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/ch/srgssr/playfff/model/DeepLinkJSContent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/ch/srgssr/playfff/model/Environment.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/ch/srgssr/playfff/model/RecommendedList.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
public class RecommendedList {

private String recommendationId;
private List<String> urns;
private final List<String> urns;
private String title;

public RecommendedList(String title, String host, String recommendationId, List<String> urns) {
Expand Down Expand Up @@ -55,4 +55,4 @@ public String getTitle() {
public void addUrn(int index, String urn) {
this.urns.add(index, urn);
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/ch/srgssr/playfff/model/UpdateResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class UpdateResult {
*/
public String text;

enum Type {
public enum Type {
None,
Optional,
Mandatory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/ch/srgssr/playfff/service/DeepLinkService.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@
public class DeepLinkService {
private static final Logger logger = LoggerFactory.getLogger(DeepLinkService.class);

private RestTemplate restTemplate;

private Set<Environment> pullEnvironmentSet = new HashSet<>();
private final Set<Environment> pullEnvironmentSet = new HashSet<>();

@Autowired
protected ApplicationContext applicationContext;
Expand All @@ -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) {
Expand All @@ -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<String, Map<String, Map<String, String>>> tvGlobalTopicMap = new HashMap<>();

for (Environment environment : pullEnvironmentSet) {
Map<String, Map<String, String>> tvTopicMap = getTvTopicMap(busMap.get(environment), environment);

if (tvTopicMap.size() > 0) {
if (!tvTopicMap.isEmpty()) {
tvGlobalTopicMap.put(environment.getPrettyName(), tvTopicMap);
}
}
Expand Down
26 changes: 13 additions & 13 deletions src/main/java/ch/srgssr/playfff/service/RecommendationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -122,10 +122,10 @@ private RecommendedList pfffRecommendedList(String urn, MediaType mediaType, Boo
List<String> 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<String> recommendationResult = null;
boolean isFullLengthUrns;
List<String> recommendationResult;

List<String> urns = null;
List<String> urns;
int index = -1;
MediaComposition mediaComposition = null;

Expand All @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -263,12 +263,12 @@ 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();
}

String recommendationId = "mediaList/recommended/byUrn/" + urn + "/" + timestamp;

return new RecommendedList(environment.getBaseUrl(), recommendationId, mediaList.getList().stream().map((i) -> i.getUrn()).collect(Collectors.toList()));
}
}
}
3 changes: 2 additions & 1 deletion src/main/java/ch/srgssr/playfff/utils/Sha1.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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());
}
}

0 comments on commit 228d9af

Please sign in to comment.