Search in sources :

Example 6 with Response

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

the class BaseTest method test_http_method_override.

@Test
public void test_http_method_override() {
    Response resp = post("/common/httpmethods?_method=DELETE", new NutMap("_method", "DELETE"));
    assertEquals(200, resp.getStatus());
    assertEquals("DELETE", resp.getContent());
}
Also used : Response(org.nutz.http.Response) NutMap(org.nutz.lang.util.NutMap) Test(org.junit.Test)

Example 7 with Response

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

the class LoachClient method updateServiceList.

@SuppressWarnings("unchecked")
public void updateServiceList() {
    try {
        String listURL = url + "/list";
        Request req = Request.create(listURL, METHOD.GET);
        req.getHeader().clear();
        req.getHeader().set("If-None-Match", lastListETag);
        Response resp = Sender.create(req).setConnTimeout(1000).setTimeout(3000).send();
        if (resp.isOK()) {
            serviceList = (Map<String, List<NutMap>>) Json.fromJson(NutMap.class, resp.getReader()).get("data");
            for (UpdateListener listener : listeners) {
                listener.onUpdate(serviceList);
            }
            lastChecked = System.currentTimeMillis();
            lastListETag = Strings.sBlank(resp.getHeader().get("ETag", "ABC"));
        } else if (resp.getStatus() == 304) {
            // ok
            lastChecked = System.currentTimeMillis();
        }
    } catch (Throwable e) {
        log.debugf("bad url? %s %s", url, e.getMessage());
    }
}
Also used : Response(org.nutz.http.Response) Request(org.nutz.http.Request) LinkedList(java.util.LinkedList) List(java.util.List) NutMap(org.nutz.lang.util.NutMap)

Example 8 with Response

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

the class LoachClient method _reg.

protected boolean _reg(String regData) {
    try {
        String regURL = url + "/reg";
        if (isDebug()) {
            log.debug("Reg URL :" + regURL);
            log.debug("Reg Data:" + regData);
        }
        Request req = Request.create(regURL, METHOD.POST);
        req.setData(regData);
        req.getHeader().clear();
        req.getHeader().asJsonContentType();
        Response resp = Sender.create(req).setTimeout(3000).send();
        if (resp.isOK()) {
            NutMap re = Json.fromJson(NutMap.class, resp.getReader());
            if (re != null && re.getBoolean("ok", false)) {
                log.infof("Reg Done id=%s url=%s", id, url);
                regOk = true;
                return true;
            } else if (re == null) {
                log.info("Reg Err, revc NULL");
                return false;
            } else {
                log.info("Reg Err " + re);
                return false;
            }
        }
    } 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 9 with Response

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

the class ProxyStreamServletStarter method doGet.

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String origin = request.getParameter("origin");
    String authorization = request.getParameter("authorization");
    if (origin == null) {
        response.setStatus(500);
        response.getWriter().println("Required parameter 'origin' missing. Example: 107.20.175.135:7001");
        return;
    }
    origin = origin.trim();
    InputStream is = null;
    boolean hasFirstParameter = false;
    StringBuilder url = new StringBuilder();
    if (!origin.startsWith("http")) {
        url.append("http://");
    }
    url.append(origin);
    if (origin.contains("?")) {
        hasFirstParameter = true;
    }
    Map<String, String[]> params = request.getParameterMap();
    for (String key : params.keySet()) {
        if (!key.equals("origin") && !key.equals("authorization")) {
            String[] values = params.get(key);
            String value = values[0].trim();
            if (hasFirstParameter) {
                url.append("&");
            } else {
                url.append("?");
                hasFirstParameter = true;
            }
            url.append(key).append("=").append(value);
        }
    }
    String proxyUrl = url.toString();
    log.debugf("Proxy opening connection to: %s", proxyUrl);
    try {
        Request req = Request.create(proxyUrl, METHOD.GET);
        if (authorization != null) {
            req.getHeader().set("Authorization", authorization);
        }
        Response resp = Sender.create(req).setConnTimeout(5000).setTimeout(15000).send();
        if (resp.isOK()) {
            String contentType = resp.getHeader().get("Content-Type");
            if (Strings.isBlank(contentType) || !contentType.startsWith("text/event-stream")) {
                log.warn("not vaild ContentType = " + contentType);
                return;
            }
            response.setHeader("Content-Type", contentType);
            // writeTo swallows exceptions and never quits even if outputstream is throwing IOExceptions (such as broken pipe) ... since the inputstream is infinite
            // httpResponse.getEntity().writeTo(new OutputStreamWrapper(response.getOutputStream()));
            // so I copy it manually ...
            is = resp.getStream();
            // copy data from source to response
            OutputStream os = response.getOutputStream();
            int b = -1;
            while ((b = is.read()) != -1) {
                try {
                    os.write(b);
                    if (b == 10) /**
                     * flush buffer on line feed
                     */
                    {
                        os.flush();
                    }
                } catch (Exception e) {
                    break;
                }
            }
        }
    } catch (Exception e) {
        log.error("Error proxying request: " + url, e);
    } finally {
        Streams.safeClose(is);
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.nutz.http.Response) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) HttpServletRequest(javax.servlet.http.HttpServletRequest) Request(org.nutz.http.Request) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

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