use of com.yahoo.vespa.clustercontroller.utils.communication.http.HttpResult in project vespa by vespa-engine.
the class StateRestAPITest method deadline_exceeded_exception_returns_http_504_error.
@Test
public void deadline_exceeded_exception_returns_http_504_error() throws Exception {
setupDummyStateApi();
stateApi.induceException(new DeadlineExceededException("argh!"));
HttpResult result = execute(new HttpRequest().setPath("/cluster/v2"));
assertEquals(result.toString(true), 504, result.getHttpReturnCode());
assertEquals(result.toString(true), "Gateway Timeout", result.getHttpReturnCodeDescription());
assertEquals(result.toString(true), "application/json", result.getHeader("Content-Type"));
String expected = "{\"message\":\"argh!\"}";
assertEquals(expected, result.getContent().toString());
}
use of com.yahoo.vespa.clustercontroller.utils.communication.http.HttpResult in project vespa by vespa-engine.
the class StateRestAPITest method testClusterState.
@Test
public void testClusterState() throws Exception {
setupDummyStateApi();
HttpResult result = execute(new HttpRequest().setPath("/cluster/v2/foo"));
assertEquals(result.toString(true), 200, result.getHttpReturnCode());
assertEquals(result.toString(true), "application/json", result.getHeader("Content-Type"));
String expected = "{\"node\": {\n" + " \"1\": {\"link\": \"\\/cluster\\/v2\\/foo\\/1\"},\n" + " \"3\": {\"link\": \"\\/cluster\\/v2\\/foo\\/3\"}\n" + "}}";
assertEquals(expected, ((JSONObject) result.getContent()).toString(2));
}
use of com.yahoo.vespa.clustercontroller.utils.communication.http.HttpResult in project vespa by vespa-engine.
the class StateRestAPITest method testRuntimeException.
@Test
public void testRuntimeException() throws Exception {
setupDummyStateApi();
stateApi.induceException(new RuntimeException("Moahaha"));
HttpResult result = execute(new HttpRequest().setPath("/cluster/v2"));
assertEquals(result.toString(true), 500, result.getHttpReturnCode());
assertEquals(result.toString(true), "Failed to process request", result.getHttpReturnCodeDescription());
assertEquals(result.toString(true), "application/json", result.getHeader("Content-Type"));
String expected = "{\"message\":\"java.lang.RuntimeException: Moahaha\"}";
assertEquals(expected, result.getContent().toString());
}
use of com.yahoo.vespa.clustercontroller.utils.communication.http.HttpResult in project vespa by vespa-engine.
the class StatusHandler method handleRequest.
@Override
public HttpResult handleRequest(HttpRequest httpRequest) throws Exception {
log.fine("Handling status request " + httpRequest);
Matcher matcher = statusRequest.matcher(httpRequest.getPath());
if (matcher.matches()) {
return handleClusterRequest(matcher.group(1), matcher.group(2));
}
matcher = clusterListRequest.matcher(httpRequest.getPath());
if (matcher.matches()) {
return handleClusterListRequest();
}
return new HttpResult().setHttpCode(404, "No page for request '" + httpRequest.getPath() + "'.");
}
use of com.yahoo.vespa.clustercontroller.utils.communication.http.HttpResult in project vespa by vespa-engine.
the class ApacheHttpInstance method execute.
/**
* This function is not threadsafe.
*/
public HttpResult execute(HttpRequest r) {
HttpRequest.HttpOp op = r.getHttpOperation();
if (op == null) {
if (r.getPostContent() != null) {
log.fine("Request " + r + " has no HTTP function specified. Assuming POST as post content is set.");
op = HttpRequest.HttpOp.POST;
} else {
log.fine("Request " + r + " has no HTTP function specified. Assuming GET as post content is set.");
op = HttpRequest.HttpOp.GET;
}
}
if (r.getPostContent() != null && !(op.equals(HttpRequest.HttpOp.POST) || op.equals(HttpRequest.HttpOp.PUT))) {
throw new IllegalStateException("A " + op + " operation can't have content");
}
try {
HttpHost target = new HttpHost(r.getHost(), r.getPort(), "http");
org.apache.http.HttpRequest req = null;
String path = r.getPath();
int uriOption = 0;
for (HttpRequest.KeyValuePair option : r.getUrlOptions()) {
path += (++uriOption == 1 ? '?' : '&');
path += option.getKey() + '=' + option.getValue();
}
switch(op) {
case POST:
HttpPost post = new HttpPost(path);
if (r.getPostContent() != null) {
post.setEntity(new StringEntity(r.getPostContent().toString()));
}
req = post;
break;
case GET:
req = new HttpGet(path);
break;
case PUT:
HttpPut put = new HttpPut(path);
put.setEntity(new StringEntity(r.getPostContent().toString()));
req = put;
break;
case DELETE:
req = new HttpDelete(path);
break;
}
for (HttpRequest.KeyValuePair header : r.getHeaders()) {
req.addHeader(header.getKey(), header.getValue());
}
HttpResponse rsp = client.execute(target, req);
HttpEntity entity = rsp.getEntity();
HttpResult result = new HttpResult();
result.setHttpCode(rsp.getStatusLine().getStatusCode(), rsp.getStatusLine().getReasonPhrase());
if (entity != null) {
result.setContent(EntityUtils.toString(entity));
}
for (Header header : rsp.getAllHeaders()) {
result.addHeader(header.getName(), header.getValue());
}
return result;
} catch (Exception e) {
HttpResult result = new HttpResult();
StringWriter writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));
writer.flush();
result.setHttpCode(500, "Got exception " + writer.toString() + " when sending message.");
return result;
}
}
Aggregations