Skip to content

Commit aed66e9

Browse files
committed
Fix some sonar issues
1 parent 097eab8 commit aed66e9

20 files changed

Lines changed: 204 additions & 330 deletions

Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/auth/ActiveCookieStore.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
@Singleton
4242
public class ActiveCookieStore implements SubSystem {
4343

44+
private static final SecureRandom SECURE_RANDOM = new SecureRandom();
4445
private static final Map<String, CookieMetadata> USERS_BY_COOKIE = new ConcurrentHashMap<>();
4546
private static long cookieExpiresAfterMs = TimeUnit.HOURS.toMillis(2L);
4647

@@ -118,8 +119,7 @@ public Optional<CookieMetadata> findCookie(@Untrusted String cookie) {
118119
}
119120

120121
public String generateNewCookie(User user, String ipAddress) {
121-
SecureRandom secureRandom = new SecureRandom();
122-
String cookie = DigestUtils.sha256Hex(user.getUsername() + UUID.randomUUID() + System.currentTimeMillis() + secureRandom.nextLong());
122+
String cookie = DigestUtils.sha256Hex(user.getUsername() + UUID.randomUUID() + System.currentTimeMillis() + SECURE_RANDOM.nextLong());
123123
long expiresAt = System.currentTimeMillis() + cookieExpiresAfterMs;
124124
USERS_BY_COOKIE.put(cookie, new CookieMetadata(user, expiresAt, ipAddress));
125125
saveNewCookie(user, cookie, System.currentTimeMillis(), ipAddress);

Plan/common/src/main/java/com/djrapitops/plan/processing/processors/move/DatabaseCopyProcessor.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ private void copyUserInfo(LookupTable<Integer> serverIdLookupTable, LookupTable<
291291
IdMapper.mapServerIds(rows, serverIdLookupTable);
292292
toDB.executeInTransaction(LargeStoreQueries.insertUserInfo(rows)).join();
293293
logProgress(rows.size(), UserInfoTable.TABLE_NAME, rows.isEmpty());
294-
return progressTracker.isDone() ? DONE_SIGNAL : rows.get(rows.size() - 1).id;
294+
return progressTracker.isDone() ? DONE_SIGNAL : rows.get(rows.size() - 1).getId();
295295
});
296296
}
297297

@@ -300,13 +300,13 @@ private LookupTable<Integer> copyMissingJoinAddresses() {
300300
LookupTable<String> joinAddressLookupTable = toDB.query(LookupTableQueries.joinAddressLookupTable());
301301
List<JoinAddressTable.Row> rows = fromDB.query(JoinAddressQueries.fetchRows());
302302
List<JoinAddressTable.Row> newRows = rows.stream()
303-
.filter(address -> joinAddressLookupTable.find(address.joinAddress).isEmpty())
303+
.filter(address -> joinAddressLookupTable.find(address.getJoinAddress()).isEmpty())
304304
.collect(Collectors.toList());
305305
toDB.executeInTransaction(LargeStoreQueries.insertJoinAddresses(newRows)).join();
306306

307307
LookupTable<String> oldLookupTable = new LookupTable<>();
308308
for (JoinAddressTable.Row row : rows) {
309-
oldLookupTable.put(row.joinAddress, row.id);
309+
oldLookupTable.put(row.getJoinAddress(), row.getId());
310310
}
311311
if (!rows.isEmpty()) {
312312
logProgress(rows.size(), JoinAddressTable.TABLE_NAME, false);
@@ -328,7 +328,7 @@ private void copyPing(LookupTable<Integer> serverIdLookupTable, LookupTable<Inte
328328
IdMapper.mapServerIds(rows, serverIdLookupTable);
329329
toDB.executeInTransaction(LargeStoreQueries.insertPing(rows)).join();
330330
logProgress(rows.size(), PingTable.TABLE_NAME, rows.isEmpty());
331-
return progressTracker.isDone() ? DONE_SIGNAL : rows.get(rows.size() - 1).id;
331+
return progressTracker.isDone() ? DONE_SIGNAL : rows.get(rows.size() - 1).getId();
332332
});
333333
}
334334

