Commit 975ad8600bf4ea6ad6ce7e9c691d2a9f525d2ad1

Authored by Thomas Ziemer
1 parent 6ea89b5f

feature: SLF4J-Logging hinzugefügt, und ein paar Refactorings

src/main/java/net/ziemers/swxercise/lg/utils/LoggerProducer.java 0 → 100644
  1 +package net.ziemers.swxercise.lg.utils;
  2 +
  3 +import org.slf4j.Logger;
  4 +import org.slf4j.LoggerFactory;
  5 +
  6 +import javax.enterprise.inject.Produces;
  7 +import javax.enterprise.inject.spi.InjectionPoint;
  8 +import javax.inject.Named;
  9 +import javax.inject.Singleton;
  10 +
  11 +/**
  12 + * Dependendy Injection mit dem SLF4J-Logger
  13 + * Quelle: http://www.pavel.cool/jee-tips/Injecting-Logger/
  14 + */
  15 +@Named
  16 +@Singleton
  17 +public class LoggerProducer {
  18 +
  19 + @Produces
  20 + public Logger produceLogger(InjectionPoint injectionPoint) {
  21 + return LoggerFactory.getLogger(injectionPoint.getBean().getBeanClass());
  22 + }
  23 +
  24 +}
0 \ No newline at end of file 25 \ No newline at end of file
src/main/java/net/ziemers/swxercise/ui/RoleViewController.java
@@ -5,6 +5,7 @@ import net.ziemers.swxercise.lg.user.dto.RoleDto; @@ -5,6 +5,7 @@ import net.ziemers.swxercise.lg.user.dto.RoleDto;
5 import net.ziemers.swxercise.lg.user.enums.RightState; 5 import net.ziemers.swxercise.lg.user.enums.RightState;
6 import net.ziemers.swxercise.lg.user.service.RoleService; 6 import net.ziemers.swxercise.lg.user.service.RoleService;
7 import net.ziemers.swxercise.ui.enums.ResponseState; 7 import net.ziemers.swxercise.ui.enums.ResponseState;
  8 +import net.ziemers.swxercise.ui.utils.RestResponse;
8 9
9 import javax.annotation.security.RolesAllowed; 10 import javax.annotation.security.RolesAllowed;
10 import javax.enterprise.context.ApplicationScoped; 11 import javax.enterprise.context.ApplicationScoped;
src/main/java/net/ziemers/swxercise/ui/UserViewController.java
@@ -14,6 +14,8 @@ import net.ziemers.swxercise.lg.model.user.User; @@ -14,6 +14,8 @@ import net.ziemers.swxercise.lg.model.user.User;
14 import net.ziemers.swxercise.lg.user.dto.UserDto; 14 import net.ziemers.swxercise.lg.user.dto.UserDto;
15 import net.ziemers.swxercise.lg.user.service.UserService; 15 import net.ziemers.swxercise.lg.user.service.UserService;
16 import net.ziemers.swxercise.ui.enums.ResponseState; 16 import net.ziemers.swxercise.ui.enums.ResponseState;
  17 +import net.ziemers.swxercise.ui.utils.RestResponse;
  18 +import org.slf4j.Logger;
17 19
18 /** 20 /**
19 * REST-Methoden für die Benutzerverwaltung. 21 * REST-Methoden für die Benutzerverwaltung.
@@ -24,6 +26,13 @@ public class UserViewController { @@ -24,6 +26,13 @@ public class UserViewController {
24 26
25 static final String webContextPath = "/"; 27 static final String webContextPath = "/";
26 28
  29 + /*
  30 + * SLF4J: "Simple Logging Facade for Java"
  31 + * Dependency Injection erfolgt mit dem {@link net.ziemers.swxercise.lg.utils.LoggerProducer}
  32 + */
  33 + @Inject
  34 + private Logger logger;
  35 +
27 @Inject 36 @Inject
28 private UserService userService; 37 private UserService userService;
29 38
@@ -72,7 +81,9 @@ public class UserViewController { @@ -72,7 +81,9 @@ public class UserViewController {
72 @Path("v1/user") 81 @Path("v1/user")
73 @Produces(MediaType.APPLICATION_JSON) 82 @Produces(MediaType.APPLICATION_JSON)
74 @RolesAllowed(RightState.Constants.LOGGED_IN) 83 @RolesAllowed(RightState.Constants.LOGGED_IN)
75 - public User getUser() { return userService.findUser(); } 84 + public User getUser() {
  85 + logger.info("Trying to get user's profile info.");
  86 + return userService.findUser(); }
76 87
77 /** 88 /**
78 * Erstellt ein neues User-Objekt mit den gewünschten Eigenschaften, welche mittels {@link UserDto} definiert werden. 89 * Erstellt ein neues User-Objekt mit den gewünschten Eigenschaften, welche mittels {@link UserDto} definiert werden.
@@ -197,6 +208,7 @@ public class UserViewController { @@ -197,6 +208,7 @@ public class UserViewController {
197 @Produces(MediaType.APPLICATION_JSON) 208 @Produces(MediaType.APPLICATION_JSON)
198 @RolesAllowed(RightState.Constants.NOT_LOGGED_IN) 209 @RolesAllowed(RightState.Constants.NOT_LOGGED_IN)
199 public RestResponse loginUser(UserDto dto) { 210 public RestResponse loginUser(UserDto dto) {
  211 + logger.info("Trying to log-in user '{}'.", dto.getUsername());
200 if (userService.loginUser(dto)) { 212 if (userService.loginUser(dto)) {
201 return new RestResponse(); 213 return new RestResponse();
202 } 214 }
@@ -216,6 +228,7 @@ public class UserViewController { @@ -216,6 +228,7 @@ public class UserViewController {
216 @Produces(MediaType.APPLICATION_JSON) 228 @Produces(MediaType.APPLICATION_JSON)
217 @RolesAllowed(RightState.Constants.LOGGED_IN) 229 @RolesAllowed(RightState.Constants.LOGGED_IN)
218 public RestResponse logoutUser() { 230 public RestResponse logoutUser() {
  231 + logger.info("Trying to log-out user.");
219 if (userService.logoutUser()) { 232 if (userService.logoutUser()) {
220 return new RestResponse(); 233 return new RestResponse();
221 } 234 }
src/main/java/net/ziemers/swxercise/ui/RestApplication.java renamed to src/main/java/net/ziemers/swxercise/ui/utils/RestApplication.java
1 -package net.ziemers.swxercise.ui; 1 +package net.ziemers.swxercise.ui.utils;
2 2
3 import javax.ws.rs.ApplicationPath; 3 import javax.ws.rs.ApplicationPath;
4 import javax.ws.rs.core.Application; 4 import javax.ws.rs.core.Application;
src/main/java/net/ziemers/swxercise/ui/RestResponse.java renamed to src/main/java/net/ziemers/swxercise/ui/utils/RestResponse.java
1 -package net.ziemers.swxercise.ui; 1 +package net.ziemers.swxercise.ui.utils;
2 2
3 import net.ziemers.swxercise.ui.enums.ResponseState; 3 import net.ziemers.swxercise.ui.enums.ResponseState;
4 4
src/main/java/net/ziemers/swxercise/ui/SecurityInterceptor.java renamed to src/main/java/net/ziemers/swxercise/ui/utils/SecurityInterceptor.java
1 -package net.ziemers.swxercise.ui; 1 +package net.ziemers.swxercise.ui.utils;
2 2
3 import javax.annotation.PostConstruct; 3 import javax.annotation.PostConstruct;
4 import javax.annotation.security.DenyAll; 4 import javax.annotation.security.DenyAll;
src/test/java/net/ziemers/swxercise/ui/UserViewControllerTest.java
@@ -9,11 +9,13 @@ import net.ziemers.swxercise.lg.user.dto.UserDto; @@ -9,11 +9,13 @@ import net.ziemers.swxercise.lg.user.dto.UserDto;
9 import net.ziemers.swxercise.lg.user.service.UserService; 9 import net.ziemers.swxercise.lg.user.service.UserService;
10 import net.ziemers.swxercise.ui.enums.ResponseState; 10 import net.ziemers.swxercise.ui.enums.ResponseState;
11 11
  12 +import net.ziemers.swxercise.ui.utils.RestResponse;
12 import org.junit.Test; 13 import org.junit.Test;
13 import org.junit.runner.RunWith; 14 import org.junit.runner.RunWith;
14 import org.mockito.InjectMocks; 15 import org.mockito.InjectMocks;
15 import org.mockito.Mock; 16 import org.mockito.Mock;
16 import org.mockito.junit.MockitoJUnitRunner; 17 import org.mockito.junit.MockitoJUnitRunner;
  18 +import org.slf4j.Logger;
17 19
18 import static org.junit.Assert.*; 20 import static org.junit.Assert.*;
19 import static org.mockito.Mockito.*; 21 import static org.mockito.Mockito.*;
@@ -31,6 +33,10 @@ public class UserViewControllerTest { @@ -31,6 +33,10 @@ public class UserViewControllerTest {
31 @InjectMocks 33 @InjectMocks
32 private UserViewController underTest; 34 private UserViewController underTest;
33 35
  36 + // das Logging interessiert uns nicht; deshalb mocken wir es weg
  37 + @Mock
  38 + private Logger logger;
  39 +
34 /* 40 /*
35 * Mockito allows to configure the return values of its mocks via a fluent API. 41 * Mockito allows to configure the return values of its mocks via a fluent API.
36 * Unspecified method calls return "empty" values 42 * Unspecified method calls return "empty" values