Search in sources :

Example 6 with HttpRequest

use of com.yahoo.vespa.clustercontroller.utils.communication.http.HttpRequest 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 7 with HttpRequest

use of com.yahoo.vespa.clustercontroller.utils.communication.http.HttpRequest 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 8 with HttpRequest

use of com.yahoo.vespa.clustercontroller.utils.communication.http.HttpRequest 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)

Example 9 with HttpRequest

use of com.yahoo.vespa.clustercontroller.utils.communication.http.HttpRequest in project vespa by vespa-engine.

the class StateRestAPITest method testTopLevelList.

@Test
public void testTopLevelList() throws Exception {
    setupDummyStateApi();
    HttpResult result = execute(new HttpRequest().setPath("/cluster/v2"));
    assertEquals(result.toString(true), 200, result.getHttpReturnCode());
    assertEquals(result.toString(true), "application/json", result.getHeader("Content-Type"));
    String expected = "{\"cluster\": {\n" + "  \"foo\": {\"link\": \"\\/cluster\\/v2\\/foo\"},\n" + "  \"bar\": {\"link\": \"\\/cluster\\/v2\\/bar\"}\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 10 with HttpRequest

use of com.yahoo.vespa.clustercontroller.utils.communication.http.HttpRequest in project vespa by vespa-engine.

the class StateRestAPITest method testNodeState.

@Test
public void testNodeState() throws Exception {
    setupDummyStateApi();
    HttpResult result = execute(new HttpRequest().setPath("/cluster/v2/foo/3"));
    assertEquals(result.toString(true), 200, result.getHttpReturnCode());
    assertEquals(result.toString(true), "application/json", result.getHeader("Content-Type"));
    String expected = "{\n" + "  \"attributes\": {\"group\": \"mygroup\"},\n" + "  \"state\": {\"current\": {\n" + "    \"state\": \"up\",\n" + "    \"reason\": \"\"\n" + "  }},\n" + "  \"metrics\": {\"doc-count\": 8}\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)

Aggregations

HttpRequest (com.yahoo.vespa.clustercontroller.utils.communication.http.HttpRequest)13 HttpResult (com.yahoo.vespa.clustercontroller.utils.communication.http.HttpResult)12 Test (org.junit.Test)8 JSONObject (org.codehaus.jettison.json.JSONObject)2 Distribution (com.yahoo.vdslib.distribution.Distribution)1 StatusHandler (com.yahoo.vespa.clustercontroller.core.status.StatusHandler)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 StringWriter (java.io.StringWriter)1 HashMap (java.util.HashMap)1 Map (java.util.Map)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 HttpGet (org.apache.http.client.methods.HttpGet)1 HttpPost (org.apache.http.client.methods.HttpPost)1 HttpPut (org.apache.http.client.methods.HttpPut)1