From 2aa8dffcaa4308a111c8ede16622c4c37cab7cb9 Mon Sep 17 00:00:00 2001 From: microwin7 Date: Sat, 15 Jul 2023 10:37:26 +0300 Subject: [PATCH] [REFACTOR] Replaced to pattern matching instanceof (#664) --- .../command/service/ComponentCommand.java | 4 ++-- .../config/LaunchServerConfig.java | 4 ++-- .../launchserver/manangers/AuthManager.java | 10 +++++----- .../launchserver/socket/WebSocketService.java | 4 +--- .../handlers/NettyIpForwardHandler.java | 4 ++-- .../handlers/WebSocketFrameHandler.java | 20 +++++++++---------- .../response/auth/JoinServerResponse.java | 4 ++-- .../response/auth/ProfilesResponse.java | 2 +- .../response/auth/SetProfileResponse.java | 4 ++-- .../secure/HardwareReportResponse.java | 4 ++-- .../secure/SecurityReportResponse.java | 10 +++++----- .../response/update/UpdateResponse.java | 2 +- 12 files changed, 35 insertions(+), 37 deletions(-) diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/command/service/ComponentCommand.java b/LaunchServer/src/main/java/pro/gravit/launchserver/command/service/ComponentCommand.java index 5e9769e3..39fd07aa 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/command/service/ComponentCommand.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/command/service/ComponentCommand.java @@ -60,8 +60,8 @@ public void invoke(String... args) throws Exception { logger.error("Component {} not found", componentName); return; } - if (component instanceof AutoCloseable) { - ((AutoCloseable) component).close(); + if (component instanceof AutoCloseable autoCloseable) { + autoCloseable.close(); } server.unregisterObject("component." + componentName, component); server.config.components.remove(componentName); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/config/LaunchServerConfig.java b/LaunchServer/src/main/java/pro/gravit/launchserver/config/LaunchServerConfig.java index bbc7d86f..f71705be 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/config/LaunchServerConfig.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/config/LaunchServerConfig.java @@ -209,9 +209,9 @@ public void close(LaunchServer.ReloadType type) { if (type.equals(LaunchServer.ReloadType.FULL)) { components.forEach((k, component) -> { server.unregisterObject("component.".concat(k), component); - if (component instanceof AutoCloseable) { + if (component instanceof AutoCloseable autoCloseable) { try { - ((AutoCloseable) component).close(); + autoCloseable.close(); } catch (Exception e) { logger.error(e); } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/AuthManager.java b/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/AuthManager.java index a6495608..7313cc9d 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/AuthManager.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/AuthManager.java @@ -133,7 +133,7 @@ public AuthReport auth(AuthResponse.AuthContext context, AuthRequest.AuthPasswor internalAuth(context.client, context.authType, context.pair, user.getUsername(), user.getUUID(), user.getPermissions(), result.isUsingOAuth()); return result; } catch (IOException e) { - if (e instanceof AuthException) throw (AuthException) e; + if (e instanceof AuthException authException) throw authException; logger.error(e); throw new AuthException("Internal Auth Error"); } @@ -272,19 +272,19 @@ public AuthRequest.AuthPasswordInterface decryptPassword(AuthRequest.AuthPasswor } private AuthRequest.AuthPasswordInterface tryDecryptPasswordPlain(AuthRequest.AuthPasswordInterface password) throws AuthException { - if (password instanceof AuthAESPassword) { + if (password instanceof AuthAESPassword authAESPassword) { try { return new AuthPlainPassword(IOHelper.decode(SecurityHelper.decrypt(server.runtime.passwordEncryptKey - , ((AuthAESPassword) password).password))); + , authAESPassword.password))); } catch (Exception ignored) { throw new AuthException("Password decryption error"); } } - if (password instanceof AuthRSAPassword) { + if (password instanceof AuthRSAPassword authRSAPassword) { try { Cipher cipher = SecurityHelper.newRSADecryptCipher(server.keyAgreementManager.rsaPrivateKey); return new AuthPlainPassword( - IOHelper.decode(cipher.doFinal(((AuthRSAPassword) password).password)) + IOHelper.decode(cipher.doFinal(authRSAPassword.password)) ); } catch (Exception ignored) { throw new AuthException("Password decryption error"); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/WebSocketService.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/WebSocketService.java index 2cd59b24..02c4cfb3 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/WebSocketService.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/WebSocketService.java @@ -145,9 +145,7 @@ void process(WebSocketRequestContext context, WebSocketServerResponse response, logger.error("WebSocket request processing failed", e); RequestEvent event; event = new ErrorRequestEvent("Fatal server error. Contact administrator"); - if (response instanceof SimpleResponse) { - event.requestUUID = ((SimpleResponse) response).requestUUID; - } + if (response instanceof SimpleResponse simpleResponse) event.requestUUID = simpleResponse.requestUUID; sendObject(ctx.channel(), event); } hookComplete.hook(context); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/handlers/NettyIpForwardHandler.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/handlers/NettyIpForwardHandler.java index d0895270..b5229a24 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/handlers/NettyIpForwardHandler.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/handlers/NettyIpForwardHandler.java @@ -19,8 +19,8 @@ public NettyIpForwardHandler(NettyConnectContext context) { @Override protected void decode(ChannelHandlerContext ctx, HttpRequest msg, List out) { - if (msg instanceof ReferenceCounted) { - ((ReferenceCounted) msg).retain(); + if (msg instanceof ReferenceCounted referenceCounted) { + referenceCounted.retain(); } if (context.ip != null) { out.add(msg); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/handlers/WebSocketFrameHandler.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/handlers/WebSocketFrameHandler.java index dffbbd46..9d2c8dcd 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/handlers/WebSocketFrameHandler.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/handlers/WebSocketFrameHandler.java @@ -63,16 +63,16 @@ protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) { } catch (Throwable ex) { logger.error("WebSocket frame handler hook error", ex); } - if (frame instanceof TextWebSocketFrame) { + if (frame instanceof TextWebSocketFrame textWebSocketFrame) { if (logger.isTraceEnabled()) { - logger.trace("Message from {}: {}", context.ip == null ? IOHelper.getIP(ctx.channel().remoteAddress()) : context.ip, ((TextWebSocketFrame) frame).text()); + logger.trace("Message from {}: {}", context.ip == null ? IOHelper.getIP(ctx.channel().remoteAddress()) : context.ip, textWebSocketFrame.text()); } try { - service.process(ctx, (TextWebSocketFrame) frame, client, context.ip); + service.process(ctx, textWebSocketFrame, client, context.ip); } catch (Throwable ex) { logger.warn("Client {} send invalid request. Connection force closed.", context.ip == null ? IOHelper.getIP(ctx.channel().remoteAddress()) : context.ip); if (logger.isTraceEnabled()) { - logger.trace("Client message: {}", ((TextWebSocketFrame) frame).text()); + logger.trace("Client message: {}", textWebSocketFrame.text()); logger.error("Process websockets request failed", ex); } ctx.channel().close(); @@ -81,10 +81,10 @@ protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) { frame.content().retain(); ctx.channel().writeAndFlush(new PongWebSocketFrame(frame.content())); //return; - } else if ((frame instanceof PongWebSocketFrame)) { + } else if (frame instanceof PongWebSocketFrame) { logger.trace("WebSocket Client received pong"); - } else if ((frame instanceof CloseWebSocketFrame)) { - int statusCode = ((CloseWebSocketFrame) frame).statusCode(); + } else if (frame instanceof CloseWebSocketFrame closeWebSocketFrame) { + int statusCode = closeWebSocketFrame.statusCode(); ctx.channel().close(); } else { String message = "unsupported frame type: " + frame.getClass().getName(); @@ -93,11 +93,11 @@ protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) { } @Override - public void channelInactive(ChannelHandlerContext ctx) throws Exception { + public void channelInactive(ChannelHandlerContext channelHandlerContext) throws Exception { if (future != null) future.cancel(true); if (logger.isTraceEnabled()) { - logger.trace("Client {} disconnected", IOHelper.getIP(ctx.channel().remoteAddress())); + logger.trace("Client {} disconnected", IOHelper.getIP(channelHandlerContext.channel().remoteAddress())); } - super.channelInactive(ctx); + super.channelInactive(channelHandlerContext); } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/JoinServerResponse.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/JoinServerResponse.java index 30125952..1a9c3851 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/JoinServerResponse.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/JoinServerResponse.java @@ -34,8 +34,8 @@ public void execute(ChannelHandlerContext ctx, Client client) { boolean success; try { server.authHookManager.joinServerHook.hook(this, client); - if (server.config.protectHandler instanceof JoinServerProtectHandler) { - success = ((JoinServerProtectHandler) server.config.protectHandler).onJoinServer(serverID, username, client); + if (server.config.protectHandler instanceof JoinServerProtectHandler joinServerProtectHandler) { + success = joinServerProtectHandler.onJoinServer(serverID, username, client); if (!success) { sendResult(new JoinServerRequestEvent(false)); return; diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/ProfilesResponse.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/ProfilesResponse.java index 12ecea6d..45067b2e 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/ProfilesResponse.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/ProfilesResponse.java @@ -36,7 +36,7 @@ public String getType() { @Override public void execute(ChannelHandlerContext ctx, Client client) { - if (server.config.protectHandler instanceof ProfilesProtectHandler && !((ProfilesProtectHandler) server.config.protectHandler).canGetProfiles(client)) { + if (server.config.protectHandler instanceof ProfilesProtectHandler profilesProtectHandler && !profilesProtectHandler.canGetProfiles(client)) { sendError("Access denied"); return; } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/SetProfileResponse.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/SetProfileResponse.java index 51f4e131..423e4a68 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/SetProfileResponse.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/SetProfileResponse.java @@ -28,8 +28,8 @@ public void execute(ChannelHandlerContext ctx, Client client) { Collection profiles = server.getProfiles(); for (ClientProfile p : profiles) { if (p.getTitle().equals(this.client)) { - if (server.config.protectHandler instanceof ProfilesProtectHandler && - !((ProfilesProtectHandler) server.config.protectHandler).canChangeProfile(p, client)) { + if (server.config.protectHandler instanceof ProfilesProtectHandler profilesProtectHandler && + !profilesProtectHandler.canChangeProfile(p, client)) { sendError("Access denied"); return; } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/secure/HardwareReportResponse.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/secure/HardwareReportResponse.java index 78a629b9..b23e5ecb 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/secure/HardwareReportResponse.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/secure/HardwareReportResponse.java @@ -21,9 +21,9 @@ public void execute(ChannelHandlerContext ctx, Client client) { sendError("Invalid request"); return; } - if (server.config.protectHandler instanceof HardwareProtectHandler) { + if (server.config.protectHandler instanceof HardwareProtectHandler hardwareProtectHandler) { try { - ((HardwareProtectHandler) server.config.protectHandler).onHardwareReport(this, client); + hardwareProtectHandler.onHardwareReport(this, client); } catch (SecurityException e) { sendError(e.getMessage()); } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/secure/SecurityReportResponse.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/secure/SecurityReportResponse.java index 21e931a6..de64309a 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/secure/SecurityReportResponse.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/secure/SecurityReportResponse.java @@ -21,12 +21,12 @@ public String getType() { @Override public void execute(ChannelHandlerContext ctx, Client client) { - if (!(server.config.protectHandler instanceof SecureProtectHandler)) { + if (!(server.config.protectHandler instanceof SecureProtectHandler secureProtectHandler)) { sendError("Method not allowed"); + } else { + SecurityReportRequestEvent event = secureProtectHandler.onSecurityReport(this, client); + server.modulesManager.invokeEvent(new SecurityReportModuleEvent(event, this, client)); + sendResult(event); } - SecureProtectHandler secureProtectHandler = (SecureProtectHandler) server.config.protectHandler; - SecurityReportRequestEvent event = secureProtectHandler.onSecurityReport(this, client); - server.modulesManager.invokeEvent(new SecurityReportModuleEvent(event, this, client)); - sendResult(event); } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/update/UpdateResponse.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/update/UpdateResponse.java index bd7bbfe8..2e7b27fd 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/update/UpdateResponse.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/update/UpdateResponse.java @@ -19,7 +19,7 @@ public String getType() { @Override public void execute(ChannelHandlerContext ctx, Client client) { - if (server.config.protectHandler instanceof ProfilesProtectHandler && !((ProfilesProtectHandler) server.config.protectHandler).canGetUpdates(dirName, client)) { + if (server.config.protectHandler instanceof ProfilesProtectHandler profilesProtectHandler && !profilesProtectHandler.canGetUpdates(dirName, client)) { sendError("Access denied"); return; }