|
| 1 | +package com.sap.cloud.sdk.cloudplatform.connectivity; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.net.InetSocketAddress; |
| 5 | +import java.net.Socket; |
| 6 | +import java.net.UnknownHostException; |
| 7 | + |
| 8 | +import javax.annotation.Nonnull; |
| 9 | + |
| 10 | +import com.sap.cloud.sdk.cloudplatform.connectivity.exception.DestinationAccessException; |
| 11 | + |
| 12 | +import lombok.extern.slf4j.Slf4j; |
| 13 | + |
| 14 | +/** |
| 15 | + * NetworkVerifier that performs TCP socket-based connectivity verification. |
| 16 | + * |
| 17 | + * <p> |
| 18 | + * This implementation uses standard Java socket connections to verify that a remote host and port combination is |
| 19 | + * reachable and accepting connections. It establishes a temporary TCP connection to the target endpoint and immediately |
| 20 | + * closes it upon successful establishment. |
| 21 | + * |
| 22 | + * <p> |
| 23 | + * <strong>Verification Process:</strong> |
| 24 | + * <ol> |
| 25 | + * <li>Creates a new TCP socket</li> |
| 26 | + * <li>Attempts to connect to the specified host and port with a timeout</li> |
| 27 | + * <li>Immediately closes the connection if successful</li> |
| 28 | + * <li>Throws appropriate exceptions for failures</li> |
| 29 | + * </ol> |
| 30 | + * |
| 31 | + * <p> |
| 32 | + * <strong>Timeout Configuration:</strong> The verification uses a fixed timeout of {@value #HOST_REACH_TIMEOUT} |
| 33 | + * milliseconds to prevent indefinite blocking on unreachable endpoints. |
| 34 | + * |
| 35 | + * <p> |
| 36 | + * <strong>Error Handling:</strong> |
| 37 | + * <ul> |
| 38 | + * <li>{@link UnknownHostException} - Host cannot be resolved to an IP address</li> |
| 39 | + * <li>{@link IOException} - Network connectivity issues or connection refused</li> |
| 40 | + * </ul> |
| 41 | + * |
| 42 | + * @see TransparentProxy |
| 43 | + * @since 5.24.0 |
| 44 | + */ |
| 45 | +@Slf4j |
| 46 | +class NetworkVerifier |
| 47 | +{ |
| 48 | + private static final int HOST_REACH_TIMEOUT = 5000; |
| 49 | + |
| 50 | + /** |
| 51 | + * {@inheritDoc} |
| 52 | + * |
| 53 | + * <p> |
| 54 | + * This implementation creates a TCP socket connection to the specified host and port to verify connectivity. The |
| 55 | + * connection is immediately closed after successful establishment. |
| 56 | + * |
| 57 | + * @param host |
| 58 | + * {@inheritDoc} |
| 59 | + * @param port |
| 60 | + * {@inheritDoc} |
| 61 | + * @throws DestinationAccessException |
| 62 | + * {@inheritDoc} |
| 63 | + * <p> |
| 64 | + * Specific error conditions: |
| 65 | + * <ul> |
| 66 | + * <li>Host resolution failure - when DNS lookup fails</li> |
| 67 | + * <li>Connection failure - when host is unreachable or port is closed</li> |
| 68 | + * <li>Network timeouts - when connection attempt exceeds timeout</li> |
| 69 | + * </ul> |
| 70 | + */ |
| 71 | + void verifyHostConnectivity( @Nonnull final String host, final int port ) |
| 72 | + throws DestinationAccessException |
| 73 | + { |
| 74 | + log.info("Verifying that transparent proxy host is reachable on {}:{}", host, port); |
| 75 | + try( Socket socket = new Socket() ) { |
| 76 | + socket.connect(new InetSocketAddress(host, port), HOST_REACH_TIMEOUT); |
| 77 | + log.info("Successfully verified that transparent proxy host is reachable on {}:{}", host, port); |
| 78 | + } |
| 79 | + catch( final UnknownHostException e ) { |
| 80 | + throw new DestinationAccessException( |
| 81 | + String.format("Host [%s] could not be resolved. Caused by: %s", host, e.getMessage()), |
| 82 | + e); |
| 83 | + } |
| 84 | + catch( final IOException e ) { |
| 85 | + throw new DestinationAccessException( |
| 86 | + String.format("Host [%s] on port [%d] is not reachable. Caused by: %s", host, port, e.getMessage()), |
| 87 | + e); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments