/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
*/
package org.apache.airavata.gfac.context.security;

import java.util.Properties;

import org.apache.airavata.gfac.GFacException;
import org.apache.airavata.gfac.utils.MyProxyManager;
import org.globus.gsi.GlobusCredential;
import org.ietf.jgss.GSSCredential;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Handles GRID related security.
 */
public class GSISecurityContext extends SecurityContext {

    protected final Logger log = LoggerFactory.getLogger(this.getClass());
    /*
     * context name
     */
    public static final String GSI_SECURITY_CONTEXT = "gsi";

    private MyProxyManager myProxyManager;

    private GSSCredential gssCredentials;

    private GlobusCredential globusCredential;

    private String tokenId;

    private String gatewayId;

    private String gatewayUser;

    public GSISecurityContext() {
    }

    public GSISecurityContext(Properties configuration, String token, String gateway, String user) {
        this.tokenId = token;
        this.gatewayId = gateway;
        this.gatewayUser = user;
        myProxyManager = new MyProxyManager(configuration);
    }

    public GSISecurityContext(Properties configuration) {

        myProxyManager = new MyProxyManager(configuration);
    }

    public GSISecurityContext(String myProxyServer, String myProxyUserName, String myProxyPassword, int myProxyLifetime, String trustedCertLoc) {

        myProxyManager = new MyProxyManager(myProxyUserName, myProxyPassword,
                myProxyLifetime, myProxyServer, trustedCertLoc);
    }

    public GSSCredential getGssCredentials() throws SecurityException {

        GSSCredential credential = null;

        try {

            credential = this.myProxyManager.getCredentialsFromStore(gatewayId, tokenId);

        } catch (Exception e) {
            log.warn("An error occurred while retrieving credentials from credential store. " +
                    "But continuing with password credentials. ", e);
        }

        if (credential == null)
            return getGssCredentialsFromUserPassword();
        else
            return credential;
    }


    public GSSCredential getGssCredentialsFromUserPassword() throws SecurityException {
        try {
            if (gssCredentials == null || gssCredentials.getRemainingLifetime() < 10 * 90) {
                gssCredentials = myProxyManager.renewProxy();
            }
            return gssCredentials;
        } catch (Exception e) {
            throw new SecurityException(e.getMessage(), e);
        }
    }


    @SuppressWarnings("UnusedDeclaration")
    public GlobusCredential getGlobusCredential() {
        try {
            if (gssCredentials == null || gssCredentials.getRemainingLifetime() < 10 * 90) {
                globusCredential = myProxyManager.getGlobusCredential();
            }
            return globusCredential;
        } catch (Exception e) {
            throw new SecurityException(e.getMessage(), e);
        }

    }


    @SuppressWarnings("UnusedDeclaration")
    public String getGatewayUser() {
        return gatewayUser;
    }

    public MyProxyManager getMyProxyManager() {
        return myProxyManager;
    }
}

@@ -892,55 +892,8 @@
 s;%0A%0A
-import org.apache.airavata.gfac.GFacException;%0A
 impo
@@ -1433,16 +1433,23 @@
 dentials
+ = null
 ;%0A%0A    p
@@ -2394,19 +2394,23 @@
         
-GSS
+if (gss
 Credenti
@@ -2411,18 +2411,50 @@
 edential
- c
+s != null)%0A            return gssC
 redentia
@@ -2454,23 +2454,17 @@
 edential
- = null
+s
 ;%0A%0A     
@@ -2485,17 +2485,20 @@
         
-c
+gssC
 redentia
@@ -2498,16 +2498,17 @@
 edential
+s
  = this.
@@ -2789,17 +2789,20 @@
     if (
-c
+gssC
 redentia
@@ -2802,16 +2802,17 @@
 edential
+s
  == null
@@ -2901,17 +2901,20 @@
  return 
-c
+gssC
 redentia
@@ -2910,24 +2910,25 @@
 ssCredential
+s
 ;%0A    %7D%0A%0A%0A  

add code not to create credentials again and again