diff --git a/src/main/java/springproject/project/WebMvcConfig.java b/src/main/java/springproject/project/WebMvcConfig.java index f630f93..cac99d7 100644 --- a/src/main/java/springproject/project/WebMvcConfig.java +++ b/src/main/java/springproject/project/WebMvcConfig.java @@ -16,7 +16,7 @@ public class WebMvcConfig implements WebMvcConfigurer { @Bean public BCryptPasswordEncoder passwordEncoder() { - return new BCryptPasswordEncoder(); + return new BCryptPasswordEncoder(); } @Bean diff --git a/src/main/java/springproject/project/WebSecurityConfig.java b/src/main/java/springproject/project/WebSecurityConfig.java index b6e6d8b..9ced27a 100644 --- a/src/main/java/springproject/project/WebSecurityConfig.java +++ b/src/main/java/springproject/project/WebSecurityConfig.java @@ -2,7 +2,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; @@ -12,15 +11,6 @@ import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.web.util.matcher.AntPathRequestMatcher; -import springproject.project.service.FacebookSignUpService; -import springproject.project.service.FacebookSignInAdapterService; - -import org.springframework.social.connect.ConnectionFactoryLocator; -import org.springframework.social.connect.UsersConnectionRepository; -import org.springframework.social.connect.mem.InMemoryUsersConnectionRepository; -import org.springframework.social.connect.web.ProviderSignInController; - - import javax.sql.DataSource; @@ -28,22 +18,6 @@ @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { - /* - TODO - Error cant find out the problem and btw the solution - - @Autowired - private ConnectionFactoryLocator connectionFactoryLocator; - - @Autowired - private UsersConnectionRepository usersConnectionRepository; - - @Autowired - private FacebookSignUpService facebookSignUpService; - */ - - - @Autowired private BCryptPasswordEncoder bCryptPasswordEncoder; @@ -85,21 +59,6 @@ protected void configure(HttpSecurity http) throws Exception { .accessDeniedPage("/access-denied"); } - /* - TODO - Same as before - - @Bean - public ProviderSignInController providerSignInController() { - ((InMemoryUsersConnectionRepository) usersConnectionRepository) - .setConnectionSignUp(facebookSignUpService); - - return new ProviderSignInController( - connectionFactoryLocator, - usersConnectionRepository, - new FacebookSignInAdapterService()); - }*/ - @Override public void configure(WebSecurity web) throws Exception { web diff --git a/src/main/java/springproject/project/controller/BucketController.java b/src/main/java/springproject/project/controller/BucketController.java index 89a0b4d..e344da3 100644 --- a/src/main/java/springproject/project/controller/BucketController.java +++ b/src/main/java/springproject/project/controller/BucketController.java @@ -1,24 +1,23 @@ package springproject.project.controller; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.security.core.Authentication; -import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.view.RedirectView; import springproject.project.model.User; -import springproject.project.model.bundle.ABundle; import springproject.project.service.UserService; @Controller public class BucketController { - @Autowired private UserService userService; @Autowired + public BucketController(UserService userService) { + this.userService = userService; + } @GetMapping(value = "/bucket") public ModelAndView getBucket() { @@ -30,9 +29,7 @@ public ModelAndView getBucket() { @GetMapping(value = "/bucket/{type}") public RedirectView applyBucket(@PathVariable(value = "type") String type) { - Authentication auth = SecurityContextHolder.getContext().getAuthentication(); - User user = userService.findUserByEmail(auth.getName()); - + User user = userService.getLoggedUser(); user.setBundle(type); userService.validate(user); diff --git a/src/main/java/springproject/project/controller/FrontController.java b/src/main/java/springproject/project/controller/FrontController.java index 5191dfe..4e5621e 100644 --- a/src/main/java/springproject/project/controller/FrontController.java +++ b/src/main/java/springproject/project/controller/FrontController.java @@ -12,11 +12,14 @@ @Controller public class FrontController { - @Autowired private ImageService imageService; - @GetMapping(value= {"/", "/home"}) + @Autowired + public FrontController(ImageService imageService) { + this.imageService = imageService; + } + @GetMapping(value = {"/", "/home"}) public ModelAndView homePage() { ModelAndView _new = new ModelAndView(); diff --git a/src/main/java/springproject/project/controller/GalleryController.java b/src/main/java/springproject/project/controller/GalleryController.java index 538170a..107faf9 100644 --- a/src/main/java/springproject/project/controller/GalleryController.java +++ b/src/main/java/springproject/project/controller/GalleryController.java @@ -1,12 +1,10 @@ package springproject.project.controller; -import org.apache.tomcat.util.http.fileupload.IOUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.FileSystemResource; import org.springframework.http.MediaType; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; -import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.*; import org.springframework.stereotype.Controller; import org.springframework.web.multipart.MultipartFile; @@ -21,29 +19,32 @@ import javax.servlet.http.HttpServletResponse; import javax.validation.Valid; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; -import java.io.InputStream; import java.nio.file.Files; @Controller public class GalleryController { - @Autowired private ImageService imageService; - @Autowired private UserService userService; - @Autowired private ImageEditor imageEditor; + @Autowired + public GalleryController(ImageService imageService, + UserService userService, + ImageEditor imageEditor) { + this.imageEditor = imageEditor; + this.imageService = imageService; + this.userService = userService; + } + @GetMapping(value = "/gallery") public ModelAndView getGallery() { ModelAndView _new = new ModelAndView(); - Authentication auth = SecurityContextHolder.getContext().getAuthentication(); - User user = userService.findUserByEmail(auth.getName()); + User user = userService.getLoggedUser(); Image image = new Image(); _new.addObject("image", image); @@ -56,8 +57,7 @@ public ModelAndView getGallery() { @PostMapping(value = "/upload") public RedirectView addPhoto(@Valid Image image, @RequestParam("file") MultipartFile file) { - Authentication auth = SecurityContextHolder.getContext().getAuthentication(); - User user = userService.findUserByEmail(auth.getName()); + User user = userService.getLoggedUser(); if (user.imageCounter() >= user.getBundleObject().numberOfFile()) return new RedirectView("gallery"); @@ -83,8 +83,7 @@ public RedirectView editPhoto(Image image) { @GetMapping(value = "delete/{imgId}") public RedirectView deletePhoto(@PathVariable(value = "imgId") int id) { - Authentication auth = SecurityContextHolder.getContext().getAuthentication(); - User user = userService.findUserByEmail(auth.getName()); + User user = userService.getLoggedUser(); Image image = imageService.getById(id); userService.deleteImage(user, image); @@ -97,21 +96,20 @@ public RedirectView deletePhoto(@PathVariable(value = "imgId") int id) { return new RedirectView("/gallery"); } - @GetMapping(value = "download/{imgId}", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE) + @GetMapping(value = "download/{imgId}", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE) @ResponseBody public FileSystemResource downloadPhoto(@PathVariable(value = "imgId") int id, - HttpServletResponse response) { + HttpServletResponse response) { Image img = imageService.getById(id); - File initialFile = new File(imageService.getImagePath() + img.getUser().getId() + "/" + img.getFilename()); - return new FileSystemResource(initialFile); - } + File initialFile = new File(imageService.getImagePath() + img.getUser().getId() + "/" + img.getFilename()); + return new FileSystemResource(initialFile); + } @GetMapping(value = "details/{imageId}") - public ModelAndView detailsPhoto(@PathVariable(value = "imageId") int id) throws IOException { + public ModelAndView detailsPhoto(@PathVariable(value = "imageId") int id) { ModelAndView _new = new ModelAndView(); - Authentication auth = SecurityContextHolder.getContext().getAuthentication(); - User user = userService.findUserByEmail(auth.getName()); + User user = userService.getLoggedUser(); Image image = imageService.getById(id); _new.addObject("image", image); diff --git a/src/main/java/springproject/project/controller/RestAdminController.java b/src/main/java/springproject/project/controller/RestAdminController.java index e163434..28a7f91 100644 --- a/src/main/java/springproject/project/controller/RestAdminController.java +++ b/src/main/java/springproject/project/controller/RestAdminController.java @@ -4,14 +4,12 @@ import java.io.IOException; import java.nio.file.Files; import java.util.Base64; -import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; -import org.springframework.util.FileCopyUtils; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import springproject.project.model.Image; @@ -20,21 +18,21 @@ import springproject.project.service.UserService; import springproject.project.validator.TokenConstraint; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import javax.validation.Valid; - @org.springframework.web.bind.annotation.RestController @Validated public class RestAdminController { - @Autowired private UserService userService; - @Autowired private ImageService imageService; + @Autowired + public RestAdminController(UserService userService, + ImageService imageService) { + this.userService = userService; + this.imageService = imageService; + } + @RequestMapping("/rest/user") public Object userInfo(@TokenConstraint @RequestHeader("token") String token, @RequestParam(value = "id", defaultValue = "0") int id) { @@ -108,7 +106,6 @@ public Object addImage(@TokenConstraint @RequestHeader("token") String token, @RequestMapping(value = "/rest/bundle", produces = "application/json") public Object getUserBundle(@RequestParam(value = "name", defaultValue = "") String name) { - System.out.println("A request is received with: " + name); if (name.isEmpty()) return null; @@ -120,10 +117,7 @@ public Object getUserBundle(@RequestParam(value = "name", defaultValue = "") Str produces = MediaType.IMAGE_JPEG_VALUE ) public ResponseEntity imageById(@TokenConstraint @RequestHeader("token") String token, - @RequestParam(value = "id", defaultValue = "0") int id, - HttpServletResponse response) throws IOException { - String path = imageService.getPathById(id); - + @RequestParam(value = "id", defaultValue = "0") int id) throws IOException { File serverFile = new File(imageService.getPathById(id)); byte[] bytes = Files.readAllBytes(serverFile.toPath()); @@ -132,12 +126,4 @@ public ResponseEntity imageById(@TokenConstraint @RequestHeader("token") .contentType(MediaType.IMAGE_JPEG) .body(bytes); } - - - public ResponseEntity key(HttpServletRequest request) { - if (request.getHeader("authorization") != "toto") { - return new ResponseEntity(HttpStatus.BAD_REQUEST); - } - return new ResponseEntity(HttpStatus.OK); - } } diff --git a/src/main/java/springproject/project/controller/UserController.java b/src/main/java/springproject/project/controller/UserController.java index 3e2604a..432a05c 100644 --- a/src/main/java/springproject/project/controller/UserController.java +++ b/src/main/java/springproject/project/controller/UserController.java @@ -2,14 +2,11 @@ import javax.validation.Valid; -import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import springproject.project.model.User; import springproject.project.service.UserService; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.security.core.Authentication; -import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; import org.springframework.web.servlet.ModelAndView; @@ -17,9 +14,13 @@ @Controller public class UserController { - @Autowired private UserService userService; + @Autowired + public UserController(UserService userService) { + this.userService = userService; + } + @GetMapping(value = "/login") public ModelAndView login() { ModelAndView modelAndView = new ModelAndView(); @@ -27,8 +28,8 @@ public ModelAndView login() { return modelAndView; } - @GetMapping(value="/registration") - public ModelAndView registration(){ + @GetMapping(value = "/registration") + public ModelAndView registration() { ModelAndView modelAndView = new ModelAndView(); User user = new User(); modelAndView.addObject("user", user); @@ -57,7 +58,7 @@ public ModelAndView createNewUser(@Valid User user, BindingResult bindingResult) return modelAndView; } - @PostMapping(value= "/update") + @PostMapping(value = "/update") public ModelAndView updateUser(@Valid User user, BindingResult bindingResult) { User userExists = userService.findUserByEmail(user.getEmail()); ModelAndView modelAndView = new ModelAndView(); @@ -75,12 +76,11 @@ public ModelAndView updateUser(@Valid User user, BindingResult bindingResult) { return modelAndView; } - @GetMapping(value="/profile") - public ModelAndView profile(Model model) { + @GetMapping(value = "/profile") + public ModelAndView profile() { ModelAndView modelAndView = new ModelAndView(); - Authentication auth = SecurityContextHolder.getContext().getAuthentication(); - User user = userService.findUserByEmail(auth.getName()); + User user = userService.getLoggedUser(); modelAndView.addObject("user", user); modelAndView.setViewName("user/profile"); diff --git a/src/main/java/springproject/project/model/Image.java b/src/main/java/springproject/project/model/Image.java index bc22279..0d511cc 100644 --- a/src/main/java/springproject/project/model/Image.java +++ b/src/main/java/springproject/project/model/Image.java @@ -13,7 +13,7 @@ @Data @Entity @Table(name = "image") -@EqualsAndHashCode(exclude="user") +@EqualsAndHashCode(exclude = "user") public class Image { @Id @@ -44,7 +44,7 @@ public class Image { @JsonIgnore @ManyToOne - @JoinColumn(name="user_id", nullable=false) + @JoinColumn(name = "user_id", nullable = false) private User user; public boolean isIsPublic() { diff --git a/src/main/java/springproject/project/model/User.java b/src/main/java/springproject/project/model/User.java index 441d357..417b144 100644 --- a/src/main/java/springproject/project/model/User.java +++ b/src/main/java/springproject/project/model/User.java @@ -4,7 +4,6 @@ import lombok.Data; import lombok.EqualsAndHashCode; import org.hibernate.validator.constraints.Length; -import springproject.project.model.bundle.ClassicBundle; import springproject.project.model.bundle.ABundle; import javax.persistence.*; @@ -15,7 +14,7 @@ @Data @Entity @Table(name = "\"user\"") -@EqualsAndHashCode(exclude="images") +@EqualsAndHashCode(exclude = "images") public class User { @Id @@ -44,16 +43,16 @@ public class User { @Column(name = "active") private int active; - @Column(name="city") - @NotEmpty(message= "Provide your city please") + @Column(name = "city") + @NotEmpty(message = "Provide your city please") private String city; - @Column(name= "username") - @NotEmpty(message= "Provide an username please") + @Column(name = "username") + @NotEmpty(message = "Provide an username please") private String username; @JsonIgnore - @Column(name= "bundle") + @Column(name = "bundle") private String bundle = "classic"; @JsonIgnore @@ -64,7 +63,7 @@ public class User { @JoinTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id")) private Set roles; - @OneToMany(mappedBy="user", cascade = CascadeType.ALL, orphanRemoval = true) + @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true) private Set images; private void bundleReset() { diff --git a/src/main/java/springproject/project/model/bundle/ABundle.java b/src/main/java/springproject/project/model/bundle/ABundle.java index 7e563f4..8833da7 100644 --- a/src/main/java/springproject/project/model/bundle/ABundle.java +++ b/src/main/java/springproject/project/model/bundle/ABundle.java @@ -15,8 +15,8 @@ public static ABundle create(String bundleName) { return new GoldBundle(); case "platinum": return new PlatinumBundle(); - default: - return null; + default: + return null; } } } diff --git a/src/main/java/springproject/project/model/bundle/ClassicBundle.java b/src/main/java/springproject/project/model/bundle/ClassicBundle.java index 9c8b0ce..c927d42 100644 --- a/src/main/java/springproject/project/model/bundle/ClassicBundle.java +++ b/src/main/java/springproject/project/model/bundle/ClassicBundle.java @@ -1,6 +1,5 @@ package springproject.project.model.bundle; -import javax.persistence.DiscriminatorValue; import java.util.Arrays; import java.util.List; diff --git a/src/main/java/springproject/project/model/bundle/GoldBundle.java b/src/main/java/springproject/project/model/bundle/GoldBundle.java index b7f73ae..94fa5b1 100644 --- a/src/main/java/springproject/project/model/bundle/GoldBundle.java +++ b/src/main/java/springproject/project/model/bundle/GoldBundle.java @@ -1,6 +1,5 @@ package springproject.project.model.bundle; -import javax.persistence.DiscriminatorValue; import java.util.Arrays; import java.util.List; @@ -14,8 +13,6 @@ public String getName() { return "gold"; } - - @Override public boolean asFilterAccess(String filter) { return filters.contains(filter); diff --git a/src/main/java/springproject/project/model/bundle/PlatinumBundle.java b/src/main/java/springproject/project/model/bundle/PlatinumBundle.java index bcc2eac..ef94b6b 100644 --- a/src/main/java/springproject/project/model/bundle/PlatinumBundle.java +++ b/src/main/java/springproject/project/model/bundle/PlatinumBundle.java @@ -1,7 +1,5 @@ package springproject.project.model.bundle; -import javax.persistence.DiscriminatorValue; - public class PlatinumBundle extends ABundle { private int files = 1000000; diff --git a/src/main/java/springproject/project/repository/ImageRepository.java b/src/main/java/springproject/project/repository/ImageRepository.java index 59d671c..f75ce4b 100644 --- a/src/main/java/springproject/project/repository/ImageRepository.java +++ b/src/main/java/springproject/project/repository/ImageRepository.java @@ -15,8 +15,6 @@ public interface ImageRepository extends JpaRepository { Image findById(int id); - Void deleteById(int id); - ArrayList findAllByIsPublic(boolean isPublic); ArrayList findAllByIsPublicAndDescriptionContaining(boolean isPublic, String filter); diff --git a/src/main/java/springproject/project/repository/UserRepository.java b/src/main/java/springproject/project/repository/UserRepository.java index b6efb79..2ae982e 100644 --- a/src/main/java/springproject/project/repository/UserRepository.java +++ b/src/main/java/springproject/project/repository/UserRepository.java @@ -4,7 +4,6 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; -import java.util.ArrayList; import java.util.List; @Repository("userRepository") diff --git a/src/main/java/springproject/project/service/ImageEditor.java b/src/main/java/springproject/project/service/ImageEditor.java index be8e4ef..6ba4b54 100644 --- a/src/main/java/springproject/project/service/ImageEditor.java +++ b/src/main/java/springproject/project/service/ImageEditor.java @@ -1,8 +1,6 @@ package springproject.project.service; -import marvin.gui.MarvinImagePanel; import marvin.image.MarvinImage; -import marvin.image.MarvinImageMask; import marvin.io.MarvinImageIO; import marvin.plugin.MarvinImagePlugin; import marvin.util.MarvinPluginLoader; @@ -11,71 +9,34 @@ @Service("imageEditor") public class ImageEditor { public void apply(String path, String filter, int value) { + MarvinImage image = MarvinImageIO.loadImage(path); + MarvinImagePlugin imagePlugin; + switch (filter) { case "sepia": - sepiaAction(path, value); + imagePlugin = MarvinPluginLoader.loadImagePlugin("org.marvinproject.image.color.sepia.jar"); + imagePlugin.setAttribute("hsIntensidade", value); break; case "blur": - blurAction(path, value); + imagePlugin = MarvinPluginLoader.loadImagePlugin("org.marvinproject.image.blur.gaussianBlur.jar"); break; case "grayscale": - grayscaleAction(path); + imagePlugin = MarvinPluginLoader.loadImagePlugin("org.marvinproject.image.color.grayScale.jar"); break; case "invert": - invertAction(path, value); + imagePlugin = MarvinPluginLoader.loadImagePlugin("org.marvinproject.image.color.invert.jar"); break; case "pixelize": - pixelizeAction(path, value); + imagePlugin = MarvinPluginLoader.loadImagePlugin("org.marvinproject.image.blur.pixelize.jar"); break; + default: + return; } + this.applyPlugin(path, image, imagePlugin); } - private void invertAction(String path, int value) { - MarvinImage image = MarvinImageIO.loadImage(path); - MarvinImagePlugin imagePlugin = MarvinPluginLoader.loadImagePlugin("org.marvinproject.image.color.invert.jar"); - - imagePlugin.process(image, image); - image.update(); - - MarvinImageIO.saveImage(image, path); - } - - private void pixelizeAction(String path, int value) { - MarvinImage image = MarvinImageIO.loadImage(path); - MarvinImagePlugin imagePlugin = MarvinPluginLoader.loadImagePlugin("org.marvinproject.image.blur.pixelize.jar"); - - imagePlugin.process(image, image); - image.update(); - - MarvinImageIO.saveImage(image, path); - } - - private void blurAction(String path, int value) { - MarvinImage image = MarvinImageIO.loadImage(path); - MarvinImagePlugin imagePlugin = MarvinPluginLoader.loadImagePlugin("org.marvinproject.image.blur.gaussianBlur.jar"); - - imagePlugin.process(image, image); - image.update(); - - MarvinImageIO.saveImage(image, path); - } - - private void grayscaleAction(String path) { - MarvinImage image = MarvinImageIO.loadImage(path); - MarvinImagePlugin imagePlugin = MarvinPluginLoader.loadImagePlugin("org.marvinproject.image.color.grayScale.jar"); - - imagePlugin.process(image, image); - image.update(); - - MarvinImageIO.saveImage(image, path); - } - - private void sepiaAction(String path, int value) { - MarvinImage image = MarvinImageIO.loadImage(path); - MarvinImagePlugin imagePlugin = MarvinPluginLoader.loadImagePlugin("org.marvinproject.image.color.sepia.jar"); - - imagePlugin.setAttribute("hsIntensidade", value); - imagePlugin.process(image, image); + private void applyPlugin(String path, MarvinImage image, MarvinImagePlugin plugin) { + plugin.process(image, image); image.update(); MarvinImageIO.saveImage(image, path); diff --git a/src/main/java/springproject/project/service/ImageService.java b/src/main/java/springproject/project/service/ImageService.java index 67fc36a..cd6315a 100644 --- a/src/main/java/springproject/project/service/ImageService.java +++ b/src/main/java/springproject/project/service/ImageService.java @@ -1,6 +1,5 @@ package springproject.project.service; -import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; import org.springframework.util.FileCopyUtils; import org.springframework.web.multipart.MultipartFile; @@ -24,18 +23,12 @@ public class ImageService { private static String IMAGE_PATH = "user-images/"; private ImageRepository imageRepository; - private ResourceLoader resourceLoader; @Autowired - public ImageService(ImageRepository imageRepository, - ResourceLoader resourceLoader) { + public ImageService(ImageRepository imageRepository) { this.imageRepository = imageRepository; - this.resourceLoader = resourceLoader; } - public Image getByUserAndFilename(int id, String name) { - return imageRepository.findByUserIdAndFilename(id, name); - } public Image getById(int id) { return imageRepository.findById(id); @@ -85,7 +78,6 @@ public void createImageFromRest(byte[] file, Image image, String filename) throw destFile.getParentFile().mkdirs(); destFile.createNewFile(); - Path destPath = destFile.toPath(); FileCopyUtils.copy(file, destFile); image.setFilename(filename); imageRepository.save(image); @@ -111,14 +103,12 @@ public Image findById(int id) { } public void removeImageById(int id) { - System.out.println("in delete method"); Image image = imageRepository.findById(id); imageRepository.delete(image); imageRepository.flush(); } - public void updateImage(Image actual, Image newDetails) - { + public void updateImage(Image actual, Image newDetails) { actual.setTitle(newDetails.getTitle()); actual.setIsPublic(newDetails.isIsPublic()); actual.setDescription(newDetails.getDescription()); diff --git a/src/main/java/springproject/project/service/Logger.java b/src/main/java/springproject/project/service/Logger.java index 1a7819c..9877e61 100644 --- a/src/main/java/springproject/project/service/Logger.java +++ b/src/main/java/springproject/project/service/Logger.java @@ -14,16 +14,17 @@ private Logger() { try { FileWriter fw = new FileWriter(logFile); writer = new PrintWriter(fw, true); - } catch (IOException e) {} + } catch (IOException e) { + } } - public static synchronized Logger getInstance(){ - if(logger == null) + public static synchronized Logger getInstance() { + if (logger == null) logger = new Logger(); return logger; } - public void logCreate (String msg) { + public void logCreate(String msg) { writer.println("##############"); writer.println("Creating: " + msg); writer.println("##############"); diff --git a/src/main/java/springproject/project/service/UserService.java b/src/main/java/springproject/project/service/UserService.java index ec92695..514f166 100644 --- a/src/main/java/springproject/project/service/UserService.java +++ b/src/main/java/springproject/project/service/UserService.java @@ -1,5 +1,7 @@ package springproject.project.service; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; import springproject.project.model.Image; import springproject.project.model.Role; import springproject.project.model.User; @@ -46,7 +48,14 @@ public List findByPartialEmail(String email) { return userRepository.findAllByEmailContaining(email); } - public void validate(User user) { userRepository.saveAndFlush(user); } + public void validate(User user) { + userRepository.saveAndFlush(user); + } + + public User getLoggedUser() { + Authentication auth = SecurityContextHolder.getContext().getAuthentication(); + return this.findUserByEmail(auth.getName()); + } public void saveUser(User user) { Logger logger = Logger.getInstance(); @@ -79,7 +88,9 @@ public List findEmailWithPackage(String name) { List users = userRepository.findByBundle(name); List emails = new ArrayList<>(); - for (User user : users) { emails.add(user.getEmail()); } + for (User user : users) { + emails.add(user.getEmail()); + } return emails; } diff --git a/src/main/java/springproject/project/service/aspect/ThrowerAccessAspect.java b/src/main/java/springproject/project/service/aspect/ThrowerAccessAspect.java index 9d43abf..c704565 100644 --- a/src/main/java/springproject/project/service/aspect/ThrowerAccessAspect.java +++ b/src/main/java/springproject/project/service/aspect/ThrowerAccessAspect.java @@ -1,6 +1,5 @@ package springproject.project.service.aspect; -import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component; diff --git a/src/main/java/springproject/project/validator/TokenValidator.java b/src/main/java/springproject/project/validator/TokenValidator.java index 25c908e..8f41709 100644 --- a/src/main/java/springproject/project/validator/TokenValidator.java +++ b/src/main/java/springproject/project/validator/TokenValidator.java @@ -6,7 +6,7 @@ public class TokenValidator implements ConstraintValidator { - String auth = "this-is-token"; + private String auth = "this-is-token"; @Override public void initialize(TokenConstraint token) { diff --git a/src/test/java/springproject/project/PageTest.java b/src/test/java/springproject/project/PageTest.java index 1b7e55a..c6e635b 100644 --- a/src/test/java/springproject/project/PageTest.java +++ b/src/test/java/springproject/project/PageTest.java @@ -1,8 +1,5 @@ package springproject.project; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.contains; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import org.junit.Test; @@ -10,14 +7,10 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.http.MediaType; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; -import springproject.project.service.UserService; -import java.beans.Transient; @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK) diff --git a/src/test/java/springproject/project/browser/BrowserTesting.java b/src/test/java/springproject/project/browser/BrowserTesting.java index f04b9a5..25ab935 100644 --- a/src/test/java/springproject/project/browser/BrowserTesting.java +++ b/src/test/java/springproject/project/browser/BrowserTesting.java @@ -1,7 +1,5 @@ package springproject.project.browser; - -import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; diff --git a/src/test/java/springproject/project/repository/ImageRepositoryTest.java b/src/test/java/springproject/project/repository/ImageRepositoryTest.java index ebc95c1..a96b233 100644 --- a/src/test/java/springproject/project/repository/ImageRepositoryTest.java +++ b/src/test/java/springproject/project/repository/ImageRepositoryTest.java @@ -22,9 +22,6 @@ public class ImageRepositoryTest { @Autowired private TestEntityManager entityManager; - @Autowired - private UserRepository userRepository; - @Autowired private ImageRepository imageRepository; diff --git a/src/test/java/springproject/project/rest/RestTest.java b/src/test/java/springproject/project/rest/RestTest.java index e936be8..c045eec 100644 --- a/src/test/java/springproject/project/rest/RestTest.java +++ b/src/test/java/springproject/project/rest/RestTest.java @@ -9,9 +9,7 @@ import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.context.annotation.Import; import org.springframework.http.HttpHeaders; -import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;