X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ffcp%2FFcpInterface.java;h=36e18e65ea4251d8497def0c0a5da4a17d2c59d4;hb=0747ce52deab6ed6e4c3118959cf76827c2ce495;hp=839930c3e5cd16e9573bf1ea654a608f4db24efd;hpb=f229fe41f708d2b275c20ceb9aba5993761218a3;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/fcp/FcpInterface.java b/src/main/java/net/pterodactylus/sone/fcp/FcpInterface.java index 839930c..36e18e6 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/FcpInterface.java +++ b/src/main/java/net/pterodactylus/sone/fcp/FcpInterface.java @@ -1,5 +1,5 @@ /* - * Sone - FcpInterface.java - Copyright © 2011–2013 David Roden + * Sone - FcpInterface.java - Copyright © 2011–2016 David Roden * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -75,7 +75,7 @@ public class FcpInterface { } /** The logger. */ - private static final Logger logger = getLogger("Sone.External.Fcp"); + private static final Logger logger = getLogger(FcpInterface.class.getName()); /** Whether the FCP interface is currently active. */ private final AtomicBoolean active = new AtomicBoolean(); @@ -153,41 +153,37 @@ public class FcpInterface { */ public void handle(PluginReplySender pluginReplySender, SimpleFieldSet parameters, Bucket data, int accessType) { if (!active.get()) { - try { - sendReply(pluginReplySender, null, new ErrorResponse(400, "FCP Interface deactivated")); - } catch (PluginNotFoundException pnfe1) { - logger.log(Level.FINE, "Could not set error to plugin.", pnfe1); - } + sendErrorReply(pluginReplySender, null, 503, "FCP Interface deactivated"); return; } AbstractSoneCommand command = commands.get(parameters.get("Message")); if ((accessType == FredPluginFCP.ACCESS_FCP_RESTRICTED) && (((fullAccessRequired.get() == FullAccessRequired.WRITING) && command.requiresWriteAccess()) || (fullAccessRequired.get() == FullAccessRequired.ALWAYS))) { - try { - sendReply(pluginReplySender, null, new ErrorResponse(401, "Not authorized")); - } catch (PluginNotFoundException pnfe1) { - logger.log(Level.FINE, "Could not set error to plugin.", pnfe1); - } + sendErrorReply(pluginReplySender, null, 401, "Not authorized"); return; } + if (command == null) { + sendErrorReply(pluginReplySender, null, 404, "Unrecognized Message: " + parameters.get("Message")); + return; + } + String identifier = parameters.get("Identifier"); + if ((identifier == null) || (identifier.length() == 0)) { + sendErrorReply(pluginReplySender, null, 400, "Missing Identifier."); + return; + } + try { + Response response = command.execute(parameters, data, AccessType.values()[accessType]); + sendReply(pluginReplySender, identifier, response); + } catch (Exception e1) { + logger.log(Level.WARNING, "Could not process FCP command “%s”.", command); + sendErrorReply(pluginReplySender, identifier, 500, "Error executing command: " + e1.getMessage()); + } + } + + private void sendErrorReply(PluginReplySender pluginReplySender, String identifier, int errorCode, String message) { try { - if (command == null) { - sendReply(pluginReplySender, null, new ErrorResponse("Unrecognized Message: " + parameters.get("Message"))); - return; - } - String identifier = parameters.get("Identifier"); - if ((identifier == null) || (identifier.length() == 0)) { - sendReply(pluginReplySender, null, new ErrorResponse("Missing Identifier.")); - return; - } - try { - Response response = command.execute(parameters, data, AccessType.values()[accessType]); - sendReply(pluginReplySender, identifier, response); - } catch (Exception e1) { - logger.log(Level.WARNING, "Could not process FCP command “%s”.", command); - sendReply(pluginReplySender, identifier, new ErrorResponse("Error executing command: " + e1.getMessage())); - } + sendReply(pluginReplySender, identifier, new ErrorResponse(errorCode, message)); } catch (PluginNotFoundException pnfe1) { - logger.log(Level.WARNING, "Could not find destination plugin: " + pluginReplySender); + logger.log(Level.FINE, "Could not send error to plugin.", pnfe1); } }