Search in sources :

Example 1 with JHttpContext

use of j.http.JHttpContext in project JFramework by gugumall.

the class SSOClient method tellServerToLogoutUser.

/**
 * @param requestURL
 * @param globalSessionId
 * @param userId
 * @throws Exception
 */
public static void tellServerToLogoutUser(String globalSessionId, String userId) throws Exception {
    if (globalSessionId == null && userId == null)
        return;
    Client client = SSOConfig.getSsoClientByIdOrUrl(SysConfig.getSysId());
    String time = SysUtil.getNow() + "";
    String md5Key = JUtilMD5.MD5EncodeToHex(client.getPassport() + time + globalSessionId + userId);
    String logout = SSOConfig.getSsoServer() + "ssoserver" + Handlers.getActionPathPattern();
    logout += "?" + Handlers.getHandler("/ssoserver").getRequestBy() + "=ssologoutuser";
    logout += "&" + Constants.SSO_CLIENT + "=" + client.getUrlDefault();
    logout += "&" + Constants.SSO_MD5_STRING + "=" + md5Key;
    logout += "&" + Constants.SSO_TIME + "=" + time;
    logout += "&" + Constants.SSO_GLOBAL_SESSION_ID + "=" + globalSessionId;
    logout += "&" + Constants.SSO_USER_ID + "=" + userId;
    logout += "&" + Constants.SSO_PASSPORT + "=" + Permission.getSSOPassport();
    JHttpContext context = http.get(null, hclient, logout);
    String _response = context.getStatus() == 200 ? context.getResponseText() : null;
    context.finalize();
    context = null;
    log.log("tellServerToLogoutUser - " + _response, -1);
}
Also used : JHttpContext(j.http.JHttpContext) JUtilString(j.util.JUtilString) HttpClient(org.apache.http.client.HttpClient)

Example 2 with JHttpContext

use of j.http.JHttpContext in project JFramework by gugumall.

the class SSOConfig method tellServerToAddUrl.

/**
 * @param clientId
 * @param url
 * @throws Exception
 */
public static void tellServerToAddUrl(String clientId, String url) throws Exception {
    if (clientId == null && url == null)
        return;
    try {
        String addUrl = SSOConfig.getSsoServer() + "ssoserver" + Handlers.getActionPathPattern();
        addUrl += "?" + Handlers.getHandler("/ssoserver").getRequestBy() + "=ssoaddurl";
        addUrl += "&" + Constants.SSO_CLIENT + "=" + clientId;
        addUrl += "&url=" + JUtilString.encodeURI(url, SysConfig.sysEncoding);
        addUrl += "&" + Constants.SSO_PASSPORT + "=" + Permission.getSSOPassport();
        // log.log("tellServerToAddUrl on url - "+addUrl,-1);
        int loop = 0;
        String _response = "";
        while (!Constants.RESPONSE_OK.equals(_response) && loop < 3) {
            JHttpContext context = http.get(null, hclient, addUrl);
            _response = context.getStatus() == 200 ? context.getResponseText() : null;
            context.finalize();
            context = null;
            loop++;
            if (!Constants.RESPONSE_OK.equals(_response)) {
                try {
                    Thread.sleep(3000);
                } catch (Exception e) {
                }
            }
        }
        log.log("tellServerToAddUrl - " + _response, Logger.LEVEL_DEBUG);
    } catch (Exception ex) {
        log.log(ex, Logger.LEVEL_ERROR);
    }
}
Also used : JHttpContext(j.http.JHttpContext) JUtilString(j.util.JUtilString)

Example 3 with JHttpContext

use of j.http.JHttpContext in project JFramework by gugumall.

the class SSONotifier method logout.

/**
 * 通知某个sso client注销全部用户
 */
private void logout(Client client) {
    long now = SysUtil.getNow();
    String md5 = JUtilMD5.MD5EncodeToHex(client.getPassport() + now);
    String url = client.getUrlDefault() + client.getLogoutInterface();
    if (url.indexOf("?") > 0) {
        url += "&" + Constants.SSO_MD5_STRING + "=" + md5;
    } else {
        url += "?" + Constants.SSO_MD5_STRING + "=" + md5;
    }
    url += "&" + Constants.SSO_TIME + "=" + now;
    url += "&" + Constants.SSO_PASSPORT + "=" + Permission.getSSOPassport();
    int loop = 0;
    while (loop < 3) {
        // 最多尝试3次
        loop++;
        try {
            JHttpContext context = http.get(null, httpClient, url);
            String response = context.getStatus() == 200 ? context.getResponseText() : null;
            context.finalize();
            context = null;
            log.log("notify " + url + ",client response:" + response, Logger.LEVEL_DEBUG);
            break;
        } catch (Exception e) {
            log.log("failed to notify " + url, Logger.LEVEL_ERROR);
            log.log(e, Logger.LEVEL_ERROR);
        }
    }
}
Also used : JHttpContext(j.http.JHttpContext)

Example 4 with JHttpContext

