Search in sources :

Example 1 with HttpRequest

use of com.yahoo.jdisc.http.HttpRequest in project vespa by vespa-engine.

the class BindingsOverviewHandler method handleRequest.

@Override
public ContentChannel handleRequest(com.yahoo.jdisc.Request request, ResponseHandler handler) {
    JSONObject json;
    int statusToReturn;
    if (request instanceof HttpRequest && ((HttpRequest) request).getMethod() != Method.GET) {
        json = errorMessageInJson();
        statusToReturn = com.yahoo.jdisc.Response.Status.METHOD_NOT_ALLOWED;
    } else {
        json = new StatusResponse(bindingsConfig).render();
        statusToReturn = com.yahoo.jdisc.Response.Status.OK;
    }
    FastContentWriter writer = new FastContentWriter(new ResponseDispatch() {

        @Override
        protected com.yahoo.jdisc.Response newResponse() {
            com.yahoo.jdisc.Response response = new com.yahoo.jdisc.Response(statusToReturn);
            response.headers().add("Content-Type", Arrays.asList(new String[] { "application/json" }));
            return response;
        }
    }.connect(handler));
    try {
        writer.write(json.toString());
    } finally {
        writer.close();
    }
    return new IgnoredContent();
}
Also used : HttpRequest(com.yahoo.jdisc.http.HttpRequest) ResponseDispatch(com.yahoo.jdisc.handler.ResponseDispatch) JSONObject(org.json.JSONObject) FastContentWriter(com.yahoo.jdisc.handler.FastContentWriter)

Example 2 with HttpRequest

use of com.yahoo.jdisc.http.HttpRequest in project vespa by vespa-engine.

the class VipStatusHandlerTestCase method testFileFound.

@Test
public final void testFileFound() throws IOException {
    final File statusFile = File.createTempFile("VipStatusHandlerTestCase", null);
    try {
        final FileWriter writer = new FileWriter(statusFile);
        final String OK = "OK\n";
        writer.write(OK);
        writer.close();
        final VipStatusConfig config = new VipStatusConfig(new VipStatusConfig.Builder().accessdisk(true).statusfile(statusFile.getAbsolutePath()).noSearchBackendsImpliesOutOfService(false));
        final VipStatusHandler handler = new VipStatusHandler(Executors.newCachedThreadPool(), config, metric);
        final MockResponseHandler responseHandler = new MockResponseHandler();
        final HttpRequest request = createRequest();
        final BufferedContentChannel requestContent = createChannel();
        handler.handleRequest(request, requestContent, responseHandler);
        final ByteBuffer b = responseHandler.channel.read();
        final byte[] asBytes = new byte[b.remaining()];
        b.get(asBytes);
        assertEquals(OK, Utf8.toString(asBytes));
    } finally {
        statusFile.delete();
    }
}
Also used : HttpRequest(com.yahoo.jdisc.http.HttpRequest) FileWriter(java.io.FileWriter) VipStatusConfig(com.yahoo.container.core.VipStatusConfig) BufferedContentChannel(com.yahoo.jdisc.handler.BufferedContentChannel) File(java.io.File) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 3 with HttpRequest

use of com.yahoo.jdisc.http.HttpRequest in project vespa by vespa-engine.

the class VipStatusHandlerTestCase method testProgrammaticallyRemovedFromRotation.

@Test
public final void testProgrammaticallyRemovedFromRotation() throws IOException {
    VipStatus vipStatus = new VipStatus();
    final VipStatusConfig config = new VipStatusConfig(new VipStatusConfig.Builder().accessdisk(false).noSearchBackendsImpliesOutOfService(true));
    final VipStatusHandler handler = new VipStatusHandler(Executors.newCachedThreadPool(), config, metric, vipStatus);
    vipStatus.removeFromRotation(this);
    {
        final MockResponseHandler responseHandler = new MockResponseHandler();
        final HttpRequest request = createRequest();
        final BufferedContentChannel requestContent = createChannel();
        handler.handleRequest(request, requestContent, responseHandler);
        final ByteBuffer b = responseHandler.channel.read();
        final byte[] asBytes = new byte[b.remaining()];
        b.get(asBytes);
        assertEquals(VipStatusHandler.StatusResponse.NO_SEARCH_BACKENDS, Utf8.toString(asBytes));
    }
    vipStatus.addToRotation(this);
    {
        final MockResponseHandler responseHandler = new MockResponseHandler();
        final HttpRequest request = createRequest();
        final BufferedContentChannel requestContent = createChannel();
        handler.handleRequest(request, requestContent, responseHandler);
        final ByteBuffer b = responseHandler.channel.read();
        final byte[] asBytes = new byte[b.remaining()];
        b.get(asBytes);
        assertEquals(VipStatusHandler.OK_MESSAGE, Utf8.toString(asBytes));
    }
}
Also used : HttpRequest(com.yahoo.jdisc.http.HttpRequest) VipStatusConfig(com.yahoo.container.core.VipStatusConfig) BufferedContentChannel(com.yahoo.jdisc.handler.BufferedContentChannel) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 4 with HttpRequest

