W3C home > Mailing lists > Public > www-jigsaw@w3.org > July to August 1996

Filter question/problem

From: Jeff Dripps <jeff@macalot.com>
Date: Fri, 9 Aug 1996 14:38:39 -0400
Message-Id: <v01540b00ae3130952db6@[207.18.93.43]>
To: www-jigsaw@w3.org
Hi folks,

I wrote a really simple filter that I thought would be great for end users
configuring Jigsaw. It's purpose was to eliminate having to hand type many
of the admin URLs discussed in the configuration tutorials. It's goal was
to replace embedded tags with the users actual host name and port number in
any html document the filter was applied to. For example in the
configuration.html example below, the tag <!--your.host.name--> is
replaced, resulting in the ability to just click the link to configure
rather than copy/paste it in.


Point your favorite browser to <A
HREF="http://<!--your.host.name-->/Admin/PropertiesEditor">/Admin/Properties
Editor</A>. This brings up a form, containing a number of fields. Below are
three more links:


The below filter works, but the problem is, it only replaces the first
instance of the found tag in the document and skips subsequent tags. Could
someone please explain to me what I am doing wrong. Many thanks! sinc,
-jeff


/*
 * @(#)HostURLFilter.java    0.01 9 Aug. 96 Jeff Dripps
 *
 */
package w3c.jigsaw.contrib;

import w3c.jigsaw.http.*;
import w3c.jigsaw.resources.*;

import java.io.*;
import java.lang.*;
import java.util.*;
import java.net.*;

/**
 * The target is to replace a tag in an html document with the host name
and port number.
 *
 * @version        0.01 9 Aug 1996
 * @author         Jeff Dripps
 */
public class HostURLFilter extends ResourceFilter {
    protected final static String URL_TAG = "<!--your.host.name-->";  //
tag to replace

    static {
        Attribute a   = null ;
        Class     cls = null ;

        try
        {
            cls = Class.forName("w3c.jigsaw.contrib.HostURLFilter") ;
        } catch (Exception ex) {
            ex.printStackTrace() ;
            System.exit(1) ;
        }

    }

    /**
     * @param request The request being processed.
     */
    public synchronized int ingoingFilter(Request request) {

        return CallOutgoing  ;
    }

    /**
     * Take the input stream and re-create a new stream for reply
     * with replace of <!--your.host.name--> tag with host info.
     *
     * @param request The request being processed.
     * @param reply   Reply to send to client
     */
    public Reply outgoingFilter(Request request, Reply reply)
    throws HTTPException
    {
        // Open the original stream
        InputStream in = reply.openStream() ;
        if ( in == null )
        {
            // System.out.println("InputStream == null");
            return reply ;
        }

        // create a DataInputStream for reading document lines
        DataInputStream ds = new DataInputStream(in);
        if (ds == null)
        {
            // System.out.println("DataInputStream == null");
            return reply;
        }

        String       strTmp;
        StringBuffer buffer = new StringBuffer();
        int          nPos   = -1;

        try {
            // Read the lines
            while ((strTmp = ds.readLine()) != null) {
               //  System.out.println(strTmp);

                // test if the line contains URL_TAG
                if ((nPos = strTmp.indexOf(URL_TAG)) != -1) {
                     System.out.println("host tag found");

                    // replace the tag with host info
                    strTmp = insertURL(request, strTmp, nPos);
                }

                buffer.append(strTmp).append("\n");
            }
        }
        catch (IOException e)
        {

        }

        // create a new stream
        StringBufferInputStream strIS = new
StringBufferInputStream(buffer.toString());

        // set the replay stream
        reply.setStream(strIS);

        return reply;
    }


    /**
     * Replace the tag with the host name and port number
     *
     * @param  strIn String in input
     * @param  nPos  start position of tag
     * @return String replaced
     */
    protected String insertURL(Request request, String strIn, int nPos)
    {
        String host = null;
        StringBuffer strOut = new StringBuffer(strIn.substring(0, nPos));
        httpd       server    = request.getClient().getServer() ;
        String port = new Integer (server.getLocalPort()).toString();
        InetAddress sadr      = server.getInetAddress() ;

        try {
            host = sadr.getLocalHost().getHostName() ;
        } catch (UnknownHostException e) {
            host = null ;
        }
        strOut.append(host);
        strOut.append(":");
        strOut.append(port);
        strOut.append(strIn.substring(nPos + URL_TAG.length()));
        return strOut.toString();
    }
}
Received on Friday, 9 August 1996 14:39:22 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Monday, 9 April 2012 12:13:25 GMT