Example usage for org.apache.commons.validator.routines InetAddressValidator getInstance

List of usage examples for org.apache.commons.validator.routines InetAddressValidator getInstance

Introduction

In this page you can find the example usage for org.apache.commons.validator.routines InetAddressValidator getInstance.

Prototype

public static InetAddressValidator getInstance() 

Source Link

Document

Returns the singleton instance of this validator.

Usage

From source file:com.vmware.photon.controller.api.frontend.backends.clients.ClusterManagerClient.java

private String createKubernetesClusterEntity(String projectId, ClusterCreateSpec spec,
        ClusterConfigurationService.State clusterConfiguration) throws SpecInvalidException {

    List<String> etcdIps = new ArrayList<>();
    for (String property : Arrays.asList(EXTENDED_PROPERTY_ETCD_IP1, EXTENDED_PROPERTY_ETCD_IP2,
            EXTENDED_PROPERTY_ETCD_IP3)) {
        String etcdIp = spec.getExtendedProperties().get(property);
        if (etcdIp != null) {
            etcdIps.add(etcdIp);/*ww  w .  java2 s . c  o  m*/
        }
    }
    String masterIp = spec.getExtendedProperties().get(ClusterManagerConstants.EXTENDED_PROPERTY_MASTER_IP);
    String containerNetwork = spec.getExtendedProperties()
            .get(ClusterManagerConstants.EXTENDED_PROPERTY_CONTAINER_NETWORK);

    if (etcdIps.size() == 0) {
        throw new SpecInvalidException("Missing extended property: etcd ips");
    }

    for (String etcdIp : etcdIps) {
        if (etcdIp != null && !InetAddressValidator.getInstance().isValidInet4Address(etcdIp)) {
            throw new SpecInvalidException("Invalid extended property: etcd ip: " + etcdIp);
        }
    }

    if (masterIp == null) {
        throw new SpecInvalidException("Missing extended property: master ip");
    } else if (!InetAddressValidator.getInstance().isValidInet4Address(masterIp)) {
        throw new SpecInvalidException("Invalid extended property: master ip: " + masterIp);
    }

    if (containerNetwork == null) {
        throw new SpecInvalidException(
                "Missing extended property: " + ClusterManagerConstants.EXTENDED_PROPERTY_CONTAINER_NETWORK);
    } else {
        String[] segments = containerNetwork.split("/");
        SpecInvalidException error = new SpecInvalidException("Invalid extended property: "
                + ClusterManagerConstants.EXTENDED_PROPERTY_CONTAINER_NETWORK + ": " + containerNetwork);

        // Check that container network is of CIDR format.
        if (segments.length != 2) {
            throw error;
        }

        // Check the first segment of the container network is a valid address.
        if (!InetAddressValidator.getInstance().isValidInet4Address(segments[0])) {
            throw error;
        }

        // Check the second segment of the container network is a valid netmask.
        try {
            int cidr = Integer.parseInt(segments[1]);
            if (cidr < 0 || cidr > 32) {
                throw error;
            }
        } catch (NumberFormatException e) {
            throw error;
        }
    }

    // Assemble the cluster entity
    ClusterService.State cluster = assembleCommonClusterEntity(ClusterType.KUBERNETES, projectId, spec,
            clusterConfiguration);
    cluster.extendedProperties.put(ClusterManagerConstants.EXTENDED_PROPERTY_CONTAINER_NETWORK,
            containerNetwork);
    cluster.extendedProperties.put(ClusterManagerConstants.EXTENDED_PROPERTY_ETCD_IPS,
            serializeIpAddresses(etcdIps));
    cluster.extendedProperties.put(ClusterManagerConstants.EXTENDED_PROPERTY_MASTER_IP, masterIp);

    // Create the cluster entity
    apiFeXenonClient.post(ClusterServiceFactory.SELF_LINK, cluster);

    return cluster.documentSelfLink;
}

From source file:com.vmware.photon.controller.apife.backends.clients.ClusterManagerClient.java

private String createMesosClusterEntity(String projectId, ClusterCreateSpec spec,
        ClusterConfigurationService.State clusterConfiguration) throws SpecInvalidException {

    List<String> zookeeperIps = new ArrayList<>();
    for (String property : Arrays.asList(EXTENDED_PROPERTY_ZOOKEEPER_IP1, EXTENDED_PROPERTY_ZOOKEEPER_IP2,
            EXTENDED_PROPERTY_ZOOKEEPER_IP3)) {
        String zookeeperIp = spec.getExtendedProperties().get(property);
        if (zookeeperIp != null) {
            zookeeperIps.add(zookeeperIp);
        }/*ww  w . j  a  v  a2 s. c om*/
    }

    if (zookeeperIps.size() == 0) {
        throw new SpecInvalidException("Missing extended property: zookeeper ips");
    }

    for (String zookeeperIp : zookeeperIps) {
        if (zookeeperIp != null && !InetAddressValidator.getInstance().isValidInet4Address(zookeeperIp)) {
            throw new SpecInvalidException("Invalid extended property: zookeeper ip: " + zookeeperIp);
        }
    }

    // Assemble the cluster entity
    ClusterService.State cluster = assembleCommonClusterEntity(ClusterType.MESOS, projectId, spec,
            clusterConfiguration);
    cluster.extendedProperties.put(ClusterManagerConstants.EXTENDED_PROPERTY_ZOOKEEPER_IPS,
            serializeIpAddresses(zookeeperIps));

    // Create the cluster entity
    apiFeDcpClient.post(ClusterServiceFactory.SELF_LINK, cluster);

    return cluster.documentSelfLink;
}

