Rewrite to use RestApi
// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.provision.restapi;
-import com.yahoo.container.jdisc.HttpRequest;
import com.yahoo.container.jdisc.HttpResponse;
import com.yahoo.container.jdisc.LoggingRequestHandler;
-import com.yahoo.restapi.ErrorResponse;
-import com.yahoo.vespa.hosted.provision.NoSuchNodeException;
+import com.yahoo.restapi.RestApi;
+import com.yahoo.restapi.RestApiRequestHandler;
import com.yahoo.vespa.hosted.provision.NodeRepository;
-import com.yahoo.yolean.Exceptions;
import javax.inject.Inject;
-import java.util.logging.Level;
/**
* @author mpolden
+ * @author jonmv
*/
-public class LoadBalancersV1ApiHandler extends LoggingRequestHandler {
+public class LoadBalancersV1ApiHandler extends RestApiRequestHandler<LoadBalancersV1ApiHandler> {
private final NodeRepository nodeRepository;
@Inject
public LoadBalancersV1ApiHandler(LoggingRequestHandler.Context parentCtx, NodeRepository nodeRepository) {
- super(parentCtx);
+ super(parentCtx, LoadBalancersV1ApiHandler::createRestApiDefinition);
this.nodeRepository = nodeRepository;
}
- @Override
- public HttpResponse handle(HttpRequest request) {
- try {
- switch (request.getMethod()) {
- case GET: return handleGET(request);
- default: return ErrorResponse.methodNotAllowed("Method '" + request.getMethod() + "' is not supported");
- }
- }
- catch (NotFoundException | NoSuchNodeException e) {
- return ErrorResponse.notFoundError(Exceptions.toMessageString(e));
- }
- catch (IllegalArgumentException e) {
- return ErrorResponse.badRequest(Exceptions.toMessageString(e));
- }
- catch (RuntimeException e) {
- log.log(Level.WARNING, "Unexpected error handling '" + request.getUri() + "'", e);
- return ErrorResponse.internalServerError(Exceptions.toMessageString(e));
- }
+ private static RestApi createRestApiDefinition(LoadBalancersV1ApiHandler self) {
+ return RestApi.builder()
+ .addRoute(RestApi.route("/loadbalancers/v1")
+ .get(self::getLoadBalancers))
+ .build();
}
- private HttpResponse handleGET(HttpRequest request) {
- String path = request.getUri().getPath();
- if (path.matches("/loadbalancers/v1/?")) return new LoadBalancersResponse(request, nodeRepository);
- throw new NotFoundException("Nothing at path '" + path + "'");
+ private HttpResponse getLoadBalancers(RestApi.RequestContext context) {
+ return new LoadBalancersResponse(context.request(), nodeRepository);
}
+
}
