Search in sources :

Example 1 with JSONRPC2Response

use of com.thetransactioncompany.jsonrpc2.JSONRPC2Response in project i2pplus by I2PPlus.

the class AdvancedSettingsHandler method process.

// Processes the requests
@SuppressWarnings("unchecked")
public JSONRPC2Response process(JSONRPC2Request req, MessageContext ctx) {
    if (req.getMethod().equals("AdvancedSettings")) {
        JSONRPC2Error err = _helper.validateParams(requiredArgs, req);
        if (err != null) {
            return new JSONRPC2Response(err, req.getID());
        }
        if (_context == null) {
            return new JSONRPC2Response(new JSONRPC2Error(JSONRPC2Error.INTERNAL_ERROR.getCode(), "RouterContext was not initialized. Query failed"), req.getID());
        }
        @SuppressWarnings("rawtypes") Map<String, Object> inParams = req.getNamedParams();
        Map outParams = new HashMap();
        if (inParams.containsKey("setAll")) {
            Object obj = inParams.get("setAll");
            if (!(obj instanceof Map)) {
                JSONRPC2Error rpcErr = new JSONRPC2Error(JSONRPC2Error.INVALID_PARAMS.getCode(), "Value of \"setAll\" is not a Map");
                return new JSONRPC2Response(rpcErr, req.getID());
            }
            @SuppressWarnings("rawtypes") Map objMap = (Map) inParams.get("setAll");
            if (objMap.size() > 0) {
                if (!(objMap.keySet().toArray()[0] instanceof String) && !(objMap.values().toArray()[0] instanceof String)) {
                    JSONRPC2Error rpcErr = new JSONRPC2Error(JSONRPC2Error.INVALID_PARAMS.getCode(), "Map of settings does not contain String keys and values");
                    return new JSONRPC2Response(rpcErr, req.getID());
                }
                if (!checkTypes(objMap)) {
                    JSONRPC2Error rpcErr = new JSONRPC2Error(JSONRPC2Error.INTERNAL_ERROR.getCode(), "Some of the supplied values are not strings");
                    return new JSONRPC2Response(rpcErr, req.getID());
                }
                Map<String, String> allSettings = (Map<String, String>) objMap;
                boolean success = setAdvancedSettings(allSettings, true);
                if (!success) {
                    JSONRPC2Error rpcErr = new JSONRPC2Error(JSONRPC2Error.INTERNAL_ERROR.getCode(), "Failed to save new config");
                    return new JSONRPC2Response(rpcErr, req.getID());
                }
            } else {
                // Empty list of settings submitted
                boolean success = setAdvancedSettings(null, true);
                if (!success) {
                    JSONRPC2Error rpcErr = new JSONRPC2Error(JSONRPC2Error.INTERNAL_ERROR.getCode(), "Failed to save new config");
                    return new JSONRPC2Response(rpcErr, req.getID());
                }
            }
        }
        if (inParams.containsKey("getAll")) {
            outParams.put("getAll", getAdvancedSettings());
        }
        if (inParams.containsKey("set")) {
            Object obj = inParams.get("set");
            if (!(obj instanceof Map)) {
                JSONRPC2Error rpcErr = new JSONRPC2Error(JSONRPC2Error.INVALID_PARAMS.getCode(), "Value of \"set\" is not a Map");
                return new JSONRPC2Response(rpcErr, req.getID());
            }
            Map objMap = (Map) inParams.get("set");
            if (objMap.size() > 0) {
                if (!(objMap.keySet().toArray()[0] instanceof String) && !(objMap.values().toArray()[0] instanceof String)) {
                    JSONRPC2Error rpcErr = new JSONRPC2Error(JSONRPC2Error.INVALID_PARAMS.getCode(), "Map of settings does not contain String keys and values");
                    return new JSONRPC2Response(rpcErr, req.getID());
                }
                if (!checkTypes(objMap)) {
                    JSONRPC2Error rpcErr = new JSONRPC2Error(JSONRPC2Error.INTERNAL_ERROR.getCode(), "Some of the supplied values are not strings");
                    return new JSONRPC2Response(rpcErr, req.getID());
                }
                Map<String, String> allSettings = (Map<String, String>) objMap;
                boolean success = setAdvancedSettings(allSettings, false);
                if (!success) {
                    JSONRPC2Error rpcErr = new JSONRPC2Error(JSONRPC2Error.INTERNAL_ERROR.getCode(), "Failed to save new config");
                    return new JSONRPC2Response(rpcErr, req.getID());
                }
            } else {
                // Empty list of settings submitted
                JSONRPC2Error rpcErr = new JSONRPC2Error(JSONRPC2Error.INVALID_PARAMS.getCode(), "Map of settings does not contain any entries");
                return new JSONRPC2Response(rpcErr, req.getID());
            }
        }
        if (inParams.containsKey("get")) {
            Object obj = inParams.get("get");
            if (!(obj instanceof String)) {
                JSONRPC2Error rpcErr = new JSONRPC2Error(JSONRPC2Error.INVALID_PARAMS.getCode(), "Value of \"get\" is not a string");
                return new JSONRPC2Response(rpcErr, req.getID());
            }
            String getStr = (String) obj;
            String getVal = getAdvancedSetting(getStr);
            Map<String, String> outMap = new HashMap<String, String>();
            outMap.put(getStr, getVal);
            outParams.put("get", outMap);
        }
        return new JSONRPC2Response(outParams, req.getID());
    } else {
        // Method name not supported
        return new JSONRPC2Response(JSONRPC2Error.METHOD_NOT_FOUND, req.getID());
    }
}
Also used : HashMap(java.util.HashMap) JSONRPC2Response(com.thetransactioncompany.jsonrpc2.JSONRPC2Response) JSONRPC2Error(com.thetransactioncompany.jsonrpc2.JSONRPC2Error) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with JSONRPC2Response

