Skip to content

Commit 533b5b5

Browse files
committed
Refactor
1 parent e2fb3fb commit 533b5b5

29 files changed

Lines changed: 346 additions & 186 deletions

src/main/java/dev/plex/HTTPDModule.java

Lines changed: 76 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import dev.plex.assets.MinecraftAssetsManager;
44
import dev.plex.authentication.AuthenticationManager;
55
import dev.plex.cache.FileCache;
6-
import dev.plex.api.PlexApi;
76
import dev.plex.config.ModuleConfig;
87
import dev.plex.logging.Log;
98
import dev.plex.module.PlexModule;
@@ -33,35 +32,41 @@
3332

3433
public class HTTPDModule extends PlexModule
3534
{
36-
public static ServletContextHandler context;
35+
@Getter
36+
private ServletContextHandler context;
3737
private Thread serverThread;
38-
private AtomicReference<Server> atomicServer = new AtomicReference<>();
38+
private final AtomicReference<Server> atomicServer = new AtomicReference<>();
3939

40-
public static ModuleConfig moduleConfig;
41-
private static PlexApi plexApi;
40+
@Getter
41+
private ModuleConfig moduleConfig;
4242

43-
public static PlexApi plexApi()
44-
{
45-
return plexApi;
46-
}
43+
@Getter
44+
private final FileCache fileCache = new FileCache();
45+
46+
@Getter
47+
private final String template = AbstractServlet.readFileReal(HTTPDModule.class.getResourceAsStream("/httpd/template.html"));
48+
49+
@Getter
50+
private AuthenticationManager authenticationManager;
4751

48-
public static final FileCache fileCache = new FileCache();
52+
@Getter
53+
private File accessLogFile;
4954

50-
public static final String template = AbstractServlet.readFileReal(HTTPDModule.class.getResourceAsStream("/httpd/template.html"));
55+
@Getter
56+
private MinecraftAssetsManager minecraftAssetsManager;
5157

5258
@Getter
53-
private static AuthenticationManager authenticationManager;
59+
private StatsBroadcaster statsBroadcaster;
5460

5561
@Getter
56-
private static File accessLogFile;
62+
private PlayersBroadcaster playersBroadcaster;
5763

5864
@Getter
59-
private static MinecraftAssetsManager minecraftAssetsManager;
65+
private PlayerInventoryBroadcaster playerInventoryBroadcaster;
6066

6167
@Override
6268
public void load()
6369
{
64-
plexApi = api();
6570
// Move it from /httpd/config.yml to /plugins/Plex/modules/Plex-HTTPD/config.yml
6671
moduleConfig = new ModuleConfig(this, "httpd/config.yml", "config.yml");
6772
}
@@ -70,17 +75,18 @@ public void load()
7075
public void enable()
7176
{
7277
moduleConfig.load();
73-
HTTPDModule.plexApi().logging().debug("HTTPD Module Port: {0}", moduleConfig.getInt("server.port"));
78+
api().logging().debug("HTTPD Module Port: {0}", moduleConfig.getInt("server.port"));
7479

7580
accessLogFile = new File(getDataFolder(), moduleConfig.getString("server.logging.file-path", "httpd.log"));
81+
Log.configure(moduleConfig, accessLogFile);
7682

77-
minecraftAssetsManager = new MinecraftAssetsManager(getDataFolder().toPath());
83+
minecraftAssetsManager = new MinecraftAssetsManager(getDataFolder().toPath(), api());
7884
minecraftAssetsManager.refreshAsync();
7985

80-
authenticationManager = new AuthenticationManager();
86+
authenticationManager = new AuthenticationManager(this);
8187
if (authenticationManager.provider() == null)
8288
{
83-
HTTPDModule.plexApi().logging().debug("Authentication is disabled or misconfigured");
89+
api().logging().debug("Authentication is disabled or misconfigured");
8490
}
8591

8692

@@ -110,33 +116,37 @@ public void enable()
110116
connector.setIdleTimeout(moduleConfig.getLong("server.limits.idle-timeout-ms", 15_000L));
111117
connector.setAcceptQueueSize(moduleConfig.getInt("server.limits.accept-queue", 32));
112118

113-
context.addFilter(new FilterHolder(new RateLimitFilter()), "/*", EnumSet.of(DispatcherType.REQUEST));
114-
115-
StatsBroadcaster.get().start();
116-
PlayersBroadcaster.get().start();
117-
PlayerInventoryBroadcaster.get().start();
118-
119-
new IndefBansEndpoint();
120-
new IndexEndpoint();
121-
new ListEndpoint();
122-
new PunishmentsEndpoint();
123-
new CommandsEndpoint();
124-
new SchematicDownloadEndpoint();
125-
new SchematicUploadEndpoint();
126-
new PlayersEndpoint();
127-
new PlayerAdminEndpoint();
128-
new AssetsEndpoint();
129-
new PunishmentsUIEndpoint();
130-
new IndefBansUIEndpoint();
131-
new AuthenticationEndpoint();
132-
133-
HTTPDModule.context.addServlet(StatsStreamServlet.class, "/api/stats/stream");
134-
HTTPDModule.context.addServlet(PlayersStreamServlet.class, "/api/players/stream");
135-
HTTPDModule.context.addServlet(StaffPlayersStreamServlet.class, "/api/players/stream/staff");
136-
HTTPDModule.context.addServlet(PlayerActionServlet.class, "/api/admin/action");
137-
HTTPDModule.context.addServlet(PlayerInventoryStreamServlet.class, "/api/player/inventory/stream");
138-
139-
ServletHolder uploadHolder = HTTPDModule.context.addServlet(SchematicUploadServlet.class, "/api/schematics/uploading");
119+
context.addFilter(new FilterHolder(new RateLimitFilter(moduleConfig)), "/*", EnumSet.of(DispatcherType.REQUEST));
120+
121+
statsBroadcaster = new StatsBroadcaster(this);
122+
playersBroadcaster = new PlayersBroadcaster(this);
123+
playerInventoryBroadcaster = new PlayerInventoryBroadcaster(this);
124+
statsBroadcaster.start();
125+
playersBroadcaster.start();
126+
playerInventoryBroadcaster.start();
127+
128+
new IndefBansEndpoint(this);
129+
new IndexEndpoint(this);
130+
new ListEndpoint(this);
131+
new PunishmentsEndpoint(this);
132+
new CommandsEndpoint(this);
133+
new SchematicDownloadEndpoint(this);
134+
new SchematicUploadEndpoint(this);
135+
new PlayersEndpoint(this);
136+
new PlayerAdminEndpoint(this);
137+
new AssetsEndpoint(this);
138+
new PunishmentsUIEndpoint(this);
139+
new IndefBansUIEndpoint(this);
140+
new AuthenticationEndpoint(this);
141+
142+
context.addServlet(new ServletHolder(new StatsStreamServlet(statsBroadcaster)), "/api/stats/stream");
143+
context.addServlet(new ServletHolder(new PlayersStreamServlet(playersBroadcaster)), "/api/players/stream");
144+
context.addServlet(new ServletHolder(new StaffPlayersStreamServlet(this, playersBroadcaster)), "/api/players/stream/staff");
145+
context.addServlet(new ServletHolder(new PlayerActionServlet(this)), "/api/admin/action");
146+
context.addServlet(new ServletHolder(new PlayerInventoryStreamServlet(this, playerInventoryBroadcaster)), "/api/player/inventory/stream");
147+
148+
ServletHolder uploadHolder = new ServletHolder(new SchematicUploadServlet(this));
149+
context.addServlet(uploadHolder, "/api/schematics/uploading");
140150

141151
File uploadLoc = new File(System.getProperty("java.io.tmpdir"), "schematic-temp-dir");
142152
if (!uploadLoc.exists())
@@ -160,41 +170,54 @@ public void enable()
160170
}
161171
}, "Jetty-Server");
162172
serverThread.start();
163-
HTTPDModule.plexApi().logging().info("Starting Jetty server on port " + moduleConfig.getInt("server.port"));
173+
api().logging().info("Starting Jetty server on port " + moduleConfig.getInt("server.port"));
164174
}
165175

