Search in sources :

Example 6 with JHttpContext

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

the class Temp method main.

/**
 * @param args
 * @throws Exception
 */
public static void main(String[] args) throws Exception {
    // String s="{\"method\":\"add\", \"params\": [1,2], \"id\": 1}";
    String s = "{\"method\":\"getdifficulty\", \"id\": 1}";
    JHttpContext context = new JHttpContext();
    JHttp http = JHttp.getInstance();
    HttpClient client = http.createClient("112.74.65.173", 8332, "http", "gugucoinuser", "gugucoinpass");
    // context.setRequestBody(s);
    s = http.postResponse(context, null, "http://112.74.65.173:8332/", null);
    // URL: http://user:pass@localhost:8332
    System.out.println(s);
}
Also used : JHttpContext(j.http.JHttpContext) JHttp(j.http.JHttp) HttpClient(org.apache.http.client.HttpClient)

Example 7 with JHttpContext

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

the class DB method setReadable.

/**
 * @param dbname
 * @param mirrorUuid
 * @param readable
 * @throws Exception
 */
public static void setReadable(String dbname, String mirrorUuid, boolean readable) throws Exception {
    Database db = DB.database(dbname);
    DBMirror mirror = db.mirror(mirrorUuid);
    String url = mirror.monitor;
    if (url.indexOf("?") > 0)
        url += "&request=setReadable";
    else
        url += "?request=setReadable";
    url += "&db_name=" + dbname;
    url += "&mirror_uuid=" + mirrorUuid;
    url += "&readable=" + readable;
    // System.out.println(url);
    JHttpContext context = jhttp.get(null, jclient, url, SysConfig.sysEncoding);
    if (context == null || context.getStatus() != 200 || Constants.AUTH_FAILED.equals(context.getResponseText()) || Constants.INVOKING_FAILED.equals(context.getResponseText())) {
        if (context != null) {
            context.finalize();
            context = null;
        }
        throw new Exception("failed to setReadable of - " + dbname + "," + mirrorUuid + "," + readable + " on " + url);
    }
    context.finalize();
    context = null;
}
Also used : JHttpContext(j.http.JHttpContext) JUtilString(j.util.JUtilString)

Example 8 with JHttpContext

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

the class RouterAgent method serviceNodesHttp.

/**
 * @param jhttp
 * @param client
 * @param clientUuid
 * @param code
 * @param md54Routing
 * @return
 * @throws Exception
 */
protected String[] serviceNodesHttp(JHttp jhttp, HttpClient client, String clientUuid, String code, String md54Routing) throws Exception {
    String cacheKey = "http|" + clientUuid + "|" + code + "|nodes";
    if (cache.containsKey(cacheKey)) {
        return (String[]) cache.get(cacheKey);
    }
    if (this.routerConfig.getHttp() == null) {
        throw new Exception("router(http) is not supported.");
    }
    if (!routerHttpAvailable) {
        throw new Exception("router(http) " + routerConfig.getUuid() + " is unavailable.");
    }
    String url = this.routerConfig.getHttp().getEntrance();
    if (url.indexOf("?") > 0)
        url += "&request=getAllServiceNodeAvailable";
    else
        url += "?request=getAllServiceNodeAvailable";
    url += "&" + Constants.JSERVICE_PARAM_CLIENT_UUID + "=" + clientUuid;
    url += "&" + Constants.JSERVICE_PARAM_SERVICE_CODE + "=" + code;
    url += "&" + Constants.JSERVICE_PARAM_MD5_STRING_4ROUTER + "=" + md54Routing;
    if (jhttp == null)
        jhttp = JHttp.getInstance();
    JHttpContext context = null;
    try {
        context = jhttp.get(null, client, url);
    } catch (Exception e) {
        throw e;
    }
    if (context != null && context.getStatus() != 200) {
        throw new Exception("failed to get http channel, the http response code is - " + context.getStatus());
    }
    String response = context.getResponseText();
    context.finalize();
    context = null;
    if (Constants.AUTH_FAILED.equals(response)) {
        throw new Exception("failed to get http channel due to - " + Constants.AUTH_FAILED);
    } else if (Constants.SERVICE_NOT_FOUND.equals(response)) {
        throw new Exception("failed to get http channel due to - " + Constants.SERVICE_NOT_FOUND);
    } else if (Constants.SERVICE_NOT_AVAIL.equals(response)) {
        throw new Exception("failed to get http channel due to - " + Constants.SERVICE_NOT_AVAIL);
    }
    String[] channels = (String[]) JObject.string2Serializable(response);
    cache.put(cacheKey, channels);
    return channels;
}
Also used : JHttpContext(j.http.JHttpContext) JUtilString(j.util.JUtilString) RemoteException(java.rmi.RemoteException)

Example 9 with JHttpContext

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

the class RouterAgent method serviceHttp.

/**
 * @param jhttp
 * @param client
 * @param clientUuid
 * @param code
 * @param md54Routing
 * @return
 * @throws Exception
 */
protected String serviceHttp(JHttp jhttp, HttpClient client, String clientUuid, String code, String md54Routing) throws Exception {
    String cacheKey = "http|" + clientUuid + "|" + code;
    if (cache.containsKey(cacheKey)) {
        return (String) cache.get(cacheKey);
    }
    if (this.routerConfig.getHttp() == null) {
        throw new Exception("router(http) is not supported.");
    }
    if (!routerHttpAvailable) {
        throw new Exception("router(http) " + routerConfig.getUuid() + " is unavailable.");
    }
    String url = this.routerConfig.getHttp().getEntrance();
    if (url.indexOf("?") > 0)
        url += "&request=service";
    else
        url += "?request=service";
    url += "&" + Constants.JSERVICE_PARAM_CLIENT_UUID + "=" + clientUuid;
    url += "&" + Constants.JSERVICE_PARAM_SERVICE_CODE + "=" + code;
    url += "&" + Constants.JSERVICE_PARAM_MD5_STRING_4ROUTER + "=" + md54Routing;
    if (jhttp == null)
        jhttp = JHttp.getInstance();
    JHttpContext context = null;
    try {
        context = jhttp.get(null, client, url);
    } catch (Exception e) {
        throw e;
    }
    if (context != null && context.getStatus() != 200) {
        throw new Exception("failed to get http channel, the http response code is - " + context.getStatus());
    }
    String response = context.getResponseText();
    context.finalize();
    context = null;
    // if(Constants.AUTH_FAILED.equals(response)){
    // throw new Exception("failed to get http channel due to - "+Constants.AUTH_FAILED);
    // }else if(Constants.SERVICE_NOT_FOUND.equals(response)){
    // throw new Exception("failed to get http channel due to - "+Constants.SERVICE_NOT_FOUND);
    // }else if(Constants.SERVICE_NOT_AVAIL.equals(response)){
    // throw new Exception("failed to get http channel due to - "+Constants.SERVICE_NOT_AVAIL);
    // }
    cache.put(cacheKey, response);
    return response;
}
Also used : JHttpContext(j.http.JHttpContext) JUtilString(j.util.JUtilString) RemoteException(java.rmi.RemoteException)

Example 10 with JHttpContext

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

the class Client method httpCallPost.

/**
 * @param jhttp
 * @param client
 * @param code
 * @param entrance
 * @param methodName
 * @param params
 * @return
 * @throws Exception
 */
public static String httpCallPost(JHttp jhttp, HttpClient client, String code, String entrance, String methodName, Map params) throws Exception {
    String url = entrance;
    if (url.indexOf("?") > 0)
        url += "&request=" + methodName;
    else
        url += "?request=" + methodName;
    if (params == null)
        params = new HashMap();
    String md54Service = md54Service(code, methodName);
    params.put(Constants.JSERVICE_PARAM_CLIENT_UUID, Manager.getClientNodeUuid());
    params.put(Constants.JSERVICE_PARAM_MD5_STRING_4SERVICE, md54Service);
    try {
        if (jhttp == null)
            jhttp = JHttp.getInstance();
        JHttpContext context = jhttp.post(null, client, url, params, SysConfig.sysEncoding);
        if (context == null || context.getStatus() != 200 || Constants.INVOKING_FAILED.equals(context.getResponseText())) {
            if (context != null) {
                context.finalize();
                context = null;
            }
            throw new Exception("failed to call service on " + url);
        }
        String responseText = context.getResponseText();
        context.finalize();
        context = null;
        return responseText;
    } catch (Exception ex) {
        log.log(ex, Logger.LEVEL_ERROR);
        throw ex;
    }
}
Also used : JHttpContext(j.http.JHttpContext) HashMap(java.util.HashMap) RemoteException(java.rmi.RemoteException)

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