001package com.thed.util; 002 003/** 004 * @author mohan 005 */ 006 007import java.io.IOException; 008import java.net.MalformedURLException; 009import java.net.URL; 010import java.net.URLConnection; 011 012import javax.net.ssl.SSLException; 013 014public class URLValidator { 015 016 public static final String INVALID_URL = "This is not a valid URL"; 017 public static final String SSL_ERROR = "SSL Exception"; 018 public static final String CONNECTION_ERROR = "Could not establish the connection"; 019 020 public static String validateURL(String string) { 021 022 String result = null; 023 URL url = null; 024 URLConnection conn = null; 025 try { 026 url = new URL(string); 027 conn = url.openConnection(); 028 conn.connect(); 029 030 result = url.getProtocol(); 031 result += "://"; 032 result += url.getHost(); 033 034 int port = url.getPort(); 035 if (port > 0) { 036 result += ":"; 037 result += port; 038 039 } 040 } catch (MalformedURLException e) { 041 result = INVALID_URL; 042 } catch (SSLException e) { 043 result = SSL_ERROR; 044 } catch (IOException e) { 045 result = CONNECTION_ERROR; 046 } 047 return result; 048 } 049 050 public static String fetchURL(String serverAddress) { 051 return validateURL(serverAddress); 052 } 053 054 055}