166176
@Override
167177
public void disable()
168178
{
169-
HTTPDModule.plexApi().logging().debug("Stopping Jetty server");
179+
api().logging().debug("Stopping Jetty server");
170180
try
171181
{
172-
StatsBroadcaster.get().shutdown();
182+
if (statsBroadcaster != null)
183+
{
184+
statsBroadcaster.shutdown();
185+
}
173186
}
174187
catch (Throwable t)
175188
{
176189
t.printStackTrace();
177190
}
178191
try
179192
{
180-
PlayersBroadcaster.get().shutdown();
193+
if (playersBroadcaster != null)
194+
{
195+
playersBroadcaster.shutdown();
196+
}
181197
}
182198
catch (Throwable t)
183199
{
184200
t.printStackTrace();
185201
}
186202
try
187203
{
188-
PlayerInventoryBroadcaster.get().shutdown();
204+
if (playerInventoryBroadcaster != null)
205+
{
206+
playerInventoryBroadcaster.shutdown();
207+
}
189208
}
190209
catch (Throwable t)
191210
{
192211
t.printStackTrace();
193212
}
194213
try
195214
{
196-
atomicServer.get().stop();
197-
atomicServer.get().destroy();
215+
Server server = atomicServer.get();
216+
if (server != null)
217+
{
218+
server.stop();
219+
server.destroy();
220+
}
198221
}
199222
catch (Exception e)
200223
{

src/main/java/dev/plex/assets/MinecraftAssetsManager.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package dev.plex.assets;
22

3-
import dev.plex.HTTPDModule;
3+
import dev.plex.api.PlexApi;
44
import org.bukkit.Bukkit;
55
import org.json.JSONArray;
66
import org.json.JSONObject;
@@ -35,12 +35,14 @@ public class MinecraftAssetsManager
3535
private final AtomicBoolean ready = new AtomicBoolean(false);
3636
private final AtomicBoolean refreshStarted = new AtomicBoolean(false);
3737
private final String minecraftVersion;
38+
private final PlexApi api;
3839

39-
public MinecraftAssetsManager(Path dataFolder)
40+
public MinecraftAssetsManager(Path dataFolder, PlexApi api)
4041
{
4142
this.root = dataFolder.resolve("minecraft-assets");
4243
this.versionFile = root.resolve("version.txt");
4344
this.minecraftVersion = detectMinecraftVersion();
45+
this.api = api;
4446
this.client = HttpClient.newBuilder()
4547
.followRedirects(HttpClient.Redirect.NORMAL)
4648
.connectTimeout(Duration.ofSeconds(20))
@@ -63,7 +65,7 @@ public void refreshAsync()
6365
}
6466
catch (Exception e)
6567
{
66-
HTTPDModule.plexApi().logging().info("Unable to download Minecraft assets for HTTPD inventory view: " + e.getMessage());
68+
api.logging().info("Unable to download Minecraft assets for HTTPD inventory view: " + e.getMessage());
6769
e.printStackTrace();
6870
}
6971
});
@@ -90,13 +92,13 @@ private void refreshIfNeeded() throws IOException, InterruptedException
9092
String cachedVersion = Files.exists(versionFile) ? Files.readString(versionFile).trim() : "";
9193
if (minecraftVersion.equals(cachedVersion) && hasAssets())
9294
{
93-
HTTPDModule.plexApi().logging().debug("HTTPD Minecraft assets are already cached for {0}", minecraftVersion);
95+
api.logging().debug("HTTPD Minecraft assets are already cached for {0}", minecraftVersion);
9496
return;
9597
}
9698