@@ -339,7 +339,7 @@ private void copyTps(LookupTable<Integer> serverIdLookupTable) {
339339
IdMapper.mapServerIds(rows, serverIdLookupTable);
340340
toDB.executeInTransaction(LargeStoreQueries.insertTps(rows)).join();
341341
logProgress(rows.size(), TPSTable.TABLE_NAME, rows.isEmpty());
342-
return progressTracker.isDone() ? DONE_SIGNAL : rows.get(rows.size() - 1).id;
342+
return progressTracker.isDone() ? DONE_SIGNAL : rows.get(rows.size() - 1).getId();
343343
});
344344
}
345345

@@ -350,7 +350,7 @@ private void copyPluginVersions(LookupTable<Integer> serverIdLookupTable) {
350350
IdMapper.mapServerIds(rows, serverIdLookupTable);
351351
toDB.executeInTransaction(LargeStoreQueries.insertPluginVersions(rows)).join();
352352
logProgress(rows.size(), PluginVersionTable.TABLE_NAME, rows.isEmpty());
353-
return progressTracker.isDone() ? DONE_SIGNAL : rows.get(rows.size() - 1).id;
353+
return progressTracker.isDone() ? DONE_SIGNAL : rows.get(rows.size() - 1).getId();
354354
});
355355
}
356356

@@ -366,7 +366,7 @@ private void copySessions(LookupTable<Integer> serverIdLookupTable, LookupTable<
366366
IdMapper.mapJoinAddressIds(rows, joinAddressLookupTable);
367367
toDB.executeInTransaction(LargeStoreQueries.insertSessionsWithOldIds(rows)).join();
368368
logProgress(rows.size(), SessionsTable.TABLE_NAME, rows.isEmpty());
369-
return progressTracker.isDone() ? DONE_SIGNAL : rows.get(rows.size() - 1).id;
369+
return progressTracker.isDone() ? DONE_SIGNAL : rows.get(rows.size() - 1).getId();
370370
});
371371

372372
toDB.executeInTransaction(SessionsTable.TemporaryIdLookupTable.INSERT_ALL_STATEMENT).join();
@@ -378,13 +378,13 @@ private LookupTable<Integer> copyWorlds() {
378378
batching(currentId -> {
379379
List<WorldTable.Row> rows = fromDB.query(WorldTimesQueries.fetchWorldRows(currentId, ROW_LIMIT))
380380
.stream().filter(row -> !worldLookupTable.contains(world ->
381-
world.getWorldName().equals(row.name)
382-
&& world.getServerUUID().equals(row.serverUUID)))
381+
world.getWorldName().equals(row.getName())
382+
&& world.getServerUUID().equals(row.getServerUUID())))
383383
.collect(Collectors.toList());
384384
mapServerUUIDs(rows);
385385
toDB.executeInTransaction(LargeStoreQueries.insertWorlds(rows)).join();
386386
logProgress(rows.size(), WorldTable.TABLE_NAME, rows.isEmpty());
387-
return progressTracker.isDone() ? DONE_SIGNAL : rows.get(rows.size() - 1).id;
387+
return progressTracker.isDone() ? DONE_SIGNAL : rows.get(rows.size() - 1).getId();
388388
});
389389
return toDB.query(LookupTableQueries.worldLookupTable())
390390
.constructIdToIdLookupTable(fromDB.query(LookupTableQueries.worldLookupTable()));
@@ -403,7 +403,7 @@ private void copyWorldTimes(LookupTable<Integer> serverIdLookupTable, LookupTabl
403403
IdMapper.mapWorldIds(rows, worldIdLookupTable);
404404
toDB.executeTransaction(LargeStoreQueries.insertWorldTimesWithOldSessionIds(rows)).join();
405405
logProgress(rows.size(), WorldTimesTable.TABLE_NAME, rows.isEmpty());
406-
return progressTracker.isDone() ? DONE_SIGNAL : rows.get(rows.size() - 1).id;
406+
return progressTracker.isDone() ? DONE_SIGNAL : rows.get(rows.size() - 1).getId();
407407
});
408408
}
409409

