Skip to content

Commit 71633ad

Browse files
committed
SiteService.logAbout{AbsentPage,FailedAuthentication}(): finally port to JDBC.
Addresed to #120 No functional changes.
1 parent 3ae362f commit 71633ad

File tree

9 files changed

+152
-115
lines changed

9 files changed

+152
-115
lines changed

src/main/java/ru/mystamps/web/Db.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (C) 2009-2016 Slava Semushin <slava.semushin@gmail.com>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*/
18+
package ru.mystamps.web;
19+
20+
public final class Db {
21+
22+
public static final class SuspiciousActivity {
23+
public static final int PAGE_URL_LENGTH = 100;
24+
public static final int REFERER_PAGE_LENGTH = 255;
25+
public static final int USER_AGENT_LENGTH = 255;
26+
}
27+
28+
}

src/main/java/ru/mystamps/web/controller/ErrorController.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import lombok.RequiredArgsConstructor;
3030

3131
import ru.mystamps.web.Url;
32-
import ru.mystamps.web.entity.User;
3332
import ru.mystamps.web.service.SiteService;
3433
import ru.mystamps.web.support.spring.security.CustomUserDetails;
3534

@@ -52,12 +51,12 @@ public void notFound(
5251
String ip = request.getRemoteAddr();
5352
String method = request.getMethod();
5453

55-
User currentUser = null;
54+
Integer userId = null;
5655
if (userDetails != null) {
57-
currentUser = userDetails.getUser();
56+
userId = userDetails.getUser().getId();
5857
}
5958

60-
siteService.logAboutAbsentPage(page, method, currentUser, ip, referer, agent);
59+
siteService.logAboutAbsentPage(page, method, userId, ip, referer, agent);
6160
}
6261

6362
@RequestMapping(Url.INTERNAL_ERROR_PAGE)

src/main/java/ru/mystamps/web/dao/SuspiciousActivityDao.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
*/
1818
package ru.mystamps.web.dao;
1919

20-
import ru.mystamps.web.entity.SuspiciousActivity;
20+
import ru.mystamps.web.dao.dto.AddSuspiciousActivityDbDto;
2121

