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;
}
}
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;
}
}
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);
}
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());
}
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();
}
Aggregations