Skip to content

Commit

Permalink
Added HTTP Support (apache#3336)
Browse files Browse the repository at this point in the history
* Added HTTP Support

* Updated documentation and removed duplicate code

* Added unit test for NettyHttpChannelInitializer
  • Loading branch information
david-streamlio authored and srkukarni committed Jan 11, 2019
1 parent 210d828 commit 7ec205a
Show file tree
Hide file tree
Showing 13 changed files with 368 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,21 @@
*/
package org.apache.pulsar.io.netty;

import java.util.Map;

import org.apache.pulsar.io.core.PushSource;
import org.apache.pulsar.io.core.SourceContext;
import org.apache.pulsar.io.core.annotations.Connector;
import org.apache.pulsar.io.core.annotations.IOType;
import org.apache.pulsar.io.netty.server.NettyServer;
import java.util.Map;

/**
* A simple Netty Tcp or Udp Source connector to listen Tcp/Udp messages and write to user-defined Pulsar topic
* A simple Netty Source connector to listen for incoming messages and write to user-defined Pulsar topic.
*/
@Connector(
name = "netty",
type = IOType.SOURCE,
help = "A simple Netty Tcp or Udp Source connector to listen Tcp/Udp messages and write to user-defined Pulsar topic",
help = "A simple Netty Source connector to listen for incoming messages and write to user-defined Pulsar topic",
configClass = NettySourceConfig.class)
public class NettySource extends PushSource<byte[]> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,22 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import lombok.*;
import lombok.experimental.Accessors;
import org.apache.pulsar.io.core.annotations.FieldDoc;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.Map;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;
import org.apache.pulsar.io.core.annotations.FieldDoc;

/**
* Netty Tcp or Udp Source Connector Config.
* Netty Source Connector Config.
*/
@Data
@Setter
Expand All @@ -45,7 +50,7 @@ public class NettySourceConfig implements Serializable {
@FieldDoc(
required = true,
defaultValue = "tcp",
help = "The tcp or udp network protocols")
help = "The network protocol to use, supported values are 'tcp', 'udp', and 'http'")
private String type = "tcp";

@FieldDoc(
Expand All @@ -63,8 +68,8 @@ public class NettySourceConfig implements Serializable {
@FieldDoc(
required = true,
defaultValue = "1",
help = "The number of threads of Netty Tcp Server to accept incoming connections and " +
"handle the traffic of the accepted connections")
help = "The number of threads of Netty Tcp Server to accept incoming connections and "
+ "handle the traffic of the accepted connections")
private int numberOfThreads = 1;

public static NettySourceConfig load(Map<String, Object> map) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* 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.pulsar.io.netty.http;

import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.ssl.SslContext;

/**
* Netty Channel Initializer to register HTTP decoder and handler.
*/
public class NettyHttpChannelInitializer extends ChannelInitializer<SocketChannel> {

private final SslContext sslCtx;
private ChannelInboundHandlerAdapter handler;

public NettyHttpChannelInitializer(ChannelInboundHandlerAdapter handler, SslContext sslCtx) {
this.handler = handler;
this.sslCtx = sslCtx;
}

@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
if (sslCtx != null) {
socketChannel.pipeline().addLast(sslCtx.newHandler(socketChannel.alloc()));
}
socketChannel.pipeline().addLast(new HttpServerCodec());
socketChannel.pipeline().addLast(this.handler);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/**
* 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.pulsar.io.netty.http;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
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.FullHttpResponse;
import io.netty.handler.codec.http.HttpContent;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaderValues;
import io.netty.handler.codec.http.HttpObject;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpUtil;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.handler.codec.http.LastHttpContent;
import io.netty.util.CharsetUtil;

import java.io.Serializable;
import java.util.Optional;

import lombok.Data;

import org.apache.pulsar.functions.api.Record;
import org.apache.pulsar.io.netty.NettySource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Handles a server-side HTTP channel.
*/
@ChannelHandler.Sharable
public class NettyHttpServerHandler extends SimpleChannelInboundHandler<Object> {

private static final Logger logger = LoggerFactory.getLogger(NettyHttpServerHandler.class);

private NettySource nettySource;

public NettyHttpServerHandler(NettySource nettySource) {
this.nettySource = nettySource;
}

private HttpRequest request;

@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
ctx.flush();
}

@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) {

if (msg instanceof HttpRequest) {
HttpRequest request = this.request = (HttpRequest) msg;

if (HttpUtil.is100ContinueExpected(request)) {
send100Continue(ctx);
}
}

if (msg instanceof HttpContent) {
HttpContent httpContent = (HttpContent) msg;

ByteBuf content = httpContent.content();
if (content.isReadable()) {
nettySource.consume(new NettyHttpRecord(Optional.ofNullable(""),
content.toString(CharsetUtil.UTF_8).getBytes()));
}

if (msg instanceof LastHttpContent) {
LastHttpContent trailer = (LastHttpContent) msg;

if (!writeResponse(trailer, ctx)) {
// If keep-alive is off, close the connection once the content is fully written.
ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}
}
}
}

private boolean writeResponse(HttpObject currentObj, ChannelHandlerContext ctx) {
// Decide whether to close the connection or not.
boolean keepAlive = HttpUtil.isKeepAlive(request);
// Build the response object.
FullHttpResponse response = new DefaultFullHttpResponse(
HttpVersion.HTTP_1_1,
currentObj.decoderResult().isSuccess() ? HttpResponseStatus.OK : HttpResponseStatus.BAD_REQUEST,
Unpooled.EMPTY_BUFFER);

response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8");

if (keepAlive) {
// Add 'Content-Length' header only for a keep-alive connection.
response.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
// Add keep alive header as per:
// - http://www.w3.org/Protocols/HTTP/1.1/draft-ietf-http-v11-spec-01.html#Connection
response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
}

// Write the response.
ctx.write(response);

return keepAlive;
}

private static void send100Continue(ChannelHandlerContext ctx) {
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.CONTINUE);
ctx.write(response);
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
logger.error("Error when processing incoming data", cause);
ctx.close();
}

@Data
static private class NettyHttpRecord implements Record<byte[]>, Serializable {
private final Optional<String> key;
private final byte[] value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* 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.pulsar.io.netty.http;
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* 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.pulsar.io.netty;
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import io.netty.handler.codec.bytes.ByteArrayDecoder;

/**
* Netty Channel Initializer to register decoder and handler
* Netty Channel Initializer to register decoder and handler.
*/
public class NettyChannelInitializer extends ChannelInitializer<SocketChannel> {

Expand Down
Loading

0 comments on commit 7ec205a

Please sign in to comment.