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; import com.plannaplan.services.ConfiguratorService;
/**
* Root class of Application.
*/
@SpringBootApplication @SpringBootApplication
@EnableScheduling @EnableScheduling
public class App { public class App {

View File

@ -3,6 +3,9 @@ package com.plannaplan;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
/**
* Class to generate logo string on start application and make logs info
*/
public class Logo { public class Logo {
public static final String ANSI_RESET = "\u001B[0m"; 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){ public static String getInitInfo(boolean isDev){
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"); DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
LocalDateTime now = LocalDateTime.now(); 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 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){ public static String getStartedInfo(boolean isDev){
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"); DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
LocalDateTime now = LocalDateTime.now(); 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.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2; import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* Config class of Swagger to generate rest api documentation
*/
@Configuration @Configuration
@EnableSwagger2 @EnableSwagger2
public class Swagger2Config extends WebMvcConfigurationSupport { public class Swagger2Config extends WebMvcConfigurationSupport {

View File

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

View File

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

View File

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

View File

@ -5,21 +5,36 @@ import java.util.List;
import io.swagger.annotations.ApiModel; 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") @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> lectures = new ArrayList<>();
private List<T> classes = 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.lectures = lectures;
this.classes = classes; this.classes = classes;
} }
/**
* @return realted classes Groups instance
*/
public List<T> getClasses() { public List<T> getClasses() {
return this.classes; return this.classes;
} }
/**
* @return realted lectures Groups instance
*/
public List<T> getLectures() { public List<T> getLectures() {
return this.lectures; return this.lectures;
} }

View File

@ -5,9 +5,21 @@ import com.plannaplan.responses.models.abstracts.CoursesResponse;
import io.swagger.annotations.ApiModel; 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") @ApiModel(description = "Response shows information about course.", value = "CoursesDefaultResponse")
public class CoursesDefaultResponse extends CoursesResponse { public class CoursesDefaultResponse extends CoursesResponse {
/**
* create new instance
*
* @param course course to map to api response
*/
public CoursesDefaultResponse(Course course) { public CoursesDefaultResponse(Course course) {
super(course); super(course);
} }

View File

@ -9,12 +9,24 @@ import com.plannaplan.types.GroupType;
import io.swagger.annotations.ApiModel; 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") @ApiModel(description = "Response shows information about groups to given course.", value = "CoursesWithGroupsResponse")
public class CoursesWithGroupsResponse extends CoursesResponse { public class CoursesWithGroupsResponse extends CoursesResponse {
private List<GroupWithCapacityResponse> lectures = new ArrayList<>(); private List<GroupWithCapacityResponse> lectures = new ArrayList<>();
private List<GroupWithCapacityResponse> classes = new ArrayList<>(); private List<GroupWithCapacityResponse> classes = new ArrayList<>();
/**
* create new instance
*
* @param course course to map to api response
*/
public CoursesWithGroupsResponse(Course course) { public CoursesWithGroupsResponse(Course course) {
super(course); super(course);
course.getGroups().stream().forEach(group -> { 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, public CoursesWithGroupsResponse(Course course, List<GroupWithCapacityResponse> lectures,
List<GroupWithCapacityResponse> classes) { List<GroupWithCapacityResponse> classes) {
super(course); super(course);
@ -33,10 +51,16 @@ public class CoursesWithGroupsResponse extends CoursesResponse {
this.classes = classes; this.classes = classes;
} }
/**
* @return list of api resposnes of classes
*/
public List<GroupWithCapacityResponse> getClasses() { public List<GroupWithCapacityResponse> getClasses() {
return this.classes; return this.classes;
} }
/**
* @return list of api resposnes of lectures
*/
public List<GroupWithCapacityResponse> getLectures() { public List<GroupWithCapacityResponse> getLectures() {
return this.lectures; return this.lectures;
} }

View File

@ -2,35 +2,72 @@ package com.plannaplan.responses.models;
import com.plannaplan.entities.Exchange; 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 { public class ExchangeResponse {
private Long id; @ApiModelProperty(value = "Database id")
private GroupDefaultResponse ownedAssignment; private Long id;
private GroupDefaultResponse desiredGroup; @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){ /**
this.id = exchange.getId(); * creat new instance
this.ownedAssignment = new GroupDefaultResponse(exchange.getOwnedAssignment().getGroup()); *
this.desiredGroup = new GroupDefaultResponse(exchange.getDesiredAssignment()); * @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() { public GroupDefaultResponse getDesiredGroup() {
return desiredGroup; return desiredGroup;
} }
/**
* @param desiredGroup set api respondse of wanted byt user group
*/
public void setDesiredGroup(GroupDefaultResponse desiredGroup) { public void setDesiredGroup(GroupDefaultResponse desiredGroup) {
this.desiredGroup = desiredGroup; this.desiredGroup = desiredGroup;
} }
/**
* @return get api respondse of owned user group
*/
public GroupDefaultResponse getOwnedAssignment() { public GroupDefaultResponse getOwnedAssignment() {
return ownedAssignment; return ownedAssignment;
} }
/**
* @param ownedAssignment set api respondse of owned user group
*/
public void setOwnedAssignment(GroupDefaultResponse ownedAssignment) { public void setOwnedAssignment(GroupDefaultResponse ownedAssignment) {
this.ownedAssignment = ownedAssignment; this.ownedAssignment = ownedAssignment;
} }
/**
* @return database id
*/
public Long getId() { public Long getId() {
return id; return id;
} }
/**
* @param id set database id
*/
public void setId(Long id) { public void setId(Long id) {
this.id = id; this.id = id;
} }
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -17,6 +17,9 @@ import org.springframework.security.web.util.matcher.RequestMatcher;
import static org.springframework.http.HttpHeaders.AUTHORIZATION; import static org.springframework.http.HttpHeaders.AUTHORIZATION;
/**
* Spring authentication filter class
*/
public class AuthenticationFilter extends AbstractAuthenticationProcessingFilter { public class AuthenticationFilter extends AbstractAuthenticationProcessingFilter {
AuthenticationFilter(final RequestMatcher requiresAuth) { 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.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
/**
* Spring authentication provider
*/
@Component @Component
public class AuthenticationProvider extends AbstractUserDetailsAuthenticationProvider { public class AuthenticationProvider extends AbstractUserDetailsAuthenticationProvider {
@ -25,8 +28,6 @@ public class AuthenticationProvider extends AbstractUserDetailsAuthenticationPro
@Override @Override
protected void additionalAuthenticationChecks(UserDetails userDetails, protected void additionalAuthenticationChecks(UserDetails userDetails,
UsernamePasswordAuthenticationToken authentication) throws AuthenticationException { UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {
// is being done in other task
} }
@Override @Override

View File

@ -6,6 +6,9 @@ import com.plannaplan.types.UserRoles;
import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.GrantedAuthority;
/**
* Users Roles for spring app
*/
public enum AuthorityRoles implements GrantedAuthority { public enum AuthorityRoles implements GrantedAuthority {
STUDENT("ROLE_STUDENT"), DEANERY("ROLE_DEANERY"), ADMIN("ROLE_ADMIN"), TEST_USER("ROLE_TESTUSER"); 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; 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) { public static final Optional<AuthorityRoles> getAuthorityRole(UserRoles role) {
switch (role) { switch (role) {
case ADMIN: 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.AnonymousAuthenticationFilter;
import org.springframework.security.web.authentication.HttpStatusEntryPoint; import org.springframework.security.web.authentication.HttpStatusEntryPoint;
/**
* Spring config class for security
*/
@Configuration @Configuration
@EnableWebSecurity @EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
private static final RequestMatcher PROTECTED_URLS = new OrRequestMatcher(new AntPathRequestMatcher("/api/**")); private static final RequestMatcher PROTECTED_URLS = new OrRequestMatcher(new AntPathRequestMatcher("/api/**"));

View File

@ -1,19 +1,36 @@
package com.plannaplan.security.cas; 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 { public class CasUserIdentity {
private String usosId; private String usosId;
private String email; 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.usosId = usosId;
this.email = email; this.email = email;
} }
/**
* @return string with usosid
*/
public String getUsosId() { public String getUsosId() {
return usosId; return usosId;
} }
/**
* @return string with email
*/
public String getEmail() { public String getEmail() {
return email; return email;
} }
} }

View File

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

View File

@ -1,6 +1,17 @@
package com.plannaplan.security.cas; 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 { 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(); 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.Cas20ServiceTicketValidator;
import org.jasig.cas.client.validation.TicketValidationException; 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 { public class CustomUAMCasValidator implements CasValidator {
private static String CAS_URL = "https://cas.amu.edu.pl/cas"; private static String CAS_URL = "https://cas.amu.edu.pl/cas";
private static String EMAIL_FIELD = "mail"; private static String EMAIL_FIELD = "mail";
@ -11,31 +15,36 @@ public class CustomUAMCasValidator implements CasValidator {
private String service; private String service;
private String ticket; 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.service = service;
this.ticket = ticket; this.ticket = ticket;
} }
@Override @Override
public CasUserIdentity validate() { public CasUserIdentity validate() {
/*
* TO DO
* Dodać case z CAS10/CAS20/CAS30
*/
final Cas20ServiceTicketValidator validator = new Cas20ServiceTicketValidator(CustomUAMCasValidator.CAS_URL); final Cas20ServiceTicketValidator validator = new Cas20ServiceTicketValidator(CustomUAMCasValidator.CAS_URL);
try { try {
final Assertion assertion = validator.validate(this.ticket, this.service); final Assertion assertion = validator.validate(this.ticket, this.service);
if (assertion == null) { 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();
return new CasUserIdentity(usosid,mail); final String mail = assertion.getPrincipal().getAttributes().get(CustomUAMCasValidator.EMAIL_FIELD)
.toString();
return new CasUserIdentity(usosid, mail);
} catch (TicketValidationException e) { } catch (TicketValidationException e) {
e.printStackTrace(); 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.impl.client.HttpClients;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
/**
* Cas Validator for localhost and not registered dopmains
*/
public class DefaultUAMCasValidator implements CasValidator { public class DefaultUAMCasValidator implements CasValidator {
private static String CAS_URL = "https://cas.amu.edu.pl/cas"; private static String CAS_URL = "https://cas.amu.edu.pl/cas";
private final CloseableHttpClient httpClient = HttpClients.createDefault(); private final CloseableHttpClient httpClient = HttpClients.createDefault();
private String service; private String service;
private String ticket; 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) { public DefaultUAMCasValidator(String service, String ticket) {
this.service = service; this.service = service;
this.ticket = ticket; this.ticket = ticket;
@ -39,13 +48,13 @@ public class DefaultUAMCasValidator implements CasValidator {
} }
String res = result.substring(result.indexOf('\n') + 1); String res = result.substring(result.indexOf('\n') + 1);
return new CasUserIdentity(null,res); return new CasUserIdentity(null, res);
} }
} }
catch (Exception e) { catch (Exception e) {
throw new CasValidationExcepiton("Cas Validation has failed."); throw new CasValidationExcepiton("Cas Validation has failed.");
} }
} }
} }

View File

@ -14,6 +14,21 @@
"name": "plannaplan.email", "name": "plannaplan.email",
"type": "java.lang.String", "type": "java.lang.String",
"description": "Email from which app sends message" "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"
} }
] ]
} }