1 19 20 package com.sslexplorer.install.validators; 21 22 import java.net.InetAddress ; 23 import java.net.UnknownHostException ; 24 import java.util.Properties ; 25 26 import com.sslexplorer.boot.CodedException; 27 import com.sslexplorer.boot.PropertyDefinition; 28 import com.sslexplorer.boot.PropertyList; 29 import com.sslexplorer.boot.PropertyValidator; 30 import com.sslexplorer.core.CoreException; 31 import com.sslexplorer.input.validators.ErrorConstants; 32 33 35 public class ActiveDirectoryBackupHostnameValidator implements PropertyValidator { 36 37 40 public void validate(PropertyDefinition definition, String value, Properties properties) throws CodedException { 41 PropertyList propertyList = new PropertyList(value); 42 for (String propertyValue : propertyList) { 43 validate(propertyValue); 44 } 45 } 46 47 private static void validate(String value) throws CoreException { 48 int indexOf = value.indexOf(":"); 49 if (indexOf == -1) { 50 if (!isHostNameValid(value)) { 51 throw new CoreException(ErrorConstants.ERR_STRING_ISNT_IP_ADDRESS, ErrorConstants.CATEGORY_NAME, ErrorConstants.BUNDLE_NAME, null, value); 52 } 53 } else { 54 String hostName = value.substring(0, indexOf); 55 String port = value.substring(indexOf + 1); 56 if (!isHostNameValid(hostName) || !isPortValid(port)) { 57 throw new CoreException(ErrorConstants.ERR_STRING_ISNT_IP_ADDRESS, ErrorConstants.CATEGORY_NAME, ErrorConstants.BUNDLE_NAME, null, value); 58 } 59 } 60 } 61 62 private static boolean isHostNameValid(String hostName) { 63 try { 64 InetAddress.getByName(hostName); 65 return true; 66 } catch (UnknownHostException e) { 67 return false; 68 } 69 } 70 71 private static boolean isPortValid(String port) { 72 try { 73 Integer.parseInt(port); 74 return true; 75 } catch (NumberFormatException e) { 76 return false; 77 } 78 } 79 } | Popular Tags |