Conexões HTTP/HTTPS em portas não padrão para saída de porta flexível

As conexões HTTP/HTTPS em portas não padrão (não 80/443) devem ser enviadas por proxy AEM as a Cloud Service, no entanto não precisam de nenhuma portForwards e pode usar AEM rede avançada AEM_PROXY_HOST e uma porta proxy reservada AEM_HTTP_PROXY_PORT ou AEM_HTTPS_PROXY_PORT dependendo de é, o destino é HTTP/HTTPS.

Suporte avançado para rede

O código de exemplo a seguir é suportado pelas seguintes opções avançadas de rede.

Verifique se a variável adequada a configuração avançada de rede foi configurada antes de seguir este tutorial.

Sem rede avançada Saída flexível da porta Endereço IP de saída dedicado Rede privada virtual
ATENÇÃO

Este exemplo de código é somente para Aumento Flexível da Porta. Um exemplo de código semelhante, mas diferente, está disponível para Conexões HTTP/HTTPS em portas não padrão para endereço IP de saída dedicado e VPN.

Exemplo de código

Este exemplo de código Java™ é de um serviço OSGi que pode ser executado em AEM as a Cloud Service que faz uma conexão HTTP com um servidor da Web externo no 8080. As conexões com servidores da Web HTTPS usam as variáveis de ambiente AEM_PROXY_HOST e AEM_HTTPS_PROXY_PORT (padrão para proxy.tunnel:3128 em versões AEM < 6094).

OBSERVAÇÃO

Recomenda-se que APIs HTTP do Java™ 11 são usadas para fazer chamadas HTTP/HTTPS do AEM.

  • 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;
        }
    }
}

Nesta página