use of com.thetransactioncompany.jsonrpc2.JSONRPC2Response in project i2pplus by I2PPlus.

the class GetRateHandler method process.

// Processes the requests
public JSONRPC2Response process(JSONRPC2Request req, MessageContext ctx) {
    if (req.getMethod().equals("GetRate")) {
        JSONRPC2Error err = _helper.validateParams(requiredArgs, req);
        if (err != null)
            return new JSONRPC2Response(err, req.getID());
        Map<String, Object> inParams = req.getNamedParams();
        String input = (String) inParams.get("Stat");
        if (input == null) {
            return new JSONRPC2Response(JSONRPC2Error.INVALID_PARAMS, req.getID());
        }
        Number p = (Number) inParams.get("Period");
        if (p == null)
            return new JSONRPC2Response(JSONRPC2Error.INVALID_PARAMS, req.getID());
        long period = p.longValue();
        RateStat rateStat = I2PAppContext.getGlobalContext().statManager().getRate(input);
        // If RateStat or the requested period doesn't already exist, create them.
        if (rateStat == null || rateStat.getRate(period) == null) {
            long[] tempArr = new long[1];
            tempArr[0] = period;
            I2PAppContext.getGlobalContext().statManager().createRequiredRateStat(input, "I2PControl", "I2PControl", tempArr);
            rateStat = I2PAppContext.getGlobalContext().statManager().getRate(input);
        }
        if (rateStat.getRate(period) == null)
            return new JSONRPC2Response(JSONRPC2Error.INTERNAL_ERROR, req.getID());
        Map<String, Object> outParams = new HashMap<String, Object>(4);
        Rate rate = rateStat.getRate(period);
        rate.coalesce();
        outParams.put("Result", rate.getAverageValue());
        return new JSONRPC2Response(outParams, req.getID());
    }
    return new JSONRPC2Response(JSONRPC2Error.METHOD_NOT_FOUND, req.getID());
}
Also used : RateStat(net.i2p.stat.RateStat) HashMap(java.util.HashMap) Rate(net.i2p.stat.Rate) JSONRPC2Response(com.thetransactioncompany.jsonrpc2.JSONRPC2Response) JSONRPC2Error(com.thetransactioncompany.jsonrpc2.JSONRPC2Error)

Example 3 with JSONRPC2Response

use of com.thetransactioncompany.jsonrpc2.JSONRPC2Response in project i2pplus by vituperative.

the class AdvancedSettingsHandler method process.

