[REFACTOR] Replaced to pattern matching instanceof (#664)

This commit is contained in:
microwin7 2023-07-15 10:37:26 +03:00 committed by GitHub
parent 216928f258
commit 2aa8dffcaa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 35 additions and 37 deletions

View file

@ -60,8 +60,8 @@ public void invoke(String... args) throws Exception {
logger.error("Component {} not found", componentName); logger.error("Component {} not found", componentName);
return; return;
} }
if (component instanceof AutoCloseable) { if (component instanceof AutoCloseable autoCloseable) {
((AutoCloseable) component).close(); autoCloseable.close();
} }
server.unregisterObject("component." + componentName, component); server.unregisterObject("component." + componentName, component);
server.config.components.remove(componentName); server.config.components.remove(componentName);

View file

@ -209,9 +209,9 @@ public void close(LaunchServer.ReloadType type) {
if (type.equals(LaunchServer.ReloadType.FULL)) { if (type.equals(LaunchServer.ReloadType.FULL)) {
components.forEach((k, component) -> { components.forEach((k, component) -> {
server.unregisterObject("component.".concat(k), component); server.unregisterObject("component.".concat(k), component);
if (component instanceof AutoCloseable) { if (component instanceof AutoCloseable autoCloseable) {
try { try {
((AutoCloseable) component).close(); autoCloseable.close();
} catch (Exception e) { } catch (Exception e) {
logger.error(e); logger.error(e);
} }

View file

@ -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()); internalAuth(context.client, context.authType, context.pair, user.getUsername(), user.getUUID(), user.getPermissions(), result.isUsingOAuth());
return result; return result;
} catch (IOException e) { } catch (IOException e) {
if (e instanceof AuthException) throw (AuthException) e; if (e instanceof AuthException authException) throw authException;
logger.error(e); logger.error(e);
throw new AuthException("Internal Auth Error"); 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 { private AuthRequest.AuthPasswordInterface tryDecryptPasswordPlain(AuthRequest.AuthPasswordInterface password) throws AuthException {
if (password instanceof AuthAESPassword) { if (password instanceof AuthAESPassword authAESPassword) {
try { try {
return new AuthPlainPassword(IOHelper.decode(SecurityHelper.decrypt(server.runtime.passwordEncryptKey return new AuthPlainPassword(IOHelper.decode(SecurityHelper.decrypt(server.runtime.passwordEncryptKey
, ((AuthAESPassword) password).password))); , authAESPassword.password)));
} catch (Exception ignored) { } catch (Exception ignored) {
throw new AuthException("Password decryption error"); throw new AuthException("Password decryption error");
} }
} }
if (password instanceof AuthRSAPassword) { if (password instanceof AuthRSAPassword authRSAPassword) {
try { try {
Cipher cipher = SecurityHelper.newRSADecryptCipher(server.keyAgreementManager.rsaPrivateKey); Cipher cipher = SecurityHelper.newRSADecryptCipher(server.keyAgreementManager.rsaPrivateKey);
return new AuthPlainPassword( return new AuthPlainPassword(
IOHelper.decode(cipher.doFinal(((AuthRSAPassword) password).password)) IOHelper.decode(cipher.doFinal(authRSAPassword.password))
); );
} catch (Exception ignored) { } catch (Exception ignored) {
throw new AuthException("Password decryption error"); throw new AuthException("Password decryption error");

View file

@ -145,9 +145,7 @@ void process(WebSocketRequestContext context, WebSocketServerResponse response,
logger.error("WebSocket request processing failed", e); logger.error("WebSocket request processing failed", e);
RequestEvent event; RequestEvent event;
event = new ErrorRequestEvent("Fatal server error. Contact administrator"); event = new ErrorRequestEvent("Fatal server error. Contact administrator");
if (response instanceof SimpleResponse) { if (response instanceof SimpleResponse simpleResponse) event.requestUUID = simpleResponse.requestUUID;
event.requestUUID = ((SimpleResponse) response).requestUUID;
}
sendObject(ctx.channel(), event); sendObject(ctx.channel(), event);
} }
hookComplete.hook(context); hookComplete.hook(context);

View file

@ -19,8 +19,8 @@ public NettyIpForwardHandler(NettyConnectContext context) {
@Override @Override
protected void decode(ChannelHandlerContext ctx, HttpRequest msg, List<Object> out) { protected void decode(ChannelHandlerContext ctx, HttpRequest msg, List<Object> out) {
if (msg instanceof ReferenceCounted) { if (msg instanceof ReferenceCounted referenceCounted) {
((ReferenceCounted) msg).retain(); referenceCounted.retain();
} }
if (context.ip != null) { if (context.ip != null) {
out.add(msg); out.add(msg);

View file

@ -63,16 +63,16 @@ protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) {
} catch (Throwable ex) { } catch (Throwable ex) {
logger.error("WebSocket frame handler hook error", ex); logger.error("WebSocket frame handler hook error", ex);
} }
if (frame instanceof TextWebSocketFrame) { if (frame instanceof TextWebSocketFrame textWebSocketFrame) {
if (logger.isTraceEnabled()) { 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 { try {
service.process(ctx, (TextWebSocketFrame) frame, client, context.ip); service.process(ctx, textWebSocketFrame, client, context.ip);
} catch (Throwable ex) { } catch (Throwable ex) {
logger.warn("Client {} send invalid request. Connection force closed.", context.ip == null ? IOHelper.getIP(ctx.channel().remoteAddress()) : context.ip); logger.warn("Client {} send invalid request. Connection force closed.", context.ip == null ? IOHelper.getIP(ctx.channel().remoteAddress()) : context.ip);
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
logger.trace("Client message: {}", ((TextWebSocketFrame) frame).text()); logger.trace("Client message: {}", textWebSocketFrame.text());
logger.error("Process websockets request failed", ex); logger.error("Process websockets request failed", ex);
} }
ctx.channel().close(); ctx.channel().close();
@ -81,10 +81,10 @@ protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) {
frame.content().retain(); frame.content().retain();
ctx.channel().writeAndFlush(new PongWebSocketFrame(frame.content())); ctx.channel().writeAndFlush(new PongWebSocketFrame(frame.content()));
//return; //return;
} else if ((frame instanceof PongWebSocketFrame)) { } else if (frame instanceof PongWebSocketFrame) {
logger.trace("WebSocket Client received pong"); logger.trace("WebSocket Client received pong");
} else if ((frame instanceof CloseWebSocketFrame)) { } else if (frame instanceof CloseWebSocketFrame closeWebSocketFrame) {
int statusCode = ((CloseWebSocketFrame) frame).statusCode(); int statusCode = closeWebSocketFrame.statusCode();
ctx.channel().close(); ctx.channel().close();
} else { } else {
String message = "unsupported frame type: " + frame.getClass().getName(); String message = "unsupported frame type: " + frame.getClass().getName();
@ -93,11 +93,11 @@ protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) {
} }
@Override @Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception { public void channelInactive(ChannelHandlerContext channelHandlerContext) throws Exception {
if (future != null) future.cancel(true); if (future != null) future.cancel(true);
if (logger.isTraceEnabled()) { 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);
} }
} }

View file

@ -34,8 +34,8 @@ public void execute(ChannelHandlerContext ctx, Client client) {
boolean success; boolean success;
try { try {
server.authHookManager.joinServerHook.hook(this, client); server.authHookManager.joinServerHook.hook(this, client);
if (server.config.protectHandler instanceof JoinServerProtectHandler) { if (server.config.protectHandler instanceof JoinServerProtectHandler joinServerProtectHandler) {
success = ((JoinServerProtectHandler) server.config.protectHandler).onJoinServer(serverID, username, client); success = joinServerProtectHandler.onJoinServer(serverID, username, client);
if (!success) { if (!success) {
sendResult(new JoinServerRequestEvent(false)); sendResult(new JoinServerRequestEvent(false));
return; return;

View file

@ -36,7 +36,7 @@ public String getType() {
@Override @Override
public void execute(ChannelHandlerContext ctx, Client client) { 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"); sendError("Access denied");
return; return;
} }

View file

@ -28,8 +28,8 @@ public void execute(ChannelHandlerContext ctx, Client client) {
Collection<ClientProfile> profiles = server.getProfiles(); Collection<ClientProfile> profiles = server.getProfiles();
for (ClientProfile p : profiles) { for (ClientProfile p : profiles) {
if (p.getTitle().equals(this.client)) { if (p.getTitle().equals(this.client)) {
if (server.config.protectHandler instanceof ProfilesProtectHandler && if (server.config.protectHandler instanceof ProfilesProtectHandler profilesProtectHandler &&
!((ProfilesProtectHandler) server.config.protectHandler).canChangeProfile(p, client)) { !profilesProtectHandler.canChangeProfile(p, client)) {
sendError("Access denied"); sendError("Access denied");
return; return;
} }

View file

@ -21,9 +21,9 @@ public void execute(ChannelHandlerContext ctx, Client client) {
sendError("Invalid request"); sendError("Invalid request");
return; return;
} }
if (server.config.protectHandler instanceof HardwareProtectHandler) { if (server.config.protectHandler instanceof HardwareProtectHandler hardwareProtectHandler) {
try { try {
((HardwareProtectHandler) server.config.protectHandler).onHardwareReport(this, client); hardwareProtectHandler.onHardwareReport(this, client);
} catch (SecurityException e) { } catch (SecurityException e) {
sendError(e.getMessage()); sendError(e.getMessage());
} }

View file

@ -21,12 +21,12 @@ public String getType() {
@Override @Override
public void execute(ChannelHandlerContext ctx, Client client) { public void execute(ChannelHandlerContext ctx, Client client) {
if (!(server.config.protectHandler instanceof SecureProtectHandler)) { if (!(server.config.protectHandler instanceof SecureProtectHandler secureProtectHandler)) {
sendError("Method not allowed"); sendError("Method not allowed");
} } else {
SecureProtectHandler secureProtectHandler = (SecureProtectHandler) server.config.protectHandler;
SecurityReportRequestEvent event = secureProtectHandler.onSecurityReport(this, client); SecurityReportRequestEvent event = secureProtectHandler.onSecurityReport(this, client);
server.modulesManager.invokeEvent(new SecurityReportModuleEvent(event, this, client)); server.modulesManager.invokeEvent(new SecurityReportModuleEvent(event, this, client));
sendResult(event); sendResult(event);
} }
}
} }

View file

@ -19,7 +19,7 @@ public String getType() {
@Override @Override
public void execute(ChannelHandlerContext ctx, Client client) { 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"); sendError("Access denied");
return; return;
} }