Merge branch 'sonar-fix' into 'dev'

Sonarqube fixes

See merge request sa4-2020/the-sanmarinoes/backend!128
This commit is contained in:
Claudio Maggioni 2020-05-09 17:54:25 +02:00
commit 1e99f5a501
7 changed files with 21 additions and 57 deletions

View file

@ -31,10 +31,4 @@ public class CORSFilter implements Filter {
chain.doFilter(req, res); chain.doFilter(req, res);
} }
@Override
public void init(FilterConfig filterConfig) {}
@Override
public void destroy() {}
} }

View file

@ -36,9 +36,9 @@ public class JWTRequestFilter extends OncePerRequestFilter {
try { try {
username = jwtTokenUtils.getUsernameFromToken(jwtToken); username = jwtTokenUtils.getUsernameFromToken(jwtToken);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
System.out.println("Unable to get JWT Token"); logger.info("Unable to get JWT Token");
} catch (ExpiredJwtException e) { } catch (ExpiredJwtException e) {
System.out.println("JWT Token has expired"); logger.info("JWT Token has expired");
} }
} else { } else {
logger.warn("JWT Token does not begin with Bearer String"); logger.warn("JWT Token does not begin with Bearer String");

View file

@ -68,7 +68,7 @@ public class JWTTokenUtils {
* @param userDetails user details to validate against * @param userDetails user details to validate against
* @return true if valid, false if not * @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); final String username = getUsernameFromToken(token);
return (username.equals(userDetails.getUsername()) && !isTokenExpired(token)); return (username.equals(userDetails.getUsername()) && !isTokenExpired(token));
} }

View file

