@Override public Object createContextArg(SwaggerInvocation swaggerInvocation) { Invocation invocation = (Invocation) swaggerInvocation; // 从rest transport来 HttpServletRequest request = (HttpServletRequest) invocation.getHandlerContext().get(RestConst.REST_REQUEST); if (request != null) { return request; } // 通过args模拟request return new InvocationToHttpServletRequest(invocation); } }
@Override protected HttpClientWithContext findHttpClientPool(Invocation invocation) { Context invocationContext = (Context) invocation.getHandlerContext().get(EdgeInvocation.EDGE_INVOCATION_CONTEXT); return clientMgr.findClientPool(invocation.isSync(), invocationContext); } }
protected Invocation prepareInvocation(Object[] args) { Invocation invocation = InvocationFactory.forConsumer(requestMeta.getReferenceConfig(), requestMeta.getOperationMeta(), args); invocation.getHandlerContext().put(RestConst.REST_CLIENT_REQUEST_PATH, path + "?" + this.uri.getRawQuery()); if (context != null) { invocation.addContext(context); } invocation.getHandlerContext().put(RestConst.CONSUMER_HEADER, httpHeaders); return invocation; }
@Override public void beforeSendRequest(Invocation invocation, HttpServletRequestEx requestEx) { HttpHeaders httpHeaders = (HttpHeaders) invocation.getHandlerContext().get(RestConst.CONSUMER_HEADER); if (httpHeaders == null) { return; } httpHeaders.forEach((key, values) -> { if (HttpHeaders.CONTENT_LENGTH.equalsIgnoreCase(key)) { LOGGER.debug("skip copy content-length into request"); return; } for (String value : values) { // null args should not be set to requestEx to avoid NullPointerException in Netty. if (null == value) { LOGGER.debug("header value is null, key = [{}]. Will not set this header into request", key); continue; } requestEx.addHeader(key, value); } }); }
public InvocationToHttpServletRequest(Invocation invocation) { this.swaggerOperation = invocation.getOperationMeta().getExtData(RestConst.SWAGGER_REST_OPERATION); this.args = invocation.getArgs(); this.sockerAddress = (SocketAddress) invocation.getHandlerContext().get(Const.REMOTE_ADDRESS); }
private ListenableFuture<ClientHttpResponse> invoke(Object[] args) { Invocation invocation = prepareInvocation(args); invocation.getHandlerContext().put(RestConst.CONSUMER_HEADER, this.getHeaders()); CompletableFuture<ClientHttpResponse> clientHttpResponseCompletableFuture = doAsyncInvoke(invocation); return new CompletableToListenableFutureAdapter<ClientHttpResponse>(clientHttpResponseCompletableFuture); }
protected String createRequestPath(RestOperationMeta swaggerRestOperation) throws Exception { URIEndpointObject address = (URIEndpointObject) invocation.getEndpoint().getAddress(); String urlPrefix = address.getFirst(Const.URL_PREFIX); String path = (String) invocation.getHandlerContext().get(RestConst.REST_CLIENT_REQUEST_PATH); if (path == null) { path = swaggerRestOperation.getPathBuilder().createRequestPath(invocation.getArgs()); } if (StringUtils.isEmpty(urlPrefix) || path.startsWith(urlPrefix)) { return path; } return urlPrefix + path; } }
protected Response prepareInvoke() throws Throwable { this.initProduceProcessor(); invocation.getHandlerContext().put(RestConst.REST_REQUEST, requestEx); invocation.getInvocationStageTrace().startServerFiltersRequest(); for (HttpServerFilter filter : httpServerFilters) { if (filter.enabled()) { Response response = filter.afterReceiveRequest(invocation, requestEx); if (response != null) { return response; } } } return null; }
@Override public void beforeSendRequest(Invocation invocation, HttpServletRequestEx requestEx) { RestClientRequestImpl restClientRequest = (RestClientRequestImpl) invocation.getHandlerContext() .get(RestConst.INVOCATION_HANDLER_REQUESTCLIENT); OperationMeta operationMeta = invocation.getOperationMeta(); RestOperationMeta swaggerRestOperation = operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION); try { RestCodec.argsToRest(invocation.getArgs(), swaggerRestOperation, restClientRequest); requestEx.setBodyBuffer(restClientRequest.getBodyBuffer()); } catch (Throwable e) { throw ExceptionFactory.convertConsumerException(e); } }
protected void handleResponse(HttpClientResponse httpClientResponse) { this.clientResponse = httpClientResponse; if (HttpStatus.isSuccess(clientResponse.statusCode()) && Part.class.equals(invocation.getOperationMeta().getMethod().getReturnType())) { ReadStreamPart part = new ReadStreamPart(httpClientWithContext.context(), httpClientResponse); invocation.getHandlerContext().put(RestConst.READ_STREAM_PART, part); processResponseBody(null); return; } httpClientResponse.exceptionHandler(e -> { LOGGER.error("Failed to receive response from {}.", httpClientResponse.netSocket().remoteAddress(), e); fail(e); }); clientResponse.bodyHandler(responseBuf -> { processResponseBody(responseBuf); }); }
private void doRunInExecutor() throws Exception { invocation.onExecuteStart(); invocation.getInvocationStageTrace().startServerFiltersRequest(); HighwayCodec.decodeRequest(invocation, header, operationProtobuf, bodyBuffer); invocation.getHandlerContext().put(Const.REMOTE_ADDRESS, this.connection.getNetSocket().remoteAddress()); invocation.getInvocationStageTrace().startHandlersRequest(); invocation.next(response -> { sendResponse(invocation.getContext(), response); }); }
@Override protected void createInvocation() { ReferenceConfig referenceConfig = new ReferenceConfig(); referenceConfig.setMicroserviceVersionRule(microserviceVersionRule); referenceConfig.setTransport(Const.ANY_TRANSPORT); this.invocation = InvocationFactory.forConsumer(referenceConfig, restOperationMeta.getOperationMeta(), null); this.invocation.setSync(false); this.invocation.setEdge(true); this.invocation.getHandlerContext().put(EDGE_INVOCATION_CONTEXT, Vertx.currentContext()); this.invocation.setResponseExecutor(new ReactiveResponseExecutor()); this.routingContext.put(RestConst.REST_INVOCATION_CONTEXT, invocation); } }
protected Response extractResponse(Invocation invocation, HttpServletResponseEx responseEx) { Object result = invocation.getHandlerContext().get(RestConst.READ_STREAM_PART); if (result != null) { return Response.create(responseEx.getStatusType(), result);
RestClientRequestImpl restClientRequest = new RestClientRequestImpl(clientRequest, httpClientWithContext.context(), asyncResp); invocation.getHandlerContext().put(RestConst.INVOCATION_HANDLER_REQUESTCLIENT, restClientRequest);
@Override public Object createContextArg(SwaggerInvocation swaggerInvocation) { Invocation invocation = (Invocation) swaggerInvocation; // 从rest transport来 HttpServletRequest request = (HttpServletRequest) invocation.getHandlerContext().get(RestConst.REST_REQUEST); if (request != null) { return request; } // 通过args模拟request return new InvocationToHttpServletRequest(invocation); } }
public InvocationToHttpServletRequest(Invocation invocation) { this.swaggerOperation = invocation.getOperationMeta().getExtData(RestConst.SWAGGER_REST_OPERATION); this.args = invocation.getArgs(); this.sockerAddress = (SocketAddress) invocation.getHandlerContext().get(Const.REMOTE_ADDRESS); }
protected String createRequestPath(RestOperationMeta swaggerRestOperation) throws Exception { URIEndpointObject address = (URIEndpointObject) invocation.getEndpoint().getAddress(); String urlPrefix = address.getFirst(Const.URL_PREFIX); String path = (String) invocation.getHandlerContext().get(RestConst.REST_CLIENT_REQUEST_PATH); if (path == null) { path = swaggerRestOperation.getPathBuilder().createRequestPath(invocation.getArgs()); } if (StringUtils.isEmpty(urlPrefix) || path.startsWith(urlPrefix)) { return path; } return urlPrefix + path; } }
private ListenableFuture<ClientHttpResponse> invoke(Object[] args) { Invocation invocation = prepareInvocation(args); invocation.getHandlerContext().put(RestConst.CONSUMER_HEADER, this.getHeaders()); CompletableFuture<ClientHttpResponse> clientHttpResponseCompletableFuture = doAsyncInvoke(invocation); return new CompletableToListenableFutureAdapter<ClientHttpResponse>(clientHttpResponseCompletableFuture); }
@Override public void beforeSendRequest(Invocation invocation, HttpServletRequestEx requestEx) { RestClientRequestImpl restClientRequest = (RestClientRequestImpl) invocation.getHandlerContext() .get(RestConst.INVOCATION_HANDLER_REQUESTCLIENT); OperationMeta operationMeta = invocation.getOperationMeta(); RestOperationMeta swaggerRestOperation = operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION); try { RestCodec.argsToRest(invocation.getArgs(), swaggerRestOperation, restClientRequest); requestEx.setBodyBuffer(restClientRequest.getBodyBuffer()); } catch (Throwable e) { throw ExceptionFactory.convertConsumerException(e); } }
private void doRunInExecutor() throws Exception { invocation.onExecuteStart(); invocation.getInvocationStageTrace().startServerFiltersRequest(); HighwayCodec.decodeRequest(invocation, header, operationProtobuf, bodyBuffer); invocation.getHandlerContext().put(Const.REMOTE_ADDRESS, this.connection.getNetSocket().remoteAddress()); invocation.getInvocationStageTrace().startHandlersRequest(); invocation.next(response -> { sendResponse(invocation.getContext(), response); }); }