@@ -414,7 +414,7 @@ private void copyKills() {
414414
mapServerUUIDs(rows);
415415
toDB.executeTransaction(LargeStoreQueries.insertKillsWithOldSessionIds(rows)).join();
416416
logProgress(rows.size(), KillsTable.TABLE_NAME, rows.isEmpty());
417-
return progressTracker.isDone() ? DONE_SIGNAL : rows.get(rows.size() - 1).id;
417+
return progressTracker.isDone() ? DONE_SIGNAL : rows.get(rows.size() - 1).getId();
418418
});
419419
}
420420

@@ -424,7 +424,7 @@ private void copyAccessLog() {
424424
List<AccessLogTable.Row> rows = fromDB.query(AccessLogTable.fetchRows(currentId, ROW_LIMIT));
425425
toDB.executeInTransaction(LargeStoreQueries.insertAccessLog(rows)).join();
426426
logProgress(rows.size(), AccessLogTable.TABLE_NAME, rows.isEmpty());
427-
return progressTracker.isDone() ? DONE_SIGNAL : rows.get(rows.size() - 1).id;
427+
return progressTracker.isDone() ? DONE_SIGNAL : rows.get(rows.size() - 1).getId();
428428
});
429429
}
430430

@@ -436,7 +436,7 @@ private void copyGeolocations(LookupTable<Integer> userIdLookupTable) {
436436
IdMapper.mapUserIds(rows, userIdLookupTable);
437437
toDB.executeInTransaction(LargeStoreQueries.upsertGeoInfo(rows, toDB.getType())).join();
438438
logProgress(rows.size(), GeoInfoTable.TABLE_NAME, rows.isEmpty());
439-
return progressTracker.isDone() ? DONE_SIGNAL : rows.get(rows.size() - 1).id;
439+
return progressTracker.isDone() ? DONE_SIGNAL : rows.get(rows.size() - 1).getId();
440440
});
441441
}
442442

@@ -448,7 +448,7 @@ private void copyNicknames() {
448448
mapServerUUIDs(rows);
449449
toDB.executeInTransaction(LargeStoreQueries.upsertNicknames(rows, toDB.getType())).join();
450450
logProgress(rows.size(), NicknamesTable.TABLE_NAME, rows.isEmpty());
451-
return progressTracker.isDone() ? DONE_SIGNAL : rows.get(rows.size() - 1).id;
451+
return progressTracker.isDone() ? DONE_SIGNAL : rows.get(rows.size() - 1).getId();
452452
});
453453
}
454454

@@ -460,7 +460,7 @@ private void copyAllowlistBounces(LookupTable<Integer> serverIdLookupTable) {
460460
IdMapper.mapServerIds(rows, serverIdLookupTable);
461461
toDB.executeInTransaction(LargeStoreQueries.upsertAllowlistBounces(rows, toDB.getType())).join();
462462
logProgress(rows.size(), AllowlistBounceTable.TABLE_NAME, rows.isEmpty());
463-
return progressTracker.isDone() ? DONE_SIGNAL : rows.get(rows.size() - 1).id;
463+
return progressTracker.isDone() ? DONE_SIGNAL : rows.get(rows.size() - 1).getId();
464464
});
465465
}
466466

