非标准端口上的HTTP/HTTPS连接,实现灵活端口出口
非标准端口(非80/443)上的HTTP/HTTPS连接必须通过AEM as a Cloud Service进行代理,但它们不需要任何特殊的portForwards
规则,并且可以使用AEM高级联网的AEM_PROXY_HOST
和保留的代理端口AEM_HTTP_PROXY_PORT
或AEM_HTTPS_PROXY_PORT
,具体取决于目标是HTTP/HTTPS。
高级联网支持
以下高级联网选项支持以下代码示例。
在执行本教程之前,请确保已设置适当的高级联网配置。
代码示例
此Java™代码示例是一个可在AEM as a Cloud Service中运行的OSGi服务,该服务与8080上的外部Web服务器建立HTTP连接。 与HTTPS Web服务器的连接使用环境变量AEM_PROXY_HOST
和AEM_HTTPS_PROXY_PORT
(在AEM版本6094之前,默认为proxy.tunnel:3128
)。
NOTE
建议使用Java™ 11 HTTP API从AEM进行HTTP/HTTPS调用。
core/src/com/adobe/aem/wknd/examples/connections/impl/HttpExternalServiceImpl.java
package com.adobe.aem.wknd.examples.core.connections.impl;
import com.adobe.aem.wknd.examples.core.connections.ExternalService;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ProxySelector;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
@Component
public class HttpExternalServiceImpl implements ExternalService {
private static final Logger log = LoggerFactory.getLogger(HttpExternalServiceImpl.class);
@Override
public boolean isAccessible() {
HttpClient client;
// Use System.getenv("AEM_PROXY_HOST") and proxy port System.getenv("AEM_HTTP_PROXY_PORT")
// or System.getenv("AEM_HTTPS_PROXY_PORT"), depending on if the destination requires HTTP/HTTPS
if (System.getenv("AEM_PROXY_HOST") != null) {
// Create a ProxySelector that uses to AEM's provided AEM_PROXY_HOST, with a fallback of proxy.tunnel, and proxy port using the AEM_HTTP_PROXY_PORT variable.
// If the destination requires HTTPS, then use the variable AEM_HTTPS_PROXY_PORT instead of AEM_HTTP_PROXY_PORT.
ProxySelector proxySelector = ProxySelector.of(new InetSocketAddress(
System.getenv().getOrDefault("AEM_PROXY_HOST", "proxy.tunnel"),
Integer.parseInt(System.getenv().get("AEM_HTTP_PROXY_PORT"))));
client = HttpClient.newBuilder().proxy(proxySelector).build();
log.debug("Using HTTPClient with AEM_PROXY_HOST");
} else {
client = HttpClient.newBuilder().build();
// If no proxy is set up (such as local dev)
log.debug("Using HTTPClient without AEM_PROXY_HOST");
}
// Prepare the full URI to request, note this will have the
// - Scheme (http/https)
// - External host name
// - External port
// The external service URI, including the scheme/host/port, is defined in code, and NOT in Cloud Manager portForwards rules.
URI uri = URI.create("http://api.example.com:8080/test.json");
// Prepare the HttpRequest
HttpRequest request = HttpRequest.newBuilder().uri(uri).timeout(Duration.ofSeconds(2)).build();
// Send the HttpRequest using the configured HttpClient
HttpResponse<String> response = null;
try {
// Request the URL
response = client.send(request, HttpResponse.BodyHandlers.ofString());
log.debug("HTTP response body: {} ", response.body());
// Our simple example returns true is response is successful! (200 status code)
return response.statusCode() == 200;
} catch (IOException e) {
return false;
} catch (InterruptedException e) {
return false;
}
}
}
recommendation-more-help
4859a77c-7971-4ac9-8f5c-4260823c6f69