io.streamnative.pulsar.handlers.kop.schemaregistry.SchemaRegistryHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pulsar-kafka-schema-registry Show documentation
Show all versions of pulsar-kafka-schema-registry Show documentation
Kafka Compatible Schema Registry
/**
* 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;
import static io.netty.handler.codec.http.HttpResponseStatus.NOT_FOUND;
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
import static io.streamnative.pulsar.handlers.kop.schemaregistry.HttpRequestProcessor.buildErrorResponse;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.util.CharsetUtil;
import io.streamnative.pulsar.handlers.kop.schemaregistry.model.impl.SchemaStorageException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@ChannelHandler.Sharable
public class SchemaRegistryHandler extends SimpleChannelInboundHandler {
private final List processors = new ArrayList<>();
private final SchemaRegistryRequestAuthenticator authenticator;
private final SchemaRegistryStats stats;
public SchemaRegistryHandler(SchemaRegistryRequestAuthenticator authenticator, SchemaRegistryStats stats) {
this.authenticator = authenticator;
this.stats = stats;
}
public SchemaRegistryHandler addProcessor(HttpRequestProcessor processor) {
this.processors.add(processor);
return this;
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
ctx.flush();
}
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) {
if (log.isDebugEnabled()) {
log.debug("SchemaRegistry at {} request {}", ctx.channel().localAddress(), request);
}
String currentTenant;
try {
currentTenant = authenticator.authenticate(request, ctx);
} catch (SchemaStorageException e) {
FullHttpResponse resp = buildErrorResponse(e.getHttpStatusCode(), e.getMessage());
ctx.writeAndFlush(resp);
return;
}
log.info("SchemaRegistry {} {} from {}", request.method(), request.uri(), ctx.channel().localAddress());
HttpRequestProcessor processor = null;
for (HttpRequestProcessor p : processors) {
if (p.acceptRequest(request)) {
processor = p;
break;
}
}
if (processor == null) {
String body = "{\n"
+ " \"message\" : \"Not found\",\n"
+ " \"error_code\" : 404\n"
+ "}";
FullHttpResponse httpResponse = new DefaultFullHttpResponse(HTTP_1_1,
NOT_FOUND,
Unpooled.copiedBuffer(body, CharsetUtil.UTF_8));
httpResponse.headers().set(HttpHeaderNames.CONTENT_TYPE, "application/vnd.schemaregistry.v1+json");
httpResponse.headers().set(HttpHeaderNames.CONTENT_LENGTH, body.length());
HttpRequestProcessor.addCORSHeaders(httpResponse);
log.info("not found {} {} from {}", request.method(), request.uri(), ctx.channel().localAddress());
if (log.isDebugEnabled()) {
log.debug("SchemaRegistry at {} request {} response {}", ctx.channel().localAddress(), request,
httpResponse);
}
ctx.writeAndFlush(httpResponse);
return;
}
CompletableFuture fullHttpResponse = processor.processRequest(request, currentTenant);
fullHttpResponse.thenAccept(resp -> {
if (resp.status().code() >= 200 && resp.status().code() < 300) {
stats.getApiCallsSuccess().inc();
} else {
stats.getApiCallsFailure().inc();
}
if (log.isDebugEnabled()) {
log.debug("SchemaRegistry at {} request {} response {}", ctx.channel().localAddress(), request,
resp);
}
log.info("SchemaRegistry {} {} from {} response {} {}", request.method(), request.uri(),
ctx.channel().localAddress(),
resp.status().code(), resp.status().reasonPhrase());
ctx.writeAndFlush(resp);
}).exceptionally(err -> {
stats.getApiCallsFailure().inc();
FullHttpResponse resp = HttpRequestProcessor.buildJsonErrorResponse(err);
if (log.isDebugEnabled()) {
log.debug("SchemaRegistry at {} request {} response {}", ctx.channel().localAddress(), request,
resp);
}
log.info("SchemaRegistry {} {} from {} response {} {}", request.method(), request.uri(),
ctx.channel().localAddress(),
resp.status().code(), resp.status().reasonPhrase());
ctx.writeAndFlush(resp);
return null;
});
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
log.error("Unhandled error, closing connection to {}", ctx.channel(), cause);
ctx.close();
}
}