HttpUtil新增重载方法
This commit is contained in:
parent
283b228b2d
commit
bcb0c51766
@ -179,8 +179,7 @@ public class WeixinMessageDispatcher {
|
||||
if (alwaysResponse) {
|
||||
context.write(BlankResponse.global);
|
||||
} else {
|
||||
context.writeAndFlush(
|
||||
HttpUtil.createHttpResponse(null, NOT_FOUND, null))
|
||||
context.writeAndFlush(HttpUtil.createHttpResponse(NOT_FOUND))
|
||||
.addListener(ChannelFutureListener.CLOSE);
|
||||
}
|
||||
}
|
||||
|
||||
@ -63,7 +63,7 @@ public class WeixinRequestHandler extends
|
||||
return;
|
||||
}
|
||||
ctx.writeAndFlush(
|
||||
HttpUtil.createHttpResponse(null, FORBIDDEN, null))
|
||||
HttpUtil.createHttpResponse(FORBIDDEN))
|
||||
.addListener(ChannelFutureListener.CLOSE);
|
||||
return;
|
||||
} else if (request.getMethod().equals(HttpMethod.POST.name())) {
|
||||
@ -72,7 +72,7 @@ public class WeixinRequestHandler extends
|
||||
request.getTimeStamp(), request.getNonce()).equals(
|
||||
request.getSignature())) {
|
||||
ctx.writeAndFlush(
|
||||
HttpUtil.createHttpResponse(null, FORBIDDEN, null))
|
||||
HttpUtil.createHttpResponse(FORBIDDEN))
|
||||
.addListener(ChannelFutureListener.CLOSE);
|
||||
return;
|
||||
}
|
||||
@ -82,14 +82,14 @@ public class WeixinRequestHandler extends
|
||||
request.getEncryptContent()).equals(
|
||||
request.getMsgSignature())) {
|
||||
ctx.writeAndFlush(
|
||||
HttpUtil.createHttpResponse(null, FORBIDDEN, null))
|
||||
HttpUtil.createHttpResponse(FORBIDDEN))
|
||||
.addListener(ChannelFutureListener.CLOSE);
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
ctx.writeAndFlush(
|
||||
HttpUtil.createHttpResponse(null, METHOD_NOT_ALLOWED, null))
|
||||
HttpUtil.createHttpResponse(METHOD_NOT_ALLOWED))
|
||||
.addListener(ChannelFutureListener.CLOSE);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -5,19 +5,17 @@ import static io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_LENGTH;
|
||||
import static io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_TYPE;
|
||||
import static io.netty.handler.codec.http.HttpHeaders.Names.DATE;
|
||||
import static io.netty.handler.codec.http.HttpHeaders.Names.USER_AGENT;
|
||||
import static io.netty.handler.codec.http.HttpResponseStatus.OK;
|
||||
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.netty.handler.codec.http.DefaultFullHttpResponse;
|
||||
import io.netty.handler.codec.http.FullHttpResponse;
|
||||
import io.netty.handler.codec.http.HttpHeaders;
|
||||
import io.netty.handler.codec.http.HttpHeaders.Values;
|
||||
import io.netty.handler.codec.http.HttpResponse;
|
||||
import io.netty.handler.codec.http.HttpResponseStatus;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.apache.http.HttpHeaders;
|
||||
|
||||
import com.foxinmy.weixin4j.startup.WeixinServerBootstrap;
|
||||
|
||||
/**
|
||||
@ -34,32 +32,54 @@ public class HttpUtil {
|
||||
private static String SERVER = "netty4";
|
||||
private static String WEIXIN4J = "weixin4j-server";
|
||||
|
||||
/**
|
||||
* 创建只有状态的HttpResponse响应
|
||||
*
|
||||
* @param status
|
||||
* 响应状态
|
||||
* @return HttpResponse
|
||||
*/
|
||||
public static HttpResponse createHttpResponse(HttpResponseStatus status) {
|
||||
FullHttpResponse httpResponse = new DefaultFullHttpResponse(HTTP_1_1,
|
||||
status);
|
||||
createHeaders(httpResponse);
|
||||
return httpResponse;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建有内容的HttpResponse响应
|
||||
*
|
||||
* @param content
|
||||
* 响应内容
|
||||
* @param status
|
||||
* 响应状态
|
||||
* @param contentType
|
||||
* 响应类型
|
||||
* @return HttpResponse
|
||||
*/
|
||||
public static HttpResponse createHttpResponse(String content,
|
||||
HttpResponseStatus status, String contentType) {
|
||||
if (content == null) {
|
||||
content = "";
|
||||
}
|
||||
if (StringUtil.isBlank(contentType)) {
|
||||
contentType = Consts.CONTENTTYPE$TEXT_PLAIN;
|
||||
}
|
||||
if (status == null) {
|
||||
status = OK;
|
||||
}
|
||||
FullHttpResponse httpResponse = new DefaultFullHttpResponse(HTTP_1_1,
|
||||
status, Unpooled.copiedBuffer(content, Consts.UTF_8));
|
||||
FullHttpResponse httpResponse = null;
|
||||
|
||||
httpResponse = new DefaultFullHttpResponse(HTTP_1_1, status,
|
||||
Unpooled.copiedBuffer(content, Consts.UTF_8));
|
||||
httpResponse.headers().set(
|
||||
CONTENT_TYPE,
|
||||
String.format("%s;encoding=%s", contentType,
|
||||
Consts.UTF_8.displayName()));
|
||||
httpResponse.headers().set(CONTENT_LENGTH,
|
||||
content.getBytes(Consts.UTF_8).length);
|
||||
createHeaders(httpResponse);
|
||||
return httpResponse;
|
||||
}
|
||||
|
||||
private static void createHeaders(FullHttpResponse httpResponse) {
|
||||
httpResponse.headers().set(CONNECTION, Values.KEEP_ALIVE);
|
||||
httpResponse.headers().set(DATE, new Date());
|
||||
httpResponse.headers().set(HttpHeaders.SERVER, SERVER);
|
||||
httpResponse.headers().set(HttpHeaders.Names.SERVER, SERVER);
|
||||
httpResponse.headers()
|
||||
.set(USER_AGENT,
|
||||
String.format("%s/%s", WEIXIN4J,
|
||||
WeixinServerBootstrap.VERSION));
|
||||
return httpResponse;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user