From source file:com.vmware.photon.controller.api.frontend.backends.clients.ClusterManagerClient.java

private String createMesosClusterEntity(String projectId, ClusterCreateSpec spec,
        ClusterConfigurationService.State clusterConfiguration) throws SpecInvalidException {

    List<String> zookeeperIps = new ArrayList<>();
    for (String property : Arrays.asList(EXTENDED_PROPERTY_ZOOKEEPER_IP1, EXTENDED_PROPERTY_ZOOKEEPER_IP2,
            EXTENDED_PROPERTY_ZOOKEEPER_IP3)) {
        String zookeeperIp = spec.getExtendedProperties().get(property);
        if (zookeeperIp != null) {
            zookeeperIps.add(zookeeperIp);
        }/*from w  ww  .j av a 2s. co  m*/
    }

    if (zookeeperIps.size() == 0) {
        throw new SpecInvalidException("Missing extended property: zookeeper ips");
    }

    for (String zookeeperIp : zookeeperIps) {
        if (zookeeperIp != null && !InetAddressValidator.getInstance().isValidInet4Address(zookeeperIp)) {
            throw new SpecInvalidException("Invalid extended property: zookeeper ip: " + zookeeperIp);
        }
    }

    // Assemble the cluster entity
    ClusterService.State cluster = assembleCommonClusterEntity(ClusterType.MESOS, projectId, spec,
            clusterConfiguration);
    cluster.extendedProperties.put(ClusterManagerConstants.EXTENDED_PROPERTY_ZOOKEEPER_IPS,
            serializeIpAddresses(zookeeperIps));

    // Create the cluster entity
    apiFeXenonClient.post(ClusterServiceFactory.SELF_LINK, cluster);

    return cluster.documentSelfLink;
}

From source file:com.cloud.utils.net.NetUtils.java

public static boolean isValidIp(final String ip) {
    final InetAddressValidator validator = InetAddressValidator.getInstance();

    return validator.isValidInet4Address(ip);
}

From source file:com.vmware.photon.controller.apife.backends.clients.ClusterManagerClient.java

private String createSwarmClusterEntity(String projectId, ClusterCreateSpec spec,
        ClusterConfigurationService.State clusterConfiguration) throws SpecInvalidException {

    List<String> etcdIps = new ArrayList<>();
    for (String property : Arrays.asList(EXTENDED_PROPERTY_ETCD_IP1, EXTENDED_PROPERTY_ETCD_IP2,
            EXTENDED_PROPERTY_ETCD_IP3)) {
        String etcdIp = spec.getExtendedProperties().get(property);
        if (etcdIp != null) {
            etcdIps.add(etcdIp);//from   w  w  w.ja v a 2  s . co  m
        }
    }

    if (etcdIps.size() == 0) {
        throw new SpecInvalidException("Missing extended property: etcd ips");
    }

    for (String etcdIp : etcdIps) {
        if (etcdIp != null && !InetAddressValidator.getInstance().isValidInet4Address(etcdIp)) {
            throw new SpecInvalidException("Invalid extended property: etcd ip: " + etcdIp);
        }
    }

    // Assemble the cluster entity
    ClusterService.State cluster = assembleCommonClusterEntity(ClusterType.SWARM, projectId, spec,
            clusterConfiguration);
    cluster.extendedProperties.put(ClusterManagerConstants.EXTENDED_PROPERTY_ETCD_IPS,
            serializeIpAddresses(etcdIps));

    // Create the cluster entity
    apiFeDcpClient.post(ClusterServiceFactory.SELF_LINK, cluster);

    return cluster.documentSelfLink;
}

From source file:com.vmware.photon.controller.api.frontend.backends.clients.ClusterManagerClient.java

