use of org.apache.bookkeeper.http.service.HttpServiceRequest in project bookkeeper by apache.
the class ListUnderReplicatedLedgerService method handle.
/*
* Print the node which holds the auditor lock.
*/
@Override
public HttpServiceResponse handle(HttpServiceRequest request) throws Exception {
HttpServiceResponse response = new HttpServiceResponse();
// parameter as this: ?missingreplica=<bookie_address>&excludingmissingreplica=<bookid_address>
Map<String, String> params = request.getParams();
if (HttpServer.Method.GET == request.getMethod()) {
final String includingBookieId;
final String excludingBookieId;
if (params != null && params.containsKey("missingreplica")) {
includingBookieId = params.get("missingreplica");
} else {
includingBookieId = null;
}
if (params != null && params.containsKey("excludingmissingreplica")) {
excludingBookieId = params.get("excludingmissingreplica");
} else {
excludingBookieId = null;
}
Predicate<List<String>> predicate = null;
if (!StringUtils.isBlank(includingBookieId) && !StringUtils.isBlank(excludingBookieId)) {
predicate = replicasList -> (replicasList.contains(includingBookieId) && !replicasList.contains(excludingBookieId));
} else if (!StringUtils.isBlank(includingBookieId)) {
predicate = replicasList -> replicasList.contains(includingBookieId);
} else if (!StringUtils.isBlank(excludingBookieId)) {
predicate = replicasList -> !replicasList.contains(excludingBookieId);
}
try {
List<Long> outputLedgers = Lists.newArrayList();
LedgerManagerFactory mFactory = bookieServer.getBookie().getLedgerManagerFactory();
LedgerUnderreplicationManager underreplicationManager = mFactory.newLedgerUnderreplicationManager();
Iterator<Long> iter = underreplicationManager.listLedgersToRereplicate(predicate);
while (iter.hasNext()) {
outputLedgers.add(iter.next());
}
if (outputLedgers.isEmpty()) {
response.setCode(HttpServer.StatusCode.NOT_FOUND);
response.setBody("No under replicated ledgers found");
return response;
} else {
response.setCode(HttpServer.StatusCode.OK);
String jsonResponse = JsonUtil.toJson(outputLedgers);
LOG.debug("output body: " + jsonResponse);
response.setBody(jsonResponse);
return response;
}
} catch (Exception e) {
LOG.error("Exception occurred while listing under replicated ledgers", e);
response.setCode(HttpServer.StatusCode.NOT_FOUND);
response.setBody("Exception when get." + e.getMessage());
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.HttpServiceRequest 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.HttpServiceRequest 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.HttpServiceRequest 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();
}
use of org.apache.bookkeeper.http.service.HttpServiceRequest in project bookkeeper by apache.
the class TestHttpService method testRecoveryBookieService.
@Test
public void testRecoveryBookieService() throws Exception {
baseConf.setZkServers(zkUtil.getZooKeeperConnectString());
HttpEndpointService recoveryBookieService = bkHttpServiceProvider.provideHttpEndpointService(HttpServer.ApiType.RECOVERY_BOOKIE);
// 1, null body of GET, should return error
HttpServiceRequest request1 = new HttpServiceRequest(null, HttpServer.Method.GET, null);
HttpServiceResponse response1 = recoveryBookieService.handle(request1);
assertEquals(HttpServer.StatusCode.NOT_FOUND.getValue(), response1.getStatusCode());
// 2, null body of PUT, should return error
HttpServiceRequest request2 = new HttpServiceRequest(null, HttpServer.Method.PUT, null);
HttpServiceResponse response2 = recoveryBookieService.handle(request2);
assertEquals(HttpServer.StatusCode.NOT_FOUND.getValue(), response2.getStatusCode());
// 3, body with bookie_src, bookie_dest and delete_cookie of PUT, should success.
String bookieSrc = getBookie(0).toString();
String putBody3 = "{\"bookie_src\": [ \"" + bookieSrc + "\" ]," + "\"delete_cookie\": false }";
HttpServiceRequest request3 = new HttpServiceRequest(putBody3, HttpServer.Method.PUT, null);
HttpServiceResponse response3 = recoveryBookieService.handle(request3);
assertEquals(HttpServer.StatusCode.OK.getValue(), response3.getStatusCode());
// 5, body with bookie_src of PUT, should success.
String putBody5 = "{\"bookie_src\": [ \"" + bookieSrc + "\" ] }";
HttpServiceRequest request5 = new HttpServiceRequest(putBody5, HttpServer.Method.PUT, null);
HttpServiceResponse response5 = recoveryBookieService.handle(request5);
assertEquals(HttpServer.StatusCode.OK.getValue(), response5.getStatusCode());
}
Aggregations