Skip to content

Commit 86f88c8

Browse files
committed
remove some code
1 parent 6976cab commit 86f88c8

File tree

5 files changed

+28
-32
lines changed

5 files changed

+28
-32
lines changed

src/main/java/io/mqtt/handler/HttpRequestHandler.java

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,22 @@
22

33
import static io.netty.handler.codec.http.HttpHeaders.isKeepAlive;
44
import static io.netty.handler.codec.http.HttpHeaders.setContentLength;
5-
import static io.netty.handler.codec.http.HttpMethod.GET;
65
import static io.netty.handler.codec.http.HttpResponseStatus.BAD_REQUEST;
7-
import static io.netty.handler.codec.http.HttpResponseStatus.FORBIDDEN;
8-
import static io.netty.handler.codec.http.HttpResponseStatus.NOT_FOUND;
96
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
10-
import io.mqtt.handler.http.HttpTransport;
11-
import io.mqtt.handler.http.HttpJsonpTransport;
127
import io.mqtt.handler.http.HttpSessionStore;
8+
import io.mqtt.handler.http.HttpTransport;
139
import io.netty.buffer.ByteBuf;
1410
import io.netty.buffer.Unpooled;
1511
import io.netty.channel.ChannelFuture;
1612
import io.netty.channel.ChannelFutureListener;
13+
import io.netty.channel.ChannelHandler.Sharable;
1714
import io.netty.channel.ChannelHandlerContext;
1815
import io.netty.channel.SimpleChannelInboundHandler;
19-
import io.netty.channel.ChannelHandler.Sharable;
2016
import io.netty.handler.codec.http.DefaultFullHttpResponse;
2117
import io.netty.handler.codec.http.FullHttpRequest;
2218
import io.netty.handler.codec.http.FullHttpResponse;
2319
import io.netty.handler.codec.http.HttpRequest;
2420
import io.netty.handler.timeout.ReadTimeoutException;
25-
import io.netty.util.AttributeKey;
2621
import io.netty.util.CharsetUtil;
2722
import io.netty.util.internal.logging.InternalLogger;
2823
import io.netty.util.internal.logging.InternalLoggerFactory;
@@ -40,10 +35,6 @@ public class HttpRequestHandler extends
4035

4136
private static Map<String, HttpTransport> transportMap = new HashMap<String, HttpTransport>(
4237
1);
43-
static {
44-
HttpJsonpTransport httpJsonpTransport = new HttpJsonpTransport();
45-
transportMap.put(HttpJsonpTransport.PREFIX, httpJsonpTransport);
46-
}
4738

4839
public HttpRequestHandler(String websocketUri) {
4940
this.websocketUri = websocketUri;
@@ -53,30 +44,18 @@ public HttpRequestHandler(String websocketUri) {
5344
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest req)
5445
throws Exception {
5546
if (!req.getDecoderResult().isSuccess()) {
47+
logger.debug("invalid http request");
5648
sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1,
5749
BAD_REQUEST));
5850
return;
5951
}
6052

6153
if (req.getUri().equalsIgnoreCase(this.websocketUri)) {
54+
logger.debug("it is websocket request");
6255
ctx.fireChannelRead(req.retain());
6356
return;
6457
}
6558

66-
// Allow only GET methods.
67-
if (req.getMethod() != GET) {
68-
sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1,
69-
FORBIDDEN));
70-
return;
71-
}
72-
73-
if ("/favicon.ico".equals(req.getUri())) {
74-
FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1,
75-
NOT_FOUND);
76-
sendHttpResponse(ctx, req, res);
77-
return;
78-
}
79-
8059
HttpTransport transport = getTransport(req);
8160
if (transport == null) {
8261
sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1,
@@ -116,7 +95,6 @@ private HttpTransport getTransport(HttpRequest req) {
11695
}
11796

11897
return null;
119-
12098
}
12199

122100
private static void sendHttpResponse(ChannelHandlerContext ctx,
@@ -136,4 +114,11 @@ private static void sendHttpResponse(ChannelHandlerContext ctx,
136114
f.addListener(ChannelFutureListener.CLOSE);
137115
}
138116
}
117+
118+
public static void registerTransport(HttpTransport httpTransport) {
119+
if (httpTransport == null)
120+
return;
121+
122+
transportMap.put(httpTransport.getPrefixUri(), httpTransport);
123+
}
139124
}