2222
public interface SuspiciousActivityDao {
23-
void add(SuspiciousActivity activity);
23+
void add(AddSuspiciousActivityDbDto activity);
2424
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (C) 2009-2016 Slava Semushin <slava.semushin@gmail.com>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*/
18+
package ru.mystamps.web.dao.dto;
19+
20+
import java.util.Date;
21+
22+
import lombok.Getter;
23+
import lombok.Setter;
24+
25+
@Getter
26+
@Setter
27+
public class AddSuspiciousActivityDbDto {
28+
private String type;
29+
private Date occurredAt;
30+
private String page;
31+
private String method;
32+
private Integer userId;
33+
private String ip;
34+
private String refererPage;
35+
private String userAgent;
36+
}

src/main/java/ru/mystamps/web/dao/impl/JdbcSuspiciousActivityDao.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import lombok.RequiredArgsConstructor;
2929

3030
import ru.mystamps.web.dao.SuspiciousActivityDao;
31-
import ru.mystamps.web.entity.SuspiciousActivity;
31+
import ru.mystamps.web.dao.dto.AddSuspiciousActivityDbDto;
3232

3333
@RequiredArgsConstructor
3434
public class JdbcSuspiciousActivityDao implements SuspiciousActivityDao {
@@ -39,15 +39,12 @@ public class JdbcSuspiciousActivityDao implements SuspiciousActivityDao {
3939
private String addSuspiciousActivitySql;
4040

4141
@Override
42-
public void add(SuspiciousActivity activity) {
42+
public void add(AddSuspiciousActivityDbDto activity) {
4343
Map<String, Object> params = new HashMap<>();
44-
params.put("type", activity.getType().getName());
44+
params.put("type", activity.getType());
4545
params.put("occurred_at", activity.getOccurredAt());
4646
params.put("page", activity.getPage());
47-
Integer userId = activity.getUser() != null // NOPMD
48-
? activity.getUser().getId()
49-
: null;
50-
params.put("user_id", userId);
47+
params.put("user_id", activity.getUserId());
5148
params.put("ip", activity.getIp());
5249
params.put("method", activity.getMethod());
5350
params.put("referer_page", activity.getRefererPage());

src/main/java/ru/mystamps/web/service/SiteService.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,12 @@
1919

2020
import java.util.Date;
2121

22-
import ru.mystamps.web.entity.User;
23-
2422
public interface SiteService {
2523
@SuppressWarnings("PMD.UseObjectForClearerAPI")
2624
void logAboutAbsentPage(
2725
String page,
2826
String method,
29-
User user,
27+
Integer userId,
3028
String ip,
3129
String referer,
3230
String agent
@@ -35,7 +33,7 @@ void logAboutAbsentPage(
3533
void logAboutFailedAuthentication(
3634
String page,
3735
String method,
38-
User user,
36+
Integer userId,
3937
String ip,
4038
String referer,
4139
String agent,

src/main/java/ru/mystamps/web/service/SiteServiceImpl.java

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@
3030

3131
import lombok.RequiredArgsConstructor;
3232

33+
import ru.mystamps.web.Db;
3334
import ru.mystamps.web.dao.SuspiciousActivityDao;
34-
import ru.mystamps.web.entity.SuspiciousActivity;
35-
import ru.mystamps.web.entity.SuspiciousActivityType;
36-
import ru.mystamps.web.entity.User;
35+
import ru.mystamps.web.dao.dto.AddSuspiciousActivityDbDto;
3736

3837
@RequiredArgsConstructor
3938
public class SiteServiceImpl implements SiteService {
@@ -54,12 +53,12 @@ public class SiteServiceImpl implements SiteService {
5453
public void logAboutAbsentPage(
5554
String page,
5655
String method,
57-
User user,
56+
Integer userId,
5857
String ip,
5958
String referer,
6059
String agent) {
6160

62-
logEvent(PAGE_NOT_FOUND, page, method, user, ip, referer, agent, new Date());
61+
logEvent(PAGE_NOT_FOUND, page, method, userId, ip, referer, agent, new Date());
6362
}
6463

6564
@Override
@@ -68,21 +67,21 @@ public void logAboutAbsentPage(
6867
public void logAboutFailedAuthentication(
6968
String page,
7069
String method,
71-
User user,
70+
Integer userId,
7271
String ip,
7372
String referer,
7473
String agent,
7574
Date date) {
7675

77-
logEvent(AUTHENTICATION_FAILED, page, method, user, ip, referer, agent, date);
76+
logEvent(AUTHENTICATION_FAILED, page, method, userId, ip, referer, agent, date);
7877
}
7978

8079
@SuppressWarnings({"PMD.UseObjectForClearerAPI", "checkstyle:parameternumber"})
8180
private void logEvent(
8281
String type,
8382
String page,
8483
String method,
85-
User user,
84+
Integer userId,
8685
String ip,
8786
String referer,
8887
String agent,
@@ -91,19 +90,13 @@ private void logEvent(
9190
Validate.isTrue(type != null, "Type of suspicious activity was not set");
9291
Validate.isTrue(page != null, "Page should be non null");
9392

94-
SuspiciousActivity activity = new SuspiciousActivity();
95-
96-
// TODO: replace entity with DTO and replace SuspiciousActivityType by String
97-
SuspiciousActivityType activityType = new SuspiciousActivityType();
98-
activityType.setName(type);
99-
activity.setType(activityType);
93+
AddSuspiciousActivityDbDto activity = new AddSuspiciousActivityDbDto();
10094

95+
activity.setType(type);
10196
activity.setOccurredAt(date == null ? new Date() : date);
10297
activity.setPage(abbreviatePage(page));
10398
activity.setMethod(method);
104-
105-
activity.setUser(user);
106-
99+
activity.setUserId(userId);
107100
activity.setIp(StringUtils.defaultString(ip));
108101
activity.setRefererPage(StringUtils.stripToNull(abbreviateRefererPage(referer)));
109102
activity.setUserAgent(StringUtils.stripToNull(abbreviateUserAgent(agent)));
@@ -112,17 +105,23 @@ private void logEvent(
112105
}
113106

114107
private static String abbreviatePage(String page) {
115-
return abbreviateIfLengthGreaterThan(page, SuspiciousActivity.PAGE_URL_LENGTH, "page");
108+
return abbreviateIfLengthGreaterThan(page, Db.SuspiciousActivity.PAGE_URL_LENGTH, "page");
116109
}
117110

118111
private static String abbreviateRefererPage(String referer) {
119-
// CheckStyle: ignore LineLength for next 1 lines
120-
return abbreviateIfLengthGreaterThan(referer, SuspiciousActivity.REFERER_PAGE_LENGTH, "referer_page");
112+
return abbreviateIfLengthGreaterThan(
113+
referer,
114+
Db.SuspiciousActivity.REFERER_PAGE_LENGTH,
115+
"referer_page"
116+
);
121117
}
122118

123119
private static String abbreviateUserAgent(String agent) {
124-
// CheckStyle: ignore LineLength for next 1 lines
125-
return abbreviateIfLengthGreaterThan(agent, SuspiciousActivity.USER_AGENT_LENGTH, "user_agent");
120+
return abbreviateIfLengthGreaterThan(
121+
agent,
122+
Db.SuspiciousActivity.USER_AGENT_LENGTH,
123+
"user_agent"
124+
);
126125
}
127126

128127
// CheckStyle: ignore LineLength for next 1 lines

0 commit comments

Comments
 (0)