package condor.gahp.gsi;

import condor.gahp.*;
import condor.gahp.gsi.GSIUtils;

import org.globus.ogsa.impl.base.gram.client.*;
import org.globus.ogsa.impl.security.authentication.Constants;
import org.globus.ogsa.impl.security.authorization.Authorization;
import org.globus.ogsa.impl.security.authorization.SelfAuthorization;
import org.globus.ogsa.base.gram.types.JobStateType;

import org.globus.ogsa.base.gram.service.ManagedJobServiceGridLocator;
import org.globus.ogsa.impl.security.authorization.SelfAuthorization;
import org.gridforum.ogsi.GridService;
import org.globus.ogsa.impl.base.gram.client.GramRsl2XmlConverter;
import org.globus.rsl.ParseException;
import org.gridforum.ogsi.GridService;
import org.gridforum.ogsi.OGSIServiceLocator;
import org.globus.ogsa.handlers.GrimProxyPolicyHandler;
import org.globus.ogsa.impl.security.authentication.Constants;
import org.globus.ogsa.utils.AnyHelper;
import org.gridforum.ogsi.ExtensibilityType;
import org.globus.ogsa.types.properties.PropertiesDetailType;
import org.gridforum.ogsi.EntryType;
import org.globus.ogsa.base.gram.service.ManagedJobServiceLocator;
import org.gridforum.ogsi.EntryContentType;
import org.gridforum.ogsi.ServiceDataValuesType;
import org.gridforum.ogsi.Factory;
import org.globus.ogsa.utils.QueryHelper;
import org.globus.ogsa.impl.security.authorization.Authorization;
import org.globus.ogsa.ServiceData;
import org.gridforum.ogsi.TerminationTimeType;
import org.gridforum.ogsi.ExtendedDateTimeType;
import org.globus.ogsa.base.gram.ManagedJobPortType;
import org.gridforum.ogsi.LocatorType;
import org.globus.ogsa.utils.GridServiceFactory;
import org.globus.ogsa.impl.base.gram.utils.rsl.RslParser;
import org.globus.ogsa.impl.base.gram.utils.rsl.RslParseException;
import org.globus.ogsa.impl.base.gram.utils.rsl.RslParserFactory;
import org.globus.ogsa.base.gram.types.JobStatusType;
import org.globus.axis.gsi.GSIConstants;
import org.globus.ogsa.base.gram.types.JobStateType;
import org.globus.ogsa.impl.base.gram.client.*;
import org.globus.ogsa.NotificationSinkCallback;
import org.globus.ogsa.client.managers.NotificationSinkManager;
import org.globus.ogsa.impl.core.service.ServicePropertiesImpl;
import org.globus.ogsa.ServicePropertiesException;
import org.w3c.dom.Element;
import javax.xml.namespace.QName;
import org.ietf.jgss.GSSCredential;
import org.ietf.jgss.GSSException;
import org.gridforum.jgss.ExtendedGSSManager;
import org.gridforum.ogsi.HandleType;
import javax.xml.rpc.Stub;
import java.net.URL;
import java.io.StringReader;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import org.globus.gsi.gssapi.GSSConstants;

public  class RefreshCredentialHandler implements CommandHandler {
    private GahpInterface gahp;
    
        public void setGahp (GahpInterface g) {
            gahp = g;
        }

    public CommandHandlerResponse handleCommand (String[] cmd) {
        Integer reqId;
        String contactString = null;

        try {
                // cmd[0] = GRAM_JOB_REQUEST
                reqId = new Integer(cmd[1]);
                contactString = cmd[2];
        }
        catch (Exception e) {
                e.printStackTrace(System.err);
                return CommandHandlerResponse.SYNTAX_ERROR;
        }


                int resultCode = 0;
                
                // construct the factory url
                try {
                    URL factoryURL = null;
                    String factoryType = ContactURL.DEFAULT_FACTORY_TYPE;
                    factoryURL = ContactURL.getURL( contactString, factoryType );
                    
                    GrimProxyPolicyHandler grimPolicyHandler = new GrimProxyPolicyHandler();
                    grimPolicyHandler.reset();

                    OGSIServiceLocator factoryLocator = new OGSIServiceLocator();
                    Factory factory = factoryLocator.getFactoryPort(factoryURL);
                    ((Stub)factory)._setProperty(Constants.GSI_SEC_MSG, Constants.SIGNATURE);
                    ((Stub)factory)._setProperty(Constants.GRIM_POLICY_HANDLER, grimPolicyHandler);
                    ((Stub)factory)._setProperty(Constants.AUTHORIZATION, SelfAuthorization.getInstance());
                    GSSCredential cred = GSIUtils.getCredential(gahp);
                    if (cred != null)  {
                        ((Stub)factory)._setProperty(GSIConstants.GSI_CREDENTIALS,cred);
                        ((Stub)factory)._setProperty(GSIConstants.GSI_MODE,GSIConstants.GSI_MODE_FULL_DELEG);
                    }
                    grimPolicyHandler.reset(); // necessary?
                    
                    GridServiceFactory gridFactory = new GridServiceFactory(factory);
                    ExtensibilityType queryResult
                        = factory.findServiceData(
                            QueryHelper.getNamesQuery(ServiceData.TERMINATION));
                }
                catch (Exception e) {
                    e.printStackTrace(System.err);
                    return new CommandHandlerResponse (CommandHandlerResponse.FAILURE);
                }
            
                return new CommandHandlerResponse (CommandHandlerResponse.SUCCESS);
    }
}
 