@@ -491,10 +491,10 @@ private LookupTable<Integer> copyWebUsers(LookupTable<Integer> webGroupLookupTab
491491
batching(currentId -> {
492492
List<SecurityTable.Row> rows = fromDB.query(WebUserQueries.fetchRows(currentId, ROW_LIMIT));
493493
IdMapper.mapGroupIds(rows, webGroupLookupTable);
494-
rows.removeIf(row -> lookupTable.contains(row.username));
494+
rows.removeIf(row -> lookupTable.contains(row.getUsername()));
495495
toDB.executeInTransaction(LargeStoreQueries.storeUsers(rows)).join();
496496
logProgress(tableCounts.get(SessionsTable.TABLE_NAME), SecurityTable.TABLE_NAME, false);
497-
return progressTracker.isDone() ? DONE_SIGNAL : rows.get(rows.size() - 1).id;
497+
return progressTracker.isDone() ? DONE_SIGNAL : rows.get(rows.size() - 1).getId();
498498
});
499499
return toDB.query(LookupTableQueries.webUserLookupTable())
500500
.constructIdToIdLookupTable(fromDB.query(LookupTableQueries.webUserLookupTable()));
@@ -507,10 +507,10 @@ private void copyUserPreferences(LookupTable<Integer> webUserIdLookupTable) {
507507
List<WebUserPreferencesTable.Row> rows = fromDB.query(WebUserPreferencesTable.fetchRows(currentId, ROW_LIMIT));
508508
int toCopy = rows.size();
509509
IdMapper.mapUserIds(rows, webUserIdLookupTable);
510-
rows.removeIf(row -> existingIds.contains(row.webUserId)); // Don't override
510+
rows.removeIf(row -> existingIds.contains(row.getUserId())); // Don't override
511511
toDB.executeInTransaction(LargeStoreQueries.insertPreferences(rows)).join();
512512
logProgress(toCopy, WebUserPreferencesTable.TABLE_NAME, toCopy != 0);
513-
return progressTracker.isDone() ? DONE_SIGNAL : rows.get(rows.size() - 1).id;
513+
return progressTracker.isDone() ? DONE_SIGNAL : rows.get(rows.size() - 1).getId();
514514
});
515515
}
516516

Plan/common/src/main/java/com/djrapitops/plan/storage/database/sql/building/Insert.java

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,10 @@
1616
*/
1717
package com.djrapitops.plan.storage.database.sql.building;
1818

19-
import com.djrapitops.plan.identification.ServerUUID;
20-
import com.djrapitops.plan.storage.database.DBType;
21-
22-
import java.util.Objects;
23-
import java.util.UUID;
24-
2519
import static com.djrapitops.plan.storage.database.sql.building.Sql.INSERT_INTO;
2620

