mirror of
https://github.com/PhotonVision/photonvision
synced 2026-06-20 00:51:41 +00:00
Javalin v5 bump (#930)
This commit is contained in:
@@ -25,6 +25,8 @@ import io.javalin.websocket.WsCloseContext;
|
||||
import io.javalin.websocket.WsConnectContext;
|
||||
import io.javalin.websocket.WsContext;
|
||||
import io.javalin.websocket.WsMessageContext;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.time.Duration;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
@@ -56,16 +58,17 @@ public class CameraSocketHandler {
|
||||
}
|
||||
|
||||
public void onConnect(WsConnectContext context) {
|
||||
context.session.setIdleTimeout(Long.MAX_VALUE); // TODO: determine better value
|
||||
var insa = context.session.getRemote().getInetSocketAddress();
|
||||
var host = insa.getAddress().toString() + ":" + insa.getPort();
|
||||
context.session.setIdleTimeout(
|
||||
Duration.ofMillis(Long.MAX_VALUE)); // TODO: determine better value
|
||||
var remote = (InetSocketAddress) context.session.getRemoteAddress();
|
||||
var host = remote.getAddress().toString() + ":" + remote.getPort();
|
||||
logger.info("New camera websocket connection from " + host);
|
||||
users.add(context);
|
||||
}
|
||||
|
||||
protected void onClose(WsCloseContext context) {
|
||||
var insa = context.session.getRemote().getInetSocketAddress();
|
||||
var host = insa.getAddress().toString() + ":" + insa.getPort();
|
||||
var remote = (InetSocketAddress) context.session.getRemoteAddress();
|
||||
var host = remote.getAddress().toString() + ":" + remote.getPort();
|
||||
var reason = context.reason() != null ? context.reason() : "Connection closed by client";
|
||||
logger.info("Closing camera websocket connection from " + host + " for reason: " + reason);
|
||||
svsManager.removeSubscription(context);
|
||||
|
||||
@@ -25,7 +25,9 @@ import io.javalin.websocket.WsCloseContext;
|
||||
import io.javalin.websocket.WsConnectContext;
|
||||
import io.javalin.websocket.WsContext;
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -68,9 +70,10 @@ public class DataSocketHandler {
|
||||
}
|
||||
|
||||
public void onConnect(WsConnectContext context) {
|
||||
context.session.setIdleTimeout(Long.MAX_VALUE); // TODO: determine better value
|
||||
var insa = context.session.getRemote().getInetSocketAddress();
|
||||
var host = insa.getAddress().toString() + ":" + insa.getPort();
|
||||
context.session.setIdleTimeout(
|
||||
Duration.ofMillis(Long.MAX_VALUE)); // TODO: determine better value
|
||||
var remote = (InetSocketAddress) context.session.getRemoteAddress();
|
||||
var host = remote.getAddress().toString() + ":" + remote.getPort();
|
||||
logger.info("New websocket connection from " + host);
|
||||
users.add(context);
|
||||
dcService.publishEvent(
|
||||
@@ -79,8 +82,8 @@ public class DataSocketHandler {
|
||||
}
|
||||
|
||||
protected void onClose(WsCloseContext context) {
|
||||
var insa = context.session.getRemote().getInetSocketAddress();
|
||||
var host = insa.getAddress().toString() + ":" + insa.getPort();
|
||||
var remote = (InetSocketAddress) context.session.getRemoteAddress();
|
||||
var host = remote.getAddress().toString() + ":" + remote.getPort();
|
||||
var reason = context.reason() != null ? context.reason() : "Connection closed by client";
|
||||
logger.info("Closing websocket connection from " + host + " for reason: " + reason);
|
||||
users.remove(context);
|
||||
@@ -332,9 +335,9 @@ public class DataSocketHandler {
|
||||
sendMessage(message, user);
|
||||
}
|
||||
} else {
|
||||
var skipUserPort = userToSkip.session.getRemote().getInetSocketAddress().getPort();
|
||||
var skipUserPort = ((InetSocketAddress) userToSkip.session.getRemoteAddress()).getPort();
|
||||
for (WsContext user : users) {
|
||||
var userPort = user.session.getRemote().getInetSocketAddress().getPort();
|
||||
var userPort = ((InetSocketAddress) user.session.getRemoteAddress()).getPort();
|
||||
if (userPort != skipUserPort) {
|
||||
sendMessage(message, user);
|
||||
}
|
||||
|
||||
@@ -69,7 +69,7 @@ public class RequestHandler {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!file.getExtension().contains("zip")) {
|
||||
if (!file.extension().contains("zip")) {
|
||||
ctx.status(400);
|
||||
ctx.result(
|
||||
"The uploaded file was not of type 'zip'. The uploaded file should be a .zip file.");
|
||||
@@ -132,7 +132,7 @@ public class RequestHandler {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!file.getExtension().contains("json")) {
|
||||
if (!file.extension().contains("json")) {
|
||||
ctx.status(400);
|
||||
ctx.result(
|
||||
"The uploaded file was not of type 'json'. The uploaded file should be a .json file.");
|
||||
@@ -174,7 +174,7 @@ public class RequestHandler {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!file.getExtension().contains("json")) {
|
||||
if (!file.extension().contains("json")) {
|
||||
ctx.status(400);
|
||||
ctx.result(
|
||||
"The uploaded file was not of type 'json'. The uploaded file should be a .json file.");
|
||||
@@ -216,7 +216,7 @@ public class RequestHandler {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!file.getExtension().contains("json")) {
|
||||
if (!file.extension().contains("json")) {
|
||||
ctx.status(400);
|
||||
ctx.result(
|
||||
"The uploaded file was not of type 'json'. The uploaded file should be a .json file.");
|
||||
@@ -258,7 +258,7 @@ public class RequestHandler {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!file.getExtension().contains("jar")) {
|
||||
if (!file.extension().contains("jar")) {
|
||||
ctx.status(400);
|
||||
ctx.result(
|
||||
"The uploaded file was not of type 'jar'. The uploaded file should be a .jar file.");
|
||||
@@ -273,7 +273,7 @@ public class RequestHandler {
|
||||
File targetFile = new File(filePath.toString());
|
||||
var stream = new FileOutputStream(targetFile);
|
||||
|
||||
file.getContent().transferTo(stream);
|
||||
file.content().transferTo(stream);
|
||||
stream.close();
|
||||
|
||||
ctx.status(200);
|
||||
@@ -492,14 +492,20 @@ public class RequestHandler {
|
||||
*/
|
||||
private static Optional<File> handleTempFileCreation(UploadedFile file) {
|
||||
var tempFilePath =
|
||||
new File(Path.of(System.getProperty("java.io.tmpdir"), file.getFilename()).toString());
|
||||
tempFilePath.getParentFile().mkdirs();
|
||||
new File(Path.of(System.getProperty("java.io.tmpdir"), file.filename()).toString());
|
||||
boolean makeDirsRes = tempFilePath.getParentFile().mkdirs();
|
||||
|
||||
if (!makeDirsRes) {
|
||||
logger.error(
|
||||
"There was an error while uploading " + file.filename() + " to the temp folder!");
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
try {
|
||||
FileUtils.copyInputStreamToFile(file.getContent(), tempFilePath);
|
||||
FileUtils.copyInputStreamToFile(file.content(), tempFilePath);
|
||||
} catch (IOException e) {
|
||||
logger.error(
|
||||
"There was an error while uploading " + file.getFilename() + " to the temp folder!");
|
||||
"There was an error while uploading " + file.filename() + " to the temp folder!");
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,8 @@
|
||||
package org.photonvision.server;
|
||||
|
||||
import io.javalin.Javalin;
|
||||
import io.javalin.http.staticfiles.Location;
|
||||
import io.javalin.plugin.bundled.CorsPluginConfig;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.StringJoiner;
|
||||
import org.photonvision.common.logging.LogGroup;
|
||||
import org.photonvision.common.logging.Logger;
|
||||
@@ -27,45 +28,45 @@ public class Server {
|
||||
private static final Logger logger = new Logger(Server.class, LogGroup.WebServer);
|
||||
|
||||
public static void start(int port) {
|
||||
Javalin app =
|
||||
var app =
|
||||
Javalin.create(
|
||||
config -> {
|
||||
config.showJavalinBanner = false;
|
||||
config.addStaticFiles("web", Location.CLASSPATH);
|
||||
config.enableCorsForAllOrigins();
|
||||
javalinConfig -> {
|
||||
javalinConfig.showJavalinBanner = false;
|
||||
javalinConfig.staticFiles.add("web");
|
||||
javalinConfig.plugins.enableCors(
|
||||
corsContainer -> {
|
||||
corsContainer.add(CorsPluginConfig::anyHost);
|
||||
});
|
||||
|
||||
config.requestLogger(
|
||||
javalinConfig.requestLogger.http(
|
||||
(ctx, ms) -> {
|
||||
StringJoiner joiner =
|
||||
new StringJoiner(" ")
|
||||
.add("Handled HTTP request of type")
|
||||
.add(ctx.req.getMethod())
|
||||
.add(ctx.req().getMethod())
|
||||
.add("from endpoint")
|
||||
.add(ctx.path())
|
||||
.add("for host")
|
||||
.add(ctx.req.getRemoteHost())
|
||||
.add(ctx.req().getRemoteHost())
|
||||
.add("in")
|
||||
.add(ms.toString())
|
||||
.add("ms");
|
||||
|
||||
logger.debug(joiner.toString());
|
||||
});
|
||||
|
||||
config.wsLogger(
|
||||
ws ->
|
||||
ws.onMessage(
|
||||
ctx -> logger.debug("Got WebSockets message: " + ctx.message())));
|
||||
|
||||
config.wsLogger(
|
||||
ws ->
|
||||
ws.onBinaryMessage(
|
||||
ctx ->
|
||||
logger.trace(
|
||||
() -> {
|
||||
var insa = ctx.session.getRemote().getInetSocketAddress();
|
||||
var host = insa.getAddress().toString() + ":" + insa.getPort();
|
||||
return "Got WebSockets binary message from host " + host;
|
||||
})));
|
||||
javalinConfig.requestLogger.ws(
|
||||
ws -> {
|
||||
ws.onMessage(ctx -> logger.debug("Got WebSockets message: " + ctx.message()));
|
||||
ws.onBinaryMessage(
|
||||
ctx ->
|
||||
logger.trace(
|
||||
() -> {
|
||||
var remote = (InetSocketAddress) ctx.session.getRemoteAddress();
|
||||
var host =
|
||||
remote.getAddress().toString() + ":" + remote.getPort();
|
||||
return "Got WebSockets binary message from host: " + host;
|
||||
}));
|
||||
});
|
||||
});
|
||||
|
||||
/*Web Socket Events for Data Exchange */
|
||||
|
||||
Reference in New Issue
Block a user