All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hubspot.imap.protocol.command.ProxyCommand Maven / Gradle / Ivy

There is a newer version: 0.6.1
Show newest version
package com.hubspot.imap.protocol.command;

import com.google.common.net.HostAndPort;
import com.google.common.net.InetAddresses;
import com.hubspot.imap.ProxyConfig;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Optional;

public class ProxyCommand extends BaseImapCommand {

  private static final String ANY = "any";

  @SuppressFBWarnings("CT_CONSTRUCTOR_THROW")
  public ProxyCommand(ProxyConfig proxyConfig) {
    this(proxyConfig.proxyHost(), proxyConfig.proxyLocalIpAddress());
  }

  @SuppressFBWarnings("CT_CONSTRUCTOR_THROW")
  public ProxyCommand(HostAndPort destination, Optional proxyLocalIp) {
    super(ImapCommandType.PROXY, false, buildProxyArg(destination, proxyLocalIp));
  }

  private static String buildProxyArg(
    HostAndPort destination,
    Optional proxyLocalIp
  ) {
    String destinationIp;
    if (InetAddresses.isInetAddress(destination.getHost())) {
      destinationIp = destination.getHost();
    } else {
      destinationIp = lookupHost(destination.getHost());
    }
    return proxyLocalIp.orElse(ANY) + ":" + destinationIp + ":" + destination.getPort();
  }

  private static String lookupHost(String host) {
    try {
      return InetAddress.getByName(host).getHostAddress();
    } catch (UnknownHostException e) {
      throw new RuntimeException(e);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy