Title: PROTOCOL CONTENT HANDLER
1 PROTOCOL / CONTENT HANDLER AND REMOTE METHOD
INVOCATION
2Agenda
- Part 1 Protocol and Content Handlers
- Part 2 Remote Method Invocation
3Part 1 Protocol And Content Handler
- Outline
- Protocol Handler
- What is it?
- URLStreamHandler Class
- Writing a protocol Hander
- Example and other techniques
- URLStreamHandlerFactory Interface
- Content Handler
- What is it?
- ContentHandler Class
- ContentHandlerFactory Interface
- FITS Image Format
4Overview
- When designing an architecture that would allow
them to build a self-extensible browser, the
engineers at Sun divided the problem into two
parts handling protocols and handling content.
5Part 1 Protocol And Content Handler
- Outline
- Protocol Handler
- What is it?
- URLStreamHandler Class
- Writing a protocol Hander
- Example and other techniques
- URLStreamHandlerFactory Interface
6What is it?
- Handling a protocol involves the interaction
between a client and a server - generating requests in the correct format
- interpreting the headers that come back with the
data - acknowledging that the data has been received
7Protocol Handler classes
- The protocol handler mechanism is implemented by
four difference class in the java.net package,
that are - URL the only concrete class in this group
- URLStreamHandler the abstract class
- URLConnection also the abstract classes
- URLStreamHandlerFactory is the interface
8How does it work???
9Part 1 Protocol And Content Handler
- Outline
- Protocol Handler
- What is it?
- URLStreamHandler Class
- Writing a protocol Hander
- Example and other techniques
- URLStreamHandlerFactory Interface
10URLStreamHandler Class
- The abstract URLStreamHandler class is a
superclass for classes that handle specific
protocols - By overriding the URLStreamHandler methods in
your own subclass, you teach the URL class how to
handle new protocols.
11URLStreamHandler Class
12URLStreamHandler Class
- Constructor
- Because URLStreamHandler is an abstract class,
this constructor is never called directly it is
only called from the constructors of subclasses. - The single constructor for URLStreamHandler
doesn't take any arguments - public URLStreamHandler( )
13URLStreamHandler Class
- Method
- The first responsibility is to split a string
representation of a URL into its component parts
and use those parts to set the various fields of
the URL object. - The parseURL( ) method splits the URL into parts,
possibly using setURL( ) to assign values to the
URL's fields.
14protected void parseURL(URL u, String spec, int
start, int limit)
- To parse the String spec into a URL u.
- The task is to set us protocol, host, port,
file, and ref fields - Any parts of the String that are before start and
after limit have already been parsed or can be
ignored. - The parseURL( ) method that Java supplies assumes
that the URL looks more or less like an http or
other hierarchical URL - protocol//www.host.comport/directory/another_dir
ectory/filefragmentID
15protected void parseURL(URL u, String spec, int
start, int limit)(cont.)
- Work ftp and gopher URLs.
- Not work mailto or news URLs and any new URL
schemes you define. - Using URLs that fit this hierarchical form, not
have to override parseURL() at all - If the URLs are completely different, must supply
parseURL( ) method that parses the URL completely - If your URL looks like a standard URL, make
parseURL( ) method that handles the nonstandard
portion of the URL and then calls super.parseURL(
) to do the rest of the work
16Example
- A mailto URL looks like mailtoabc_at_hcm.edu.vn.
- How to map this into the URL class's protocol,
host, port, file, and ref fields. - The protocol mailto.
- After the _at_ can be the host.
- what to do with the username.
- Mailto URL really doesn't have a file portion,
using the URL class's file field to hold the
username. - The ref the empty string or null.
17protected String toExternalForm(URL u)
- Pieces of the URL uprotocol, host, port, file,
and ref fieldsback together in a String. - A class that overrides parseURL() should also
override toExternalForm( ) - return "mailto" u.getFile( ) "_at_"
u.getHost( )
18protected void setURL(URL u, String protocol,
String host, int port, String authority, String
userInfo, String path, String query, String
fragmentID)
- Set the protocol, host, port, authority,
userInfo, path, query, and ref fields of the URL
u to the given values. - parseURL( ) uses this method set these fields to
the values it has found by parsing the URL. - the host, port, and user info together make up
the authority, are used in preference to the
authority when deciding which site to connect to.
19- protected int getDefaultPort( )
- returns the default port for the protocol, e.g.,
80 for HTTP - protected InetAddress getHostAddress(URL u)
- returns an InetAddress object pointing to the
server in the URL. If the host can't be located,
it simply returns null.
20- protected boolean hostsEqual(URL u1, URL u2)
- determines whether the two URLs refer to the same
server. This method does use DNS to look up the
hosts. - If the DNS lookups succeed, it can tell that
- if the DNS lookup fails for any reason, then
hostsEqual( ) falls back to a simple
case-insensitive string comparison, in which case
it would think these were two different hosts.
21- protected boolean sameFile(URL u1, URL u2)
- determines whether two URLs point to the same
file. - It does this by comparing the protocol, host,
port, and path. - The files are considered to be the same only if
each of those four pieces is the same.
22- protected boolean equals(URL u1, URL u2)
- Tests almost the entire URL, including protocol,
host, file, path, and fragment identifier. Only
the query string is ignored. - All five of these must be equal for the two URLs
to be considered equal. - protected int hashCode(URL u)
- Change the default hash code calculation by
overriding this method. - Do this if you override equals( ), sameFile(), or
hostsEqual( ) to make sure that two equal URL
objects will have the same hash code, and two
unequal URL objects will not have the same hash
code
23A Method for Connecting
- The second responsibility of a URLStreamHandler
is to create a URLConnection object appropriate
to the URL
24protected abstract URLConnection
openConnection(URL u) throws IOException
- u the URL to connect to.
- It returns an unopened URLConnection, directed at
the resource u points to. Each subclass of
URLStreamHandler should know how to find the
right subclass of URLConnection for the protocol
it handles. - The URL u that is passed as an argument is the
URL that needs a connection. - The subclass's openConnection( ) method is
usually extremely simple in most cases, it just
calls the constructor for the appropriate
subclass of URLConnection
25protected URLConnection openConnection(URL u,
Proxy p) throws IOException
- To specify a proxy server for the connection
- Rather than connecting to the host directly, this
URLConnection connects to the specified proxy
server, which relays data back and forth between
the client and the server. - Protocols that do not support proxies can simply
ignore the second argument. - To bypass the usual proxy server and connect
directly instead, pass the constant
Proxy.NO_PROXY as the second argument.
26Part 1 Protocol And Content Handler
- Outline
- Protocol Handler
- What is it?
- URLStreamHandler Class
- Writing a protocol Hander
- Example and other techniques
- URLStreamHandlerFactory Interface
27Writing a Protocol Handler
- Demonstrate by writing one for the finger
protocol defined in RFC 1288 - Finger is a relatively simple protocol compared
to JDK-supported protocols such as HTTP and FTP
28Writing a Protocol Handler
- Implementing a finger URL like
- finger//hostnameport/usernames
- getContentType() method get the content type
returned by the finger protocol's - A finger server returns ASCII text, so the
getContentType() method should return the string
text/plain.
29Writing a Protocol Handler
- New protocols such as HTTP use MIME headers to
indicate the content type. - Most protocols precede MIME gt need to specify
the MIME type explicitly or use the static
methods - URLConnection.guessContentTypeFromName(String
name) and - URLConnection.guessContentTypeFromStream(InputStre
am in)
30Writing a Protocol Handler
31Part 1 Protocol And Content Handler
- Outline
- Protocol Handler
- What is it?
- URLStreamHandler Class
- Writing a protocol Hander
- Example and other techniques
- URLStreamHandlerFactory Interface
32More Protocol Handler Examples and Techniques
- Five basic steps of creating a new protocol
handler - Design a URL for the protocol if a standard URL
for that protocol doesn't already exist. - Decide what MIME type should be returned by the
protocol handler's getContentType( ) method. - Write a subclass of URLConnection that
understands this protocol. - Write a subclass of URLStreamHandler with an
openConnection( ) method - Implement the URLStreamHandlerFactory interface
and the createStreamHandler( ) method -
33More Protocol Handler Examples and Techniques
- Daytime Protocol
- Chargen Protocol
34A daytime Protocol Handler
- URL look like daytime///vision.poly.edu
- Allow for nonstandard port assignments
- Ex daytime///vision.poly.edu2082
- Allow a terminating slash and ignore everything
following the slash - Ex daytime///vision.poly.edu/index.html is
equivalent to daytime///vision.poly.edu.
35A daytime Protocol Handler
- Using the default toExternalForm( ) and
parseURL() methods. - Although the content returned by the daytime
protocol is really text/plain, this protocol
handler is going to reformat the data into an
HTML page.
36A daytime Protocol Handler
- The page can be broken up into three different
strings - Everything before the time
- The time
- Everything after the time
- First and the third strings can be calculated
before the connection is even opened. - Formulate these as byte arrays of ASCII text and
use them to create two ByteArrayInputStreams - Use SequenceInputStream to combine those two
streams with the data actually returned from the
server.
37A chargen Protocol Handler
- Defined in RFC 864
- Is a very simple protocol designed for testing
clients. - A chargen URL like chargen//hostnameport
- getContentType() method choose the content type
to be returned by the chargen protocol handler's - A chargen server returns ASCII text, so the
getContentType( ) method should return the string
text/plain.
38Part 1 Protocol And Content Handler
- Outline
- Protocol Handler
- What is it?
- URLStreamHandler Class
- Writing a protocol Hander
- Example and other techniques
- URLStreamHandlerFactory Interface
39The URLStreamHandlerFactory Interface
- The URLStreamHandlerFactory interface declares a
single method, createURLStreamHandler( ) - This method loads the appropriate protocol
handler for the specified protocol. - To use this method, write a class that implements
the URLStreamHandlerFactory interface and include
a createURLStreamHandler( ) method in that class.
40The URLStreamHandlerFactory Interface
- createURLStreamHandler( ) method does not need to
know the names of all the installed protocol
handlers. - To install the stream handler factory, pass an
instance of the class that implements the
URLStreamHandlerFactory interface to the static
method URL.setURLStreamHandlerFactory( ) at the
start of the program.
41The URLStreamHandlerFactory Interface
42Part 1 Protocol And Content Handler
- Outline
- Content Handler
- What is it?
- ContentHandler Class
- ContentHandlerFactory Interface
- FITS Image Format
43Idea
- The browser could automatically download the code
that was needed to view that content type. - Avoiding to stop, FTP a plug-in, quit the
browser, install the plug-in, restart the
browser, and reload the page.
44What is content handler?
- Handling the content involves converting the raw
data into a format Java understands - for example, an InputStream or an AudioClip.
- The content handler would be responsible for
parsing the content and displaying it to the user
in the web browser window.
45Content Handler Classes
- The abstract class that content handlers for
specific data types such as PNG or RTF would
extend was java.net.ContentHandler
46How does it work???
47Disavantage
- The ContentHandler class was too generic,
providing too little information about what kind
of object was being downloaded and how it should
be displayed.
48Part 1 Protocol And Content Handler
- Outline
- Content Handler
- What is it?
- ContentHandler Class
- ContentHandlerFactory Interface
- FITS Image Format
49The ContentHandler Class
- Quite simple A text/plain content handler is
quite simple - Quite complex A text/rtf content handler would
be very complex. - Depending almost entirely on the complexity of
the content type to parse. - public ContentHandler( )Since ContentHandler is
an abstract class, call only from inside the
constructors of subclasses.
50public abstract Object getContent(URLConnection
uc) throws IOException
- Use the URLConnection's InputStream to create an
object - Depends on what the application requesting the
content expects. - Text-like content bundled with the JDK ? return
some subclass of InputStream. - Images ? return ImageProducer objects
- The InputStream includes only the content's raw
data. - Any MIME headers or other protocol-specific
information that come from the server should be
stripped by the URLConnection before it passes
the stream to the ContentHandler. - A ContentHandler is only responsible for content,
not for any protocol overhead that may be present.
51Tab-Separated Values
- Produced by many database and spreadsheet
programs. - JPE Associates ?341 Lafayette Street, Suite 1025
? - New York ?NY? 10012
- Each line is a record, and the data before each
tab is a field. - what kind of Java object should we convert the
tab- separated values to? - The simplest and most general way to store each
record is as an array of Strings. Successive
records can be collected in a Vector - public class Address private String name
private String street private String city
private String state private String zip
52Using Content Handlers
- The application that uses the tab-separated-values
does not need to know about it explicitly. - Simply call the getContent( ) method of URL or
URLConnection on a URL with a matching MIME type.
- Content-type of text/plain, application/octet-stre
am, or some other value, or not any Content-type
at all, the server is misconfigured and must be
fixed before you continue.
53Choosing Return Types
- public Object getContent(URLConnection uc,
Class classes) throws IOException - Allows the caller to request that the content be
returned as one of the types in the array and
enables content handlers to support multiple
types. - Text/tab-separated-values content handler could
return data as a Vector, an array, a string, or
an InputStream. - The client doesn't request any of the classes
this ContentHandler knows how to provide, it
returns null.
54Choosing Return Types (cont.)
- To call this method, the client invokes the
method with the same arguments in a URL or
URLConnection object. - It passes an array of Class objects in the order
it wishes to receive the data. - Thus, if receiving a String but is willing to
accept an InputStream and will take a Vector as a
last resort, it puts String.class in the zeroth
component of the array, InputStream.class in the
first component of the array, and Vector.class in
the last component of the array. - Then it uses instanceof to test what was actually
returned and either process it or convert it into
the preferred type.
55Part 1 Protocol And Content Handler
- Outline
- Content Handler
- What is it?
- ContentHandler Class
- ContentHandlerFactory Interface
- FITS Image Format
56The ContentHandlerFactory Interfaces
- A ContentHandlerFactory defines the rules for
where ContentHandler classes are stored. - The createContentHandler() method in the
ContentHandlerFactory should return null if no
ContentHandler appropriates for a MIME type. - If it is, Java will look for ContentHandler
classes in the default locations. - The method setContentHandlerFactory() of the
URLConnection will initialize a
ContentHandlerFactory.
57The createContentHandler() method
- public abstract ContentHandler createContentHandle
r(String mimeType) - This method should be called only by the
getContent() method of a URLConnection object.
58The createContentHandler() method
- Example TabFactory
- package com.macfaq.net.www.content.
- import java.net.
- public class TabFactory implements
ContentHandlerFactory - public ContentHandler createContentHandler(String
mineType) - if (mimeType.equals(text/tab-separated-values)
- return new com.macfaq.net.www.content.text.tab-sep
arated-values() - else
- return null // look for the handler in the
default locations -
-
59The createContentHandler() method
- The createContentHandler() method also suggests
handlers for - text/tab-separeated-values
- application/x-time
- text/plain
- video/mpeg
- mode/vrml
- ....
60Install Content Handler Factories
- Use the static URLConnection.setContentHandlerFact
ory() method to install ContentHandlerFactory. - public static void setContentHandlerFacotry(
ContentHandlerFactory fac) throws
SecurityException, Error
61Install Content Handler Factories
- Example TabLoader that uses a ContentHandlerFacot
ry - import java.io.
- import java.net.
- import java.uitl.
- import com.macfaq.net.www.content.
- public class TabLoader
- public static void main(String args)
- URLConnection.setContentHandlerFactory(new
TabFacotry()) - for (int i 0 ilt args.length() i)
- try
- URL u new URL(argsi)
- Object content u.getContent()
- Vector v (Vector) content
- for (Enumeration e v.elements()
e.hasMoreElements() ) - String sa (String) e.nextElement()
- for (int j 0 j lt sa.length() j)
- System.out.print(saj \t)
- System.out,println()
-
62Install Content Handler Factories (cont)
- java TabLoader http//metalab.unc.edu/javafaq/ad
dresses.tab - JPE Associates
- ?
- 341 Lafayette St, Suite 1025
- ?
- New York
- ?
- NY
- ?
- 10012
- O'Reilly Associates
- ?
- 103 Morris St, Suite A
- ?
- Sebastopol
- ?
- CA
- ?
- 95472
63Part 1 Protocol And Content Handler
- Outline
- Content Handler
- What is it?
- ContentHandler Class
- ContentHandlerFactory Interface
- FITS Image Format
64A Content Handler for an Image Format
image.x-fits
- We produce an object that implements the
java.awt.ImageProducer interface rather than an
Input Stream object. - For more details about the FITS format and how to
handle FITS file, please see Encyclopedia of
Graphics File Formats, Second Edition, by James
D. Murray and William vanRyper (OReilly
Associates, Inc.)
65A Content Handler for an Image Format
image.x-fits
- FITS files are broken up into blocks of exactly
2,880 bytes. - Each FITS file has two parts, the header and the
primary data unit. The header occupies an
integral number of blocks, as does the primary
data unit. - If the FITS file contains extensions, there may
be additional data after the primary data unit,
however we dont care about that.
66A Content Handler for an Image Format
image.x-fits
- Here is a simple header taken from a FITS file
- SIMPLE T /
- BITPIX 16 /
- NAXIS 2 /
- NAXIS1 242 /
- NAXIS2 252 /
- DATE '19 Aug 1996' /
- TELESC 'NSO/SP - VTT' /
- IMAGE 'Continuum' /
- COORDS 'N29.1W34.2' /
- OBSEND
67A Content Handler for an Image Format
image.x-fits
- Example an x-fits Content Handler
68A Content Handler for an Image Format
image.x-fits
- Example the FITS ContentHandlerFacotry
69Part 2 Remote Method Invocation
- Outline
- What is it?
- Implementation
- Loading class at run time
- The java.rmi Package
- The java.rmi.registry Package
- The java.rmi.server Package
70Java Remote Object Invocation (RMI)
- Java RMI allowed programmer to execute remote
function class using the same semantics as local
functions calls.
71Idea
- Distribute object across difference machine to
take advantages of hardware and dedicated
software. - Developer builds network service and install it
on specified machine. - User requests an instance of class using URL
syntax. - User uses object as though it were a regular,
local object
72RMI operations
- Stub
- Packet identifier of remote object
- Package method identifier
- Marshall parameter
- Send package to server skeleton
- Skeleton
- Unmarshall parameters
- Calls return value or exception
- Marshall method return
- Send package to client stub
73The General RMI Architecture
- The server must first bind its name to the
registry - The client lookup the server name in the registry
to establish remote references. - The Stub serializing the parameters to skeleton,
the skeleton invoking the remote method and
serializing the result back to the stub.
74Three Main Packages
- java.rmi defines the classes, interfaces, and
exceptions that will be seen on the client side
access remote objects but are not themselves
remote objects. - java.rmi.server defines the classes, interfaces,
and exceptions that will be visible on the server
side. Use these classes when you are writing a
remote object that will be called by clients - java.rmi.registry defines the classes,
interfaces, and exceptions that are used to
locate and name remote objects.
75Part 2 Remote Method Invocation
- Outline
- What is it?
- Implementation
- Loading class at run time
- The java.rmi Package
- The java.rmi.registry Package
- The java.rmi.server Package
76The Remote Interface
- In RMI, a common remote interface is the minimum
amount of information that must be shared in
advance between client and server machines.
It defines a high-level protocol through which
the machines will communicate. - A remote interface is a normal Java interface,
which must extent the marker interface
java.rmi.Remote. - Corollaries because the visible parts of a
remote object are defined through a Java
interface, constructors, static methods and
non-constant fields are not remotely accessible
(because Java interfaces cant contain such
things). - All methods in a remote interface must be
declared to throw the java.rmi.RemoteException
exception.
77A Simple Example
- A file MessageWriter.java contains the interface
definition - import java.rmi.
- public interface MessageWriter extends
Remote - void writeMessage(String s) throws
RemoteException -
- This interface defines a single remote method,
writeMessage().
78java.rmi.Remote
- The interface java.rmi.Remote is a marker
interface. - It declares no methods or fields however,
extending it tells the RMI system to treat the
interface concerned as a remote interface. - In particular we will see that the rmic compiler
generates extra code for classes that implement
remote interfaces. This code allows their
methods to be called remotely.
79java.rmi.RemoteException
- Requiring all remote methods be declared to throw
RemoteException was a philosophical choice by the
designers of RMI. - RMI makes remote invocations look syntactically
like local invocation. In practice, though, it
cannot defend from problems unique to distributed
computingunexpected failure of the network or
remote machine.
80java.rmi.RemoteException
- Forcing the programmer to handle remote
exceptions helps to encourage thinking about how
these partial failures should be dealt with. - See the influential essay A Note on Distributed
Computing by Waldo et al, republished in The
Jini Specification - http//java.sun.com/docs/boo
ks/jini
81class UnicastRemoteObject
- Extend java.rmi.server.UnicastRemoteObject,
either directly or indirectly - public class UnicastRemoteObject extends
RemoteServer - Marshalling is the process by which arguments and
return values are converted into a stream of
bytes that can be sent over the network. - Unmarshalling is the reverse the conversion of a
stream of bytes into a group of arguments or a
return value.) - A UnicastRemoteObject exists only as long as the
server that created it still runs. - When the server dies, the object is gone forever.
Activatable objects allow clients to reconnect to
servers at different times across server
shutdowns and restarts and still access the same
remote objects.
82The Remote Object
- A remote object is an instance of a class that
implements a remote interface. - Most often this class also extends the library
class java.rmi.server.UnicastRemoteObject. This
class includes a constructor that exports the
object to the RMI system when it is created, thus
making the object visible to the outside world.
83The Remote Object
- Usually you will not have to deal with this class
explicitlyyour remote object classes just have
to extend it. - One fairly common convention is to name the class
of the remote object after the name of the remote
interface it implements, but append Impl to the
end.
84A Remote Object Implementation Class
- The file MessageWriterImpl.java contains the
class declaration - import java.rmi.
- import java.rmi.server.
- public class MessageWriterImpl extends
UnicastRemoteObject -
implements MessageWriter - public MessageWriterImpl() throws
RemoteException -
- public void writeMessage(String s) throws
RemoteException - System.out.println(s)
-
-
85Compiling the Remote Object Class
- To compile classes that implement Remote, you
must use the rmic compiler. The reasons will be
discussed later. For example - sirah rmic MessageWriterImpl
86Client and Server Programs
- We have completed the Java files for the remote
object class itself, but we still need the actual
client and server programs that use this class. - In general there are some pieces of administrivia
one has to deal withpublishing class files and
installing security managers.
87Client and Server Programs
- We initially make the simplifying assumption that
both client and server have copies of all class
files for MessageWriter (e.g., they may share
access through shared NFS directories). - Then publishing class files is not an issue,
and we also dont need a security manager,
because all code is local, and therefore
trusted.
88A Server Program
- We assume the file HelloServer.java contains the
class declaration - import java.rmi.
- public class HelloServer
- public static void main(String args)
throws Exception - MessageWriter server new
MessageWriterImpl() - Naming.rebind(messageservice,
server) -
89Remarks
- This program does two things
- It creates a remote object with local name
server. - It publishes a remote reference to that object
with external name MessageWriter. - The call to Naming.rebind() places a reference to
server in an RMI registry running on the local
host (i.e., the host where the HelloServer
program is run). - Client programs can obtain a reference to the
remote object by looking it up in this registry.
90A Client Program
- We assume the file HelloClient.java contains the
class declaration - import java.rmi.
- public class HelloClient
- public static void main(String args)
throws Exception - MessageWriter server
- (MessageWriter)
Naming.lookup( -
rmi//sirah.csit.fsu.edu/messageservice) - server.writeMessage(Hello, other
world) -
91Remarks
- Again the program does two things
- It looks up a reference to a remote object with
external name MessageWriter, and stores the
returned reference with local name server. - Finally (!), it invokes the remote method,
writeMessage(), on server. - The call to Naming.lookup() searches in a remote
RMI registry. Its argument is a URL, with
protocol tag rmi. - This example assumes the remote object lives on
the host sirah, and has been registered in the
default RMI registry (which happens to listen on
port 1099) on that machine.
92Compiling and Running the Example
- Compile HelloServer and HelloClient on their
respective hosts, e.g. - sirah javac HelloServer
- merlot javac HelloClient
- Either ensure client and server share the current
directory, or copy all files with names of the
form MessageWriter .class to the clients
current directory.
93Part 2 Remote Method Invocation
- Outline
- What is it?
- Implementation
- Loading class at run time
- The java.rmi Package
- The java.rmi.registry Package
- The java.rmi.server Package
94Loading class at run time
- All the client really has to know about the
remote object is its remote interface. - Everything else it needsfor instance, the stub
classescan be loaded from a web server (though
not an RMI server) at runtime using a class
loader. - Indeed, this ability to load classes from the
network is one of the unique features of Java.
95Solution
- The solution is that we can use Applet.
- The reason is that whenever a web page including
an applet is loaded, all of the necessary classes
will be loaded too.
96Thank you for your listening