use of j.http.JHttpContext in project JFramework by gugumall.

the class JUtilTimestamp method run.

/*
	 *  (non-Javadoc)
	 * @see java.lang.Runnable#run()
	 */
public void run() {
    JHttp jhttp = JHttp.getInstance();
    int loop = -1;
    boolean done = false;
    while (true) {
        try {
            Thread.sleep(1000);
        } catch (Exception e) {
        }
        if (!done && (loop == -1 || loop == 30)) {
            try {
                JHttpContext context = new JHttpContext();
                context.addRequestHeader("Referer", "http://bjtime.cn/");
                context = jhttp.get(context, null, "http://bjtime.cn/header6.asp");
                String response = context != null && context.getStatus() == 200 ? context.getResponseText() : null;
                if (response != null) {
                    String t = "";
                    int start = response.indexOf("nyear=");
                    int end = -1;
                    if (start > 0) {
                        end = response.indexOf(";", start);
                        t += response.substring(start + 6, end) + "-";
                        start = response.indexOf("nmonth=");
                        end = response.indexOf(";", start);
                        String s = response.substring(start + 7, end);
                        if (s.length() == 1)
                            s = "0" + s;
                        t += s + "-";
                        start = response.indexOf("nday=");
                        end = response.indexOf(";", start);
                        s = response.substring(start + 5, end);
                        if (s.length() == 1)
                            s = "0" + s;
                        t += s + " ";
                        start = response.indexOf("nhrs=");
                        end = response.indexOf(";", start);
                        s = response.substring(start + 5, end);
                        if (s.length() == 1)
                            s = "0" + s;
                        t += s + ":";
                        start = response.indexOf("nmin=");
                        end = response.indexOf(";", start);
                        s = response.substring(start + 5, end);
                        if (s.length() == 1)
                            s = "0" + s;
                        t += s + ":";
                        start = response.indexOf("nsec=");
                        end = response.indexOf(";", start);
                        s = response.substring(start + 5, end);
                        if (s.length() == 1)
                            s = "0" + s;
                        t += s;
                        bjTime = Timestamp.valueOf(t).getTime() - System.currentTimeMillis();
                        done = true;
                        System.out.println("get bj time from bjtime.cn: " + bjTime + ", local time: " + System.currentTimeMillis());
                    }
                }
            } catch (Exception e) {
            // e.printStackTrace();
            }
            loop = 0;
        }
        loop++;
    }
}
Also used : JHttpContext(j.http.JHttpContext) JHttp(j.http.JHttp)

Example 5 with JHttpContext

use of j.http.JHttpContext in project JFramework by gugumall.

the class RegionGetter method fromBaidu.

/**
 * @throws Exception
 */
