All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.streamnative.pulsar.handlers.kop.schemaregistry.resources.AbstractResource Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (c) 2019 - 2024 StreamNative, Inc.. All Rights Reserved.
 */
/**
 * Licensed 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 io.streamnative.pulsar.handlers.kop.schemaregistry.resources;

import io.netty.handler.codec.http.HttpResponseStatus;
import io.streamnative.pulsar.handlers.kop.schemaregistry.SchemaRegistryHandler;
import io.streamnative.pulsar.handlers.kop.schemaregistry.model.SchemaStorage;
import io.streamnative.pulsar.handlers.kop.schemaregistry.model.SchemaStorageAccessor;
import io.streamnative.pulsar.handlers.kop.schemaregistry.model.impl.SchemaStorageException;
import java.util.List;
import java.util.Map;
import lombok.AllArgsConstructor;

@AllArgsConstructor
public abstract class AbstractResource {

    public static final String INT_PATTERN = "([0-9]+)";
    public static final String STRING_PATTERN = "(.+)";
    public static final String VERSION_PATTERN = "([0-9]+|latest)";

    public static final String GET = "GET";
    public static final String POST = "POST";
    public static final String DELETE = "DELETE";
    public static final String PUT = "PUT";

    public static final String DEFAULT_TENANT = "public";

    protected final SchemaStorageAccessor schemaStorageAccessor;
    protected final String defaultNamespace;


    protected SchemaStorage getSchemaStorage(String currentTenant) throws SchemaStorageException {
        if (currentTenant == null) {
            throw new SchemaStorageException("Missing or failed authentication",
                    HttpResponseStatus.UNAUTHORIZED);
        }
        return schemaStorageAccessor.getSchemaStorageForTenant(currentTenant);
    }

    /**
     * Register all processors.
     * @param schemaRegistryHandler
     */
    public abstract void register(SchemaRegistryHandler schemaRegistryHandler);

    boolean getBooleanQueryParam(String queryParam, String defaultValue, Map> queryParams) {
        return Boolean.parseBoolean(queryParams.getOrDefault(queryParam, List.of(defaultValue)).get(0));
    }

    String getStringQueryParam(String queryParam, String defaultValue, Map> queryParams) {
        if (queryParams.containsKey(queryParam) && !queryParams.get(queryParam).isEmpty()) {
            return queryParams.get(queryParam).get(0);
        }
        return defaultValue;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy