import java.net.*;
import java.awt.*;

        // Check that the files have a valid suffix and that
        // the protocol is either http:// or file:/
class ImageURLHandler {
    public ImageURLHandler(String url) throws Exception {
        resource = new URL(url);
    }

        // Show the image associated with this URL.
        // Check that the file has a valid suffix and that
        // the protocol is either http:// or file:/
    protected void show() throws Exception {
        URL resource = getResource();
        // Check the suffix.
        if(!imageSuffix(resource.getFile())){
            throw new Exception(
                    "Unknown image suffix: "+resource.getFile());
        }

        // Check the protocol.
        String protocol = resource.getProtocol();
        if(protocol.equalsIgnoreCase("http") ||
                   (protocol.equalsIgnoreCase("file") &&
                   resource.getHost().equals(""))){
            // Load the image via the Toolkit.
            Toolkit tk = Toolkit.getDefaultToolkit();
            Image image = tk.getImage(resource);
            // Display the image.
            new ImageViewer(image);
        }
        else{
            throw new Exception("Only http or local file requests accepted: "+
                        resource.toExternalForm());
        }
    }

    // This version is prepared to accept common GIF and JPEG suffixes.
    protected boolean imageSuffix(String filename){
        int dot = filename.lastIndexOf('.');
        if(dot < 0){
            return false;
        }
        else{
            filename = filename.substring(dot).toLowerCase();
            return filename.equals(".gif") ||
                    filename.equals(".jpg") ||
                    filename.equals(".jpeg");
        }
    }

    protected URL getResource(){
        return resource;
    }

    private final URL resource;
}