private static void fromBaidu() throws Exception {
    JHttp http = JHttp.getInstance();
    HttpClient client = http.createClient();
    String continentId = "0";
    String countryId = "1";
    String provinceId = null;
    String cityId = null;
    String countyId = null;
    JUtilTextWriter log = new JUtilTextWriter(new File("F:\\work\\JFramework_v2.0\\doc\\regions.sql"), "UTF-8");
    log.addLine("use jframework;");
    String s = JDFSFile.read(new File("F:\\work\\JFramework_v2.0\\doc\\淘宝地域\\tdist_py.js"), "GBK");
    String[] ls = JUtilString.getTokens(s, "\n");
    for (int i = 0; i < ls.length; i++) {
        if (ls[i].indexOf(":[") < 0)
            continue;
        String id = ls[i].substring(0, ls[i].indexOf(":["));
        id = JUtilString.replaceAll(id, "'", "");
        String info = ls[i].substring(ls[i].indexOf(":[") + 2, ls[i].indexOf("]"));
        info = JUtilString.replaceAll(info, "'", "");
        String[] cells = info.split(",");
        String name = cells[0];
        String pid = cells[1];
        String nameEn = cells[2];
        int idi = Integer.parseInt(id);
        // 外国城市不要
        if (idi >= 1000 && idi < 110000)
            continue;
        // 国家
        if (idi < 1000) {
            log.addLine("insert into j_country values ('" + id + "','" + pid + "','','" + name + "','" + JUtilString.toZhTw(name) + "','" + nameEn + "','',0,'T');");
            System.out.println("insert into j_country values ('" + id + "','" + pid + "','','" + name + "','" + JUtilString.toZhTw(name) + "','" + nameEn + "','',0,'T');");
        } else if (id.endsWith("0000")) {
            // 省
            if (provinceId == null || !provinceId.equals(id)) {
                provinceId = id;
            }
            String nameShort = name;
            nameShort = nameShort.replaceAll("市", "");
            nameShort = nameShort.replaceAll("维吾尔自治区", "");
            nameShort = nameShort.replaceAll("壮族自治区", "");
            nameShort = nameShort.replaceAll("回族自治区", "");
            nameShort = nameShort.replaceAll("自治区", "");
            log.addLine(" insert into j_province values ('" + id + "','" + pid + "','" + continentId + "','" + name + "','" + nameShort + "','" + JUtilString.toZhTw(name) + "','" + nameEn + "','',0,'','T');");
            System.out.println(" insert into j_province values ('" + id + "','" + pid + "','" + continentId + "','" + name + "','" + nameShort + "','" + JUtilString.toZhTw(name) + "','" + nameEn + "','',0,'','T');");
        } else if (pid.equals(provinceId)) {
            if (cityId == null || !cityId.equals(id)) {
                cityId = id;
            }
            String areaCode = "";
            String postCode = "";
            String[] data = (String[]) codes.get(id);
            if (data != null) {
                areaCode = data[0];
                postCode = data[1];
            }
            log.addLine("  insert into j_city values ('" + id + "','" + pid + "','" + countryId + "','" + continentId + "','" + name + "','" + JUtilString.toZhTw(name) + "','" + nameEn + "','" + areaCode + "',0,'" + postCode + "','T');");
            System.out.println("  insert into j_city values ('" + id + "','" + pid + "','" + countryId + "','" + continentId + "','" + name + "','" + JUtilString.toZhTw(name) + "','" + nameEn + "','" + areaCode + "',0,'" + postCode + "','T');");
        } else {
            if (countyId == null || !countyId.equals(id)) {
                countyId = id;
            }
            String areaCode = "";
            String postCode = "";
            String[] data = (String[]) codes.get(id);
            if (data != null) {
                areaCode = data[0];
                postCode = data[1];
            }
            log.addLine("   insert into j_county values ('" + id + "','" + pid + "','" + provinceId + "','" + countryId + "','" + continentId + "','" + name + "','" + JUtilString.toZhTw(name) + "','" + nameEn + "','" + areaCode + "',0,'" + postCode + "','T');");
            System.out.println("   insert into j_county values ('" + id + "','" + pid + "','" + provinceId + "','" + countryId + "','" + continentId + "','" + name + "','" + JUtilString.toZhTw(name) + "','" + nameEn + "','" + areaCode + "',0,'" + postCode + "','T');");
            JHttpContext context = new JHttpContext();
            context.addRequestHeader("Referer", "http://buy.taobao.com/auction/buy_now.jhtml");
            String zones = http.postResponse(context, client, "http://lsp.wuliu.taobao.com/locationservice/addr/output_address_town.do?l1=" + provinceId + "&l2=" + cityId + "&l3=" + countyId + "&_ksTS=1396014988093_459&callback=jsonp460", null, "UTF-8");
            zones = zones.substring(zones.indexOf("result:{") + "result:{".length());
            zones = JUtilString.replaceAll(zones, "],", "],\r\n");
            String[] zs = JUtilString.getTokens(zones, "\r\n");
            for (int j = 0; j < zs.length; j++) {
                if (zs[j].indexOf(":[") < 0)
                    continue;
                String id2 = zs[j].substring(0, zs[j].indexOf(":["));
                id2 = JUtilString.replaceAll(id2, "'", "");
                String info2 = zs[j].substring(zs[j].indexOf(":[") + 2, zs[j].indexOf("]"));
                info2 = JUtilString.replaceAll(info2, "'", "");
                String[] cells2 = info2.split(",");
                String name2 = cells2[0];
                String nameEn2 = cells2[2];
                log.addLine("    insert into j_zone values ('" + id2 + "','" + countyId + "','" + cityId + "','" + provinceId + "','" + countryId + "','" + continentId + "','" + name2 + "','" + JUtilString.toZhTw(name2) + "','" + nameEn2 + "','',0,'','T');");
                System.out.println("    insert into j_zone values ('" + id2 + "','" + countyId + "','" + cityId + "','" + provinceId + "','" + countryId + "','" + continentId + "','" + name2 + "','" + JUtilString.toZhTw(name2) + "','" + nameEn2 + "','',0,'','T');");
            }
        }
    }
    System.exit(0);
}
Also used : JUtilTextWriter(j.util.JUtilTextWriter) JHttpContext(j.http.JHttpContext) JHttp(j.http.JHttp) HttpClient(org.apache.http.client.HttpClient) JUtilString(j.util.JUtilString) JDFSFile(j.fs.JDFSFile) File(java.io.File)

Aggregations

JHttpContext (j.http.JHttpContext)26 JUtilString (j.util.JUtilString)16 RemoteException (java.rmi.RemoteException)8 JHttp (j.http.JHttp)6 HttpClient (org.apache.http.client.HttpClient)5 HashMap (java.util.HashMap)4 ConcurrentMap (j.util.ConcurrentMap)3 Map (java.util.Map)2 InitialContext (javax.naming.InitialContext)2 Client (j.app.sso.Client)1 JObject (j.common.JObject)1 JDFSFile (j.fs.JDFSFile)1 ServiceBase (j.service.server.ServiceBase)1 ConcurrentList (j.util.ConcurrentList)1 JUtilTextWriter (j.util.JUtilTextWriter)1 File (java.io.File)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Properties (java.util.Properties)1 Context (javax.naming.Context)1