1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
public class NettyHttpServerHandler extends SimpleChannelInboundHandler<FullHttpRequest> {
@Override protected void channelRead0(ChannelHandlerContext channelHandlerContext, FullHttpRequest fullHttpRequest) throws Exception { System.out.println(fullHttpRequest);
FullHttpResponse response = null; String str = "/index.html";
InputStream is = null; if(fullHttpRequest.getMethod().equals(HttpMethod.GET)) { System.out.println(getGetParamsFromChannel(fullHttpRequest)); System.out.println("uri-------" + fullHttpRequest.getUri()); String origin = fullHttpRequest.uri(); int index = fullHttpRequest.getUri().indexOf('?'); if(index != -1) str = origin.substring(0, index); System.out.println("real--------" + str); if(fullHttpRequest.getUri().equals("/") || str.equals(fullHttpRequest.getUri())) { is = this.getClass().getResourceAsStream("/index.html"); } else { is = this.getClass().getResourceAsStream("/404.html"); } String resource = new Scanner(is).useDelimiter("\\Z").next(); ByteBuf buf = copiedBuffer(resource, CharsetUtil.UTF_8); response = responseOK(HttpResponseStatus.OK, buf); } else if (fullHttpRequest.getMethod().equals(HttpMethod.POST)) { System.out.println(getPostParamsFromChannel(fullHttpRequest)); if (str.equals(fullHttpRequest.getUri())) { is = this.getClass().getResourceAsStream("/index.html");
} else { is = this.getClass().getResourceAsStream("/404.html"); } String resource = new Scanner(is).useDelimiter("\\Z").next(); ByteBuf buf = copiedBuffer(resource, CharsetUtil.UTF_8); response = responseOK(HttpResponseStatus.OK, buf); } else { response = responseOK(HttpResponseStatus.INTERNAL_SERVER_ERROR, null); }
channelHandlerContext.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
private Map<String, Object> getGetParamsFromChannel(FullHttpRequest fullHttpRequest) { Map<String, Object> params = new HashMap<String, Object>();
if(fullHttpRequest.getMethod().equals(HttpMethod.GET)) { QueryStringDecoder decoder = new QueryStringDecoder(fullHttpRequest.getUri()); Map<String, List<String>> paramList = decoder.parameters(); for(Map.Entry<String, List<String>> entry : paramList.entrySet()) { params.put(entry.getKey(), entry.getValue().get(0)); } return params; } else { return null; } }
private Map<String, Object> getPostParamsFromChannel(FullHttpRequest fullHttpRequest) { Map<String, Object> params = new HashMap<String, Object>();
if(fullHttpRequest.getMethod().equals(HttpMethod.POST)) { String strContentType = fullHttpRequest.headers().get("Content-Type").trim(); if(strContentType.contains("x-www-form-urlencoded")) { } else if (strContentType.contains("application/json")) { } else { return null; } return params; } else { return null; } }
private Map<String, Object> getFormParams(FullHttpRequest fullHttpRequest) { Map<String, Object> params = new HashMap<String, Object>();
HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(new DefaultHttpDataFactory(false), fullHttpRequest); List<InterfaceHttpData> postData = decoder.getBodyHttpDatas();
for(InterfaceHttpData data : postData) { if(data.getHttpDataType() == InterfaceHttpData.HttpDataType.Attribute) { MemoryAttribute attribute = (MemoryAttribute) data; params.put(attribute.getName(), attribute.getValue()); } } return params; }
private Map<String, Object> getJsonParams(FullHttpRequest fullHttpRequest) { Map<String, Object> params = new HashMap<String, Object>();
ByteBuf content = fullHttpRequest.content(); byte[] reqContent = new byte[content.readableBytes()]; content.readBytes(reqContent); String strContent = new String(reqContent, StandardCharsets.UTF_8);
JSONObject jsonParams = JSONObject.fromObject(strContent); for (Object key : jsonParams.keySet()) params.put(key.toString(), jsonParams.get(key));
return params; }
private FullHttpResponse responseOK(HttpResponseStatus status, ByteBuf content) { FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, content); if(content != null) { response.headers().set("Content-Type", "text/plain;charset=UTF-8"); response.headers().set("Content_Length", response.content().readableBytes()); } return response; } }
|