/*
 * 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-03 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.dynamo.xmlrpc;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.silverwrist.dynamo.Namespaces;
import com.silverwrist.dynamo.Verb;
import com.silverwrist.dynamo.app.ApplicationContainer;
import com.silverwrist.dynamo.except.*;
import com.silverwrist.dynamo.iface.*;
import com.silverwrist.dynamo.servlet.HttpMethod;
import com.silverwrist.dynamo.servlet.ServletBase;
import com.silverwrist.dynamo.util.*;

public class XmlRpcServlet extends ServletBase implements ComponentShutdown
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private XmlRpcSubSystem m_subsystem;   // pointer to the XML-RPC subsystem

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

  protected Object process(Request r, Application app) throws Exception
  {
    try
    { // dispatch the method call
      return m_subsystem.dispatch(r,app);

    } // end try
    catch (FaultCode fc)
    { // return the FaultCode to be output
      return fc;

    } // end catch

  } // end process

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

  protected void initServlet(ServiceProvider services) throws DynamoException
  {
    // Get the name of the XML-RPC subsystem object.
    ObjectProvider oprov = (ObjectProvider)(services.queryService(ObjectProvider.class));
    String subsys_name = (String)(oprov.getObject(Namespaces.SERVLET_INIT_NAMESPACE,"subsystem.object"));

    // Get the subsystem object.
    m_subsystem = (XmlRpcSubSystem)(GetObjectUtils.getDynamoComponent(services,XmlRpcSubSystem.class,
								      subsys_name));

  } // end initServlet

  protected boolean supportsMethod(HttpMethod method, Application app)
  {
    return HttpMethod.POST.equals(method);

  } // end supportsMethod

  protected ServiceProvider createRequestServices(HttpServletRequest req, HttpServletResponse resp)
  {
    // Create the service provider interface and return it.
    SimpleServiceProvider services = new SimpleServiceProvider("Request Services");
    services.addService(SessionInfoProvider.class,NullSessionInfo.getProvider());
    services.addService(BrowserDataProvider.class,createBrowserDataProvider(req));
    services.addService(HeaderInput.class,createHeaderInput(req));
    services.addService(XmlRpcCreateSession.class,m_subsystem);
    return services;

  } // end createRequestServices

  protected Request createRequest(HttpServletRequest req, Verb verb, ServiceProvider services, Request prev)
      throws RequestParseException
  {
    XmlRpcRequest xreq = null;
    try
    { // create our special request type
      xreq = new XmlRpcRequest(req,verb,services,prev);

    } // end try
    catch (FaultCode fc)
    { // "wrap" the FaultCode - we'll get back to it
      throw new RequestParseException(fc.getMessage(),fc);

    } // end catch

    return xreq;

  } // end createRequest

  protected Object translateRequestParseException(RequestParseException e)
  {
    Throwable cause = e.getCause();
    if ((cause!=null) && (cause instanceof FaultCode))
      return cause;
    return super.translateRequestParseException(e);

  } // end translateRequestParseException

  protected Object translateException(Request r, Exception e)
  {
    return m_subsystem.translateException(r,e);

  } // end translateException

  protected ServiceProvider createOutputServices(HttpServletRequest req, HttpServletResponse resp)
  {
    return new SingletonServiceProvider("Rendering Services",BrowserDataProvider.class,
					createBrowserDataProvider(req));

  } // end createOutputServices

  protected void outputResult(HttpServletRequest req, HttpServletResponse resp,
			      ServiceProvider services, Object result) throws IOException, RenderingException
  {
    if (result instanceof FaultCode)
      super.outputResult(req,resp,services,result);
    else if (result instanceof XmlRpcResult)
      super.outputResult(req,resp,services,result);
    else
      super.outputResult(req,resp,services,new XmlRpcResult(result));

  } // end outputResult

  /*--------------------------------------------------------------------------------
   * Implementations from interface ComponentShutdown
   *--------------------------------------------------------------------------------
   */

  public void shutdown()
  {
    m_subsystem = null;

  } // end shutdown

} // end class XmlRpcServlet
