Resrservice docs 1st part

This commit is contained in:
Filip Izydorczyk 2021-01-15 17:45:29 +01:00
parent 061c3a24fe
commit f77213a01e
25 changed files with 406 additions and 35 deletions

View File

@ -21,6 +21,9 @@ import org.springframework.scheduling.annotation.EnableScheduling;
import com.plannaplan.services.ConfiguratorService;
/**
* Root class of Application.
*/
@SpringBootApplication
@EnableScheduling
public class App {

View File

@ -3,6 +3,9 @@ package com.plannaplan;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
* Class to generate logo string on start application and make logs info
*/
public class Logo {
public static final String ANSI_RESET = "\u001B[0m";
@ -35,6 +38,11 @@ ANSI_RESET;
}
/**
* return init string to log
* @param isDev is spring profile dev
* @return string to print in log
*/
public static String getInitInfo(boolean isDev){
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
LocalDateTime now = LocalDateTime.now();
@ -45,6 +53,11 @@ ANSI_RESET;
return ANSI_BLACK + dtf.format(now) + ANSI_YELLOW + " plannaplan" + ANSI_RESET + " initializing [" +ANSI_BLUE + "prod" + ANSI_RESET +"]";
}
/**
* return start string to log
* @param isDev is spring profile dev
* @return string to print in log
*/
public static String getStartedInfo(boolean isDev){
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
LocalDateTime now = LocalDateTime.now();

View File

@ -18,6 +18,9 @@ import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* Config class of Swagger to generate rest api documentation
*/
@Configuration
@EnableSwagger2
public class Swagger2Config extends WebMvcConfigurationSupport {

View File

@ -8,6 +8,9 @@ import com.plannaplan.types.GroupType;
import io.swagger.annotations.ApiModel;
/**
* Assignment entity api response
*/
@ApiModel(description = "Response shows information about given assigment to course.", value = "AssignmentResponse")
public class AssignmentResponse {
private Long id;
@ -15,6 +18,11 @@ public class AssignmentResponse {
private GroupWithCapacityResponse classes;
private GroupWithCapacityResponse lecture;
/**
* @param course course entity
* @param lecture lecture Groups entity
* @param classes class Groups entity
*/
public AssignmentResponse(Course course, Groups lecture, Groups classes) {
this.id = course.getId();
this.name = course.getName();
@ -22,6 +30,13 @@ public class AssignmentResponse {
this.classes = classes == null ? null : new GroupWithCapacityResponse(classes);
}
/**
* @param course course entity
* @param lecture lecture Groups entity
* @param classes class Groups entity
* @param ammounts map with ammounts key - group id, value - ammounts of taken
* places
*/
public AssignmentResponse(Course course, Groups lecture, Groups classes, HashMap<Long, Integer> ammounts) {
this.id = course.getId();
this.name = course.getName();
@ -29,28 +44,51 @@ public class AssignmentResponse {
this.classes = classes == null ? null : new GroupWithCapacityResponse(classes, ammounts.get(classes.getId()));
}
/**
*
* @param course course entity
* @param group class/lecture entity
*/
public AssignmentResponse(Course course, Groups group) {
this(course, group.getType() == GroupType.LECTURE ? group : null,
group.getType() == GroupType.CLASS ? group : null);
}
/**
* @param course course entity
* @param group class/lecture entity
* @param ammounts map with ammounts key - group id, value - ammounts of taken
* places
*/
public AssignmentResponse(Course course, Groups group, HashMap<Long, Integer> ammounts) {
this(course, group.getType() == GroupType.LECTURE ? group : null,
group.getType() == GroupType.CLASS ? group : null, ammounts);
}
/**
* @return Lecture in api response forms
*/
public GroupWithCapacityResponse getLecture() {
return this.lecture;
}
/**
* @return Class in api response forms
*/
public GroupWithCapacityResponse getClasses() {
return this.classes;
}
/**
* @return String course name
*/
public String getName() {
return this.name;
}
/**
* @return db assignment id
*/
public Long getId() {
return this.id;
}

View File

@ -5,6 +5,9 @@ import com.plannaplan.entities.Commision;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
/**
* Commision api response
*/
@ApiModel(description = "Response shows information about commision.", value = "CommisionResponse")
public class CommisionResponse {
@ApiModelProperty(value = "ID created by database")
@ -19,6 +22,9 @@ public class CommisionResponse {
@ApiModelProperty(value = "Timestamp where the user commit the commision")
private String commisionDate;
/**
* @param commision commision to map to api response
*/
public CommisionResponse(Commision commision) {
this.id = commision.getId();
this.commisionDate = commision.getCommisionDate().toString();
@ -27,18 +33,30 @@ public class CommisionResponse {
: null;
}
/**
* @return get Commiter user as api response
*/
public UserResponse getCommiter() {
return commiter;
}
/**
* @return get Owner user as api response
*/
public UserResponse getOwner() {
return owner;
}
/**
* @return when commision was created string formated
*/
public String getCommisionDate() {
return commisionDate;
}
/**
* @return db id
*/
public Long getId() {
return id;
}

View File

@ -11,12 +11,18 @@ import com.plannaplan.entities.Commision;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
/**
* Commision With Groups api Response. It extends CommisionResponse repsone
*/
@ApiModel(description = "Response shows information about commision and its groups.", value = "CommisionWithGroupsResponse")
public class CommisionWithGroupsResponse extends CommisionResponse {
@ApiModelProperty(value = "List of groups ids in databse that belongs to commision")
private List<Long> groups;
/**
* @param commision commision to map to api response
*/
public CommisionWithGroupsResponse(Commision commision) {
super(commision);
this.groups = commision.getAssignments().stream().filter(Objects::nonNull)
@ -29,6 +35,9 @@ public class CommisionWithGroupsResponse extends CommisionResponse {
}).collect(Collectors.toList());
}
/**
* @return lsit of fetured groups ids
*/
public List<Long> getGroups() {
return groups;
}

View File

@ -5,21 +5,36 @@ import java.util.List;
import io.swagger.annotations.ApiModel;
/**
* Course respose with all realted groups as api response. Alse needs to specify
* what type of Groups api response will be kept here. For excmaple it can be
* <b> GroupDefaultRespnse </b>
*/
@ApiModel(description = "Response shows information about groups to given course.", value = "CourseWithGroupsResponse")
public class CourseWithGroupsResponse <T> {
public class CourseWithGroupsResponse<T> {
private List<T> lectures = new ArrayList<>();
private List<T> classes = new ArrayList<>();
public CourseWithGroupsResponse(List<T> classes, List<T> lectures ){
/**
* @param classes realted classes Groups instance
* @param lectures realted lectures Groups instance
*/
public CourseWithGroupsResponse(List<T> classes, List<T> lectures) {
this.lectures = lectures;
this.classes = classes;
}
/**
* @return realted classes Groups instance
*/
public List<T> getClasses() {
return this.classes;
}
/**
* @return realted lectures Groups instance
*/
public List<T> getLectures() {
return this.lectures;
}

View File

@ -5,9 +5,21 @@ import com.plannaplan.responses.models.abstracts.CoursesResponse;
import io.swagger.annotations.ApiModel;
/**
* Courses Default Api Response. It extends abstract response - CoursesResponse.
* It was one of first repsones created in system. Later we resigned from
* asbstract and used settig
* <code>spring.jackson.default-property-inclusion = NON_NULL</code> in
* properties instead.
*/
@ApiModel(description = "Response shows information about course.", value = "CoursesDefaultResponse")
public class CoursesDefaultResponse extends CoursesResponse {
/**
* create new instance
*
* @param course course to map to api response
*/
public CoursesDefaultResponse(Course course) {
super(course);
}

View File

@ -9,12 +9,24 @@ import com.plannaplan.types.GroupType;
import io.swagger.annotations.ApiModel;
/**
* Courses With Groups Api Response . It extends abstract response -
* CoursesResponse. It was one of first repsones created in system. Later we
* resigned from asbstract and used settig
* <code>spring.jackson.default-property-inclusion = NON_NULL</code> in
* properties instead.
*/
@ApiModel(description = "Response shows information about groups to given course.", value = "CoursesWithGroupsResponse")
public class CoursesWithGroupsResponse extends CoursesResponse {
private List<GroupWithCapacityResponse> lectures = new ArrayList<>();
private List<GroupWithCapacityResponse> classes = new ArrayList<>();
/**
* create new instance
*
* @param course course to map to api response
*/
public CoursesWithGroupsResponse(Course course) {
super(course);
course.getGroups().stream().forEach(group -> {
@ -26,6 +38,12 @@ public class CoursesWithGroupsResponse extends CoursesResponse {
});
}
/**
*
* @param course course to map to api response
* @param lectures list of api resposnes of lectures
* @param classes list of api resposnes of classes
*/
public CoursesWithGroupsResponse(Course course, List<GroupWithCapacityResponse> lectures,
List<GroupWithCapacityResponse> classes) {
super(course);
@ -33,10 +51,16 @@ public class CoursesWithGroupsResponse extends CoursesResponse {
this.classes = classes;
}
/**
* @return list of api resposnes of classes
*/
public List<GroupWithCapacityResponse> getClasses() {
return this.classes;
}
/**
* @return list of api resposnes of lectures
*/
public List<GroupWithCapacityResponse> getLectures() {
return this.lectures;
}

View File

@ -2,35 +2,72 @@ package com.plannaplan.responses.models;
import com.plannaplan.entities.Exchange;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
/**
* Exchange proposal api repsonse
*/
@ApiModel(description = "Response shows information about user exchanges", value = "CourseWithGroupsResponse")
public class ExchangeResponse {
@ApiModelProperty(value = "Database id")
private Long id;
@ApiModelProperty(value = "Assignmetn that user want to trade")
private GroupDefaultResponse ownedAssignment;
@ApiModelProperty(value = "Group that user want to get")
private GroupDefaultResponse desiredGroup;
public ExchangeResponse(Exchange exchange){
/**
* creat new instance
*
* @param exchange entity to map to api repsone
*/
public ExchangeResponse(Exchange exchange) {
this.id = exchange.getId();
this.ownedAssignment = new GroupDefaultResponse(exchange.getOwnedAssignment().getGroup());
this.desiredGroup = new GroupDefaultResponse(exchange.getDesiredAssignment());
}
/**
* @return get api respondse of wanted byt user group
*/
public GroupDefaultResponse getDesiredGroup() {
return desiredGroup;
}
/**
* @param desiredGroup set api respondse of wanted byt user group
*/
public void setDesiredGroup(GroupDefaultResponse desiredGroup) {
this.desiredGroup = desiredGroup;
}
/**
* @return get api respondse of owned user group
*/
public GroupDefaultResponse getOwnedAssignment() {
return ownedAssignment;
}
/**
* @param ownedAssignment set api respondse of owned user group
*/
public void setOwnedAssignment(GroupDefaultResponse ownedAssignment) {
this.ownedAssignment = ownedAssignment;
}
/**
* @return database id
*/
public Long getId() {
return id;
}
/**
* @param id set database id
*/
public void setId(Long id) {
this.id = id;
}
}

View File

@ -8,6 +8,9 @@ import com.plannaplan.types.GroupType;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
/**
* Default api response for Groups entity
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
@ApiModel(description = "Response shows information about given group.", value = "GroupDefaultResponse")
public class GroupDefaultResponse {
@ -36,6 +39,11 @@ public class GroupDefaultResponse {
@ApiModelProperty(value = "Value shows how many places is already taken by other students.")
private Integer takenPlaces;
/**
* creat new entity
*
* @param group entity to map to api response
*/
public GroupDefaultResponse(Groups group) {
this.id = group.getId() != null ? group.getId() : null;
this.day = group.getDay() != null ? group.getDay().label : -1;
@ -46,43 +54,77 @@ public class GroupDefaultResponse {
this.type = group.getType() != null ? group.getType() : null;
}
/**
*
* @param group entity to map to api response
* @param takenPlaces ammount of taken places for group by other users
*/
public GroupDefaultResponse(Groups group, int takenPlaces) {
this(group);
this.takenPlaces = takenPlaces;
}
/**
* creat new entity
*
* @param assignment entity to map to api response
*/
public GroupDefaultResponse(Assignment assignment) {
this(assignment.getGroup());
}
/**
* @return what typew of group is this (lecture or class)
*/
public GroupType getType() {
return type;
}
/**
* @return room where classes will take place
*/
public String getRoom() {
return room;
}
/**
* @return name of group lecturer
*/
public String getLecturer() {
return lecturer;
}
/**
* @return time when group is scheduled on
*/
public String getTime() {
return time;
}
/**
* @return time when class ends
*/
public String getEndTime() {
return endTime;
}
/**
* @return int what day is it. 0-6 (Monday - Sunday)
*/
public int getDay() {
return day;
}
/**
* @return db id
*/
public Long getId() {
return id;
}
/**
* @return ammount of taken places for group by other users
*/
public Integer getTakenPlaces() {
return this.takenPlaces;
}

View File

@ -4,29 +4,57 @@ import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Groups;
import io.swagger.annotations.ApiModel;
/**
* Group api response featuring group capacity
*/
@ApiModel(description = "Response shows information about group with included capacity.", value = "GroupWithCapacityResponse")
public class GroupWithCapacityResponse extends GroupDefaultResponse {
private int capacity;
/**
* create new instance
*
* @param group entity to map to api response
*/
public GroupWithCapacityResponse(Groups group) {
super(group);
this.capacity = group.getCapacity();
}
/**
* create new instance
*
* @param group entity to map to api response
* @param takenPlaces group taken places
*/
public GroupWithCapacityResponse(Groups group, int takenPlaces) {
super(group, takenPlaces);
this.capacity = group.getCapacity();
}
/**
* create new instance
*
* @param assignment entity to map to api response
*/
public GroupWithCapacityResponse(Assignment assignment) {
this(assignment.getGroup());
}
/**
* create new instance
*
* @param assignment entity to map to api response
* @param takenPlaces group taken places
*/
public GroupWithCapacityResponse(Assignment assignment, int takenPlaces) {
this(assignment.getGroup(), takenPlaces);
}
/**
* @return group taken places
*/
public int getCapacity() {
return capacity;
}

View File

@ -5,6 +5,9 @@ import com.plannaplan.entities.User;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
/**
* Response to show after successfully login cation
*/
@ApiModel(description = "Response shows information about logged user.", value = "TokenResponse")
public class TokenResponse {
@ApiModelProperty(value = "user token used to verify requests")
@ -18,6 +21,9 @@ public class TokenResponse {
@ApiModelProperty(value = "user unviersity email")
private String email;
/**
* @param user user to be mapped to api response
*/
public TokenResponse(User user) {
this.id = user.getId();
this.authorityRole = user.getRole().toString();
@ -26,22 +32,37 @@ public class TokenResponse {
this.refreshToken = user.getRefreshToken();
}
/**
* @return user email
*/
public String getEmail() {
return email;
}
/**
* @return user role in system
*/
public String getAuthorityRole() {
return authorityRole;
}
/**
* @return db id
*/
public Long getId() {
return id;
}
/**
* @return user token to authorize other requests
*/
public String getToken() {
return token;
}
/**
* @return user refresh token
*/
public String getRefreshToken() {
return this.refreshToken;
}

View File

@ -4,6 +4,9 @@ import com.plannaplan.entities.User;
import io.swagger.annotations.ApiModel;
/**
* Model for api response for user serach results.
*/
@ApiModel(description = "Response shows information about user.", value = "UserResponse")
public class UserResponse {
@ -12,6 +15,9 @@ public class UserResponse {
private String surname;
private String email;
/**
* @param user entity to be mapped to api response
*/
public UserResponse(User user) {
this.id = user.getId();
this.name = user.getName() != null ? user.getName() : "";
@ -19,18 +25,30 @@ public class UserResponse {
this.email = user.getEmail();
}
/**
* @return user email
*/
public String getEmail() {
return email;
}
/**
* @return user surname
*/
public String getSurname() {
return surname;
}
/**
* @return user name
*/
public String getName() {
return name;
}
/**
* @return db id
*/
public Long getId() {
return id;
}

View File

@ -2,20 +2,34 @@ package com.plannaplan.responses.models.abstracts;
import com.plannaplan.entities.Course;
/**
* Course entity api response
*/
public abstract class CoursesResponse {
private Long id;
private String name;
/**
* create instance
*
* @param course entity to map to api response
*/
public CoursesResponse(Course course) {
this.id = course.getId() != null ? course.getId() : null;
this.name = course.getName() != null ? course.getName() : "";
}
/**
* @return course name
*/
public String getName() {
return name;
}
/**
* @return db id
*/
public Long getId() {
return id;
}

View File

@ -17,6 +17,9 @@ import org.springframework.security.web.util.matcher.RequestMatcher;
import static org.springframework.http.HttpHeaders.AUTHORIZATION;
/**
* Spring authentication filter class
*/
public class AuthenticationFilter extends AbstractAuthenticationProcessingFilter {
AuthenticationFilter(final RequestMatcher requiresAuth) {

View File

@ -16,6 +16,9 @@ import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
/**
* Spring authentication provider
*/
@Component
public class AuthenticationProvider extends AbstractUserDetailsAuthenticationProvider {
@ -25,8 +28,6 @@ public class AuthenticationProvider extends AbstractUserDetailsAuthenticationPro
@Override
protected void additionalAuthenticationChecks(UserDetails userDetails,
UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {
// is being done in other task
}
@Override

View File

@ -6,6 +6,9 @@ import com.plannaplan.types.UserRoles;
import org.springframework.security.core.GrantedAuthority;
/**
* Users Roles for spring app
*/
public enum AuthorityRoles implements GrantedAuthority {
STUDENT("ROLE_STUDENT"), DEANERY("ROLE_DEANERY"), ADMIN("ROLE_ADMIN"), TEST_USER("ROLE_TESTUSER");
@ -20,6 +23,12 @@ public enum AuthorityRoles implements GrantedAuthority {
return this.role;
}
/**
* map buisness logic UserRoles enum to spring enum
*
* @param role buisness logic enum
* @return restservice AuthorityRoles enum
*/
public static final Optional<AuthorityRoles> getAuthorityRole(UserRoles role) {
switch (role) {
case ADMIN:

View File

@ -16,9 +16,11 @@ import org.springframework.http.HttpStatus;
import org.springframework.security.web.authentication.AnonymousAuthenticationFilter;
import org.springframework.security.web.authentication.HttpStatusEntryPoint;
/**
* Spring config class for security
*/
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
private static final RequestMatcher PROTECTED_URLS = new OrRequestMatcher(new AntPathRequestMatcher("/api/**"));

View File

@ -1,18 +1,35 @@
package com.plannaplan.security.cas;
/**
* Model to keep data from Cas response. It's important to remember that wee
* need to register our domain name in CAS in order to get this data. Otherwise
* CAS will retuned what user typed as login.
*/
public class CasUserIdentity {
private String usosId;
private String email;
public CasUserIdentity(String usosId, String email){
/**
* creates new instance
*
* @param usosId usosId retured from CAS
* @param email emial returned from CAS
*/
public CasUserIdentity(String usosId, String email) {
this.usosId = usosId;
this.email = email;
}
/**
* @return string with usosid
*/
public String getUsosId() {
return usosId;
}
/**
* @return string with email
*/
public String getEmail() {
return email;
}

View File

@ -1,9 +1,9 @@
package com.plannaplan.security.cas;
public class CasValidationExcepiton extends RuntimeException {
/**
*
/**
* Exception to throw when cas didn't validate provided ticket
*/
public class CasValidationExcepiton extends RuntimeException {
private static final long serialVersionUID = 1L;
public CasValidationExcepiton(String s) {

View File

@ -1,6 +1,17 @@
package com.plannaplan.security.cas;
/**
* We can authorize in different contexts. For example if we rgister our domain
* we will get more info than on localhost. This abstraction let's us handle
* both cases based on spring profile
*/
public interface CasValidator {
/**
* validate ticket (should be provided in costructor or by setter)
*
* @return CasUserIdentity instance with values from response or null if
* validation failed
*/
CasUserIdentity validate();
}

View File

@ -4,6 +4,10 @@ import org.jasig.cas.client.validation.Assertion;
import org.jasig.cas.client.validation.Cas20ServiceTicketValidator;
import org.jasig.cas.client.validation.TicketValidationException;
/**
* Cas Validator for UAM with domain registered for email and usosid as a
* repsonse
*/
public class CustomUAMCasValidator implements CasValidator {
private static String CAS_URL = "https://cas.amu.edu.pl/cas";
private static String EMAIL_FIELD = "mail";
@ -11,31 +15,36 @@ public class CustomUAMCasValidator implements CasValidator {
private String service;
private String ticket;
public CustomUAMCasValidator(String service, String ticket){
/**
* craste new instance
*
* @param service string with url of our service from where request began
* @param ticket ticket returned after user provided creeds on cas page
*/
public CustomUAMCasValidator(String service, String ticket) {
this.service = service;
this.ticket = ticket;
}
@Override
public CasUserIdentity validate() {
/*
* TO DO
* Dodać case z CAS10/CAS20/CAS30
*/
final Cas20ServiceTicketValidator validator = new Cas20ServiceTicketValidator(CustomUAMCasValidator.CAS_URL);
try {
final Assertion assertion = validator.validate(this.ticket, this.service);
if (assertion == null) {
throw new CasValidationExcepiton("Validation failed. Assertion could not be retrieved for ticket " + "");
throw new CasValidationExcepiton(
"Validation failed. Assertion could not be retrieved for ticket " + "");
}
final String usosid = assertion.getPrincipal().getAttributes().get(CustomUAMCasValidator.USOS_ID).toString();
final String usosid = assertion.getPrincipal().getAttributes().get(CustomUAMCasValidator.USOS_ID)
.toString();
final String mail = assertion.getPrincipal().getAttributes().get(CustomUAMCasValidator.EMAIL_FIELD).toString();
final String mail = assertion.getPrincipal().getAttributes().get(CustomUAMCasValidator.EMAIL_FIELD)
.toString();
return new CasUserIdentity(usosid,mail);
return new CasUserIdentity(usosid, mail);
} catch (TicketValidationException e) {
e.printStackTrace();

View File

@ -9,12 +9,21 @@ import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
/**
* Cas Validator for localhost and not registered dopmains
*/
public class DefaultUAMCasValidator implements CasValidator {
private static String CAS_URL = "https://cas.amu.edu.pl/cas";
private final CloseableHttpClient httpClient = HttpClients.createDefault();
private String service;
private String ticket;
/**
* craste new instance
*
* @param service string with url of our service from where request began
* @param ticket ticket returned after user provided creeds on cas page
*/
public DefaultUAMCasValidator(String service, String ticket) {
this.service = service;
this.ticket = ticket;
@ -39,7 +48,7 @@ public class DefaultUAMCasValidator implements CasValidator {
}
String res = result.substring(result.indexOf('\n') + 1);
return new CasUserIdentity(null,res);
return new CasUserIdentity(null, res);
}
}

View File

@ -14,6 +14,21 @@
"name": "plannaplan.email",
"type": "java.lang.String",
"description": "Email from which app sends message"
},
{
"name": "plannaplan.apiurl",
"type": "java.lang.String",
"description": "Url to usos api endpoints"
},
{
"name": "plannaplan.apikey",
"type": "java.lang.String",
"description": "Api consumer key"
},
{
"name": "plannaplan.apisecret",
"type": "java.lang.String",
"description": "Api consumer secret"
}
]
}