Search in sources :

Example 6 with HttpResult

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());
}
Also used : HttpRequest(com.yahoo.vespa.clustercontroller.utils.communication.http.HttpRequest) HttpResult(com.yahoo.vespa.clustercontroller.utils.communication.http.HttpResult) Test(org.junit.Test)

Example 7 with HttpResult

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));
}
Also used : HttpRequest(com.yahoo.vespa.clustercontroller.utils.communication.http.HttpRequest) HttpResult(com.yahoo.vespa.clustercontroller.utils.communication.http.HttpResult) Test(org.junit.Test)

Example 8 with HttpResult

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());
}
Also used : HttpRequest(com.yahoo.vespa.clustercontroller.utils.communication.http.HttpRequest) HttpResult(com.yahoo.vespa.clustercontroller.utils.communication.http.HttpResult) Test(org.junit.Test)

Example 9 with HttpResult

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() + "'.");
}
Also used : Matcher(java.util.regex.Matcher) HttpResult(com.yahoo.vespa.clustercontroller.utils.communication.http.HttpResult)

Example 10 with HttpResult

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;
    }
}
Also used : HttpRequest(com.yahoo.vespa.clustercontroller.utils.communication.http.HttpRequest) HttpPost(org.apache.http.client.methods.HttpPost) HttpDelete(org.apache.http.client.methods.HttpDelete) HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) HttpPut(org.apache.http.client.methods.HttpPut) StringEntity(org.apache.http.entity.StringEntity) Header(org.apache.http.Header) StringWriter(java.io.StringWriter) HttpHost(org.apache.http.HttpHost) HttpResult(com.yahoo.vespa.clustercontroller.utils.communication.http.HttpResult) PrintWriter(java.io.PrintWriter)

Aggregations

HttpResult (com.yahoo.vespa.clustercontroller.utils.communication.http.HttpResult)16 HttpRequest (com.yahoo.vespa.clustercontroller.utils.communication.http.HttpRequest)13 Test (org.junit.Test)7 JSONObject (org.codehaus.jettison.json.JSONObject)3 StringWriter (java.io.StringWriter)2 Distribution (com.yahoo.vdslib.distribution.Distribution)1 StatusHandler (com.yahoo.vespa.clustercontroller.core.status.StatusHandler)1 StatusPageResponse (com.yahoo.vespa.clustercontroller.core.status.statuspage.StatusPageResponse)1 StatusPageServer (com.yahoo.vespa.clustercontroller.core.status.statuspage.StatusPageServer)1 AsyncOperationImpl (com.yahoo.vespa.clustercontroller.utils.communication.async.AsyncOperationImpl)1 SyncHttpClient (com.yahoo.vespa.clustercontroller.utils.communication.http.SyncHttpClient)1 PrintWriter (java.io.PrintWriter)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Matcher (java.util.regex.Matcher)1 Header (org.apache.http.Header)1 HttpEntity (org.apache.http.HttpEntity)1 HttpHost (org.apache.http.HttpHost)1 HttpResponse (org.apache.http.HttpResponse)1 HttpDelete (org.apache.http.client.methods.HttpDelete)1