use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.QueryStringEncoder in project moco by dreamhead.
the class AbstractProxyResponseHandler method remoteUrl.
private Optional<URL> remoteUrl(final HttpRequest request) {
Optional<String> remoteUrl = this.doRemoteUrl(request);
if (!remoteUrl.isPresent()) {
return absent();
}
QueryStringEncoder encoder = new QueryStringEncoder(remoteUrl.get());
for (Map.Entry<String, String[]> entry : request.getQueries().entrySet()) {
for (String value : entry.getValue()) {
encoder.addParam(entry.getKey(), value);
}
}
try {
return of(toUrl(encoder.toString()));
} catch (IllegalArgumentException e) {
return absent();
}
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.QueryStringEncoder in project vertx-web by vert-x3.
the class HTTPRequestValidationTest method testQueryParamsArrayAndPathParamsWithIncludedTypes.
@Test
public void testQueryParamsArrayAndPathParamsWithIncludedTypes() throws Exception {
HTTPRequestValidationHandler validationHandler = HTTPRequestValidationHandler.create().addPathParam("pathParam1", ParameterType.INT).addQueryParamsArray("awesomeArray", ParameterType.EMAIL, true).addQueryParam("anotherParam", ParameterType.DOUBLE, true);
router.get("/testQueryParams/:pathParam1").handler(validationHandler);
router.get("/testQueryParams/:pathParam1").handler(routingContext -> {
RequestParameters params = routingContext.get("parsedParameters");
routingContext.response().setStatusMessage(params.pathParameter("pathParam1").getInteger().toString() + params.queryParameter("awesomeArray").getArray().size() + params.queryParameter("anotherParam").getDouble().toString()).end();
}).failureHandler(generateFailureHandler(false));
String pathParam = getSuccessSample(ParameterType.INT).getInteger().toString();
String arrayValue1 = getSuccessSample(ParameterType.EMAIL).getString();
String arrayValue2 = getSuccessSample(ParameterType.EMAIL).getString();
String anotherParam = getSuccessSample(ParameterType.DOUBLE).getDouble().toString();
QueryStringEncoder encoder = new QueryStringEncoder("/testQueryParams/" + URLEncoder.encode(pathParam, "UTF-8"));
encoder.addParam("awesomeArray", arrayValue1);
encoder.addParam("awesomeArray", arrayValue2);
encoder.addParam("anotherParam", anotherParam);
testRequest(HttpMethod.GET, encoder.toString(), 200, pathParam + "2" + anotherParam);
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.QueryStringEncoder in project vertx-web by vert-x3.
the class HTTPRequestValidationTest method testQueryParamsWithIncludedTypes.
@Test
public void testQueryParamsWithIncludedTypes() throws Exception {
HTTPRequestValidationHandler validationHandler = HTTPRequestValidationHandler.create().addQueryParam("param1", ParameterType.BOOL, true).addQueryParam("param2", ParameterType.INT, true);
router.get("/testQueryParams").handler(validationHandler);
router.get("/testQueryParams").handler(routingContext -> {
RequestParameters params = routingContext.get("parsedParameters");
routingContext.response().setStatusMessage(params.queryParameter("param1").getBoolean().toString() + params.queryParameter("param2").getInteger().toString()).end();
}).failureHandler(generateFailureHandler(false));
QueryStringEncoder encoder = new QueryStringEncoder("/testQueryParams");
String param1 = getSuccessSample(ParameterType.BOOL).getBoolean().toString();
String param2 = getSuccessSample(ParameterType.INT).getInteger().toString();
encoder.addParam("param1", param1);
encoder.addParam("param2", param2);
testRequest(HttpMethod.GET, encoder.toString(), 200, param1 + param2);
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.QueryStringEncoder in project vertx-web by vert-x3.
the class HttpContext method sendRequest.
private void sendRequest() {
Future<HttpClientResponse> responseFuture = Future.<HttpClientResponse>future().setHandler(ar -> {
Context context = Vertx.currentContext();
if (ar.succeeded()) {
HttpClientResponse resp = ar.result();
Future<HttpResponse<Object>> fut = Future.future();
fut.setHandler(r -> {
// We are running on a context (the HTTP client mandates it)
context.runOnContext(v -> currentResponseHandler.handle(r));
});
resp.exceptionHandler(err -> {
if (!fut.isComplete()) {
fut.fail(err);
}
});
resp.pause();
((BodyCodec<Object>) request.codec).create(ar2 -> {
resp.resume();
if (ar2.succeeded()) {
BodyStream<Object> stream = ar2.result();
stream.exceptionHandler(err -> {
if (!fut.isComplete()) {
fut.fail(err);
}
});
resp.endHandler(v -> {
if (!fut.isComplete()) {
stream.end();
if (stream.result().succeeded()) {
fut.complete(new HttpResponseImpl<>(resp, null, stream.result().result()));
} else {
fut.fail(stream.result().cause());
}
}
});
Pump responsePump = Pump.pump(resp, stream);
responsePump.start();
} else {
currentResponseHandler.handle(Future.failedFuture(ar2.cause()));
}
});
} else {
currentResponseHandler.handle(Future.failedFuture(ar.cause()));
}
});
HttpClientRequest req;
String requestURI;
if (request.queryParams() != null && request.queryParams().size() > 0) {
QueryStringEncoder enc = new QueryStringEncoder(request.uri);
request.queryParams().forEach(param -> enc.addParam(param.getKey(), param.getValue()));
requestURI = enc.toString();
} else {
requestURI = request.uri;
}
int port = request.port;
String host = request.host;
if (request.ssl != request.options.isSsl()) {
req = request.client.client.request(request.method, new RequestOptions().setSsl(request.ssl).setHost(host).setPort(port).setURI(requestURI));
} else {
if (request.protocol != null && !request.protocol.equals("http") && !request.protocol.equals("https")) {
// we have to create an abs url again to parse it in HttpClient
try {
URI uri = new URI(request.protocol, null, host, port, requestURI, null, null);
req = request.client.client.requestAbs(request.method, uri.toString());
} catch (URISyntaxException ex) {
currentResponseHandler.handle(Future.failedFuture(ex));
return;
}
} else {
req = request.client.client.request(request.method, port, host, requestURI);
}
}
if (request.virtualHost != null) {
String virtalHost = request.virtualHost;
if (port != 80) {
virtalHost += ":" + port;
}
req.setHost(virtalHost);
}
req.setFollowRedirects(request.followRedirects);
if (request.headers != null) {
req.headers().addAll(request.headers);
}
req.handler(responseFuture::tryComplete);
if (request.timeout > 0) {
req.setTimeout(request.timeout);
}
if (body != null) {
if (contentType != null) {
String prev = req.headers().get(HttpHeaders.CONTENT_TYPE);
if (prev == null) {
req.putHeader(HttpHeaders.CONTENT_TYPE, contentType);
} else {
contentType = prev;
}
}
if (body instanceof ReadStream<?>) {
ReadStream<Buffer> stream = (ReadStream<Buffer>) body;
if (request.headers == null || !request.headers.contains(HttpHeaders.CONTENT_LENGTH)) {
req.setChunked(true);
}
Pump pump = Pump.pump(stream, req);
req.exceptionHandler(err -> {
pump.stop();
stream.endHandler(null);
stream.resume();
responseFuture.tryFail(err);
});
stream.exceptionHandler(err -> {
req.reset();
responseFuture.tryFail(err);
});
stream.endHandler(v -> {
req.exceptionHandler(responseFuture::tryFail);
req.end();
pump.stop();
});
pump.start();
} else {
Buffer buffer;
if (body instanceof Buffer) {
buffer = (Buffer) body;
} else if (body instanceof MultiMap) {
try {
MultiMap attributes = (MultiMap) body;
boolean multipart = "multipart/form-data".equals(contentType);
DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, io.netty.handler.codec.http.HttpMethod.POST, "/");
HttpPostRequestEncoder encoder = new HttpPostRequestEncoder(request, multipart);
for (Map.Entry<String, String> attribute : attributes) {
encoder.addBodyAttribute(attribute.getKey(), attribute.getValue());
}
encoder.finalizeRequest();
for (String headerName : request.headers().names()) {
req.putHeader(headerName, request.headers().get(headerName));
}
if (encoder.isChunked()) {
buffer = Buffer.buffer();
while (true) {
HttpContent chunk = encoder.readChunk(new UnpooledByteBufAllocator(false));
ByteBuf content = chunk.content();
if (content.readableBytes() == 0) {
break;
}
buffer.appendBuffer(Buffer.buffer(content));
}
} else {
ByteBuf content = request.content();
buffer = Buffer.buffer(content);
}
} catch (Exception e) {
throw new VertxException(e);
}
} else if (body instanceof JsonObject) {
buffer = Buffer.buffer(((JsonObject) body).encode());
} else {
buffer = Buffer.buffer(Json.encode(body));
}
req.exceptionHandler(responseFuture::tryFail);
req.end(buffer);
}
} else {
req.exceptionHandler(responseFuture::tryFail);
req.end();
}
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.QueryStringEncoder in project VX-API-Gateway by EliMirren.
the class VxApiRouteHandlerHttpServiceImpl method handle.
@Override
public void handle(RoutingContext rct) {
// 看有没有可用的服务连接
if (policy.isHaveService()) {
// 有可用连接
// 后台服务连接信息
VxApiServerURLInfo urlInfo;
if (serOptions.getBalanceType() == LoadBalanceEnum.IP_HASH) {
String ip = rct.request().remoteAddress().host();
urlInfo = policy.getUrl(ip);
} else {
urlInfo = policy.getUrl();
}
// 执行监控
VxApiTrackInfos trackInfo = new VxApiTrackInfos(appName, api.getApiName());
// 统计请求内容长度
String resLen = rct.request().getHeader("Content-Length");
trackInfo.setRequestBufferLen(resLen == null ? 0 : StrUtil.getintTry(resLen));
// 获得请求连接与进行请求
String requestPath = urlInfo.getUrl();
MultiMap headers = new CaseInsensitiveHeaders();
if (serOptions.getParams() != null) {
// 路径参数
QueryStringEncoder queryParam = new QueryStringEncoder("");
for (VxApiParamOptions p : serOptions.getParams()) {
String param = null;
if (p.getType() == 0 || p.getType() == 2) {
if (p.getApiParamPosition() == ParamPositionEnum.HEADER) {
param = rct.request().getHeader(p.getApiParamName());
} else {
param = rct.request().getParam(p.getApiParamName());
}
} else if (p.getType() == 1) {
if (p.getSysParamType() == ParamSystemVarTypeEnum.CLIENT_HOST) {
param = rct.request().remoteAddress().host();
} else if (p.getSysParamType() == ParamSystemVarTypeEnum.CLIENT_PORT) {
param = Integer.toString(rct.request().remoteAddress().port());
} else if (p.getSysParamType() == ParamSystemVarTypeEnum.CLIENT_PATH) {
param = rct.request().path() == null ? "" : rct.request().path();
} else if (p.getSysParamType() == ParamSystemVarTypeEnum.CLIENT_SESSION_ID) {
param = rct.session().id();
} else if (p.getSysParamType() == ParamSystemVarTypeEnum.CLIENT_ABSOLUTE_URI) {
param = rct.request().absoluteURI();
} else if (p.getSysParamType() == ParamSystemVarTypeEnum.CLIENT_REQUEST_SCHEMA) {
param = rct.request().scheme();
} else if (p.getSysParamType() == ParamSystemVarTypeEnum.SERVER_API_NAME) {
param = api.getApiName();
} else if (p.getSysParamType() == ParamSystemVarTypeEnum.SERVER_UNIX_TIME) {
param = Long.toString(System.currentTimeMillis());
} else if (p.getSysParamType() == ParamSystemVarTypeEnum.SERVER_USER_AGENT) {
param = VxApiGatewayAttribute.VX_API_USER_AGENT;
}
} else if (p.getType() == 9) {
param = p.getParamValue().toString();
} else {
continue;
}
if (p.getSerParamPosition() == ParamPositionEnum.HEADER) {
headers.add(p.getSerParamName(), param);
} else if (p.getSerParamPosition() == ParamPositionEnum.PATH) {
requestPath = requestPath.replace(":" + p.getSerParamName(), param);
} else {
queryParam.addParam(p.getSerParamName(), param);
}
}
requestPath += queryParam.toString();
}
HttpClientRequest request = httpClient.requestAbs(serOptions.getMethod(), requestPath).setTimeout(serOptions.getTimeOut());
request.handler(resp -> {
// 设置请求响应时间
trackInfo.setResponseTime(Instant.now());
HttpServerResponse response = rct.response().putHeader(VxApiRouteConstant.SERVER, VxApiGatewayAttribute.FULL_NAME).putHeader(VxApiRouteConstant.CONTENT_TYPE, api.getContentType()).setChunked(true);
Pump.pump(resp, response).start();
resp.endHandler(end -> {
// 透传header
Set<String> tranHeaders = api.getResult().getTranHeaders();
if (tranHeaders != null && tranHeaders.size() > 0) {
tranHeaders.forEach(h -> {
rct.response().putHeader(h, resp.getHeader(h) == null ? "" : resp.getHeader(h));
});
}
if (isNext) {
// 告诉后置处理器当前操作成功执行
rct.put(VxApiAfterHandler.PREV_IS_SUCCESS_KEY, Future.<Boolean>succeededFuture(true));
rct.next();
} else {
rct.response().end();
}
// 统计响应长度
String repLen = resp.getHeader("Content-Length");
trackInfo.setResponseBufferLen(repLen == null ? 0 : StrUtil.getintTry(repLen));
trackInfo.setEndTime(Instant.now());
rct.vertx().eventBus().send(thisVertxName + VxApiEventBusAddressConstant.SYSTEM_PLUS_TRACK_INFO, trackInfo.toJson());
});
});
// 异常处理
request.exceptionHandler(e -> {
if (isNext) {
// 告诉后置处理器当前操作成功执行
rct.put(VxApiAfterHandler.PREV_IS_SUCCESS_KEY, Future.<Boolean>failedFuture(e));
rct.next();
} else {
HttpServerResponse response = rct.response().putHeader(VxApiRouteConstant.SERVER, VxApiGatewayAttribute.FULL_NAME).putHeader(VxApiRouteConstant.CONTENT_TYPE, api.getContentType());
// 如果是连接异常返回无法连接的错误信息,其他异常返回相应的异常
if (e instanceof ConnectException || e instanceof TimeoutException) {
response.setStatusCode(api.getResult().getCantConnServerStatus()).end(api.getResult().getCantConnServerExample());
} else {
response.setStatusCode(api.getResult().getFailureStatus()).end(api.getResult().getFailureExample());
}
}
// 提交连接请求失败
policy.reportBadService(urlInfo.getIndex());
trackInfo.setEndTime(Instant.now());
// 记录与后台交互发生错误
trackInfo.setSuccessful(false);
trackInfo.setErrMsg(e.getMessage());
trackInfo.setErrStackTrace(e.getStackTrace());
rct.vertx().eventBus().send(thisVertxName + VxApiEventBusAddressConstant.SYSTEM_PLUS_TRACK_INFO, trackInfo.toJson());
});
// 设置请求时间
trackInfo.setRequestTime(Instant.now());
if (headers != null && headers.size() > 0) {
request.headers().addAll(headers);
}
// 设置User-Agent
String agnet = request.headers().get("User-Agent");
if (agnet == null) {
agnet = VxApiGatewayAttribute.VX_API_USER_AGENT;
} else {
agnet += " " + VxApiGatewayAttribute.VX_API_USER_AGENT;
}
request.putHeader("User-Agent", agnet);
request.end();
// 判断是否有坏的连接
if (policy.isHaveBadService()) {
if (!policy.isCheckWaiting()) {
if (webClient == null) {
WebClient.create(rct.vertx());
}
policy.setCheckWaiting(true);
rct.vertx().setTimer(serOptions.getRetryTime(), testConn -> {
List<VxApiServerURLInfo> service = policy.getBadService();
for (VxApiServerURLInfo urlinfo : service) {
webClient.requestAbs(serOptions.getMethod(), urlinfo.getUrl()).timeout(serOptions.getTimeOut()).send(res -> {
if (res.succeeded()) {
policy.reportGreatService(urlinfo.getIndex());
}
});
}
policy.setCheckWaiting(false);
});
}
}
} else {
// 无可用连接时,结束当前处理器并尝试重新尝试连接是否可用
if (isNext) {
// 告诉后置处理器当前操作执行结果
rct.put(VxApiAfterHandler.PREV_IS_SUCCESS_KEY, Future.<Boolean>failedFuture(new ConnectException("无法连接上后台交互的服务器")));
rct.next();
} else {
rct.response().putHeader(VxApiRouteConstant.SERVER, VxApiGatewayAttribute.FULL_NAME).putHeader(VxApiRouteConstant.CONTENT_TYPE, api.getContentType()).setStatusCode(api.getResult().getCantConnServerStatus()).end(api.getResult().getCantConnServerExample());
}
if (!policy.isCheckWaiting()) {
policy.setCheckWaiting(true);
rct.vertx().setTimer(serOptions.getRetryTime(), testConn -> {
List<VxApiServerURLInfo> service = policy.getBadService();
for (VxApiServerURLInfo urlinfo : service) {
webClient.requestAbs(serOptions.getMethod(), urlinfo.getUrl()).timeout(serOptions.getTimeOut()).send(res -> {
if (res.succeeded()) {
policy.reportGreatService(urlinfo.getIndex());
}
});
}
policy.setCheckWaiting(false);
});
}
}
}
Aggregations