Search in sources :

Example 21 with HttpServiceResponse

use of org.apache.bookkeeper.http.service.HttpServiceResponse in project bookkeeper by apache.

the class GetLedgerMetaService method handle.

@Override
public HttpServiceResponse handle(HttpServiceRequest request) throws Exception {
    HttpServiceResponse response = new HttpServiceResponse();
    Map<String, String> params = request.getParams();
    if (HttpServer.Method.GET == request.getMethod() && (params != null) && params.containsKey("ledger_id")) {
        Long ledgerId = Long.parseLong(params.get("ledger_id"));
        LedgerManagerFactory mFactory = bookieServer.getBookie().getLedgerManagerFactory();
        LedgerManager manager = mFactory.newLedgerManager();
        // output <ledgerId: ledgerMetadata>
        Map<String, String> output = Maps.newHashMap();
        ListLedgerService.ReadLedgerMetadataCallback cb = new ListLedgerService.ReadLedgerMetadataCallback(ledgerId);
        manager.readLedgerMetadata(ledgerId, cb);
        LedgerMetadata md = cb.get();
        output.put(ledgerId.toString(), new String(md.serialize(), UTF_8));
        manager.close();
        String jsonResponse = JsonUtil.toJson(output);
        LOG.debug("output body:" + jsonResponse);
        response.setBody(jsonResponse);
        response.setCode(HttpServer.StatusCode.OK);
        return response;
    } else {
        response.setCode(HttpServer.StatusCode.NOT_FOUND);
        response.setBody("Not found method. Should be GET method");
        return response;
    }
}
Also used : LedgerManager(org.apache.bookkeeper.meta.LedgerManager) LedgerMetadata(org.apache.bookkeeper.client.LedgerMetadata) HttpServiceResponse(org.apache.bookkeeper.http.service.HttpServiceResponse) LedgerManagerFactory(org.apache.bookkeeper.meta.LedgerManagerFactory)

Example 22 with HttpServiceResponse

use of org.apache.bookkeeper.http.service.HttpServiceResponse in project bookkeeper by apache.

the class ListBookiesService method handle.

@Override
public HttpServiceResponse handle(HttpServiceRequest request) throws Exception {
    HttpServiceResponse response = new HttpServiceResponse();
    // GET
    if (HttpServer.Method.GET == request.getMethod()) {
        Collection<BookieSocketAddress> bookies = new ArrayList<BookieSocketAddress>();
        Map<String, String> params = request.getParams();
        // default print rw
        boolean readOnly = (params != null) && params.containsKey("type") && params.get("type").equals("ro");
        // default not print hostname
        boolean printHostname = (params != null) && params.containsKey("print_hostnames") && params.get("print_hostnames").equals("true");
        if (readOnly) {
            bookies.addAll(bka.getReadOnlyBookies());
        } else {
            bookies.addAll(bka.getAvailableBookies());
        }
        // output <bookieSocketAddress: hostname>
        Map<String, String> output = Maps.newHashMap();
        for (BookieSocketAddress b : bookies) {
            output.putIfAbsent(b.toString(), printHostname ? b.getHostName() : null);
            LOG.debug("bookie: " + b.toString() + " hostname:" + b.getHostName());
        }
        String jsonResponse = JsonUtil.toJson(output);
        response.setBody(jsonResponse);
        response.setCode(HttpServer.StatusCode.OK);
        return response;
    } else {
        response.setCode(HttpServer.StatusCode.NOT_FOUND);
        response.setBody("Not found method. Should be GET method");
        return response;
    }
}
Also used : BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) ArrayList(java.util.ArrayList) HttpServiceResponse(org.apache.bookkeeper.http.service.HttpServiceResponse)

Example 23 with HttpServiceResponse

use of org.apache.bookkeeper.http.service.HttpServiceResponse in project bookkeeper by apache.

the class TwitterAbstractHandler method processRequest.

/**
 * Process the request using the given httpEndpointService.
 */