9799
if (!cachedVersion.isEmpty() && !minecraftVersion.equals(cachedVersion))
98100
{
99-
HTTPDModule.plexApi().logging().info("Minecraft version changed from " + cachedVersion + " to " + minecraftVersion + "; recreating HTTPD asset cache");
101+
api.logging().info("Minecraft version changed from " + cachedVersion + " to " + minecraftVersion + "; recreating HTTPD asset cache");
100102
}
101103
recreateCache();
102104
}
@@ -114,7 +116,7 @@ private void recreateCache() throws IOException, InterruptedException
114116
deleteDirectory(root);
115117
Files.createDirectories(root);
116118

117-
HTTPDModule.plexApi().logging().info("Downloading Minecraft " + minecraftVersion + " client assets for HTTPD inventory view");
119+
api.logging().info("Downloading Minecraft " + minecraftVersion + " client assets for HTTPD inventory view");
118120
JSONObject version = findVersionJson();
119121
String clientUrl = version.getJSONObject("downloads").getJSONObject("client").getString("url");
120122

@@ -142,7 +144,7 @@ private void recreateCache() throws IOException, InterruptedException
142144
}
143145

144146
Files.writeString(versionFile, minecraftVersion + System.lineSeparator());
145-
HTTPDModule.plexApi().logging().info("HTTPD Minecraft assets cached for " + minecraftVersion);
147+
api.logging().info("HTTPD Minecraft assets cached for " + minecraftVersion);
146148
}
147149

