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());
}
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());
}
}
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;
}
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);
}
}
Aggregations