Implemented supprto for authorization tokens in the client.
This commit is contained in:
@ -17,14 +17,15 @@ import com.thetransactioncompany.jsonrpc2.JSONRPC2Response;
|
||||
import com.thetransactioncompany.jsonrpc2.client.JSONRPC2Session;
|
||||
import com.thetransactioncompany.jsonrpc2.client.JSONRPC2SessionException;
|
||||
|
||||
|
||||
public class JSONInterface{
|
||||
public class JSONInterface {
|
||||
private static Log _log;
|
||||
private static ConfigurationManager _conf;
|
||||
private static String DEFAULT_PASSWORD = "itoopie";
|
||||
private static int nonce;
|
||||
private static final int MAX_NBR_RETRIES = 2;
|
||||
private static JSONRPC2Session session;
|
||||
|
||||
|
||||
private static String token;
|
||||
|
||||
static {
|
||||
_log = LogFactory.getLog(JSONInterface.class);
|
||||
_conf = ConfigurationManager.getInstance();
|
||||
@ -32,89 +33,154 @@ public class JSONInterface{
|
||||
nonce = rnd.nextInt();
|
||||
setupSession();
|
||||
}
|
||||
|
||||
private static synchronized int incrNonce(){
|
||||
|
||||
private static synchronized int incrNonce() {
|
||||
return ++nonce;
|
||||
}
|
||||
|
||||
public static void setupSession(){
|
||||
|
||||
public static void setupSession() {
|
||||
URL srvURL = null;
|
||||
String srvHost = _conf.getConf("server-hostname", "localhost");
|
||||
int srvPort = _conf.getConf("server-port", 7656);
|
||||
String srvTarget = _conf.getConf("server-target", "jsonrpc");
|
||||
try {
|
||||
srvURL = new URL("https://"+srvHost+":"+srvPort+"/"+srvTarget);
|
||||
} catch (MalformedURLException e){
|
||||
_log.error("Bad URL: https://"+srvHost+":"+srvPort+"/"+srvTarget, e);
|
||||
srvURL = new URL("https://" + srvHost + ":" + srvPort + "/"
|
||||
+ srvTarget);
|
||||
} catch (MalformedURLException e) {
|
||||
_log.error("Bad URL: https://" + srvHost + ":" + srvPort + "/"
|
||||
+ srvTarget, e);
|
||||
}
|
||||
session = new JSONRPC2Session(srvURL);
|
||||
session.trustAllCerts(true);
|
||||
}
|
||||
|
||||
|
||||
private static JSONRPC2Response sendReq(JSONRPC2Request req){
|
||||
JSONRPC2Response resp = null;
|
||||
try {
|
||||
resp = session.send(req);
|
||||
} catch (JSONRPC2SessionException e) {
|
||||
_log.error(req.toString(), e);
|
||||
}
|
||||
return resp;
|
||||
|
||||
private static JSONRPC2Response sendReq(JSONRPC2Request req)
|
||||
throws InvalidPasswordException, UnrecoverableFailedRequestException{
|
||||
return sendReq(req, 1);
|
||||
}
|
||||
|
||||
private static JSONRPC2Response sendReq(JSONRPC2Request req, int tryNbr)
|
||||
throws InvalidPasswordException, UnrecoverableFailedRequestException{
|
||||
if (tryNbr <= MAX_NBR_RETRIES){
|
||||
HashMap outParams = (HashMap) req.getParams();
|
||||
outParams.put("token", token); // Add authentication token
|
||||
req.setParams(outParams);
|
||||
|
||||
JSONRPC2Response resp = null;
|
||||
try {
|
||||
resp = session.send(req);
|
||||
JSONRPC2Error err = resp.getError();
|
||||
if (err != null) {
|
||||
switch (err.getCode()) {
|
||||
case -32700:
|
||||
// Parse error
|
||||
break;
|
||||
case -32600:
|
||||
// Invalid request
|
||||
break;
|
||||
case -32601:
|
||||
// Method not found
|
||||
break;
|
||||
case -32602:
|
||||
// Invalid params
|
||||
break;
|
||||
case -32603:
|
||||
// Internal error
|
||||
break;
|
||||
|
||||
// Custom errors (as defined by the I2PControl API)
|
||||
case -32001:
|
||||
// Invalid password
|
||||
throw new InvalidPasswordException();
|
||||
// break;
|
||||
case -32002:
|
||||
// No token
|
||||
token = getNewToken();
|
||||
throw new FailedRequestException();
|
||||
// break;
|
||||
case -32003:
|
||||
// Invalid token
|
||||
token = getNewToken();
|
||||
throw new FailedRequestException();
|
||||
//break;
|
||||
case -32004:
|
||||
// Token expired
|
||||
token = getNewToken();
|
||||
throw new FailedRequestException();
|
||||
// break;
|
||||
}
|
||||
}
|
||||
} catch (JSONRPC2SessionException e) {
|
||||
_log.error(req.toString(), e);
|
||||
} catch (FailedRequestException e) {
|
||||
// Try sending the same request again with a current authentication token.
|
||||
return sendReq(req, ++tryNbr);
|
||||
}
|
||||
return resp;
|
||||
} else {
|
||||
throw new UnrecoverableFailedRequestException(); // Max retries reached. Throw exception.
|
||||
}
|
||||
}
|
||||
|
||||
private static synchronized String getNewToken()
|
||||
throws InvalidPasswordException {
|
||||
System.out.println("getNewToken()");
|
||||
JSONRPC2Request req = new JSONRPC2Request("authenticate", incrNonce());
|
||||
|
||||
Map outParams = new HashMap();
|
||||
outParams.put("password",
|
||||
_conf.getConf("server.password", DEFAULT_PASSWORD));
|
||||
req.setParams(outParams);
|
||||
|
||||
JSONRPC2Response resp = null;
|
||||
try {
|
||||
resp = sendReq(req);
|
||||
}catch (UnrecoverableFailedRequestException e) {
|
||||
return null; // Shouldn't normaly happen.
|
||||
}
|
||||
Map inParams = (HashMap) resp.getResult();
|
||||
return (String) inParams.get("token");
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static Double getRateStat(String stat, long period) throws JSONRPC2Error{
|
||||
|
||||
public static Double getRateStat(String stat, long period)
|
||||
throws InvalidPasswordException {
|
||||
|
||||
JSONRPC2Request req = new JSONRPC2Request("getRate", incrNonce());
|
||||
@SuppressWarnings("rawtypes")
|
||||
Map params = new HashMap();
|
||||
params.put("stat", stat);
|
||||
params.put("period", period);
|
||||
req.setParams(params);
|
||||
|
||||
JSONRPC2Response resp = sendReq(req);
|
||||
if (resp.indicatesSuccess()){
|
||||
Map inParams = (HashMap)resp.getResult();
|
||||
return (Double) inParams.get("result");
|
||||
} else {
|
||||
throw resp.getError();
|
||||
}
|
||||
|
||||
JSONRPC2Response resp = null;
|
||||
try {
|
||||
resp = sendReq(req);
|
||||
}catch (UnrecoverableFailedRequestException e) {
|
||||
_log.error("getRateStat failed.", e);
|
||||
}
|
||||
|
||||
Map inParams = (HashMap) resp.getResult();
|
||||
return (Double) inParams.get("result");
|
||||
}
|
||||
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static String getEcho(String str) throws JSONRPC2Error{
|
||||
|
||||
public static String getEcho(String str) throws InvalidPasswordException {
|
||||
|
||||
JSONRPC2Request req = new JSONRPC2Request("echo", incrNonce());
|
||||
@SuppressWarnings("rawtypes")
|
||||
Map params = new HashMap();
|
||||
params.put("echo", str);
|
||||
req.setParams(params);
|
||||
|
||||
JSONRPC2Response resp = sendReq(req);
|
||||
if (resp.indicatesSuccess()){
|
||||
Map inParams = (HashMap)resp.getResult();
|
||||
return (String) inParams.get("result");
|
||||
} else {
|
||||
throw resp.getError();
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static String getServerCert(String str) throws JSONRPC2Error{
|
||||
|
||||
JSONRPC2Request req = new JSONRPC2Request("echo", incrNonce());
|
||||
@SuppressWarnings("rawtypes")
|
||||
Map params = new HashMap();
|
||||
params.put("echo", str);
|
||||
req.setParams(params);
|
||||
|
||||
JSONRPC2Response resp = sendReq(req);
|
||||
if (resp.indicatesSuccess()){
|
||||
Map inParams = (HashMap)resp.getResult();
|
||||
return (String) inParams.get("serverCert");
|
||||
} else {
|
||||
throw resp.getError();
|
||||
}
|
||||
|
||||
JSONRPC2Response resp = null;
|
||||
try {
|
||||
resp = sendReq(req);
|
||||
} catch (UnrecoverableFailedRequestException e) {
|
||||
_log.error("getEcho failed.", e);
|
||||
}
|
||||
System.out.println("Response: " + resp.toJSON().toJSONString());
|
||||
Map inParams = (HashMap) resp.getResult();
|
||||
return (String) inParams.get("result");
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user