From 7bb05b705f721e6411690f7afd71ce0428733945 Mon Sep 17 00:00:00 2001 From: Claudio Maggioni Date: Fri, 13 Mar 2020 15:48:03 +0100 Subject: [PATCH] Neutralized id value from client in device routes for creation. Reconfigured Springfox for authentication in device and room routes. --- .../smarthut/config/SpringFoxConfig.java | 43 ++++++++++++------ .../smarthut/controller/DeviceController.java | 44 +++++++++++++++++++ .../controller/DimmableLightController.java | 1 + .../controller/KnobDimmerController.java | 1 + .../controller/MotionSensorController.java | 1 + .../controller/RegularLightController.java | 1 + .../smarthut/controller/RoomController.java | 4 +- .../smarthut/controller/SensorController.java | 1 + .../controller/SmartPlugController.java | 1 + .../smarthut/controller/SwitchController.java | 1 + .../controller/WelcomeController.java | 15 ------- .../smarthut/dto/ButtonDimmerSaveRequest.java | 4 ++ .../smarthut/dto/DeviceSaveRequest.java | 42 ++++++++++++++++++ .../dto/DimmableLightSaveRequest.java | 4 ++ .../smarthut/dto/KnobDimmerSaveRequest.java | 4 ++ .../smarthut/dto/MotionSensorSaveRequest.java | 4 ++ .../smarthut/dto/RegularLightSaveRequest.java | 4 ++ .../smarthut/dto/SensorSaveRequest.java | 4 ++ .../smarthut/dto/SmartPlugSaveRequest.java | 4 ++ .../smarthut/dto/SwitchSaveRequest.java | 4 ++ .../smarthut/error/BadDataException.java | 11 +++++ .../sa4/sanmarinoes/smarthut/models/Room.java | 14 +++--- 22 files changed, 174 insertions(+), 38 deletions(-) create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DeviceController.java delete mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/WelcomeController.java create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/DeviceSaveRequest.java create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/error/BadDataException.java diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/SpringFoxConfig.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/SpringFoxConfig.java index 4011592..2fdab4e 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/SpringFoxConfig.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/SpringFoxConfig.java @@ -1,6 +1,5 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.config; -import static springfox.documentation.builders.PathSelectors.regex; import java.util.List; import java.util.function.Predicate; @@ -10,10 +9,9 @@ import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; -import springfox.documentation.service.ApiInfo; -import springfox.documentation.service.ApiKey; -import springfox.documentation.service.SecurityScheme; +import springfox.documentation.service.*; import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spi.service.contexts.SecurityContext; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @@ -39,7 +37,8 @@ public class SpringFoxConfig { .paths(paths()::test) .build() .apiInfo(apiInfo()) - .securitySchemes(securitySchemes()); + .securitySchemes(securitySchemes()) + .securityContexts(List.of(securityContext())); } /** @@ -51,14 +50,32 @@ public class SpringFoxConfig { return List.of(new ApiKey("Bearer", "Authorization", "header")); } - /** - * Return a Java functional API predicate for regex matches - * - * @param regex the regex to match on - * @return a Java functional API predicate - */ - private Predicate regexPredicate(final String regex) { - return regex(regex)::apply; + private SecurityContext securityContext() { + return SecurityContext.builder() + .securityReferences(defaultAuth()) + .forPaths(authenticatedPaths()::test) + .build(); + } + + private List defaultAuth() { + final AuthorizationScope authorizationScope = + new AuthorizationScope("global", "accessEverything"); + return List.of( + new SecurityReference("Bearer", new AuthorizationScope[] {authorizationScope})); + } + + private Predicate authenticatedPaths() { + return ((Predicate) PathSelectors.regex("/auth/update")::apply) + .or(PathSelectors.regex("/room.*")::apply) + .or(PathSelectors.regex("/device.*")::apply) + .or(PathSelectors.regex("/buttonDimmer.*")::apply) + .or(PathSelectors.regex("/dimmableLight.*")::apply) + .or(PathSelectors.regex("/knobDimmer.*")::apply) + .or(PathSelectors.regex("/regularLight.*")::apply) + .or(PathSelectors.regex("/sensor.*")::apply) + .or(PathSelectors.regex("/smartPlug.*")::apply) + .or(PathSelectors.regex("/switch.*")::apply) + .or(PathSelectors.regex("/motionSensor.*")::apply); } /** diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DeviceController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DeviceController.java new file mode 100644 index 0000000..fa3242d --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DeviceController.java @@ -0,0 +1,44 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; + +import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.DeviceSaveRequest; +import ch.usi.inf.sa4.sanmarinoes.smarthut.error.BadDataException; +import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Device; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DeviceRepository; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RoomRepository; +import javax.validation.Valid; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@EnableAutoConfiguration +@RequestMapping("/device") +public class DeviceController { + + @Autowired private DeviceRepository deviceRepository; + @Autowired private RoomRepository roomRepository; + + @PutMapping + public Device update(@Valid @RequestBody DeviceSaveRequest deviceSaveRequest) + throws NotFoundException, BadDataException { + final Device d = + deviceRepository + .findById(deviceSaveRequest.getId()) + .orElseThrow(NotFoundException::new); + + // check if roomId is valid + roomRepository + .findById(deviceSaveRequest.getRoomId()) + .orElseThrow(() -> new BadDataException("roomId is not a valid room id")); + + d.setRoomId(deviceSaveRequest.getRoomId()); + d.setName(deviceSaveRequest.getName()); + + deviceRepository.save(d); + return d; + } +} 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 3220b76..063a4a6 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 @@ -42,6 +42,7 @@ public class DimmableLightController { @PutMapping public DimmableLight update(@Valid @RequestBody DimmableLightSaveRequest dl) { + dl.setId(0); return this.create(dl); } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/KnobDimmerController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/KnobDimmerController.java index 81f54ea..6544c8d 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/KnobDimmerController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/KnobDimmerController.java @@ -42,6 +42,7 @@ public class KnobDimmerController { @PutMapping public KnobDimmer update(@Valid @RequestBody KnobDimmerSaveRequest kd) { + kd.setId(0); return this.create(kd); } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/MotionSensorController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/MotionSensorController.java index a09f900..c0ba65a 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/MotionSensorController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/MotionSensorController.java @@ -42,6 +42,7 @@ public class MotionSensorController { @PutMapping public MotionSensor update(@Valid @RequestBody MotionSensorSaveRequest ms) { + ms.setId(0); return this.create(ms); } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightController.java index 061f12c..acffb30 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightController.java @@ -49,6 +49,7 @@ public class RegularLightController { @PutMapping public RegularLight update(@Valid @RequestBody RegularLightSaveRequest rl) { + rl.setId(0); return this.create(rl); } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RoomController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RoomController.java index 3a2ba78..a5e6436 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RoomController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RoomController.java @@ -44,12 +44,12 @@ public class RoomController { newRoom.setUserId(userId); newRoom.setName(r.getName()); if (img != null) { - newRoom.setImage(img.getBytes()); + newRoom.setImage(img); } else if (setWhenNull) { newRoom.setImage(null); } if (icon != null) { - newRoom.setIcon(icon.getBytes()); + newRoom.setIcon(icon); } else if (setWhenNull) { newRoom.setIcon(null); } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorController.java index d738a37..cceb05a 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorController.java @@ -43,6 +43,7 @@ public class SensorController { @PutMapping public Sensor update(@Valid @RequestBody SensorSaveRequest s) { + s.setId(0); return this.create(s); } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SmartPlugController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SmartPlugController.java index 43b66d9..e05ce72 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SmartPlugController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SmartPlugController.java @@ -42,6 +42,7 @@ public class SmartPlugController { @PutMapping public SmartPlug update(@Valid @RequestBody SmartPlugSaveRequest sp) { + sp.setId(0); return this.create(sp); } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchController.java index 5df72cf..9166e15 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchController.java @@ -42,6 +42,7 @@ public class SwitchController { @PutMapping public Switch update(@Valid @RequestBody SwitchSaveRequest s) { + s.setId(0); return this.create(s); } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/WelcomeController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/WelcomeController.java deleted file mode 100644 index a81eec6..0000000 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/WelcomeController.java +++ /dev/null @@ -1,15 +0,0 @@ -package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; - -import org.springframework.boot.autoconfigure.*; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.*; - -@RestController -@EnableAutoConfiguration -public class WelcomeController { - - @GetMapping - ResponseEntity testConnection() { - return ResponseEntity.ok(null); - } -} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/ButtonDimmerSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/ButtonDimmerSaveRequest.java index 31a22d8..7dda699 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/ButtonDimmerSaveRequest.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/ButtonDimmerSaveRequest.java @@ -60,4 +60,8 @@ public class ButtonDimmerSaveRequest { public String getName() { return name; } + + public void setId(long id) { + this.id = id; + } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/DeviceSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/DeviceSaveRequest.java new file mode 100644 index 0000000..a975117 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/DeviceSaveRequest.java @@ -0,0 +1,42 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; + +import javax.validation.constraints.NotEmpty; +import javax.validation.constraints.NotNull; + +public class DeviceSaveRequest { + /** Device identifier */ + private long id; + + /** + * The room this device belongs in, as a foreign key id. To use when updating and inserting from + * a REST call. + */ + @NotNull private Long roomId; + + /** The name of the device as assigned by the user (e.g. 'Master bedroom light') */ + @NotNull @NotEmpty private String name; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public Long getRoomId() { + return roomId; + } + + public void setRoomId(Long roomId) { + this.roomId = roomId; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/DimmableLightSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/DimmableLightSaveRequest.java index 8edff94..75fb74e 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/DimmableLightSaveRequest.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/DimmableLightSaveRequest.java @@ -66,4 +66,8 @@ public class DimmableLightSaveRequest { } this.intensity = intensity; } + + public void setId(long id) { + this.id = id; + } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/KnobDimmerSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/KnobDimmerSaveRequest.java index ce053e3..73d44d2 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/KnobDimmerSaveRequest.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/KnobDimmerSaveRequest.java @@ -48,4 +48,8 @@ public class KnobDimmerSaveRequest { public Set getLights() { return lights; } + + public void setId(long id) { + this.id = id; + } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/MotionSensorSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/MotionSensorSaveRequest.java index ba73495..a44c343 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/MotionSensorSaveRequest.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/MotionSensorSaveRequest.java @@ -44,4 +44,8 @@ public class MotionSensorSaveRequest { public void setDetected(boolean detected) { this.detected = detected; } + + public void setId(long id) { + this.id = id; + } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/RegularLightSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/RegularLightSaveRequest.java index ac1324d..99211e5 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/RegularLightSaveRequest.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/RegularLightSaveRequest.java @@ -45,4 +45,8 @@ public class RegularLightSaveRequest { public void setOn(boolean on) { this.on = on; } + + public void setId(long id) { + this.id = id; + } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SensorSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SensorSaveRequest.java index 421523c..0985db2 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SensorSaveRequest.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SensorSaveRequest.java @@ -78,4 +78,8 @@ public class SensorSaveRequest { public void setValue(int newValue) { this.value = newValue; } + + public void setId(long id) { + this.id = id; + } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SmartPlugSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SmartPlugSaveRequest.java index 3318505..6b2f9b5 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SmartPlugSaveRequest.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SmartPlugSaveRequest.java @@ -45,4 +45,8 @@ public class SmartPlugSaveRequest { public void setOn(boolean on) { this.on = on; } + + public void setId(long id) { + this.id = id; + } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SwitchSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SwitchSaveRequest.java index c7516f2..88050c0 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SwitchSaveRequest.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SwitchSaveRequest.java @@ -45,4 +45,8 @@ public class SwitchSaveRequest { public void setOn(boolean on) { this.on = on; } + + public void setId(long id) { + this.id = id; + } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/error/BadDataException.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/error/BadDataException.java new file mode 100644 index 0000000..2c6c4d4 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/error/BadDataException.java @@ -0,0 +1,11 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.error; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(code = HttpStatus.BAD_REQUEST) +public class BadDataException extends Exception { + public BadDataException(String message) { + super(message); + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Room.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Room.java index e15805b..72859d5 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Room.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Room.java @@ -20,13 +20,11 @@ public class Room { * https://www.baeldung.com/java-base64-image-string * https://docs.oracle.com/javase/8/docs/api/java/util/Base64.html */ - @Lob - @Column(name = "icon", columnDefinition = "TEXT") - private byte[] icon; + @Column private String icon; @Lob @Column(name = "image", columnDefinition = "TEXT") - private byte[] image; + private String image; /** * User that owns the house this room is in as a foreign key id. To use when updating and @@ -65,19 +63,19 @@ public class Room { this.name = name; } - public byte[] getIcon() { + public String getIcon() { return icon; } - public void setIcon(byte[] icon) { + public void setIcon(String icon) { this.icon = icon; } - public byte[] getImage() { + public String getImage() { return image; } - public void setImage(byte[] image) { + public void setImage(String image) { this.image = image; }