private String createSwarmClusterEntity(String projectId, ClusterCreateSpec spec,
        ClusterConfigurationService.State clusterConfiguration) throws SpecInvalidException {

    List<String> etcdIps = new ArrayList<>();
    for (String property : Arrays.asList(EXTENDED_PROPERTY_ETCD_IP1, EXTENDED_PROPERTY_ETCD_IP2,
            EXTENDED_PROPERTY_ETCD_IP3)) {
        String etcdIp = spec.getExtendedProperties().get(property);
        if (etcdIp != null) {
            etcdIps.add(etcdIp);// ww  w . jav  a2  s . co  m
        }
    }

    if (etcdIps.size() == 0) {
        throw new SpecInvalidException("Missing extended property: etcd ips");
    }

    for (String etcdIp : etcdIps) {
        if (etcdIp != null && !InetAddressValidator.getInstance().isValidInet4Address(etcdIp)) {
            throw new SpecInvalidException("Invalid extended property: etcd ip: " + etcdIp);
        }
    }

    // Assemble the cluster entity
    ClusterService.State cluster = assembleCommonClusterEntity(ClusterType.SWARM, projectId, spec,
            clusterConfiguration);
    cluster.extendedProperties.put(ClusterManagerConstants.EXTENDED_PROPERTY_ETCD_IPS,
            serializeIpAddresses(etcdIps));

    // Create the cluster entity
    apiFeXenonClient.post(ClusterServiceFactory.SELF_LINK, cluster);

    return cluster.documentSelfLink;
}

From source file:io.minio.MinioClient.java

/**
 * Returns true if given endpoint is valid else false.
 *///  w w w  .  j av  a  2s . com
private boolean isValidEndpoint(String endpoint) {
    if (InetAddressValidator.getInstance().isValid(endpoint)) {
        return true;
    }

    // endpoint may be a hostname
    // refer https://en.wikipedia.org/wiki/Hostname#Restrictions_on_valid_host_names
    // why checks are done like below
    if (endpoint.length() < 1 || endpoint.length() > 253) {
        return false;
    }

    for (String label : endpoint.split("\\.")) {
        if (label.length() < 1 || label.length() > 63) {
            return false;
        }

        if (!(label.matches("^[a-zA-Z0-9][a-zA-Z0-9-]*") && endpoint.matches(".*[a-zA-Z0-9]$"))) {
            return false;
        }
    }

    return true;
}

From source file:edu.harvard.iq.dvn.core.web.admin.OptionsPage.java

private static boolean doValidate(Object value) {
    boolean valid = false;
    String address = value.toString();
    // first, assume it's a domain name
    if (address.startsWith("*.")) {
        StringBuffer sb = new StringBuffer(address);
        sb.setCharAt(0, 'a');
        address = sb.toString();//from   www.  j  a va 2s . c o m
    }
    valid = validateDomainName(address);
    if (!valid) {
        // Try to validate it as an ip address
        String ipAddress = value.toString();

        // for the purposes of validation, if the string ends in ".*",
        // replace it with dummy data for the validator.
        if (ipAddress.endsWith(".*")) {
            StringBuffer sb = new StringBuffer(ipAddress);
            sb.setCharAt(ipAddress.length() - 1, '1');
            ipAddress = sb.toString();
            // if necessary, add dummy values to the end of the string,
            // so it will pass validation.
            String[] splitStrings = ipAddress.split("\\.");
            if (splitStrings.length == 2) {
                ipAddress += ".1.1";
            } else if (splitStrings.length == 3) {
                ipAddress += ".1";
            }
        }
        InetAddressValidator val = InetAddressValidator.getInstance();
        valid = val.isValid(ipAddress);
    }
    return valid;
}

From source file:com.osparking.osparking.Settings_System.java

private boolean someIPaddressWrong() {
    InetAddressValidator validator = InetAddressValidator.getInstance();

    for (int i = 0; i < gateCount; i++) {
        JTextField txtField;//  w w w  .jav  a  2s .  c om

        for (DeviceType devType : DeviceType.values()) {
            String devName = devType.toString();
            txtField = (JTextField) getComponentByName(devName + (i + 1) + "_IP_TextField");
            if (!validator.isValidInet4Address(txtField.getText())) {
                GatesTabbedPane.setSelectedIndex(i);
                txtField.requestFocusInWindow();
                JOptionPane.showConfirmDialog(this,
                        devType.getContent() + " #" + (i + 1) + " " + IP_ADDR_ERROR_1.getContent()
                                + System.lineSeparator() + IP_ADDR_ERROR_2.getContent() + "127.0.0.1",
                        IP_ERROR_TITLE.getContent(), JOptionPane.PLAIN_MESSAGE, WARNING_MESSAGE);
                return true;
            }
        }
    }
    return false;
}

From source file:org.apache.geode.distributed.internal.membership.gms.membership.HostAddress.java

public HostAddress(InetSocketAddress loc, String locStr) {
    this.socketInetAddress = loc;
    this.hostname = locStr;
    this.port = loc.getPort();
    this.isIpString = InetAddressValidator.getInstance().isValid(locStr);
}