use of com.yahoo.jdisc.http.HttpRequest in project vespa by vespa-engine.

the class DiscFilterResponseTest method testAddCookie.

@Test
public void testAddCookie() {
    URI uri = URI.create("http://example.com/test");
    HttpRequest httpReq = newRequest(uri, HttpRequest.Method.GET, HttpRequest.Version.HTTP_1_1);
    HttpResponse httpResp = newResponse(httpReq, 200);
    DiscFilterResponse response = new JdiscFilterResponse(httpResp);
    response.addCookie(JDiscCookieWrapper.wrap(new Cookie("name", "value")));
    List<Cookie> cookies = response.getCookies();
    Assert.assertEquals(cookies.size(), 1);
    Assert.assertEquals(cookies.get(0).getName(), "name");
}
Also used : HttpRequest(com.yahoo.jdisc.http.HttpRequest) Cookie(com.yahoo.jdisc.http.Cookie) HttpResponse(com.yahoo.jdisc.http.HttpResponse) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 5 with HttpRequest

use of com.yahoo.jdisc.http.HttpRequest in project vespa by vespa-engine.

the class DiscFilterResponseTest method testSetCookie.

@Test
public void testSetCookie() {
    URI uri = URI.create("http://example.com/test");
    HttpRequest httpReq = newRequest(uri, HttpRequest.Method.GET, HttpRequest.Version.HTTP_1_1);
    HttpResponse httpResp = newResponse(httpReq, 200);
    DiscFilterResponse response = new JdiscFilterResponse(httpResp);
    response.setCookie("name", "value");
    List<Cookie> cookies = response.getCookies();
    Assert.assertEquals(cookies.size(), 1);
    Assert.assertEquals(cookies.get(0).getName(), "name");
}
Also used : HttpRequest(com.yahoo.jdisc.http.HttpRequest) Cookie(com.yahoo.jdisc.http.Cookie) HttpResponse(com.yahoo.jdisc.http.HttpResponse) URI(java.net.URI) Test(org.testng.annotations.Test)

Aggregations

HttpRequest (com.yahoo.jdisc.http.HttpRequest)48 Test (org.testng.annotations.Test)36 URI (java.net.URI)27 ResponseHandler (com.yahoo.jdisc.handler.ResponseHandler)7 HttpResponse (com.yahoo.jdisc.http.HttpResponse)7 RequestFilter (com.yahoo.jdisc.http.filter.RequestFilter)5 VipStatusConfig (com.yahoo.container.core.VipStatusConfig)4 BufferedContentChannel (com.yahoo.jdisc.handler.BufferedContentChannel)4 ContentChannel (com.yahoo.jdisc.handler.ContentChannel)4 Cookie (com.yahoo.jdisc.http.Cookie)4 InetSocketAddress (java.net.InetSocketAddress)4 ByteBuffer (java.nio.ByteBuffer)4 Test (org.junit.Test)4 Response (com.yahoo.jdisc.Response)2 TestDriver (com.yahoo.jdisc.test.TestDriver)2 HeaderFields (com.yahoo.jdisc.HeaderFields)1 ResourceReference (com.yahoo.jdisc.ResourceReference)1 AbstractRequestHandler (com.yahoo.jdisc.handler.AbstractRequestHandler)1 BindingNotFoundException (com.yahoo.jdisc.handler.BindingNotFoundException)1 FastContentWriter (com.yahoo.jdisc.handler.FastContentWriter)1