2721
public class Insert extends SqlBuilder {
2822

29-
private String[] columns;
30-
private int rowCount = 0;
31-
3223
public Insert(String table) {
3324
super(INSERT_INTO + table + ' ');
3425
}
@@ -54,76 +45,6 @@ public static String values(String table, String... columns) {
5445
return builder.toString();
5546
}
5647

57-
@Deprecated
58-
public static Insert into(String table, String... columns) {
59-
Insert builder = new Insert(table);
60-
builder.append('(');
61-
builder.columns = columns;
62-
int size = columns.length;
63-
for (int i = 0; i < size; i++) {
64-
if (size > 1 && i > 0) {
65-
builder.append(',');
66-
}
67-
builder.append(columns[i]);
68-
}
69-
builder.append(") ");
70-
return builder;
71-
}
72-
73-
private static String valueAsString(Object value) {
74-
String asString = Objects.toString(value);
75-
return value instanceof String || value instanceof UUID || value instanceof ServerUUID ? "'" + asString + "'" : asString;
76-
}
77-
78-
/**
79-
* Appends values directly into the sql statement.
80-
* <p>
81-
* Should be used with care to avoid SQL injection.
82-
*
83-
* @param values Values to insert
84-
* @return This builder.
85-
*/
86-
@Deprecated
87-
public Insert appendRow(DBType dbType, Object... values) {
88-
int size = values.length;
89-
if (dbType == DBType.MYSQL) {
90-
if (rowCount > 0) {
91-
append(',');
92-
} else {
93-
append("VALUES ");
94-
}
95-
append('(');
96-
for (int i = 0; i < size; i++) {
97-
if (size > 1 && i > 0) {
98-
append(',');
99-
}
100-
append(valueAsString(values[i]));
101-
}
102-
append(')');
103-
} else {
104-
if (rowCount == 0) {
105-
append("SELECT ");
106-
for (int i = 0; i < size; i++) {
107-
if (size > 1 && i > 0) {
108-
append(',');
109-
}
110-
append(valueAsString(values[i]));
111-
append(" AS " + columns[i]);
112-
}
113-
} else {
114-
append(" UNION ALL SELECT ");
115-
for (int i = 0; i < size; i++) {
116-
if (size > 1 && i > 0) {
117-
append(',');
118-
}
119-
append(valueAsString(values[i]));
120-
}
121-
}
122-
}
123-
rowCount++;
124-
return this;
125-
}
126-
12748
public String build() {
12849
return toString();
12950
}

Plan/common/src/main/java/com/djrapitops/plan/storage/database/sql/tables/AccessLogTable.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ public static Query<List<Row>> fetchRows(int currentId, int rowLimit) {
7272
}
7373

7474
public static class Row {
75-
public int id;
76-
public long time;
77-
public String fromIp;
78-
public String requestMethod;
79-
public String requestUri;
80-
public int responseCode;
75+
private int id;
76+
private long time;
77+
private String fromIp;
78+
private String requestMethod;
79+
private String requestUri;
80+
private int responseCode;
8181

8282
public static Row extract(ResultSet set) throws SQLException {
8383
Row row = new Row();
@@ -90,6 +90,10 @@ public static Row extract(ResultSet set) throws SQLException {
9090
return row;
9191
}
9292

93+
public int getId() {
94+
return id;
95+
}
96+
9397
public void insert(PreparedStatement statement) throws SQLException {
9498
statement.setLong(1, time);
9599
statement.setString(2, fromIp);

Plan/common/src/main/java/com/djrapitops/plan/storage/database/sql/tables/AllowlistBounceTable.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,12 @@ public static Query<List<Row>> fetchRows(int currentId, int rowLimit) {
9999
}
100100

101101
public static class Row implements ServerIdentifiable {
102-
public int id;
103-
public String uuid;
104-
public String userName;
105-
public int serverId;
106-
public int times;
107-
public long lastBounce;
102+
private int id;
103+
private String uuid;
104+
private String userName;
105+
private int serverId;
106+
private int times;
107+
private long lastBounce;
108108

109109
public static Row extract(ResultSet set) throws SQLException {
110110
Row row = new Row();
@@ -117,6 +117,10 @@ public static Row extract(ResultSet set) throws SQLException {
117117
return row;
118118
}
119119

120+
public int getId() {
121+
return id;
122+
}
123+
120124
@Override
121125
public int getServerId() {
122126
return serverId;

Plan/common/src/main/java/com/djrapitops/plan/storage/database/sql/tables/GeoInfoTable.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@ public static Query<List<Row>> fetchRows(int currentId, int rowLimit) {
9696
}
9797

9898
public static class Row implements UserIdentifiable {
99-
public int id;
100-
public int userId;
101-
public String geolocation;
102-
public long lastUsed;
99+
private int id;
100+
private int userId;
101+
private String geolocation;
102+
private long lastUsed;
103103

104104
public static Row extract(ResultSet set) throws SQLException {
105105
Row row = new Row();
@@ -110,6 +110,10 @@ public static Row extract(ResultSet set) throws SQLException {
110110
return row;
111111
}
112112

113+
public int getId() {
114+
return id;
115+
}
116+
113117
public void upsert(PreparedStatement statement) throws SQLException {
114118
statement.setInt(1, userId);
115119
statement.setString(2, geolocation);

Plan/common/src/main/java/com/djrapitops/plan/storage/database/sql/tables/JoinAddressTable.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ public static String createTableSQL(DBType dbType) {
4848
}
4949

5050
public static class Row {
51-
public int id;
52-
public String joinAddress;
51+
private int id;
52+
private String joinAddress;
5353

5454
public static Row extract(ResultSet set) throws SQLException {
5555
Row row = new Row();
@@ -58,6 +58,14 @@ public static Row extract(ResultSet set) throws SQLException {
5858
return row;
5959
}
6060

61+
public String getJoinAddress() {
62+
return joinAddress;
63+
}
64+
65+
public int getId() {
66+
return id;
67+
}
68+
6169
public void insert(PreparedStatement statement) throws SQLException {
6270
statement.setString(1, joinAddress);
6371
}

0 commit comments

Comments
 (0)