/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.net.axis.server;

import org.jboss.axis.AxisFault;
import org.jboss.axis.ConfigurationException;
import org.jboss.axis.MessageContext;
import org.jboss.axis.server.AxisServer;
import org.jboss.axis.transport.http.AxisServlet;
import org.jboss.axis.transport.http.HTTPConstants;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * <p>
 * An AxisServlet that is able to extract the corresponding AxisEngine 
 * from its installation context and builds the right message contexts for the
 * JBoss classloading and deployment architecture.
 * </p>
 * <p>
 * This servlet will also reconstruct proper SOAPAction for http-get accesses
 * in order not to rely on unsecure url-mapping
 * </p>
 * @author <a href="mailto:Christoph.Jung@infor.de">Christoph G. Jung</a>
 * @created 7. September 2001, 19:17
 * @version $Revision: 1.2.4.7 $
 */

public class AxisServiceServlet extends AxisServlet
{

   /** reference to the server */
   protected AxisServer server = null;

   /** Creates new AxisServlet */
   public AxisServiceServlet()
   {
   }

   /** 
    * override AxisServlet.getEngine() in order to redirect to
    * the corresponding AxisEngine.
    */
   public org.jboss.axis.server.AxisServer getEngine() throws AxisFault
   {
      if (server == null)
      {
         // we need to extract the engine from the 
         // rootcontext
         String installation =
                 getConfigurationContext();
         // call the static service method to find the installed engine
         try
         {
            server =
                    JMXEngineConfigurationFactory
                    .newJMXFactory(installation)
                    .getAxisServer();
         }
         catch (NullPointerException e)
         {
            throw new AxisFault("Could not access JMX configuration factory.",
                    e);
         }
      }

      return server;
   }

   /** access configuration context */
   protected String getConfigurationContext()
   {
      return getInitParameter(org.jboss.net.axis.Constants.CONFIGURATION_CONTEXT);
   }

   /* (non-Javadoc)
    * a list request comes through get and 
    * does not need a particularly specified messagecontext
    * except to hook classloading information on. 
    * @see org.jboss.axis.transport.http.AxisServlet#reportAvailableServices(javax.servlet.http.HttpServletResponse, java.io.PrintWriter, javax.servlet.http.HttpServletRequest)
    */
   protected void reportAvailableServices(HttpServletResponse arg0,
                                          PrintWriter arg1,
                                          HttpServletRequest arg2)
           throws ConfigurationException, AxisFault
   {
      MessageContext fake = new MessageContext(getEngine());
      AxisServer.setCurrentMessageContext(fake);
      super.reportAvailableServices(arg0, arg1, arg2);
   }

   /* (non-Javadoc)
    * through get, we will not be presented with a correct soapaction,
    * hence we fake it to the context path (see AxisServlet.getSoapAction())
    * @see org.jboss.axis.transport.http.AxisServlet#processMethodRequest(org.jboss.axis.MessageContext, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.io.PrintWriter)
    */
//   protected void processMethodRequest(
//      MessageContext arg0,
//      HttpServletRequest arg1,
//      HttpServletResponse arg2,
//      PrintWriter arg3)
//      throws AxisFault
//   {
//      fakeSoapAction(arg0);
//      super.processMethodRequest(arg0, arg1, arg2, arg3);
//   }

   /* (non-Javadoc)
    * through get, we will not be presented with a correct soapaction,
    * hence we fake it to the context path (see AxisServlet.getSoapAction())
    * @see org.jboss.axis.transport.http.AxisServlet#processWsdlRequest(org.jboss.axis.MessageContext, javax.servlet.http.HttpServletResponse, java.io.PrintWriter)
    */
//   protected void processWsdlRequest(
//      MessageContext arg0,
//      HttpServletResponse arg1,
//      PrintWriter arg2)
//      throws AxisFault
//   {
//      fakeSoapAction(arg0);
//      super.processWsdlRequest(arg0, arg1, arg2);
//   }
   
   /** set soap action uri to the path info */
   protected void fakeSoapAction(MessageContext arg0)
   {
      String pathInfo = (String)arg0.getProperty(HTTPConstants.MC_HTTP_SERVLETPATHINFO);
      if (pathInfo == null)
      {
         pathInfo = "";
      }
      else if (pathInfo.startsWith("/"))
      {
         pathInfo = pathInfo.substring(1);
      }
      arg0.setSOAPActionURI(pathInfo);
   }

}