org.switchboard.util
Class NetUtil

java.lang.Object
  extended by org.switchboard.util.NetUtil

public class NetUtil
extends Object

A collection of network-related static functions.


Constructor Summary
NetUtil()
           
 
Method Summary
static boolean exists(String url)
          Checks to see if a remote file exists.
static String getFileAsString(URL url)
          Gets the contents of a URL as a string.
static String getInputStreamAsString(InputStream is)
          Gets the contents of an input stream as a String
static String[] getIPs(URL url)
          Parses a document and returns any IP addresses that appear on it.
static String[] getURLs(String text)
           
static String getVarFromUrl(String url, String key)
          Gets a particular value from a URL with variables in it
static Map getVarsFromURL(String url)
          Parses a URL, picking out the key/value pairs
static boolean hasContents(String url)
           
 boolean isValidURL(String url)
           
static String makeRestRequest(String endpoint, Map params)
          Makes a REST request URI out of an endpoint and a hashmap of parameters
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NetUtil

public NetUtil()
Method Detail

getFileAsString

public static String getFileAsString(URL url)
                              throws IOException,
                                     MalformedURLException
Gets the contents of a URL as a string.

Parameters:
url - The URL to fetch.
Returns:
A string containing the contents of the URL.
Throws:
IOException - If the document cannot be found.
MalformedURLException - If a bad URL is given.

getInputStreamAsString

public static String getInputStreamAsString(InputStream is)
                                     throws IOException
Gets the contents of an input stream as a String

Parameters:
is - the InputStream to read from
Returns:
A string with the contents of the InputStream
Throws:
IOException - If the page can't be found.

getIPs

public static String[] getIPs(URL url)
                       throws IOException
Parses a document and returns any IP addresses that appear on it.

Parameters:
url - The URL of the page to search.
Returns:
an array of Strings that contain the IP addresses.
Throws:
IOException - If the parser cannot open the document.

getURLs

public static String[] getURLs(String text)

makeRestRequest

public static String makeRestRequest(String endpoint,
                                     Map params)
Makes a REST request URI out of an endpoint and a hashmap of parameters

Parameters:
endpoint - The endpoint of the REST service.
params - A list of params, with the variable name as the hashmap key, and the value as the hashmap value
Returns:
The REST request as a URL
Throws:
MalformedURLException - If the endpoint is a bad URL.

hasContents

public static boolean hasContents(String url)

exists

public static boolean exists(String url)
Checks to see if a remote file exists.

Parameters:
url - The URL to check
Returns:
True if the HTTP request returns HTTP/1.0 200 OK, false otherwise

getVarsFromURL

public static Map getVarsFromURL(String url)
Parses a URL, picking out the key/value pairs

Parameters:
url - The URL to parse
Returns:
A Hashmap where the keys are the key names, and the values are the key values

getVarFromUrl

public static String getVarFromUrl(String url,
                                   String key)
Gets a particular value from a URL with variables in it

Parameters:
url - The URL to parse
key - The variable key
Returns:
The value

isValidURL

public boolean isValidURL(String url)