Search in sources :

Example 1 with Response

use of org.nutz.http.Response in project nutzboot by nutzam.

the class CloudConfig method bySimple.

protected static void bySimple(PropertiesProxy conf, NutMap params, String host, String fileName) {
    String url = String.format("http://%s/%s/%s/%s/%s", host, params.get("zone"), params.get("id"), params.get("label"), fileName);
    try {
        Request req = Request.create(url, METHOD.GET);
        // long now = System.currentTimeMillis();
        // String once = R.UU32();
        // String sign = Lang.sha1(String.format("%s,%s,%s,%s,%s", params.get("zone"), params.get("id"), now, params.get("token"), once));
        // req.getHeader().set("X-Client-Once", once);
        // req.getHeader().set("X-Client-Time", now + "");
        // req.getHeader().set("X-Client-Sign", sign);
        req.getHeader().set("Authorization", Base64.encodeToString((params.get("zone") + ":" + params.getString("password")).getBytes(), false));
        Response resp = Sender.create(req).setTimeout(3000).send();
        if (resp.isOK()) {
            Logs.get().debug("load from " + url);
            conf.load(resp.getReader());
            return;
        } else {
            Logs.get().warnf("url=%s code=%s", url, resp.getStatus());
        }
    } catch (Exception e) {
        Logs.get().warn("url=" + url, e);
    }
    throw new RuntimeException("FAILED url=" + url);
}
Also used : Response(org.nutz.http.Response) Request(org.nutz.http.Request) IOException(java.io.IOException)

Example 2 with Response

use of org.nutz.http.Response in project nutzcloud by nutzam.

the class LoachClient method _ping.

protected boolean _ping() {
    try {
        String pingURL = url + "/ping/" + getServiceName() + "/" + id;
        if (isDebug())
            log.debug("Ping URL=" + pingURL);
        Request req = Request.create(pingURL, METHOD.GET);
        req.getHeader().clear();
        req.getHeader().set("If-Not-Match", lastPingETag);
        Response resp = Sender.create(req, conf.getInt("loach.client.ping.timeout", 1000)).setConnTimeout(1000).send();
        String cnt = resp.getContent();
        if (isDebug())
            log.debug("Ping result : " + cnt);
        if (resp.isOK()) {
            lastPingETag = Strings.sBlank(resp.getHeader().get("ETag"), "ABC");
            NutMap re = Json.fromJson(NutMap.class, cnt);
            if (re != null && re.getBoolean("ok", false))
                return true;
        } else if (resp.getStatus() == 304) {
            return true;
        }
    } catch (Throwable e) {
        log.debugf("bad url? %s %s", url, e.getMessage());
    }
    return false;
}
Also used : Response(org.nutz.http.Response) Request(org.nutz.http.Request) NutMap(org.nutz.lang.util.NutMap)

Example 3 with Response

use of org.nutz.http.Response in project nutz by nutzam.

the class JsonRPC method mapper.

/**
 * 客户端. 用于生成一个代理接口的实例,透明访问json-rpc服务
 * @param klass 需要代理的接口
 * @param endpoint jsonrpc URL入口
 * @param namespace 命名空间,非json-rpc标准,扩展用,不需要就传null
 * @param timeout 超时设置,若永不超时,设置为-1
 * @return 代理实例
 */
public static <T> T mapper(Class<T> klass, final String endpoint, final String namespace, final int timeout) {
    return (T) Proxy.newProxyInstance(klass.getClassLoader(), new Class<?>[] { klass }, new InvocationHandler() {

        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            NutMap jreq = new NutMap();
            jreq.setv("jsonrpc", "2.0").setv("id", R.UU32()).setv("method", method.getName());
            if (!Strings.isBlank(namespace)) {
                jreq.put("namespace", namespace);
            }
            jreq.setv("params", args);
            Request req = Request.create(endpoint, METHOD.POST);
            req.setData(Json.toJson(jreq));
            Response resp = Sender.create(req).setTimeout(timeout).send();
            if (resp.isOK()) {
                if (method.getReturnType() == Void.class)
                    return null;
                return Json.fromJson(method.getGenericReturnType(), resp.getReader());
            }
            throw new RuntimeException("resp code=" + resp.getStatus());
        }
    });
}
Also used : Response(org.nutz.http.Response) Request(org.nutz.http.Request) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) NutMap(org.nutz.lang.util.NutMap)

Example 4 with Response

use of org.nutz.http.Response in project nutz by nutzam.

the class UploadTest method test_upload.

@Test
public void test_upload() throws Throwable {
    Request req = Request.create(getBaseURL() + "/upload/image", METHOD.POST);
    File f = File.createTempFile("nutz", "data");
    FileWriter fw = new FileWriter(f);
    fw.write("abc");
    fw.flush();
    fw.close();
    req.getParams().put("file", f);
    FilePostSender sender = new FilePostSender(req);
    Response resp = sender.send();
    assertEquals("image&3", resp.getContent());
}
Also used : Response(org.nutz.http.Response) FileWriter(java.io.FileWriter) Request(org.nutz.http.Request) FilePostSender(org.nutz.http.sender.FilePostSender) File(java.io.File) BaseWebappTest(org.nutz.mvc.testapp.BaseWebappTest) Test(org.junit.Test)

Example 5 with Response

use of org.nutz.http.Response in project incubator-skywalking by apache.

the class SenderSendInterceptor method afterMethod.

@Override
public Object afterMethod(final EnhancedInstance objInst, final Method method, final Object[] allArguments, final Class<?>[] argumentsTypes, Object ret) throws Throwable {
    Response response = (Response) ret;
    int statusCode = response.getStatus();
    AbstractSpan span = ContextManager.activeSpan();
    if (statusCode >= 400) {
        span.errorOccurred();
        Tags.STATUS_CODE.set(span, Integer.toString(statusCode));
    }
    ContextManager.stopSpan();
    return ret;
}
Also used : Response(org.nutz.http.Response) AbstractSpan(org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan)

Aggregations

Response (org.nutz.http.Response)9 Request (org.nutz.http.Request)7 NutMap (org.nutz.lang.util.NutMap)5 IOException (java.io.IOException)2 Test (org.junit.Test)2 File (java.io.File)1 FileWriter (java.io.FileWriter)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 InvocationHandler (java.lang.reflect.InvocationHandler)1 Method (java.lang.reflect.Method)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 ServletException (javax.servlet.ServletException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 AbstractSpan (org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan)1 FilePostSender (org.nutz.http.sender.FilePostSender)1 BaseWebappTest (org.nutz.mvc.testapp.BaseWebappTest)1