// Processes the requests
@SuppressWarnings("unchecked")
public JSONRPC2Response process(JSONRPC2Request req, MessageContext ctx) {
    if (req.getMethod().equals("AdvancedSettings")) {
        JSONRPC2Error err = _helper.validateParams(requiredArgs, req);
        if (err != null) {
            return new JSONRPC2Response(err, req.getID());
        }
        if (_context == null) {
            return new JSONRPC2Response(new JSONRPC2Error(JSONRPC2Error.INTERNAL_ERROR.getCode(), "RouterContext was not initialized. Query failed"), req.getID());
        }
        @SuppressWarnings("rawtypes") Map<String, Object> inParams = req.getNamedParams();
        Map outParams = new HashMap();
        if (inParams.containsKey("setAll")) {
            Object obj = inParams.get("setAll");
            if (!(obj instanceof Map)) {
                JSONRPC2Error rpcErr = new JSONRPC2Error(JSONRPC2Error.INVALID_PARAMS.getCode(), "Value of \"setAll\" is not a Map");
                return new JSONRPC2Response(rpcErr, req.getID());
            }
            @SuppressWarnings("rawtypes") Map objMap = (Map) inParams.get("setAll");
            if (objMap.size() > 0) {
                if (!(objMap.keySet().toArray()[0] instanceof String) && !(objMap.values().toArray()[0] instanceof String)) {
                    JSONRPC2Error rpcErr = new JSONRPC2Error(JSONRPC2Error.INVALID_PARAMS.getCode(), "Map of settings does not contain String keys and values");
                    return new JSONRPC2Response(rpcErr, req.getID());
                }
                if (!checkTypes(objMap)) {
                    JSONRPC2Error rpcErr = new JSONRPC2Error(JSONRPC2Error.INTERNAL_ERROR.getCode(), "Some of the supplied values are not strings");
                    return new JSONRPC2Response(rpcErr, req.getID());
                }
                Map<String, String> allSettings = (Map<String, String>) objMap;
                boolean success = setAdvancedSettings(allSettings, true);
                if (!success) {
                    JSONRPC2Error rpcErr = new JSONRPC2Error(JSONRPC2Error.INTERNAL_ERROR.getCode(), "Failed to save new config");
                    return new JSONRPC2Response(rpcErr, req.getID());
                }
            } else {
                // Empty list of settings submitted
                boolean success = setAdvancedSettings(null, true);
                if (!success) {
                    JSONRPC2Error rpcErr = new JSONRPC2Error(JSONRPC2Error.INTERNAL_ERROR.getCode(), "Failed to save new config");
                    return new JSONRPC2Response(rpcErr, req.getID());
                }
            }
        }
        if (inParams.containsKey("getAll")) {
            outParams.put("getAll", getAdvancedSettings());
        }
        if (inParams.containsKey("set")) {
            Object obj = inParams.get("set");
            if (!(obj instanceof Map)) {
                JSONRPC2Error rpcErr = new JSONRPC2Error(JSONRPC2Error.INVALID_PARAMS.getCode(), "Value of \"set\" is not a Map");
                return new JSONRPC2Response(rpcErr, req.getID());
            }
            Map objMap = (Map) inParams.get("set");
            if (objMap.size() > 0) {
                if (!(objMap.keySet().toArray()[0] instanceof String) && !(objMap.values().toArray()[0] instanceof String)) {
                    JSONRPC2Error rpcErr = new JSONRPC2Error(JSONRPC2Error.INVALID_PARAMS.getCode(), "Map of settings does not contain String keys and values");
                    return new JSONRPC2Response(rpcErr, req.getID());
                }
                if (!checkTypes(objMap)) {
                    JSONRPC2Error rpcErr = new JSONRPC2Error(JSONRPC2Error.INTERNAL_ERROR.getCode(), "Some of the supplied values are not strings");
                    return new JSONRPC2Response(rpcErr, req.getID());
                }
                Map<String, String> allSettings = (Map<String, String>) objMap;
                boolean success = setAdvancedSettings(allSettings, false);
                if (!success) {
                    JSONRPC2Error rpcErr = new JSONRPC2Error(JSONRPC2Error.INTERNAL_ERROR.getCode(), "Failed to save new config");
                    return new JSONRPC2Response(rpcErr, req.getID());
                }
            } else {
                // Empty list of settings submitted
                JSONRPC2Error rpcErr = new JSONRPC2Error(JSONRPC2Error.INVALID_PARAMS.getCode(), "Map of settings does not contain any entries");
                return new JSONRPC2Response(rpcErr, req.getID());
            }
        }
        if (inParams.containsKey("get")) {
            Object obj = inParams.get("get");
            if (!(obj instanceof String)) {
                JSONRPC2Error rpcErr = new JSONRPC2Error(JSONRPC2Error.INVALID_PARAMS.getCode(), "Value of \"get\" is not a string");
                return new JSONRPC2Response(rpcErr, req.getID());
            }
            String getStr = (String) obj;
            String getVal = getAdvancedSetting(getStr);
            Map<String, String> outMap = new HashMap<String, String>();
            outMap.put(getStr, getVal);
            outParams.put("get", outMap);
        }
        return new JSONRPC2Response(outParams, req.getID());
    } else {
        // Method name not supported
        return new JSONRPC2Response(JSONRPC2Error.METHOD_NOT_FOUND, req.getID());
    }
}
Also used : HashMap(java.util.HashMap) JSONRPC2Response(com.thetransactioncompany.jsonrpc2.JSONRPC2Response) JSONRPC2Error(com.thetransactioncompany.jsonrpc2.JSONRPC2Error) Map(java.util.Map) HashMap(java.util.HashMap)

Example 4 with JSONRPC2Response

use of com.thetransactioncompany.jsonrpc2.JSONRPC2Response in project i2pplus by vituperative.

the class AuthenticateHandler method process.

// Processes the requests
public JSONRPC2Response process(JSONRPC2Request req, MessageContext ctx) {
    if (req.getMethod().equals("Authenticate")) {
        JSONRPC2Error err = _helper.validateParams(requiredArgs, req, JSONRPC2Helper.USE_NO_AUTH);
        if (err != null)
            return new JSONRPC2Response(err, req.getID());
        Map<String, Object> inParams = req.getNamedParams();
        String pwd = (String) inParams.get("Password");
        // Try get an AuthToken
        AuthToken token = _secMan.validatePasswd(pwd);
        if (token == null) {
            return new JSONRPC2Response(JSONRPC2ExtendedError.INVALID_PASSWORD, req.getID());
        }
        Object api = inParams.get("API");
        err = validateAPIVersion(api);
        if (err != null)
            return new JSONRPC2Response(err, req.getID());
        Map<String, Object> outParams = new HashMap<String, Object>(4);
        outParams.put("Token", token.getId());
        outParams.put("API", I2PControlVersion.API_VERSION);
        return new JSONRPC2Response(outParams, req.getID());
    } else {
        // Method name not supported
        return new JSONRPC2Response(JSONRPC2Error.METHOD_NOT_FOUND, req.getID());
    }
}
Also used : HashMap(java.util.HashMap) JSONRPC2Response(com.thetransactioncompany.jsonrpc2.JSONRPC2Response) JSONRPC2Error(com.thetransactioncompany.jsonrpc2.JSONRPC2Error) AuthToken(net.i2p.i2pcontrol.security.AuthToken)

Example 5 with JSONRPC2Response

use of com.thetransactioncompany.jsonrpc2.JSONRPC2Response in project i2pplus by vituperative.

the class EchoHandler method process.

// Processes the requests
public JSONRPC2Response process(JSONRPC2Request req, MessageContext ctx) {
    if (req.getMethod().equals("Echo")) {
        JSONRPC2Error err = _helper.validateParams(requiredArgs, req);
        if (err != null)
            return new JSONRPC2Response(err, req.getID());
        Map<String, Object> inParams = req.getNamedParams();
        String echo = (String) inParams.get("Echo");
        Map<String, Object> outParams = new HashMap<String, Object>(4);
        outParams.put("Result", echo);
        return new JSONRPC2Response(outParams, req.getID());
    } else {
        // Method name not supported
        return new JSONRPC2Response(JSONRPC2Error.METHOD_NOT_FOUND, req.getID());
    }
}
Also used : HashMap(java.util.HashMap) JSONRPC2Response(com.thetransactioncompany.jsonrpc2.JSONRPC2Response) JSONRPC2Error(com.thetransactioncompany.jsonrpc2.JSONRPC2Error)

Aggregations

JSONRPC2Response (com.thetransactioncompany.jsonrpc2.JSONRPC2Response)31 JSONRPC2Error (com.thetransactioncompany.jsonrpc2.JSONRPC2Error)24 HashMap (java.util.HashMap)24 Map (java.util.Map)6 JSONObject (com.alibaba.fastjson.JSONObject)4 JSONRPC2Request (com.thetransactioncompany.jsonrpc2.JSONRPC2Request)4 InetAddress (java.net.InetAddress)3 UnknownHostException (java.net.UnknownHostException)3 ClientAppManager (net.i2p.app.ClientAppManager)3 AuthToken (net.i2p.i2pcontrol.security.AuthToken)3 Router (net.i2p.router.Router)3 RouterVersion (net.i2p.router.RouterVersion)3 ReseedChecker (net.i2p.router.networkdb.reseed.ReseedChecker)3 Rate (net.i2p.stat.Rate)3 RateStat (net.i2p.stat.RateStat)3 UpdateManager (net.i2p.update.UpdateManager)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1