HttpClientRequest createRequest(HttpClient client, Invocation invocation, IpPort ipPort, String path, AsyncResponse asyncResp) { URIEndpointObject endpoint = (URIEndpointObject) invocation.getEndpoint().getAddress(); RequestOptions requestOptions = new RequestOptions(); requestOptions.setHost(ipPort.getHostOrIp()) .setPort(ipPort.getPort()) .setSsl(endpoint.isSslEnabled()) .setURI(path); HttpMethod method = getMethod(invocation); LOGGER.debug("Sending request by rest, method={}, qualifiedName={}, path={}, endpoint={}.", method, invocation.getMicroserviceQualifiedName(), path, invocation.getEndpoint().getEndpoint()); HttpClientRequest request = client.request(method, requestOptions, response -> { handleResponse(invocation, response, asyncResp); }); return request; }
/** * Builds the request options suitable for HttpClient from a URI and a relative * path. * * @param uri * URI * @return request options */ public static RequestOptions toRequestOptions(final URI uri, final String relativeUri) { final RequestOptions options = new RequestOptions() .setSsl("https".equals(uri.getScheme())) .setHost(uri.getHost()); if (uri.getPort() > 0) { options.setPort(uri.getPort()); } else if (options.isSsl()) { options.setPort(443); } else { options.setPort(80); } final String rawPath = uri.getRawPath() + relativeUri; if (uri.getRawQuery() == null) { options.setURI(rawPath); } else { options.setURI(rawPath + "?" + uri.getRawQuery()); } return options; }
public static RequestOptions toRequestOptions(final URI uri) { final RequestOptions options = new RequestOptions() .setSsl("https".equals(uri.getScheme())) .setHost(uri.getHost()); if (uri.getPort() > 0) { options.setPort(uri.getPort()); } else if (options.isSsl()) { options.setPort(443); } else { options.setPort(80); } if (uri.getRawQuery() == null) { options.setURI(uri.getRawPath()); } else { options.setURI(uri.getRawPath() + "?" + uri.getRawQuery()); } return options; }
@Test public void testConversions() { final URI uri = URI.create("https://github.com/eclipse/vert.x/blob/a30703379a06502faaae0aeda1eaa9a9b1152004/src/main/java/io/vertx/core/net/impl/SSLHelper.java"); final RequestOptions requestOptions = Conversions.toRequestOptions(uri); Json.prettyPrint(requestOptions); }
@Test public void testConversionsWithEscapedDataQuery() { final URI uri = URI.create("https://encrypted.google.com/search?q=face+book+%25+prime+%3F&oq=face+book+%25+prime+%3F&gs_l=psy-ab.3..0i13k1l10.12446.12733.0.13108.2.2.0.0.0.0.140.248.0j2.2.0....0...1.1.64.psy-ab..0.2.246....0.QRedmYd5GAY"); final RequestOptions requestOptions = Conversions.toRequestOptions(uri); Json.prettyPrint(requestOptions); }
@Test public void testConversionsWithQuery() { final URI uri = URI.create("https://news.google.com/news/?ned=ca&hl=en-CA"); final RequestOptions requestOptions = Conversions.toRequestOptions(uri); Json.prettyPrint(requestOptions); }
@Override public void process(ClientRequest request, Handler<AsyncResult<ClientResponse>> result) { MultiMap requestHeaders = buildHeaders(clientDestination.getString("hostHeader"), request.getHeaders()); RequestOptions httpRequestData = buildRequestData(request); if (LOGGER.isDebugEnabled()) { LOGGER.debug("GET HTTP Repository: {}://{}:{}/{} with headers [{}]", httpRequestData.isSsl() ? "https" : "http", httpRequestData.getHost(), httpRequestData.getPort(), httpRequestData.getURI(), DataObjectsUtil.toString(requestHeaders) ); } get(httpClient, httpRequestData, requestHeaders) .doOnNext(this::traceHttpResponse) .flatMap(this::processResponse) .subscribe( response -> result.handle(Future.succeededFuture(response)), error -> { LOGGER.error(ERROR_MESSAGE, error); result.handle(Future.succeededFuture(toInternalError())); } ); }
private RequestOptions buildRequestData(ClientRequest request) { return new RequestOptions() .setSsl(clientDestination.getString("scheme", "http").equals("https") ? true : false) .setURI(buildRepoUri(request)) .setPort(clientDestination.getInteger("port")) .setHost(clientDestination.getString("domain")); }
private Observable<HttpClientResponse> get(HttpClient client, RequestOptions requestOptions, MultiMap headers) { return Observable.unsafeCreate(subscriber -> { HttpClientRequest req = client.get(requestOptions); req.headers().addAll(headers); if (headers.get(HttpHeaderNames.HOST.toString()) != null) { req.setHost(headers.get(HttpHeaderNames.HOST.toString())); } Observable<HttpClientResponse> resp = req.toObservable(); resp.subscribe(subscriber); req.end(); }); }
@Override public void handle(final RoutingContext context) { // if null or it is marked as protected then go on. final PathContext pathContext = PathContext.get(context); if (pathContext == null) { context.next(); return; } LOG.debug("Handling {} with from={} to={} protected={} ended={}", context, pathContext.getFrom(), pathContext.getTo(), pathContext.isProtected(), context.request().isEnded()); final HttpServerRequest contextRequest = context.request(); contextRequest.setExpectMultipart(context.parsedHeaders().contentType().isPermitted() && "multipart".equals(context.parsedHeaders().contentType().component())); final RequestOptions clientRequestOptions = Conversions.toRequestOptions(pathContext.getTo(), contextRequest.uri().substring(pathContext.getFrom().length())); final HttpClientRequest clientRequest = httpClient .request(contextRequest.method(), clientRequestOptions, clientResponse -> { contextRequest.response().setChunked(clientResponse.getHeader(HttpHeaders.CONTENT_LENGTH) == null) .setStatusCode(clientResponse.statusCode()); clientResponse.headers().forEach(e -> contextRequest.response().putHeader(e.getKey(), e.getValue())); clientResponse.endHandler(v -> contextRequest.response().end()); Pump.pump(clientResponse, contextRequest.response()).start(); }).exceptionHandler(context::fail) .setChunked(true); StreamSupport.stream(contextRequest.headers().spliterator(), false) .filter(Predicates.HEADER_FORWARDABLE) .forEach(e -> clientRequest.putHeader(e.getKey(), e.getValue())); clientRequest.putHeader(REQUEST_ID, (String) context.get(REQUEST_ID)); clientRequest.putHeader(DATE, RFC_1123_DATE_TIME.format(now(UTC))); final Map<String, String> additionalHeaders = context.get("additional_headers"); if (additionalHeaders != null) { additionalHeaders.forEach(clientRequest::putHeader); } contextRequest.endHandler(v -> clientRequest.end()); Pump.pump(contextRequest, clientRequest).start(); contextRequest.resume(); }
@Override public HttpRequest<Buffer> request(HttpMethod method, RequestOptions requestOptions) { return new HttpRequestImpl<>(this, method, requestOptions.isSsl(), requestOptions.getPort(), requestOptions.getHost(), requestOptions.getURI(), BodyCodecImpl.BUFFER, options); }
/** * Builds the request options suitable for HttpClient from a URI. * * @param uri * URI * @return request options */ public static RequestOptions toRequestOptions(final URI uri) { return toRequestOptions(uri, ""); }
/** * Create an HTTP request to send to the server at the specified host and port. * @param method the HTTP method * @param options the request options * @return an HTTP client request object */ HttpRequest<Buffer> request(HttpMethod method, RequestOptions options);