You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by am...@apache.org on 2001/03/27 22:38:01 UTC

cvs commit: jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/util/ssi ResponseIncludeWrapper.java ServletOutputStreamWrapper.java SsiCommand.java SsiConfig.java SsiEcho.java SsiExec.java SsiFlastmod.java SsiFsize.java SsiInclude.java SsiMediator.java

amyroh      01/03/27 12:38:01

  Added:       catalina/src/share/org/apache/catalina/util/ssi
                        ResponseIncludeWrapper.java
                        ServletOutputStreamWrapper.java SsiCommand.java
                        SsiConfig.java SsiEcho.java SsiExec.java
                        SsiFlastmod.java SsiFsize.java SsiInclude.java
                        SsiMediator.java
  Log:
  SSI feature util
  
  Revision  Changes    Path
  1.1                  jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/util/ssi/ResponseIncludeWrapper.java
  
  Index: ResponseIncludeWrapper.java
  ===================================================================
  /*
   * ResponseIncludeWrapper.java
   * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/util/ssi/ResponseIncludeWrapper.java,v 1.1 2001/03/27 20:38:00 amyroh Exp $
   * $Revision: 1.1 $
   * $Date: 2001/03/27 20:38:00 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */
  
  package org.apache.catalina.util.ssi;
  
  import java.io.OutputStream;
  import java.io.PrintWriter;
  import javax.servlet.ServletOutputStream;
  import javax.servlet.http.HttpServletResponse;
  import javax.servlet.http.HttpServletResponseWrapper;
  
  /**
   * @author Bip Thelin
   * @version $Revision: 1.1 $, $Date: 2001/03/27 20:38:00 $
   *
   */
  public final class ResponseIncludeWrapper
      extends HttpServletResponseWrapper {
  
      private ServletOutputStream _out;
  
      private boolean printwriter = false;
  
      private boolean outputstream = false;
  
      public ResponseIncludeWrapper(HttpServletResponse res,
  				  ServletOutputStream out) {
  	super(res);
  	this._out = out;
      }
  
      public PrintWriter getWriter()
  	throws java.io.IOException {
  	if(!outputstream) {
  	    printwriter=true;
  	    return (new PrintWriter(_out));
  	} else {
  	    throw new IllegalStateException();
  	}
      }
  
      public ServletOutputStream getOutputStream()
  	throws java.io.IOException {
  	if(!printwriter) {
  	    outputstream=true;
  	    return _out;
  	} else {
  	    throw new IllegalStateException();
  	}
      }
  }
  
  
  
  1.1                  jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/util/ssi/ServletOutputStreamWrapper.java
  
  Index: ServletOutputStreamWrapper.java
  ===================================================================
  /*
   * ServletOutputStreamWrapper.java
   * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/util/ssi/ServletOutputStreamWrapper.java,v 1.1 2001/03/27 20:38:00 amyroh Exp $
   * $Revision: 1.1 $
   * $Date: 2001/03/27 20:38:00 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */
  
  package org.apache.catalina.util.ssi;
  
  import java.io.OutputStream;
  import java.io.IOException;
  import java.io.ByteArrayOutputStream;
  import javax.servlet.ServletOutputStream;
  
  /**
   * @author Bip Thelin
   * @version $Revision: 1.1 $, $Date: 2001/03/27 20:38:00 $
   *
   */
  public final class ServletOutputStreamWrapper
      extends ServletOutputStream {
  
      private ByteArrayOutputStream _buf = null;
  
      public ServletOutputStreamWrapper() {
  	super();
  	_buf = new ByteArrayOutputStream();
      }
  
      public final void writeTo(OutputStream out) throws IOException {
  	out.write(_buf.toByteArray());
      }
  
      public final void write(int b) {
  	_buf.write(b);
      }
  }
  
  
  
  1.1                  jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/util/ssi/SsiCommand.java
  
  Index: SsiCommand.java
  ===================================================================
  /*
   * SsiCommand.java
   * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/util/ssi/SsiCommand.java,v 1.1 2001/03/27 20:38:00 amyroh Exp $
   * $Revision: 1.1 $
   * $Date: 2001/03/27 20:38:00 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */
  
  package org.apache.catalina.util.ssi;
  
  /**
   * @author Bip Thelin
   * @version $Revision: 1.1 $, $Date: 2001/03/27 20:38:00 $
   *
   */
  public interface SsiCommand {
  
      public String getStream(String[] strParamType,
  			    String[] strParam);
  
      public void process(String[] strParamType,
  			String[] strParam);
  
      public boolean isPrintable();
  
      public boolean isModified();
  
  }
  
  
  
  1.1                  jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/util/ssi/SsiConfig.java
  
  Index: SsiConfig.java
  ===================================================================
  /*
   * SsiConfig.java
   * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/util/ssi/SsiConfig.java,v 1.1 2001/03/27 20:38:00 amyroh Exp $
   * $Revision: 1.1 $
   * $Date: 2001/03/27 20:38:00 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */
  
  package org.apache.catalina.util.ssi;
  
  import java.util.Properties;
  
  /**
   * @author Bip Thelin
   * @version $Revision: 1.1 $, $Date: 2001/03/27 20:38:00 $
   *
   */
  public final class SsiConfig
      extends SsiMediator implements SsiCommand {
  
      private boolean modified = false;
  
      private byte[] errmsg;
  
      private String sizefmt;
  
      private String timefmt;
  
      private static Properties translate;
  
      static {
  	translate = new Properties();
  	translate.put("a","EEE");
  	translate.put("A","EEEE");
  	translate.put("b","MMM");
  	translate.put("h","MMM");
  	translate.put("B","MMMM");
  	translate.put("d","dd");
  	translate.put("D","MM/dd/yy");
  	translate.put("e","d");
  	translate.put("H","HH");
  	translate.put("I","hh");
  	translate.put("j","E");
  	translate.put("m","M");
  	translate.put("M","m");
  	translate.put("p","a");
  	translate.put("r","hh:mm:ss a");
  	translate.put("S","s");
  	translate.put("T","HH:mm:ss");
  	translate.put("U","w");
  	translate.put("W","w");
  	translate.put("w","E");
  	translate.put("y","yy");
  	translate.put("Y","yyyy");
  	translate.put("z","z");
      }
  
      public SsiConfig() {
  	init();
      }
  
      public String getStream(String[] strParamType, String[] strParam) {
  	return "";
      }
  
      /**
       * Process request.
       *
       * @param strParamType a value of type 'String[]'
       * @param strParam a value of type 'String[]'
       */
      public final void process(String[] strParamType, String[] strParam) {
  	modified = true;
  
  	for(int i=0;i<strParamType.length;i++) {
  	    if(strParamType[i].equals("errmsg"))
  		errmsg = strParam[i].getBytes();
  	    else if(strParamType[i].equals("sizefmt"))
  		sizefmt = strParam[i];
  	    else if(strParamType[i].equals("timefmt"))
  		timefmt = convertFormat(strParam[i]);
  	}
      }
  
      /**
       * Return the current error message.
       *
       * @return a value of type 'byte[]'
       */
      public final byte[] getError() {
  	return errmsg;
      }
  
      /**
       * Return the current Size format.
       *
       * @return a value of type 'String'
       */
      public final String getSizefmt() {
  	return sizefmt;
      }
  
      /**
       * Return the current Time format.
       *
       * @return a value of type 'String'
       */
      public final String getTimefmt() {
  	return timefmt;
      }
  
      /**
       * Initialize, run once per page being parsed.
       *
       */
      public final void flush() {
  	init();
      }
  
      /**
       * Returns true if SSI Command does any output.
       *
       * @return a value of type 'boolean'
       */
      public final boolean isPrintable() { return false;}
  
      /**
       * Return true if we're modified.
       *
       * @return a value of type 'boolean'
       */
      public final boolean isModified() { return modified; }
  
      private String convertFormat(String pattern) {
  	boolean inside = false;
  	boolean mark = false;
  	StringBuffer retString = new StringBuffer();
  	String sRetString = "";
  
  	for(int i = 0; i<pattern.length();i++) {
  	    if(pattern.charAt(i)=='%'&&!mark) {
  		mark=true;
  		continue;
  	    }
  
  	    if(pattern.charAt(i)=='%'&&mark) {
  		mark=false;
  	    }
  
  	    if(mark) {
  		if(inside) {
  		    retString.append("'");
  		    inside=false;
  		}
  
  		retString.append(translateCommand(pattern.charAt(i)));
  		mark=false;
  		continue;
  	    }
  
  	    if(!inside) {
  		retString.append("'");
  		inside = true;
  	    }
  
  	    retString.append(pattern.charAt(i));
  	}
  
  	sRetString = retString.toString();
  
  	if(!sRetString.endsWith("'")&&inside)
  	    sRetString = sRetString.concat("'");
  
  	return sRetString;
      }
  
      private String translateCommand(char c) {
  	String retCommand = translate.getProperty("".valueOf(c));
  
  	return retCommand==null?"":retCommand;
      }
  
      /**
       * Called from <code>flush</code>.
       *
       */
      private void init() {
  	errmsg =
  	    "[an error occurred while processing this directive]".getBytes();
  	sizefmt = "abbrev";
  	timefmt = "EEE, dd MMM yyyyy HH:mm:ss z";
      }
  }
  
  
  
  1.1                  jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/util/ssi/SsiEcho.java
  
  Index: SsiEcho.java
  ===================================================================
  /*
   * SsiEcho.java
   * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/util/ssi/SsiEcho.java,v 1.1 2001/03/27 20:38:00 amyroh Exp $
   * $Revision: 1.1 $
   * $Date: 2001/03/27 20:38:00 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */
  
  package org.apache.catalina.util.ssi;
  
  /**
   * @author Bip Thelin
   * @version $Revision: 1.1 $, $Date: 2001/03/27 20:38:00 $
   *
   */
  public final class SsiEcho
      extends SsiMediator implements SsiCommand {
  
      public SsiEcho() {}
  
      public final String getStream(String[] strParamType,
  				  String[] strParam) {
  	String retString;
  
  	if(strParamType[0].equals("var"))
  	    retString =
  		retString = super.getServerVariable(strParam[0]);
  	else
  	    retString = new String(super.getError());
  
  	return retString;
      }
  
      public final void process(String[] strParamType, String[] strParam) {}
      public final boolean isPrintable() { return true; }
  
      public final boolean isModified() { return false; }
  }
  
  
  
  1.1                  jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/util/ssi/SsiExec.java
  
  Index: SsiExec.java
  ===================================================================
  /*
   * SsiExec.java
   * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/util/ssi/SsiExec.java,v 1.1 2001/03/27 20:38:00 amyroh Exp $
   * $Revision: 1.1 $
   * $Date: 2001/03/27 20:38:00 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */
  
  package org.apache.catalina.util.ssi;
  
  /**
   * @author Bip Thelin
   * @version $Revision: 1.1 $, $Date: 2001/03/27 20:38:00 $
   *
   */
  public final class SsiExec
      extends SsiMediator implements SsiCommand {
  
      public SsiExec() {}
  
      public final String getStream(String[] strParamType,
  				  String[] strParam) {
  	return "";
      }
  
      public final void process(String[] strParamType, String[] strParam) {}
  
      public final boolean isPrintable() { return true; }
  
      public final boolean isModified() { return false; }
  }
  
  
  
  1.1                  jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/util/ssi/SsiFlastmod.java
  
  Index: SsiFlastmod.java
  ===================================================================
  /*
   * SsiFlastmod.java
   * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/util/ssi/SsiFlastmod.java,v 1.1 2001/03/27 20:38:00 amyroh Exp $
   * $Revision: 1.1 $
   * $Date: 2001/03/27 20:38:00 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */
  
  package org.apache.catalina.util.ssi;
  
  import java.util.Date;
  import java.io.IOException;
  import java.net.URL;
  import java.net.MalformedURLException;
  import javax.servlet.ServletContext;
  
  /**
   * @author Bip Thelin
   * @version $Revision: 1.1 $, $Date: 2001/03/27 20:38:00 $
   *
   */
  public final class SsiFlastmod
      extends SsiMediator implements SsiCommand {
  
      public SsiFlastmod() {}
  
      public final String getStream(String[] strParamType,
  				  String[] strParam) {
  	String path = new String();
  	long lastModified;
  	String sLastModified = null;
  	URL url = null;
  
  	if(strParamType[0].equals("file")) {
  	    path = super.getFilePath(strParam[0]);
  	} else if(strParamType[0].equals("virtual")) {
  	    path = super.getVirtualPath(strParam[0]);
  	}
  
  	try {
  	    url = super.servletContext.getResource(path);
  	    lastModified = url.openConnection().getLastModified();
  	} catch (MalformedURLException e){
  	    lastModified = 0;
  	} catch (IOException e) {
  	    lastModified = 0;
  	} catch (NullPointerException e) {
  	    lastModified = 0;
  	}
  
  	if(lastModified==0)
  	    sLastModified = (new String(super.getError()));
  	else
  	    sLastModified = super.timefmt(new Date(lastModified));
  
  	return sLastModified;
      }
  
      protected String getDate(String path) {
  	long lastModified;
  	String sLastModified = null;
  	URL url = null;
  
  	path = super.getVirtualPath(path);
  
  	try {
  	    url = super.servletContext.getResource(path);
  	    lastModified = url.openConnection().getLastModified();
  	} catch (MalformedURLException e){
  	    lastModified = 0;
  	} catch (IOException e) {
  	    lastModified = 0;
  	} catch (NullPointerException e) {
  	    lastModified = 0;
  	}
  
  	if(lastModified==0)
  	    sLastModified = (new String(super.getError()));
  	else
  	    sLastModified = super.timefmt(new Date(lastModified));
  
  	return sLastModified;
      }
  
      public final void process(String[] strParamType, String[] strParam) {}
  
      public final boolean isPrintable() { return true; }
  
      public final boolean isModified() { return false; }
  }
  
  
  
  1.1                  jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/util/ssi/SsiFsize.java
  
  Index: SsiFsize.java
  ===================================================================
  /*
   * SsiFsize.java
   * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/util/ssi/SsiFsize.java,v 1.1 2001/03/27 20:38:00 amyroh Exp $
   * $Revision: 1.1 $
   * $Date: 2001/03/27 20:38:00 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */
  
  package org.apache.catalina.util.ssi;
  
  import java.io.IOException;
  import java.net.URL;
  import java.net.MalformedURLException;
  import javax.servlet.ServletContext;
  
  /**
   * @author Bip Thelin
   * @version $Revision: 1.1 $, $Date: 2001/03/27 20:38:00 $
   *
   */
  public final class SsiFsize
      extends SsiMediator implements SsiCommand {
  
      public SsiFsize() {}
  
      /**
       * Figure out the length/size of a given file.
       *
       * @param strParamType a value of type 'String[]'
       * @param strParam a value of type 'String[]'
       * @param req a value of type 'HttpServletRequest'
       * @param servletContext a value of type 'ServletContext'
       * @return a value of type 'String'
       */
      public final String getStream(String[] strParamType,
  				  String[] strParam) {
  	String length = "";
  	String retLength = "";
  	String path = "";
  	URL url = null;
  	long lLength = -1;
  
  	if(strParamType[0].equals("file")) {
  	    path = super.getFilePath(strParam[0]);
  	} else if(strParamType[0].equals("virtual")) {
  	    path = super.getVirtualPath(strParam[0]);
  	}
  
  	try {
  	    url = super.servletContext.getResource(path);
  	    lLength = url.openConnection().getContentLength();
  	    length = (new Long(lLength)).toString();
  	} catch (MalformedURLException e){
  	    length = null;
  	} catch (IOException e) {
  	    length = null;
  	} catch (NullPointerException e) {
  	    length = null;
  	}
  
  	if(length == null)
  	    retLength = (new String(super.getError()));
  	else
  	    retLength =
  		formatSize(length,
  			   ((SsiConfig)super.getCommand("config")).getSizefmt());
  
  	return retLength;
      }
  
      public final void process(String[] strParamType, String[] strParam) {}
  
      public final boolean isPrintable() { return true; }
  
      public final boolean isModified() { return false; }
  
      //----------------- Private methods
  
      private String formatSize(String length, String format) {
  	String retString = "";
  
  	if(format.equalsIgnoreCase("bytes")) {
  	    retString = commaFormat(length);
  	} else {
  	    double lParse = (new Long(length)).doubleValue();
  
  	    if(lParse>=1048576) {
  	        double abbrevSize = lParse/1048576;
  		long splitSize = (long)abbrevSize;
  		int  catSize = (int)(100 * (abbrevSize - splitSize));
  
  		retString =
  		    commaFormat((new Long(splitSize)).toString())+
  		    "."+catSize+" MB";
  	    } else if(lParse>=1024) {
  	        double abbrevSize = lParse/1024;
  		long splitSize = (long)abbrevSize;
  		int  catSize = (int)(100 * (abbrevSize - splitSize));
  
  		retString =
  		    commaFormat((new Long(splitSize)).toString())+
  		    "."+catSize+" KB";
  	    } else {
  		retString =
  		    commaFormat(length)+" bytes";
  	    }
  	}
  
  	return retString;
      }
  
      private String commaFormat(String length) {
  	String retString = "";
  
  	for(int i = length.length();i-1>=0;i--) {
  	    retString = (length.substring(i-1,i)).concat(retString);
  	    if((length.length()-(i-1))%3==0&&
  	       retString.length()<length.length())
  		retString = ",".concat(retString);
  	}
  
  	return retString;
      }
  }
  
  
  
  1.1                  jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/util/ssi/SsiInclude.java
  
  Index: SsiInclude.java
  ===================================================================
  /*
   * SsiInclude.java
   * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/util/ssi/SsiInclude.java,v 1.1 2001/03/27 20:38:00 amyroh Exp $
   * $Revision: 1.1 $
   * $Date: 2001/03/27 20:38:00 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */
  
  package org.apache.catalina.util.ssi;
  
  import java.io.IOException;
  import javax.servlet.ServletException;
  import javax.servlet.ServletContext;
  import javax.servlet.ServletOutputStream;
  import javax.servlet.RequestDispatcher;
  
  /**
   * @author Bip Thelin
   * @version $Revision: 1.1 $, $Date: 2001/03/27 20:38:00 $
   *
   */
  public final class SsiInclude
      extends SsiMediator implements SsiCommand {
  
      public SsiInclude() {}
  
      public final String getStream(String[] strParamType,
  				  String[] strParam) {
  	String retString = "";
  	String path = "";
  
  	if(strParamType[0].equals("file"))
  	    path = super.getFilePath(strParam[0]);
  	else if(strParamType[0].equals("virtual"))
  	    path = super.getVirtualPath(strParam[0]);
  
  	if(path != null) {
  	    try {
  		if(super.servletContext.getResource(path) != null) {
  		    RequestDispatcher rd =
  			super.servletContext.getRequestDispatcher(path);
  		    rd.include(super.req,
  			       new ResponseIncludeWrapper(super.res,
  							  (ServletOutputStream)super.out));
  		} else{
  		    retString = new String(super.getError());
  		}
  	    } catch (IOException e) {
  		retString = new String(super.getError());
  	    } catch (ServletException e) {
  		retString = new String(super.getError());
  	    } catch (IllegalArgumentException e) {
  		retString = new String(super.getError());
  	    } catch (NullPointerException e) {
  		retString = new String(super.getError());
  	    }
  	} else {
  	    retString = new String(super.getError());
  	}
  
  	return retString;
      }
  
      public final void process(String[] strParamType, String[] strParam) {}
  
      public final boolean isPrintable() { return true; }
  
      public final boolean isModified() { return false; }
  }
  
  
  
  1.1                  jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/util/ssi/SsiMediator.java
  
  Index: SsiMediator.java
  ===================================================================
  /*
   * SsiMediator.java
   * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/util/ssi/SsiMediator.java,v 1.1 2001/03/27 20:38:00 amyroh Exp $
   * $Revision: 1.1 $
   * $Date: 2001/03/27 20:38:00 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */
  
  package org.apache.catalina.util.ssi;
  
  import java.io.OutputStream;
  import java.util.Hashtable;
  import java.util.Date;
  import java.text.DateFormat;
  import java.text.SimpleDateFormat;
  import java.text.ParseException;
  import javax.servlet.ServletContext;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import org.apache.catalina.util.RequestUtil;
  import org.apache.catalina.util.DateTool;
  import org.apache.catalina.util.ssi.SsiConfig;
  import org.apache.catalina.util.ssi.SsiCommand;
  import org.apache.catalina.util.ssi.SsiInclude;
  import org.apache.catalina.util.ssi.SsiEcho;
  import org.apache.catalina.util.ssi.SsiFsize;
  import org.apache.catalina.util.ssi.SsiFlastmod;
  import org.apache.catalina.util.ssi.SsiExec;
  
  /**
   * @author Bip Thelin
   * @version $Revision: 1.1 $, $Date: 2001/03/27 20:38:00 $
   *
   */
  public class SsiMediator {
  
      protected static Hashtable serverVariables =
  	new Hashtable(17);
  
      /**
       * The Commands associated with SSI.
       */
      protected static Hashtable ssiCommands = null;
  
      protected static HttpServletRequest req = null;
  
      protected static HttpServletResponse res = null;
  
      protected static OutputStream out = null;
  
      protected static ServletContext servletContext = null;
  
      protected static String relpath = "/";
  
      protected static String path = new String();
  
      protected static int debug = 0;
  
      public SsiMediator() {}
  
      static {
  	ssiCommands = new Hashtable(6);
  	ssiCommands.put("config", new SsiConfig());
  	ssiCommands.put("include", new SsiInclude());
  	ssiCommands.put("echo", new SsiEcho());
  	ssiCommands.put("fsize", new SsiFsize());
  	ssiCommands.put("flastmod", new SsiFlastmod());
  	ssiCommands.put("exec", new SsiExec());
      }
  
      public SsiMediator(HttpServletRequest req,
  		       HttpServletResponse res,
  		       OutputStream out,
  		       ServletContext servletContext,
  		       int debug,
  		       String path) {
  	this.debug = debug;
  	flush(req, res, out, servletContext, path);
      }
  
      public final SsiCommand getCommand(String cmd) {
  	return (SsiCommand)ssiCommands.get(cmd);
      }
  
      public void flush(HttpServletRequest req,
  		      HttpServletResponse res,
  		      OutputStream out,
  		      ServletContext servletContext,
  		      String path) {
  	this.req = req;
  	this.res = res;
  	this.out = out;
  	this.servletContext = servletContext;
  	this.path = path;
  	this.relpath = path.substring(0, path.lastIndexOf("/")+1);
  	int c=0;
  
  	serverVariables.put("AUTH_TYPE",
  			    nullToString(req.getAuthType()));
  	serverVariables.put("CONTENT_LENGTH",
  			    nullToString(((c=req.getContentLength())<=0)?
  					 null:
  					 (new Integer(c)).toString()));
  	serverVariables.put("CONTENT_TYPE",
  			    nullToString(req.getContentType()));
  	serverVariables.put("GATEWAY_INTERFACE",
  			    "CGI/1.1");
  	serverVariables.put("PATH_INFO",
  			    nullToString(req.getPathInfo()));
  	serverVariables.put("PATH_TRANSLATED ",
  			    nullToString(req.getPathTranslated()));
  	serverVariables.put("QUERY_STRING",
  			    nullToString(req.getQueryString()));
  	serverVariables.put("REMOTE_ADDR",
  			    nullToString(req.getRemoteAddr()));
  	serverVariables.put("REMOTE_HOST",
  			    nullToString(req.getRemoteHost()));
  	serverVariables.put("REMOTE_IDENT", "");
  	serverVariables.put("REMOTE_USER",
  			    nullToString(req.getRemoteUser()));
  	serverVariables.put("REQUEST_METHOD",
  			    nullToString(req.getMethod()));
  	serverVariables.put("SCRIPT_NAME",
  			    nullToString(req.getServletPath()));
  	serverVariables.put("SERVER_NAME",
  			    nullToString(req.getServerName()));
  	serverVariables.put("SERVER_PORT",
  			    (new Integer(req.getServerPort())).toString());
  	serverVariables.put("SERVER_PROTOCOL",
  			    nullToString(req.getProtocol()));
  	serverVariables.put("SERVER_SOFTWARE",
  			    nullToString(servletContext.getServerInfo()));
  	serverVariables.put("DOCUMENT_NAME",
  			    nullToString(path.substring(path.lastIndexOf("/")+1,
  							path.length())));
  	serverVariables.put("DOCUMENT_URI",
  			    nullToString(path));
  	serverVariables.put("QUERY_STRING_UNESCAPED",
  			    nullToString(""));
  
  	flushDate();
  	((SsiConfig)ssiCommands.get("config")).flush();
      }
  
      public byte[] getError() {
  	return ((SsiConfig)ssiCommands.get("config")).getError();
      }
  
      protected void flushDate() {
  	serverVariables.put("DATE_LOCAL",
  			    timefmt(new Date()));
  	serverVariables.put("DATE_GMT",
  			    timefmt(getGMTDate()));
  	serverVariables.put("LAST_MODIFIED",
  			    ((SsiFlastmod)ssiCommands.get("flastmod")).getDate(path));
      }
  
      protected String timefmt(Date date) {
  	String pattern = ((SsiConfig)ssiCommands.get("config")).getTimefmt();
  	DateFormat dateFormat = new SimpleDateFormat(pattern, DateTool.LOCALE_US);
  
  	return dateFormat.format(date);
      }
  
      protected String timefmt(String date) {
  	DateFormat dateFormat = DateFormat.getDateTimeInstance();
  	Date parsedDate = null;
  
  	try {
  	    parsedDate = dateFormat.parse(date);
  	} catch (ParseException e) {
  	    return new String(getError());
  	}
  
  	return timefmt(parsedDate);
      }
  
      protected String getServerVariable(String serverVar) {
  	flushDate();
  
  	if(serverVariables.get(serverVar)==null)
  	    return new String(getError());
  	else
  	    return (String)serverVariables.get(serverVar);
      }
  
      protected String getVirtualPath(String path) {
          if (path == null)
              return null;
  
  	// Resolve encoded characters in the normalized path,
  	// which also handles encoded spaces so we can skip that later.
  	// Placed at the beginning of the chain so that encoded 
  	// bad stuff(tm) can be caught by the later checks
          String normalized = path;
          if (normalized.indexOf('%') >= 0)
              normalized = RequestUtil.URLDecode(normalized, "UTF8");
          if (normalized == null)
              return (null);
  	
  	// Normalize the slashes and add leading slash if necessary
  	if (normalized.indexOf('\\') >= 0)
  	    normalized = normalized.replace('\\', '/');
  	if (!normalized.startsWith("/"))
  	    normalized = relpath.concat(normalized);
  
  	// Resolve occurrences of "//" in the normalized path
  	while (true) {
  	    int index = normalized.indexOf("//");
  	    if (index < 0)
  		break;
  	    normalized = normalized.substring(0, index) +
  		normalized.substring(index + 1);
  	}
  
  	// Resolve occurrences of "/./" in the normalized path
  	while (true) {
  	    int index = normalized.indexOf("/./");
  	    if (index < 0)
  		break;
  	    normalized = normalized.substring(0, index) +
  		normalized.substring(index + 2);
  	}
  
  	// Resolve occurrences of "/../" in the normalized path
  	while (true) {
  	    int index = normalized.indexOf("/../");
  	    if (index < 0)
  		break;
  	    if (index == 0)
  		return (null);	// Trying to go outside our context
  	    int index2 = normalized.lastIndexOf('/', index - 1);
  	    normalized = normalized.substring(0, index2) +
  		normalized.substring(index + 3);
  	}
  
  	return (normalized);
      }
  
      /**
       * Return a path relative to the file being parsed, if they
       * try to use "../" or have a trailing "/" we return
       * <code>null</code> since that is not allowed within
       * a SSI file directive.
       *
       * Example of valid path:
       * "test/path/test.file"
       *
       * @param path Path to be normalized
       */
      protected String getFilePath(String path) {
          if (path == null)
              return null;
  
  	// Resolve encoded characters in the normalized path,
  	// which also handles encoded spaces so we can skip that later.
  	// Placed at the beginning of the chain so that encoded 
  	// bad stuff(tm) can be caught by the later checks
          String normalized = path;
          if (normalized.indexOf('%') >= 0)
              normalized = RequestUtil.URLDecode(normalized, "UTF8");
          if (normalized == null)
              return (null);
  
  	// Normalize the slashes
  	if (normalized.indexOf('\\') >= 0)
  	    normalized = normalized.replace('\\', '/');
  
  	// Resolve occurrences of "//" in the normalized path
  	while (true) {
  	    int index = normalized.indexOf("//");
  	    if (index < 0)
  		break;
  	    normalized = normalized.substring(0, index) +
  		normalized.substring(index + 1);
  	}
  
  	// If it starts with a "/" or contains "../" we
  	// return <code>null</code>.
  	if (normalized.startsWith("/") || normalized.indexOf("../") >= 0)
  	    return (null);
  
  	// Return the normalized path that we have completed
  	return (relpath.concat(normalized));
      }
  
      private String nullToString(String s) {
  	return s==null?"":s;
      }
  
      private String getGMTDate() {
  	Date date = new Date();
  	DateFormat dateFormat = DateFormat.getDateTimeInstance();
  
  	dateFormat.setTimeZone(DateTool.GMT_ZONE);
  	return dateFormat.format(date);
      }
  }