Future<Response> processRequest(HttpEndpointService httpEndpointService, Request request) {
    HttpServiceRequest httpServiceRequest = new HttpServiceRequest().setMethod(convertMethod(request)).setParams(convertParams(request)).setBody(request.contentString());
    HttpServiceResponse httpServiceResponse = null;
    try {
        httpServiceResponse = httpEndpointService.handle(httpServiceRequest);
    } catch (Exception e) {
        httpServiceResponse = new ErrorHttpService().handle(httpServiceRequest);
    }
    Response response = Response.apply();
    response.setContentString(httpServiceResponse.getBody());
    response.statusCode(httpServiceResponse.getStatusCode());
    return Future.value(response);
}
Also used : Response(com.twitter.finagle.http.Response) HttpServiceResponse(org.apache.bookkeeper.http.service.HttpServiceResponse) ErrorHttpService(org.apache.bookkeeper.http.service.ErrorHttpService) HttpServiceRequest(org.apache.bookkeeper.http.service.HttpServiceRequest) HttpServiceResponse(org.apache.bookkeeper.http.service.HttpServiceResponse)

Example 24 with HttpServiceResponse

use of org.apache.bookkeeper.http.service.HttpServiceResponse in project bookkeeper by apache.

the class VertxAbstractHandler method processRequest.

/**
 * Process the request using the given httpEndpointService.
 */
void processRequest(HttpEndpointService httpEndpointService, RoutingContext context) {
    HttpServerRequest httpRequest = context.request();
    HttpServerResponse httpResponse = context.response();
    HttpServiceRequest request = new HttpServiceRequest().setMethod(convertMethod(httpRequest)).setParams(convertParams(httpRequest)).setBody(context.getBodyAsString());
    HttpServiceResponse response = null;
    try {
        response = httpEndpointService.handle(request);
    } catch (Exception e) {
        response = new ErrorHttpService().handle(request);
    }
    httpResponse.setStatusCode(response.getStatusCode());
    httpResponse.end(response.getBody());
}
Also used : ErrorHttpService(org.apache.bookkeeper.http.service.ErrorHttpService) HttpServerRequest(io.vertx.core.http.HttpServerRequest) HttpServerResponse(io.vertx.core.http.HttpServerResponse) HttpServiceRequest(org.apache.bookkeeper.http.service.HttpServiceRequest) HttpServiceResponse(org.apache.bookkeeper.http.service.HttpServiceResponse)

Example 25 with HttpServiceResponse

use of org.apache.bookkeeper.http.service.HttpServiceResponse in project bookkeeper by apache.

the class TestHttpService method testWhoIsAuditorService.

@Test
public void testWhoIsAuditorService() throws Exception {
    baseConf.setZkServers(zkUtil.getZooKeeperConnectString());
    startAuditorElector();
    HttpEndpointService whoIsAuditorService = bkHttpServiceProvider.provideHttpEndpointService(HttpServer.ApiType.WHO_IS_AUDITOR);
    // 1,  GET, should return success
    HttpServiceRequest request1 = new HttpServiceRequest(null, HttpServer.Method.GET, null);
    HttpServiceResponse response1 = whoIsAuditorService.handle(request1);
    assertEquals(HttpServer.StatusCode.OK.getValue(), response1.getStatusCode());
    LOG.info(response1.getBody());
    stopAuditorElector();
}
Also used : HttpEndpointService(org.apache.bookkeeper.http.service.HttpEndpointService) HttpServiceRequest(org.apache.bookkeeper.http.service.HttpServiceRequest) HttpServiceResponse(org.apache.bookkeeper.http.service.HttpServiceResponse) Test(org.junit.Test)

Aggregations

HttpServiceResponse (org.apache.bookkeeper.http.service.HttpServiceResponse)35 HttpServiceRequest (org.apache.bookkeeper.http.service.HttpServiceRequest)19 HttpEndpointService (org.apache.bookkeeper.http.service.HttpEndpointService)18 Test (org.junit.Test)16 BookKeeper (org.apache.bookkeeper.client.BookKeeper)8 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)7 BookieSocketAddress (org.apache.bookkeeper.net.BookieSocketAddress)6 Map (java.util.Map)4 File (java.io.File)3 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 LedgerManager (org.apache.bookkeeper.meta.LedgerManager)3 LedgerManagerFactory (org.apache.bookkeeper.meta.LedgerManagerFactory)3 LedgerMetadata (org.apache.bookkeeper.client.LedgerMetadata)2 ClientConfiguration (org.apache.bookkeeper.conf.ClientConfiguration)2 ErrorHttpService (org.apache.bookkeeper.http.service.ErrorHttpService)2 LedgerUnderreplicationManager (org.apache.bookkeeper.meta.LedgerUnderreplicationManager)2 JsonUtil (org.apache.bookkeeper.util.JsonUtil)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Lists (com.google.common.collect.Lists)1