package mgg.webServiceAdapter.serviceRequestHandler.soapProvider;

import java.io.* ;
import javax.servlet.* ;
import javax.servlet.http.* ;
import org.apache.soap.* ;
import org.apache.soap.rpc.* ;
import org.apache.soap.server.* ;
import org.apache.soap.server.http.* ;
import org.apache.soap.util.* ;
import mgg.webServiceAdapter.serviceRequestHandler.AbstractServiceRequestHandler;

public class MyRPCJavaProvider implements Provider {
    protected DeploymentDescriptor dd ;
    protected Envelope             envelope ;
    protected Call                 call ;
    protected String               methodName ;
    protected String               targetObjectURI ;
    protected HttpServlet          servlet ;
    protected HttpSession          session ;

    protected Object               targetObject ;

    public void locate( DeploymentDescriptor dd,
                        Envelope env,
                        Call call,
                        String methodName,
                        String targetObjectURI,
                        SOAPContext reqContext )
        throws SOAPException {

      HttpServlet servlet = (HttpServlet) reqContext.getProperty( Constants.BAG_HTTPSERVLET );
      HttpSession session = (HttpSession) reqContext.getProperty( Constants.BAG_HTTPSESSION );

      this.dd              = dd ;
      this.envelope        = env ;
      this.call            = call ;
      this.methodName      = methodName ;
      this.targetObjectURI = targetObjectURI ;
      this.servlet         = servlet ;
      this.session         = session ;

      ServletConfig  config  = null ;
      ServletContext context = null ;

      if ( servlet != null ) config  = servlet.getServletConfig();
      if ( config != null ) context = config.getServletContext ();

      ServiceManager serviceManager =
        ServerHTTPUtils.getServiceManagerFromContext (context);

      // Default processing for 'java' and 'script' providers
      // call on a valid method name?
      if (!RPCRouter.validCall (dd, call)) {
        throw new SOAPException (Constants.FAULT_CODE_SERVER,
                                 "Method '" + call.getMethodName () +
                                 "' is not supported.");
      }

      // get at the target object
      targetObject = ServerHTTPUtils.getTargetObject (serviceManager,
                                                      dd, targetObjectURI,
                                                      servlet, session,
                                                      reqContext,
                                                      context);



      // 18.10.2001, mgg: saving request context
      ((AbstractServiceRequestHandler)targetObject).setRequestContext( reqContext );

    };


    public void invoke(SOAPContext reqContext, SOAPContext resContext)
               throws SOAPException {
      // invoke the method on the target object
      try {
        Response resp = RPCRouter.invoke( dd, call, targetObject,
                                          reqContext, resContext );
        Envelope env = resp.buildEnvelope();
        StringWriter  sw = new StringWriter();
        env.marshall( sw, call.getSOAPMappingRegistry(), resContext );
        resContext.setRootPart( sw.toString(), Constants.HEADERVAL_CONTENT_TYPE_UTF8);
      }
      catch( Exception e ) {
        if ( e instanceof SOAPException ) throw (SOAPException ) e ;
        throw new SOAPException( Constants.FAULT_CODE_SERVER, e.toString() );
      }
    };
};
