CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina
4/1/20042 URL Classes A framework for easily accessing and decoding URL resources Enable an application to create URL object using standard URL addressing scheme Then call getContent() method to download and decode the addressed object Two chains of control Protocol chain Content chain
4/1/20043 URL Chains of Control URL URLStreamHandler URLConnection URLStreamHandlerFactory HTTP / FTP / … URLConnection ContentHandler ContentHandlerFactory gif / au / … Protocol chainContent chain
4/1/20044 Class URL Constructors URL(String url) throws MalformedURLException URL(String protocol, String host, String file) throws MalformedURLException URL(String protocol, String host, int port, String file) throws MalformedURLException URL(String protocol, String host, int port, String file, URLStreamHandler handler) throws MalformedURLException URL(URL context, String spec) throws MalformedURLException URL(URL context, String spec, URLStreamHandler handler) throws MalformedURLException
4/1/20045 Class URL Methods String getProtocol() String getHost() int getPort() String getFile() String getRef() boolean sameFile(URL other) String toExternalForm() URLConnection openConnection() throws IOException InputStream openStream() throws IOException Object getContent() throws IOException protected void set(String protocol, String host, int port, String file, String ref) static void setURLStreamHandlerFactory(URLStreamHandlerFactory factory)
4/1/20046 Class URL Exceptions IOException MalformedURLException
4/1/20047 Example of Using URL Class URL url = new URL ( InputStream in = url.openStream (); Reader reader = new InputStreamReader (in, “latin1”); BufferedReader bufferedReader = new BufferedReader (reader); PrintWriter console = new PrintWriter (System.out); String line; while ((line = bufferedReader.readLine ())_ != null) console.println (line); console.flush(); bufferedReader.close ();
4/1/20048 Class URLConnection Constructor protected URLConnection(URL url)
4/1/20049 Class URLConnection Methods URL getURL() void setDoInput(boolean doInput) boolean getDoInput() void setDoOutput(boolean doOutput) boolean getDoOutput() void setAllowUserInteraction(boolean allow) boolean getAllowUserInteraction() void setUseCaches(boolean use) boolean getUseCaches() void setRequestProperty(String key, String value) String getRequestProperty(String key) void setIfModifiedSince(long ifModified) long getIfModifiedSince() abstract void connect() throws IOException
4/1/ Class URLConnection Methods OutputStream getOutputStream() throws IOException InputStream getInputStream() throws IOException Object getContent() throws IOException String getHeaderField(String name) String getHeaderFieldKey(int n) String getHeaderField(int n) int getHeaderFieldInt(String name, int alt) long getHeaderFieldDate(String name, long alt) int getContentLength() String getContentType() String getContentEncoding() long getExpiration() long getDate() long getLastModified()
4/1/ Class URLConnection Static methods void setDefaultAllowUserInteraction(boolean defaultAllow) boolean getDefaultAllowUserInteraction() void setDefaultRequestProperty(String key, String value) String getDefaultRequestProperty(String key) protected String guessContentTypeFromName(String fname) String guessContentTypeFromStream(InputStream is) FileNameMap getFileNameMap() void setFileNameMap(FileNameMap map) void setContentHandlerFactory(ContentHandlerFactory factory)
4/1/ Class URLConnection Variables protected URL url protected boolean doInput protected boolean doOutput protected boolean allowUserInteraction protected boolean useCaches protected long ifModifiedSince protected boolean connected Exceptions IOException
4/1/ Example of Using URLConnection Class // String user, password; URL url = new URL (getCodeBase (), “/cgi-bin/update.cgi”); URLConnection connection = url.openConnection (); connection.setDoOutput (true); OutputStream out = connection.getOutputStream (); Writer writer = new OutputStreamWriter (out, “latin1”); writer.write (“user=” + URLEncoder.encode (user)); writer.write (“&password=” + URLEncoder.encode (password)); writer.close (); InputStream in = connection.getInputStream (); Reader reader = new InputStreamReader (in, “latin1”); BufferedReader bufferedReader = new BufferedReader (reader); PrintWriter console = new PrintWriter (System.out); String line; while ((line = bufferedReader.readLine ())_ != null) console.println (line); console.flush(); bufferedReader.close ();
4/1/ Class HttpURLConnection Constructor protected HttpURLConnection(URL url)
4/1/ Class HttpURLConnection Methods void setRequestMethod(string method) throws ProtocolException String getRequestMethod() int getResponseCode() throws IOException String getResponseMessage() throws IOException abstract void disconnect() boolean abstract usingProxy()
4/1/ Class HttpURLConnection Static methods void setFollowRedirects(boolean set) boolean getFollowRedirects()
4/1/ Class HttpURLConnection Variables protected String method protected int responseCode protected String responseMessage Static variables Success codes Redirect codes Client error codes Server error codes
4/1/ Example of HttpURLConnection Class URL url = new URL (getCodeBase (), “/cgi-bin/update.cgi”); URLConnection conn = url.openConnection (); HttpURLConnection httpConn = (HttpURLConnection) conn; int responseCode = httpConn.getResponseCode (); if (responseCode == HttpURLConnection.HTTP_OK) { InputStream in = httpConn.getInputStream (); Reader reader = new InputStreamReader (in, “latin1”); BufferedReader bufferedReader = new BufferedReader (reader); PrintWriter console = new PrintWriter (System.out); String line; while ((line = bufferedReader.readLine ())_ != null) console.println (line); console.flush(); bufferedReader.close (); }
4/1/ Interface URLStreamHandlerFactory Method URLStreamHandler createURLStreamHandler(String protocol)
4/1/ Class URLStreamHandler Create an appropriate instance of URLConnection class capable of serving a particular protocol Method protected abstract URLConnection openConnection(URL url) throws IOException protected void parseURL(URL url, String spec, int start, int limit) protected String toExternalForm(URL url) protected void setURL(URL url, String protocol, String host, int port, String file, String ref)
4/1/ Interface ContentHandlerFactory Method ContentHandler createContentHandler(String mimetype)
4/1/ Class ContentHandler Decode content of a URL and produce result that is usable by Java run time Method abstract Object getContent(URLConnection connection) throws IOException
4/1/ An HTTP Protocol Handler Example Examine headers sent by server to determine type of file and to invoke appropriate handler URL getContent() HTTPURLStreamHandler openConnection() HTTPURLConnection getInputStream() URLStreamHandlerFactoryImpl createURLStreamHandler() URLConnection getContent() TextPlainContentHandler getContent() ContentHandlerFactoryImpl createContentHandler() PageViewer main()
4/1/ Class URLStreamHandlerFactoryImpl /* * Java Network Programming, Second Edition * Merlin Hughes, Michael Shoffner, Derek Hamner * Manning Publications Company; ISBN X * * * * Copyright (c) Merlin Hughes, Michael Shoffner, Derek Hamner; * all rights reserved; see license.txt for details. */ import java.io.*; import java.net.*; public class URLStreamHandlerFactoryImpl implements URLStreamHandlerFactory { public URLStreamHandler createURLStreamHandler (String protocol) { if (protocol.equalsIgnoreCase ("http")) return new HTTPURLStreamHandler (); else if (protocol.equalsIgnoreCase ("finger")) return new FingerURLStreamHandler (); else if (protocol.equalsIgnoreCase ("rawfinger")) return new RawFingerURLStreamHandler (); else return null; }
4/1/ Class HTTPURLStreamHandler /* * Java Network Programming, Second Edition * Merlin Hughes, Michael Shoffner, Derek Hamner * Manning Publications Company; ISBN X * * * * Copyright (c) Merlin Hughes, Michael Shoffner, Derek Hamner; * all rights reserved; see license.txt for details. */ import java.io.*; import java.net.*; public class HTTPURLStreamHandler extends URLStreamHandler { protected URLConnection openConnection (URL url) throws IOException { return new HTTPURLConnection (url); }
4/1/ Class HTTPURLConnection /* * Java Network Programming, Second Edition * Merlin Hughes, Michael Shoffner, Derek Hamner * Manning Publications Company; ISBN X * * * * Copyright (c) Merlin Hughes, Michael Shoffner, Derek Hamner; * all rights reserved; see license.txt for details. */ import java.io.*; import java.net.*; import java.util.*; Import java.text.*; public class HTTPURLConnection extends URLConnection { // public HTTPURLConnection (URL url) … // public void setRequestProperty (String name, String value) … // public String getRequestProperty (String name) … // public synchronized void connect () throws IOException … // public InputStream getInputStream () throws IOException … // public String getHeaderFieldKey (int index) … // public String getHeaderField (int index) … // public String getHeaderField (String key) … }
4/1/ Constructor HTTPURLConnection protected Hashtable requestProperties; protected Vector keys; protected Hashtable headers; public HTTPURLConnection (URL url) { super (url); requestProperties = new Hashtable (); keys = new Vector (); headers = new Hashtable (); }
4/1/ Method setRequestProperty public void setRequestProperty (String name, String value) { if (connected) throw new IllegalStateException ("Already connected."); requestProperties.put (name, value); } public String getRequestProperty (String name) { return (String) requestProperties.get (name); }
4/1/ Method connect protected InputStream in; public synchronized void connect () throws IOException { if (!connected) { String host = url.getHost (); int port = (url.getPort () == -1) ? 80 : url.getPort (); Socket socket = new Socket (host, port); sendRequest (socket.getOutputStream ()); in = new BufferedInputStream (socket.getInputStream ()); readHeaders (); connected = true; } // protected void sendRequest (OutputStream out) throws IOException … // protected void readHeaders () throws IOException …
4/1/ Method sendRequest protected void sendRequest (OutputStream out) throws IOException { setRequestProperty ("User-Agent", "JNP-HTTP/2e"); if (ifModifiedSince != 0) { Date since = new Date (ifModifiedSince); SimpleDateFormat formatter = new SimpleDateFormat ("EEE, d MMM yyyy hh:mm:ss z"); formatter.setTimeZone (TimeZone.getTimeZone ("GMT")); setRequestProperty ("If-Modified-Since", formatter.format (since)); } StringBuffer request = new StringBuffer ("GET "); request.append (URLEncoder.encode (url.getFile ())); request.append (" HTTP/1.0\r\n"); Enumeration keys = requestProperties.keys (); while (keys.hasMoreElements ()) { String key = (String) keys.nextElement (); request.append (key); request.append (": "); request.append (requestProperties.get (key)); request.append ("\r\n"); } request.append ('\n'); out.write (request.toString ().getBytes ("latin1")); }
4/1/ Method readHeaders protected void readHeaders () throws IOException { String status = readLine (); String header; while (((header = readLine ()) != null) && (!header.trim ().equals (""))) { int colon = header.indexOf (":"); if (colon >= 0) { String key = header.substring (0, colon).trim (); String value = header.substring (colon + 1).trim (); keys.addElement (key); headers.put (key.toLowerCase (), value); } // protected String readLine () throws IOException …
4/1/ Method readLine protected String readLine () throws IOException { StringBuffer result = new StringBuffer (); int chr; while (((chr = in.read ()) != -1) && (chr != 10) && (chr != 13)) result.append ((char) chr); if ((chr == -1) && (result.length () == 0)) return null; if (chr == 13) { in.mark (1); if (in.read () != 10) in.reset (); } return result.toString (); }
4/1/ Method getInputStream public InputStream getInputStream () throws IOException { if (!doInput) throw new IllegalStateException ("Input disabled."); connect (); return in; }
4/1/ Methods to Read Headers public String getHeaderFieldKey (int index) { if (!connected) throw new IllegalStateException ("Not connected."); if (index < keys.size ()) return (String) keys.elementAt (index); else return null; } public String getHeaderField (int index) { if (!connected) throw new IllegalStateException ("Not connected."); if (index < keys.size ()) return getHeaderField ((String) keys.elementAt (index)); else return null; } public String getHeaderField (String key) { if (!connected) throw new IllegalStateException ("Not connected."); return (String) headers.get (key.toLowerCase ()); }
4/1/ Class ContentHandlerFactoryImpl /* * Java Network Programming, Second Edition * Merlin Hughes, Michael Shoffner, Derek Hamner * Manning Publications Company; ISBN X * * * * Copyright (c) Merlin Hughes, Michael Shoffner, Derek Hamner; * all rights reserved; see license.txt for details. */ import java.io.*; import java.net.*; public class ContentHandlerFactoryImpl implements ContentHandlerFactory { public ContentHandler createContentHandler (String mimeType) { if (mimeType.equalsIgnoreCase ("text/plain")) return new TextPlainContentHandler (); else return null; }
4/1/ Class TextPlainContentHandler /* * Java Network Programming, Second Edition * Merlin Hughes, Michael Shoffner, Derek Hamner * Manning Publications Company; ISBN X * * * * Copyright (c) Merlin Hughes, Michael Shoffner, Derek Hamner; * all rights reserved; see license.txt for details. */ import java.io.*; import java.net.*; public class TextPlainContentHandler extends ContentHandler { public Object getContent (URLConnection connection) throws IOException { InputStream in = connection.getInputStream (); Reader reader = new InputStreamReader (in, "latin1"); BufferedReader bufferedReader = new BufferedReader (reader); StringBuffer content = new StringBuffer (); String line; while ((line = bufferedReader.readLine ()) != null) content.append (line).append ('\n'); bufferedReader.close (); return content.toString (); }
4/1/ Class PageViewer /* * Java Network Programming, Second Edition * Merlin Hughes, Michael Shoffner, Derek Hamner * Manning Publications Company; ISBN X * * * * Copyright (c) Merlin Hughes, Michael Shoffner, Derek Hamner; * all rights reserved; see license.txt for details. */ import java.io.*; import java.net.*; public class PageViewer { public static void main (String args[]) throws IOException { if (args.length != 1) throw new IllegalArgumentException ("Usage: PageViewer "); URL.setURLStreamHandlerFactory (new URLStreamHandlerFactoryImpl ()); URLConnection.setContentHandlerFactory (new ContentHandlerFactoryImpl ()); URL url = new URL (args[0]); System.out.println (url.getContent ()); }
4/1/ Next Class Remote method invocation (RMI) Read JNP Ch. 23