Swagger configuration

This commit is contained in:
Claudio Maggioni 2020-03-01 15:13:37 +01:00
parent 54168a4261
commit 6ce90e61cb
4 changed files with 65 additions and 102 deletions

View file

@ -1,25 +1,77 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.config; package ch.usi.inf.sa4.sanmarinoes.smarthut.config;
import ch.usi.inf.sa4.sanmarinoes.smarthut.controller.AuthenticationController; import static springfox.documentation.builders.PathSelectors.regex;
import java.util.List;
import java.util.function.Predicate;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.SecurityScheme;
import springfox.documentation.spi.DocumentationType; import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2; import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* This class configures the automated REST documentation tool Swagger for this project. The
* documentation can be seen by going to http://localhost:8080/swaggeer-ui.html
*/
@Configuration @Configuration
@EnableSwagger2 @EnableSwagger2
@ComponentScan(basePackageClasses = {AuthenticationController.class}) @ComponentScan("ch.usi.inf.sa4.sanmarinoes.smarthut")
public class SpringFoxConfig { public class SpringFoxConfig {
/**
* Main definition of Springfox / swagger configuration
*
* @return a Docket object containing the swagger configuration
*/
@Bean @Bean
public Docket api() { public Docket api() {
return new Docket(DocumentationType.SWAGGER_2) return new Docket(DocumentationType.SWAGGER_2)
.select() .select()
.apis(RequestHandlerSelectors.any()) .apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any()) .paths(paths()::test)
.build()
.apiInfo(apiInfo())
.securitySchemes(securitySchemes());
}
/**
* Configures the documentation about the smarthut authentication system
*
* @return a list of springfox authentication configurations
*/
private static List<? extends SecurityScheme> securitySchemes() {
return List.of(new ApiKey("Bearer", "Authorization", "header"));
}
/**
* Configures the paths the documentation must be generated for. Add a path here only when the
* spec has been totally defined.
*
* @return A predicate that tests whether a path must be included or not
*/
private Predicate<String> paths() {
return regex("/auth.*")::apply;
}
/**
* Returns the metadata about the smarthut project
*
* @return metadata about smarthut
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("SmartHut.sm API")
.description("Backend API for the SanMariones version of the SA4 SmartHut project")
.termsOfServiceUrl("https://www.youtube.com/watch?v=9KxTcDsy9Gs")
.license("WTFPL")
.version("dev branch")
.build(); .build();
} }
} }

View file

@ -5,6 +5,7 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.JWTRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.JWTResponse; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.JWTResponse;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.UserUpdateRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.UserUpdateRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import io.swagger.annotations.Authorization;
import java.security.Principal; import java.security.Principal;
import javax.validation.Valid; import javax.validation.Valid;
import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.AuthenticationManager;
@ -61,15 +62,18 @@ public class AuthenticationController {
return user; return user;
} }
@Authorization(value = "Bearer")
@PatchMapping("/update") @PatchMapping("/update")
public User update(@Valid @RequestBody final UserUpdateRequest u, final Principal principal) { public User update(
@Valid @RequestBody final UserUpdateRequest userData, final Principal principal) {
final User oldUser = userRepository.findByUsername(principal.getName()); final User oldUser = userRepository.findByUsername(principal.getName());
if (u.getName() != null) oldUser.setName(u.getName()); if (userData.getName() != null) oldUser.setName(userData.getName());
if (u.getEmail() != null) { if (userData.getEmail() != null) {
oldUser.setEmail(u.getEmail()); oldUser.setEmail(userData.getEmail());
// TODO: handle email verification // TODO: handle email verification
} }
if (u.getPassword() != null) oldUser.setPassword(encoder.encode(u.getPassword())); if (userData.getPassword() != null)
oldUser.setPassword(encoder.encode(userData.getPassword()));
return userRepository.save(oldUser); return userRepository.save(oldUser);
} }

View file

@ -1,9 +1,6 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.OneToMany;
/** /**
* Represents a dimmer that can only instruct an increase or decrease of intensity (i.e. like a * Represents a dimmer that can only instruct an increase or decrease of intensity (i.e. like a
@ -11,47 +8,7 @@ import javax.persistence.OneToMany;
*/ */
@Entity @Entity
public class ButtonDimmer extends Dimmer { public class ButtonDimmer extends Dimmer {
@OneToMany(mappedBy = "button_dimmer")
private Set<DimmableLight> lights = new HashSet<DimmableLight>();
public ButtonDimmer() { public ButtonDimmer() {
super("button-dimmer"); super("button-dimmer");
} }
/** Increases the current intensity level of the dimmable light by 1 */
public void increaseIntensity() {
for (DimmableLight dl : lights) {
dl.setIntensity(dl.getIntensity() + 1);
}
}
/** Decreases the current intensity level of the dimmable light by 1 */
public void decreaseIntensity() {
for (DimmableLight dl : lights) {
dl.setIntensity(dl.getIntensity() - 1);
}
}
/**
* Adds a DimmableLight to this set of DimmableLights
*
* @param dl The DimmableLight to be added
*/
public void addLight(DimmableLight dl) {
lights.add(dl);
}
/**
* Removes the given DimmableLight
*
* @param dl The DimmableLight to be removed
*/
public void removeLight(DimmableLight dl) {
lights.remove(dl);
}
/** Clears this set */
public void clearSet() {
lights.clear();
}
} }

View file

@ -1,9 +1,6 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.OneToMany;
/** /**
* Represents a dimmer able to set absolute intensity values (i.e. knowing the absolute intensity * Represents a dimmer able to set absolute intensity values (i.e. knowing the absolute intensity
@ -11,54 +8,7 @@ import javax.persistence.OneToMany;
*/ */
@Entity @Entity
public class KnobDimmer extends Dimmer { public class KnobDimmer extends Dimmer {
@OneToMany(mappedBy = "knob_dimmer")
private Set<DimmableLight> lights = new HashSet<DimmableLight>();
public KnobDimmer() { public KnobDimmer() {
super("knob-dimmer"); super("knob-dimmer");
} }
/**
* Increases or decreases the current intensity level by 5, moving between absolute multiples of
* 5 between 0 and 100, of all dimmable lights mapped to this knob
*
* @param inc The direction the knob is turned with
*/
public void modifyIntensity(boolean inc) {
for (DimmableLight dl : lights) {
int remainder = dl.getIntensity() / 5;
if (inc) {
dl.setIntensity(dl.getIntensity() - remainder);
dl.setIntensity((dl.getIntensity() + 5) % 105);
} else {
dl.setIntensity(dl.getIntensity() + (5 - remainder));
dl.setIntensity((dl.getIntensity() - 5) % 105);
}
}
}
/**
* Adds a DimmableLight to this set of DimmableLights
*
* @param dl The DimmableLight to be added
*/
public void addLight(DimmableLight dl) {
lights.add(dl);
}
/**
* Removes the given DimmableLight
*
* @param dl The DimmableLight to be removed
*/
public void removeLight(DimmableLight dl) {
lights.remove(dl);
}
/** Clears this set */
public void clearSet() {
lights.clear();
}
} }