From bd47a571d53ce47b6e14fc9e1a032250fbe29126 Mon Sep 17 00:00:00 2001 From: Claudio Maggioni Date: Sat, 9 May 2020 17:53:58 +0200 Subject: [PATCH] Sonarqube fixes --- .../smarthut/config/CORSFilter.java | 6 --- .../smarthut/config/JWTRequestFilter.java | 4 +- .../smarthut/config/JWTTokenUtils.java | 2 +- .../config/RuntimeTypeAdapterFactory.java | 41 ++++--------------- .../controller/AutomationController.java | 3 +- .../controller/DimmableLightController.java | 18 ++++---- .../smarthut/controller/GuestController.java | 4 +- 7 files changed, 21 insertions(+), 57 deletions(-) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/CORSFilter.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/CORSFilter.java index d5e19ae..bfade8a 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/CORSFilter.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/CORSFilter.java @@ -31,10 +31,4 @@ public class CORSFilter implements Filter { chain.doFilter(req, res); } - - @Override - public void init(FilterConfig filterConfig) {} - - @Override - public void destroy() {} } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/JWTRequestFilter.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/JWTRequestFilter.java index 503f7cd..3cfc094 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/JWTRequestFilter.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/JWTRequestFilter.java @@ -36,9 +36,9 @@ public class JWTRequestFilter extends OncePerRequestFilter { try { username = jwtTokenUtils.getUsernameFromToken(jwtToken); } catch (IllegalArgumentException e) { - System.out.println("Unable to get JWT Token"); + logger.info("Unable to get JWT Token"); } catch (ExpiredJwtException e) { - System.out.println("JWT Token has expired"); + logger.info("JWT Token has expired"); } } else { logger.warn("JWT Token does not begin with Bearer String"); diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/JWTTokenUtils.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/JWTTokenUtils.java index 9d2d939..78e5e5e 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/JWTTokenUtils.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/JWTTokenUtils.java @@ -68,7 +68,7 @@ public class JWTTokenUtils { * @param userDetails user details to validate against * @return true if valid, false if not */ - public Boolean validateToken(String token, UserDetails userDetails) { + public boolean validateToken(String token, UserDetails userDetails) { final String username = getUsernameFromToken(token); return (username.equals(userDetails.getUsername()) && !isTokenExpired(token)); } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/RuntimeTypeAdapterFactory.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/RuntimeTypeAdapterFactory.java index dbe1331..d1d4e76 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/RuntimeTypeAdapterFactory.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/RuntimeTypeAdapterFactory.java @@ -1,21 +1,5 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.config; -/* - * Copyright (C) 2011 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - import com.google.gson.Gson; import com.google.gson.JsonElement; import com.google.gson.JsonObject; @@ -158,8 +142,8 @@ import java.util.Map; public final class RuntimeTypeAdapterFactory implements TypeAdapterFactory { private final Class baseType; private final String typeFieldName; - private final Map> labelToSubtype = new LinkedHashMap>(); - private final Map, String> subtypeToLabel = new LinkedHashMap, String>(); + private final Map> labelToSubtype = new LinkedHashMap<>(); + private final Map, String> subtypeToLabel = new LinkedHashMap<>(); private final boolean maintainType; private RuntimeTypeAdapterFactory( @@ -179,7 +163,7 @@ public final class RuntimeTypeAdapterFactory implements TypeAdapterFactory { */ public static RuntimeTypeAdapterFactory of( Class baseType, String typeFieldName, boolean maintainType) { - return new RuntimeTypeAdapterFactory(baseType, typeFieldName, maintainType); + return new RuntimeTypeAdapterFactory<>(baseType, typeFieldName, maintainType); } /** @@ -187,7 +171,7 @@ public final class RuntimeTypeAdapterFactory implements TypeAdapterFactory { * the type field name. Type field names are case sensitive. */ public static RuntimeTypeAdapterFactory of(Class baseType, String typeFieldName) { - return new RuntimeTypeAdapterFactory(baseType, typeFieldName, false); + return new RuntimeTypeAdapterFactory<>(baseType, typeFieldName, false); } /** @@ -195,7 +179,7 @@ public final class RuntimeTypeAdapterFactory implements TypeAdapterFactory { * field name. */ public static RuntimeTypeAdapterFactory of(Class baseType) { - return new RuntimeTypeAdapterFactory(baseType, "type", false); + return new RuntimeTypeAdapterFactory<>(baseType, "type", false); } /** @@ -216,26 +200,15 @@ public final class RuntimeTypeAdapterFactory implements TypeAdapterFactory { return this; } - /** - * Registers {@code type} identified by its {@link Class#getSimpleName simple name}. Labels are - * case sensitive. - * - * @throws IllegalArgumentException if either {@code type} or its simple name have already been - * registered on this type adapter. - */ - public RuntimeTypeAdapterFactory registerSubtype(Class type) { - return registerSubtype(type, type.getSimpleName()); - } - public TypeAdapter create(Gson gson, TypeToken type) { if (type.getRawType() != baseType) { return null; } final Map> labelToDelegate = - new LinkedHashMap>(labelToSubtype.size()); + new LinkedHashMap<>(labelToSubtype.size()); final Map, TypeAdapter> subtypeToDelegate = - new LinkedHashMap, TypeAdapter>(labelToSubtype.size()); + new LinkedHashMap<>(labelToSubtype.size()); for (Map.Entry> entry : labelToSubtype.entrySet()) { TypeAdapter delegate = gson.getDelegateAdapter(this, TypeToken.get(entry.getValue())); diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AutomationController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AutomationController.java index 5f93b86..c3027ae 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AutomationController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AutomationController.java @@ -33,8 +33,7 @@ public class AutomationController { @GetMapping public List getAll( @RequestParam(value = "hostId", required = false) Long hostId, - final Principal principal) - throws NotFoundException { + final Principal principal) { final Long userId = userService.findByUsername(principal.getName()).getId(); return automationRepository.findAllByUserId(userId); } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableLightController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableLightController.java index ffd39fa..990c105 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableLightController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableLightController.java @@ -49,10 +49,10 @@ public class DimmableLightController extends GuestEnabledController sceneBinding( @PathVariable("id") long deviceId, diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/GuestController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/GuestController.java index f010b52..4b7109d 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/GuestController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/GuestController.java @@ -5,7 +5,6 @@ import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.GuestPermissionsRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.GuestsUpdateRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.UserResponse; -import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.EagerUserRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User; import java.security.Principal; @@ -46,8 +45,7 @@ public class GuestController { @PutMapping("/guests") public List setGuests( - @RequestBody @Valid GuestsUpdateRequest g, final Principal principal) - throws NotFoundException { + @RequestBody @Valid GuestsUpdateRequest g, final Principal principal) { Iterable guests = userRepository.findAllById(g.ids); User host = userRepository.findByUsername(principal.getName());