src/main/java/io/mqtt/handler/http/HttpJsonpTransport.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,25 @@ public class HttpJsonpTransport extends HttpTransport {
4646
private static final InternalLogger logger = InternalLoggerFactory
4747
.getInstance(HttpJsonpTransport.class);
4848

49-
public static final String PREFIX = "/jsonp/";
49+
private static final String PREFIX = "/jsonp/";
5050

5151
private final static String HEADER_CONTENT_TYPE = "text/javascript; charset=UTF-8";
5252
private final static String TEMPLATE = "%s(%s);";
5353
private final static Gson gson = new Gson();
5454

55+
@Override
56+
public String getPrefixUri() {
57+
return PREFIX;
58+
}
59+
5560
@Override
5661
public void handleRequest(ChannelHandlerContext ctx, FullHttpRequest req)
5762
throws Exception {
5863
if (req.getUri().contains("/jsonp/connect")) {
5964
handleConnect(ctx, req);
6065
} else if (req.getUri().contains("/jsonp/subscribe")) {
6166
handleSubscrible(ctx, req);
62-
} else if (req.getUri().contains("/jsonp/polling")) {
67+
} else if (req.getUri().contains("/jsonp/waiting")) {
6368
handleWaitingMsg(ctx, req);
6469
} else if (req.getUri().contains("/jsonp/unsubscrible")) {
6570
handleUnsubscrible(ctx, req);
@@ -95,7 +100,6 @@ private void handlePublish(ChannelHandlerContext ctx, FullHttpRequest req) {
95100

96101
String topic = HttpSessionStore.getParameter(req, "topic");
97102
String payload = HttpSessionStore.getParameter(req, "payload");
98-
String qosStr = HttpSessionStore.getParameter(req, "qos");
99103

100104
if (StringUtils.isBlank(topic) || StringUtils.isBlank(payload)) {
101105
ByteBuf content = ctx

src/main/java/io/mqtt/handler/http/HttpTransport.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,7 @@ public void handleTimeout(ChannelHandlerContext ctx) {
1111

1212
public abstract void handleRequest(ChannelHandlerContext ctx,
1313
FullHttpRequest req) throws Exception;
14+
15+
16+
public abstract String getPrefixUri();
1417
}

src/main/java/io/mqtt/server/HttpChannelInitializer.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.mqtt.handler.MqttMessageHandler;
55
import io.mqtt.handler.coder.MqttMessageWebSocketFrameDecoder;
66
import io.mqtt.handler.coder.MqttMessageWebSocketFrameEncoder;
7+
import io.mqtt.handler.http.HttpJsonpTransport;
78
import io.netty.channel.ChannelInitializer;
89
import io.netty.channel.socket.SocketChannel;
910
import io.netty.handler.codec.http.HttpObjectAggregator;
@@ -16,12 +17,16 @@ public class HttpChannelInitializer extends ChannelInitializer<SocketChannel> {
1617
private HttpRequestHandler httpRequestHandler = new HttpRequestHandler(
1718
websocketUri);
1819

20+
static {
21+
HttpJsonpTransport httpJsonpTransport = new HttpJsonpTransport();
22+
HttpRequestHandler.registerTransport(httpJsonpTransport);
23+
}
24+
1925
@Override
2026
public void initChannel(final SocketChannel ch) throws Exception {
2127
ch.pipeline().addLast(new HttpServerCodec(),
2228
new MqttMessageWebSocketFrameEncoder(),
23-
new HttpObjectAggregator(65536),
24-
httpRequestHandler,
29+
new HttpObjectAggregator(65536), httpRequestHandler,
2530
new WebSocketServerProtocolHandler(websocketUri),
2631
new MqttMessageWebSocketFrameDecoder(),
2732
new MqttMessageHandler());

src/main/java/io/mqtt/tool/MemPool.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import io.netty.channel.Channel;
66
import io.netty.channel.ChannelFuture;
77
import io.netty.channel.ChannelFutureListener;
8-
import io.netty.handler.codec.http.HttpRequest;
98

109
import java.util.HashSet;
1110
import java.util.Set;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy