/*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2002 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.dynamo.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.log4j.Logger;
import com.silverwrist.dynamo.Namespaces;
import com.silverwrist.dynamo.except.*;
import com.silverwrist.dynamo.iface.*;

public class ScriptExecServlet extends ServletBase
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static Logger logger = Logger.getLogger(ScriptExecServlet.class);

  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private String m_extension_strip;
  private String m_script_prefix;

  /*--------------------------------------------------------------------------------
   * Abstract implementations from class ServletBase
   *--------------------------------------------------------------------------------
   */

  protected Object process(Request r, Application app) throws Exception
  {
    // Determine the request name, and get the name of the script to execute.
    String script_name = r.getRequestPath();
    if (logger.isDebugEnabled())
      logger.debug("Incoming request path = " + script_name);
    if (script_name.endsWith(m_extension_strip))
      script_name = script_name.substring(0,script_name.length()-m_extension_strip.length());
    if (script_name.startsWith("/"))
      script_name = m_script_prefix + script_name;
    else
      script_name = m_script_prefix + "/" + script_name;
    if (logger.isDebugEnabled())
      logger.debug("Final script path = " + script_name);

    // Execute the script!
    ScriptExecute exec = (ScriptExecute)(r.queryService(ScriptExecute.class));
    return exec.executeScript(r,script_name);

  } // end process

  /*--------------------------------------------------------------------------------
   * Overrides from class ServletBase
   *--------------------------------------------------------------------------------
   */

  protected void initServlet(ServiceProvider services) throws DynamoException
  {
    ObjectProvider oprov = (ObjectProvider)(services.queryService(ObjectProvider.class));
    try
    { // get the remove extension
      String s = (String)(oprov.getObject(Namespaces.SERVLET_INIT_NAMESPACE,"remove.extension"));
      m_extension_strip = "." + s;

    } // end try
    catch (NoSuchObjectException e)
    { // default to null extension strip
      m_extension_strip = null;

    } // end catch

    if (logger.isDebugEnabled())
      logger.debug("Extension to strip: " + m_extension_strip);

    try
    { // get the script prefix
      String s = (String)(oprov.getObject(Namespaces.SERVLET_INIT_NAMESPACE,"script.prefix"));
      while (s.endsWith("/"))
	s = s.substring(0,s.length()-1);
      if (!(s.startsWith("/")))
	s = "/" + s;
      m_script_prefix = s;

    } // end try
    catch (NoSuchObjectException e)
    { // default the script prefix
      m_script_prefix = "";

    } // end catch

    if (logger.isDebugEnabled())
      logger.debug("Script prefix: " + m_script_prefix);

  } // end initServlet

  protected Object translateException(Request r, Exception e)
  {
    if (e instanceof ScriptingException)
    { // "unwrap" any DynamoExceptions that were nested inside ScriptingExceptions
      Throwable t = e.getCause();
      if ((t instanceof DynamoException) || (t instanceof DynamoRuntimeException))
	return super.translateException(r,(Exception)t);

    } // end if

    return super.translateException(r,e);

  } // end translateException

} // end class ScriptExecServlet
