package foundation.icall.callout; import java.io.IOException; import java.util.Date; import foundation.action.ActionContext; import foundation.icall.ICall; import foundation.util.Util; public class NCCSource extends HttpServerSource { private static int TimeOutMinute = 20; public Date lastTime; private String secret; private String clientId; private String signature; private String token; @Override public void login(ActionContext context, ICall iCall) throws Exception { if (!tokenExpired()) { return; } //1. 生成 secret 及 sing createSecretAndSign(); //2. 登录服务器, 获取 token getToken(); } @Override public void logout(ActionContext context, ICall iCall) { } @Override public ICallRequest createRequest(String url) { // TODO Auto-generated method stub return null; } private boolean tokenExpired() { if (Util.isEmpty(token)) { return true; } Date now = new Date(); boolean result = lastTime.getTime() - now.getTime() >= TimeOutMinute * 60 * 1000; return result; } private void createSecretAndSign() throws Exception { String publicKey = meta.getString("publicKey"); String clientSecret = meta.getString("clientSecret"); clientId = meta.getString("clientId"); // secret = Encryption.pubEncrypt(publicKey, clientSecret); signature = clientId + clientSecret + publicKey; // signature = SHA256Util.getSHA256(signature, publicKey); } private void getToken() throws IOException { String url = meta.getString("url"); String bizCenter = meta.getString("bizCenter"); String userCode = meta.getString("userCode"); ICallRequest request = new ICallRequest(url); request.addOneParam("grant_type", "client_credentials"); request.addOneParam("client_id", clientId); request.addOneParam("client_secret", secret); request.addOneParam("signature", signature); request.addOneParam("biz_center", bizCenter); request.addOneParam("usercode", userCode); // token = post(request, ResponseType.String); } @Override public String getName() { // TODO Auto-generated method stub return null; } }