use of com.wing.apirecord.core.model.Response in project apiRecord by tobecoder2015.
the class ContentTypeFilter method doFilter.
@Override
public boolean doFilter(Message message, FilterChain chain) {
if (message.getMsg() instanceof Response) {
Response response = (Response) message.getMsg();
String[] contentTypes = pattern.split(",");
boolean ok = true;
for (String contentType : contentTypes) {
if (!response.getHeaders().containsKey("Content-Type") || response.getHeaders().containsKey("Content-Type") && response.getHeaders().get("Content-Type").contains(contentType)) {
ok = false;
break;
}
}
if (!ok) {
return chain.doFilter(message, chain);
} else {
log.debug("消息体不满足contentType过滤器 " + response.getHeaders().get("Content-Type"));
return true;
}
}
return chain.doFilter(message, chain);
}
use of com.wing.apirecord.core.model.Response in project apiRecord by tobecoder2015.
the class TransferUtil method toReponse.
public static Response toReponse(HttpResponse httpResponse) {
Response response = new Response();
response.setCode(httpResponse.status().code());
Iterator headers = httpResponse.headers().iteratorAsString();
while (headers.hasNext()) {
Map.Entry<String, String> header = (Map.Entry<String, String>) headers.next();
response.addHeader(header.getKey(), header.getValue());
}
return response;
}
use of com.wing.apirecord.core.model.Response in project apiRecord by tobecoder2015.
the class ApirecordApplication method main.
public static void main(String[] args) {
SpringApplication.run(ApirecordApplication.class, args);
System.setProperty("proxySet", "true");
System.setProperty("http.proxyHost", "127.0.0.1");
System.setProperty("http.proxyPort", "9999");
log.info("本地代理服务器ip:" + System.getProperty("http.proxyHost"));
log.info("本地代理服务器port:" + System.getProperty("http.proxyPort"));
// 配置文件
RecordQueue.getInstance().start();
FilterChain.addFilter(new ContentTypeFilter(FilterService.contentType));
FilterChain.addFilter(new UrlFilter(FilterService.urls));
FilterChain.addFilter(new MethodFilter(FilterService.methods));
FileService.setSavePath(null);
new NettyHttpProxyServer().initProxyInterceptFactory(() -> new HttpProxyIntercept() {
@Override
public boolean beforeRequest(Channel clientChannel, HttpRequest httpRequest) {
log.debug("request " + clientChannel.id().asShortText() + " " + httpRequest.toString());
Request request = TransferUtil.toRequest(httpRequest);
Message message = new Message(clientChannel.id().asShortText(), Message.REQUEST_HEAD, request);
FilterChain filterChain = FilterChain.getFilterChain();
if (filterChain.doFilter(message, filterChain)) {
//
} else {
RecordQueue.getInstance().add(message);
}
return true;
}
@Override
public boolean beforeRequest(Channel clientChannel, HttpContent httpContent) {
log.debug("request httpContent:" + clientChannel.id().asShortText() + " " + clientChannel.id().asShortText() + " " + httpContent.content().toString(Charset.defaultCharset()));
if (!RecordMap.containKey(clientChannel.id().asShortText()))
return true;
Message message = new Message(clientChannel.id().asShortText(), Message.REQUEST_BODY, httpContent.content().toString(io.netty.util.CharsetUtil.UTF_8));
FilterChain filterChain = FilterChain.getFilterChain();
if (filterChain.doFilter(message, filterChain)) {
//
} else {
RecordQueue.getInstance().add(message);
}
return true;
}
@Override
public boolean afterResponse(Channel clientChannel, Channel proxyChannel, HttpResponse httpResponse) {
log.debug("httpResponse:" + clientChannel.id().asShortText() + " " + httpResponse.toString());
if (!RecordMap.containKey(clientChannel.id().asShortText()))
return true;
Response response = TransferUtil.toReponse(httpResponse);
Message message = new Message(clientChannel.id().asShortText(), Message.RESPONSE_HEAD, response);
FilterChain filterChain = FilterChain.getFilterChain();
if (filterChain.doFilter(message, filterChain)) {
message.setFilter(true);
RecordQueue.getInstance().add(message);
} else {
RecordQueue.getInstance().add(message);
}
return true;
}
@Override
public boolean afterResponse(Channel clientChannel, Channel proxyChannel, HttpContent httpContent) {
log.debug("httpContent:" + clientChannel.id().asShortText() + " " + httpContent.content().toString(Charset.defaultCharset()));
if (!RecordMap.containKey(clientChannel.id().asShortText()))
return true;
ByteBuf buf = httpContent.content().copy();
byte[] bytes = new byte[buf.readableBytes()];
buf.readBytes(bytes);
Message message = new Message(clientChannel.id().asShortText(), Message.RESPONSE_BODY, bytes);
buf.writeBytes(bytes);
if (httpContent instanceof LastHttpContent)
message.setFinish(true);
FilterChain filterChain = FilterChain.getFilterChain();
if (filterChain.doFilter(message, filterChain)) {
//
} else {
RecordQueue.getInstance().add(message);
}
return true;
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.fireExceptionCaught(cause);
// log.error("异常",cause);
}
}).start(9999);
}
Aggregations