From 26cba32a91da999c0008b7ed29e03f564458a315 Mon Sep 17 00:00:00 2001 From: dev Date: Thu, 21 Jul 2011 15:43:10 +0000 Subject: [PATCH] Added missing class. --- .../i2pcontrol/methods/SetI2PControl.java | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/net/i2p/itoopie/i2pcontrol/methods/SetI2PControl.java diff --git a/src/net/i2p/itoopie/i2pcontrol/methods/SetI2PControl.java b/src/net/i2p/itoopie/i2pcontrol/methods/SetI2PControl.java new file mode 100644 index 000000000..39548f0b9 --- /dev/null +++ b/src/net/i2p/itoopie/i2pcontrol/methods/SetI2PControl.java @@ -0,0 +1,66 @@ +package net.i2p.itoopie.i2pcontrol.methods; + +import java.util.EnumMap; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; +import java.util.Map.Entry; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import net.i2p.itoopie.i2pcontrol.InvalidParametersException; +import net.i2p.itoopie.i2pcontrol.InvalidPasswordException; +import net.i2p.itoopie.i2pcontrol.JSONRPC2Interface; +import net.i2p.itoopie.i2pcontrol.UnrecoverableFailedRequestException; +import net.i2p.itoopie.i2pcontrol.methods.I2PControl.I2P_CONTROL; + +import com.thetransactioncompany.jsonrpc2.JSONRPC2Request; +import com.thetransactioncompany.jsonrpc2.JSONRPC2Response; +import com.thetransactioncompany.jsonrpc2.client.JSONRPC2SessionException; + +public class SetI2PControl { + private final static Log _log = LogFactory.getLog(SetI2PControl.class); + + + public static EnumMap execute(Map settings) + throws InvalidPasswordException, JSONRPC2SessionException, InvalidParametersException{ + + JSONRPC2Request req = new JSONRPC2Request("I2PControl", JSONRPC2Interface.incrNonce()); + + Map outParams = new HashMap(); + + Set> set = settings.entrySet(); + for (Entry e : set){ + if(e.getKey().isWritable()){ + outParams.put(e.getKey().toString(), e.getValue()); + } + } + req.setParams(outParams); + + JSONRPC2Response resp = null; + try { + resp = JSONRPC2Interface.sendReq(req); + HashMap map = (HashMap) resp.getResult(); + if (map != null){ + Set inputSet = map.entrySet(); + EnumMap output = new EnumMap(I2P_CONTROL.class); + // Present the result as an map. + for (Entry e: inputSet){ + String key = (String) e.getKey(); + I2P_CONTROL I2PC = I2PControl.getEnum(key); + // If the enum exists. They should exists, but safety first. + if (I2PC != null){ + output.put(I2PC, e.getValue()); + } + } + return output; + } else { + return new EnumMap(I2P_CONTROL.class); + } + } catch (UnrecoverableFailedRequestException e) { + _log.error("setI2PControl failed.", e); + } + return null; + } +}