arjunashok commented on code in PR #231: URL: https://github.com/apache/cassandra-sidecar/pull/231#discussion_r2304935606
########## server/src/main/java/org/apache/cassandra/sidecar/handlers/validations/ValidateKeyspaceExistenceHandler.java: ########## @@ -0,0 +1,82 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.cassandra.sidecar.handlers.validations; + +import com.datastax.driver.core.KeyspaceMetadata; +import com.google.inject.Inject; +import com.google.inject.Singleton; +import io.netty.handler.codec.http.HttpResponseStatus; +import io.vertx.core.http.HttpServerRequest; +import io.vertx.core.net.SocketAddress; +import io.vertx.ext.web.RoutingContext; +import org.apache.cassandra.sidecar.common.server.data.Name; +import org.apache.cassandra.sidecar.concurrent.ExecutorPools; +import org.apache.cassandra.sidecar.handlers.AbstractHandler; +import org.apache.cassandra.sidecar.utils.CassandraInputValidator; +import org.apache.cassandra.sidecar.utils.InstanceMetadataFetcher; +import org.jetbrains.annotations.NotNull; + +import static org.apache.cassandra.sidecar.utils.HttpExceptions.wrapHttpException; + +/** + * Validate the request keyspace should exist in Cassandra, when the endpoint + * contains keyspace name. + * On successful validation, it stores the fetched {@link KeyspaceMetadata} + * in the {@link RoutingContext} + */ +@Singleton +public class ValidateKeyspaceExistenceHandler extends AbstractHandler<Name> +{ + @Inject + public ValidateKeyspaceExistenceHandler(InstanceMetadataFetcher metadataFetcher, + ExecutorPools executorPools, + CassandraInputValidator validator) + { + super(metadataFetcher, executorPools, validator); + } + + @Override + protected Name extractParamsOrThrow(RoutingContext context) + { + return keyspace(context, false); + } + + @Override + protected void handleInternal(RoutingContext context, + HttpServerRequest httpRequest, + @NotNull String host, + SocketAddress remoteAddress, + Name keyspace) + { + if (keyspace == null) + { + context.fail(wrapHttpException(HttpResponseStatus.BAD_REQUEST, "Keyspace parameter is required but not provided")); + return; + } + + ValidationUtils.validateKeyspaceExists(context, metadataFetcher, executorPools, host, keyspace.name()) + .onComplete(ar -> { + if (ar.succeeded() && !context.failed()) + { + context.next(); + } + // Context has already been failed by the utility method when validation fails Review Comment: Simplified this a little bit. Lmk if you have Qs from the latest revision. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org For additional commands, e-mail: pr-h...@cassandra.apache.org