@ -1,21 +1,5 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.config; 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.Gson;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
@ -158,8 +142,8 @@ import java.util.Map;
public final class RuntimeTypeAdapterFactory<T> implements TypeAdapterFactory { public final class RuntimeTypeAdapterFactory<T> implements TypeAdapterFactory {
private final Class<?> baseType; private final Class<?> baseType;
private final String typeFieldName; private final String typeFieldName;
private final Map<String, Class<?>> labelToSubtype = new LinkedHashMap<String, Class<?>>(); private final Map<String, Class<?>> labelToSubtype = new LinkedHashMap<>();
private final Map<Class<?>, String> subtypeToLabel = new LinkedHashMap<Class<?>, String>(); private final Map<Class<?>, String> subtypeToLabel = new LinkedHashMap<>();
private final boolean maintainType; private final boolean maintainType;
private RuntimeTypeAdapterFactory( private RuntimeTypeAdapterFactory(
@ -179,7 +163,7 @@ public final class RuntimeTypeAdapterFactory<T> implements TypeAdapterFactory {
*/ */
public static <T> RuntimeTypeAdapterFactory<T> of( public static <T> RuntimeTypeAdapterFactory<T> of(
Class<T> baseType, String typeFieldName, boolean maintainType) { Class<T> baseType, String typeFieldName, boolean maintainType) {
return new RuntimeTypeAdapterFactory<T>(baseType, typeFieldName, maintainType); return new RuntimeTypeAdapterFactory<>(baseType, typeFieldName, maintainType);
} }
/** /**
@ -187,7 +171,7 @@ public final class RuntimeTypeAdapterFactory<T> implements TypeAdapterFactory {
* the type field name. Type field names are case sensitive. * the type field name. Type field names are case sensitive.
*/ */
public static <T> RuntimeTypeAdapterFactory<T> of(Class<T> baseType, String typeFieldName) { public static <T> RuntimeTypeAdapterFactory<T> of(Class<T> baseType, String typeFieldName) {
return new RuntimeTypeAdapterFactory<T>(baseType, typeFieldName, false); return new RuntimeTypeAdapterFactory<>(baseType, typeFieldName, false);
} }
/** /**
@ -195,7 +179,7 @@ public final class RuntimeTypeAdapterFactory<T> implements TypeAdapterFactory {
* field name. * field name.
*/ */
public static <T> RuntimeTypeAdapterFactory<T> of(Class<T> baseType) { public static <T> RuntimeTypeAdapterFactory<T> of(Class<T> baseType) {
return new RuntimeTypeAdapterFactory<T>(baseType, "type", false); return new RuntimeTypeAdapterFactory<>(baseType, "type", false);
} }
/** /**
@ -216,26 +200,15 @@ public final class RuntimeTypeAdapterFactory<T> implements TypeAdapterFactory {
return this; 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<T> registerSubtype(Class<? extends T> type) {
return registerSubtype(type, type.getSimpleName());
}
public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) { public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
if (type.getRawType() != baseType) { if (type.getRawType() != baseType) {
return null; return null;
} }
final Map<String, TypeAdapter<?>> labelToDelegate = final Map<String, TypeAdapter<?>> labelToDelegate =
new LinkedHashMap<String, TypeAdapter<?>>(labelToSubtype.size()); new LinkedHashMap<>(labelToSubtype.size());
final Map<Class<?>, TypeAdapter<?>> subtypeToDelegate = final Map<Class<?>, TypeAdapter<?>> subtypeToDelegate =
new LinkedHashMap<Class<?>, TypeAdapter<?>>(labelToSubtype.size()); new LinkedHashMap<>(labelToSubtype.size());
for (Map.Entry<String, Class<?>> entry : labelToSubtype.entrySet()) { for (Map.Entry<String, Class<?>> entry : labelToSubtype.entrySet()) {
TypeAdapter<?> delegate = TypeAdapter<?> delegate =
gson.getDelegateAdapter(this, TypeToken.get(entry.getValue())); gson.getDelegateAdapter(this, TypeToken.get(entry.getValue()));

View file

@ -33,8 +33,7 @@ public class AutomationController {
@GetMapping @GetMapping
public List<Automation> getAll( public List<Automation> getAll(
@RequestParam(value = "hostId", required = false) Long hostId, @RequestParam(value = "hostId", required = false) Long hostId,
final Principal principal) final Principal principal) {
throws NotFoundException {
final Long userId = userService.findByUsername(principal.getName()).getId(); final Long userId = userService.findByUsername(principal.getName()).getId();
return automationRepository.findAllByUserId(userId); return automationRepository.findAllByUserId(userId);
} }

View file

@ -49,9 +49,9 @@ public class DimmableLightController extends GuestEnabledController<DimmableLigh
} }
} }
/* /**
Assume that only the host can create a device * Assume that only the host can create a device Here save always as host, but remember to
Here save always as host, but remember to propagate change to guests (DeviceService.saveAsOwner()) * propagate change to guests (DeviceService.saveAsOwner())
*/ */
@PostMapping @PostMapping
public DimmableLight create( public DimmableLight create(
@ -61,9 +61,7 @@ public class DimmableLightController extends GuestEnabledController<DimmableLigh
return save(new DimmableLight(), dl, principal.getName(), null); return save(new DimmableLight(), dl, principal.getName(), null);
} }
/* /** Logic for saving either as owner or guest is handled in method save of this controller */
Logic for saving either as owner or guest is handled in method save of this controller
*/
@PutMapping @PutMapping
public DimmableLight update( public DimmableLight update(
@Valid @RequestBody DimmableSaveRequest sp, @Valid @RequestBody DimmableSaveRequest sp,
@ -84,8 +82,10 @@ public class DimmableLightController extends GuestEnabledController<DimmableLigh
deviceService.deleteByIdAsOwner(id, principal.getName()); deviceService.deleteByIdAsOwner(id, principal.getName());
} }
// the full url should be: "/dimmableLight/{id}/state?sceneId={sceneId} /**
// however it is not necessary to specify the query in the mapping * the full url should be: "/dimmableLight/{id}/state?sceneId={sceneId} however it is not
* necessary to specify the query in the mapping
*/
@PostMapping("/{id}/state") @PostMapping("/{id}/state")
public State<? extends Dimmable> sceneBinding( public State<? extends Dimmable> sceneBinding(
@PathVariable("id") long deviceId, @PathVariable("id") long deviceId,

View file

@ -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.GuestPermissionsRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.GuestsUpdateRequest; 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.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.EagerUserRepository;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User;
import java.security.Principal; import java.security.Principal;
@ -46,8 +45,7 @@ public class GuestController {
@PutMapping("/guests") @PutMapping("/guests")
public List<User> setGuests( public List<User> setGuests(
@RequestBody @Valid GuestsUpdateRequest g, final Principal principal) @RequestBody @Valid GuestsUpdateRequest g, final Principal principal) {
throws NotFoundException {
Iterable<User> guests = userRepository.findAllById(g.ids); Iterable<User> guests = userRepository.findAllById(g.ids);
User host = userRepository.findByUsername(principal.getName()); User host = userRepository.findByUsername(principal.getName());