148150
private JSONObject findVersionJson() throws IOException, InterruptedException

src/main/java/dev/plex/authentication/AuthenticationManager.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ public class AuthenticationManager
77
{
88
private final OAuth2Provider provider;
99

10-
public AuthenticationManager()
10+
public AuthenticationManager(HTTPDModule module)
1111
{
12-
final boolean enabled = HTTPDModule.moduleConfig.getBoolean("authentication.enabled", false);
12+
final boolean enabled = module.getModuleConfig().getBoolean("authentication.enabled", false);
1313
if (!enabled)
1414
{
1515
provider = null;
1616
return;
1717
}
1818

19-
HTTPDModule.plexApi().logging().info("[HTTPD] XenForo OAuth2 authentication is enabled");
20-
provider = new XenForoOAuth2Provider();
19+
module.api().logging().info("[HTTPD] XenForo OAuth2 authentication is enabled");
20+
provider = new XenForoOAuth2Provider(module);
2121
}
2222

2323
public OAuth2Provider provider()

src/main/java/dev/plex/authentication/impl/XenForoOAuth2Provider.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,21 @@ public class XenForoOAuth2Provider implements OAuth2Provider
4949
private final String clientSecret;
5050
private final String redirectUri;
5151
private final Duration sessionTtl;
52+
private final HTTPDModule module;
5253

53-
public XenForoOAuth2Provider()
54+
public XenForoOAuth2Provider(HTTPDModule module)
5455
{
55-
String domain = HTTPDModule.moduleConfig.getString("authentication.provider.xenforo.domain", "");
56-
this.clientId = HTTPDModule.moduleConfig.getString("authentication.provider.xenforo.clientId", "");
57-
this.clientSecret = HTTPDModule.moduleConfig.getString("authentication.provider.xenforo.clientSecret", "");
58-
this.redirectUri = HTTPDModule.moduleConfig.getString("authentication.provider.redirectUri", "");
59-
long ttlMinutes = HTTPDModule.moduleConfig.getLong("authentication.provider.xenforo.sessionMinutes", 1440L);
56+
this.module = module;
57+
String domain = module.getModuleConfig().getString("authentication.provider.xenforo.domain", "");
58+
this.clientId = module.getModuleConfig().getString("authentication.provider.xenforo.clientId", "");
59+
this.clientSecret = module.getModuleConfig().getString("authentication.provider.xenforo.clientSecret", "");
60+
this.redirectUri = module.getModuleConfig().getString("authentication.provider.redirectUri", "");
61+
long ttlMinutes = module.getModuleConfig().getLong("authentication.provider.xenforo.sessionMinutes", 1440L);
6062
this.sessionTtl = Duration.ofMinutes(Math.max(ttlMinutes, 1L));
6163

6264
if (domain.isEmpty() || clientId.isEmpty() || clientSecret.isEmpty() || redirectUri.isEmpty())
6365
{
64-
HTTPDModule.plexApi().logging().error("XenForo OAuth2 misconfigured: domain, clientId, clientSecret, redirectUri are all required.");
66+
module.api().logging().error("XenForo OAuth2 misconfigured: domain, clientId, clientSecret, redirectUri are all required.");
6567
}
6668

6769
String base = "https://" + domain.replaceFirst("^https?://", "").replaceAll("/+$", "");
@@ -285,7 +287,7 @@ private void revokeToken(String accessToken)
285287
}
286288
catch (Exception e)
287289
{
288-
HTTPDModule.plexApi().logging().debug("Failed to revoke XenForo token: {0}", e.getMessage());
290+
module.api().logging().debug("Failed to revoke XenForo token: {0}", e.getMessage());
289291
}
290292
}
291293

0 commit comments

Comments
 (0)