<commit_before>/*
 * Copyright 2006-2011 The Virtual Laboratory for e-Science (VL-e) 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License").  
 * You may not use this file except in compliance with the License. 
 * For details, see the LICENCE.txt file location in the root directory of this 
 * distribution or obtain the Apache Licence at the following location: 
 *     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.
 * 
 * See: http://www.vl-e.nl/ 
 * See: LICENCE.txt (located in the root folder of this distribution). 
 * ---
 * $Id: GridProxyDialogController.java,v 1.12 2011-05-11 14:34:02 ptdeboer Exp $  
 * $Date: 2011-05-11 14:34:02 $
 */ 
// source: 

package nl.uva.vlet.gui.util.proxy;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.logging.Level;

import javax.swing.SwingUtilities;
import javax.swing.Timer;

import nl.uva.vlet.ClassLogger;
import nl.uva.vlet.Global;
import nl.uva.vlet.data.StringUtil;
import nl.uva.vlet.exception.VlException;
import nl.uva.vlet.grid.voms.VO;
import nl.uva.vlet.gui.GuiSettings;
import nl.uva.vlet.gui.UIGlobal;
import nl.uva.vlet.gui.dialog.ExceptionForm;
import nl.uva.vlet.gui.dialog.SimpleDialog;
import nl.uva.vlet.tasks.ActionTask;
import nl.uva.vlet.util.cog.GridProxy;
import nl.uva.vlet.vrs.VRSContext;

/**
 *  Proxy Dailog Controller
 */
class GridProxyDialogController implements ActionListener, WindowListener, FocusListener
{
    private GridProxyDialog proxyDialog = null;

    GridProxy gridProxy = null;

    /** optionally save new nl.uva.vlet.gui.utils.proxy 
     * @param context */

    public GridProxyDialogController(VRSContext context, GridProxyDialog proxyDialog)
    {
        this.proxyDialog = proxyDialog;

        if (context==null)
        	gridProxy = UIGlobal.getVRSContext().getGridProxy();
        else
        	gridProxy = context.getGridProxy();
    }
    
    ActionTask proxyCreationTask;

    boolean isOk=false;
    
    Runnable runProxyCreated=new Runnable()
        {
            public void run()
            {
                proxyCreated(); // call proxy created;
            }
        };

	private Timer timer=null;
    
    public void createProxy()
    {
    	debugPrintf("createProxy()\n"); 
    	
        final char[] chars = proxyDialog.passwordTextField.getPassword();

        proxyDialog.setCursor(GuiSettings.getBusyCursor());
        
        proxyDialog.createButton.setEnabled(false);
       
        // run creation of proxy in background
        proxyCreationTask = new ActionTask(null, "Proxy Creation Task")
        {
            protected void doTask() 
            {
               try
               {
            	   //Debug("GridProxy.createWithPasswd"); 
                   boolean result=gridProxy.createWithPassword(new String(chars));
                   // clear password chars from memory 
                   for (int i=0;i<chars.length;i++) 
                	   chars[i]=0; 
                   
                   //Debug("result="+result);
                   //Debug("proxy path="+gridProxy.getProxyFilename()); 
                   //Debug("isValid()="+gridProxy.isValid()); 
                   //
                   // When credentials are expired, globus still creates a zero time proxy.
                   // Which was ofcourse discovered after MY credentials expired :-).
                   //
                   if (gridProxy.getTimeLeft()<=0) 
                   {
                	   throw new VlException(
                			   "GlobusCredentialException",
                			   "Zero proxy lifetime detected after succesful proxy creation\n"+
                			   "Are the credentials still valid ?\n"+
                			   "(Proxy Created="+result+",Timeleft="+gridProxy.getTimeLeft()+")");
                   }
                   
               }
               catch (VlException e)
               {
                  this.setException(e); 
                  Global.debugPrintStacktrace(e);
                  debugPrintf("Exception:%s\n",e);  
               }
               // calback:
               SwingUtilities.invokeLater(runProxyCreated);  
            }
            
            @Override
            public void stopTask()
            {
            }
         };
         
         proxyCreationTask.startTask(); 
    }
    
    /** Is called after background task has created the proxy */ 
    protected void proxyCreated()
    {
    	debugPrintf("proxyCreated\n");
        proxyDialog.setCursor(GuiSettings.getDefaultCursor());

        Exception ex=proxyCreationTask.getException();
        VO vo=null;
        
        try 
        {
			vo=this.gridProxy.getProvider().getVO(gridProxy.getDefaultVOName()); 
		} 
        catch (VlException e) 
        {
        	logException("Couldn't get default VO information.",e);
		} 
        
        if (ex!=null)
        {
        	//currently only happens when VOMS server isn't known.  
        	if ((ex instanceof nl.uva.vlet.exception.VlUnknownCAException) && (vo!=null)) 
        	{
        		ExceptionForm.show(this.proxyDialog,ex,true);
        		
        		Boolean val=SimpleDialog.askConfirmation("The Certificate Authority (CA) doesn't seem to be recognized for:"+vo.getDefaultHost()
        				+"\nDo you want to import the CA certificate ?\nAfter importing the new certificate, you have to restart the VBrowser.", true);
        		if (val)
        		{
        			checkImportVomsCA(true);
        			this.gridProxy.loadCertificates(); 
        		}
        	}
        	else
        	{
        		// MODALITY CHAINING: when showing a modal dialog DURING another
        		// modal dialog, the parent dialog MUST be supplied :
        		ExceptionForm.show(this.proxyDialog,ex,true);
        		// use JOptionPane during MODAL dialog 
       	 		/* JOptionPane.showMessageDialog(this.proxyDialog,
         		    ex.getMessage(),
         		    ex.getName(),
         		    JOptionPane.ERROR_MESSAGE);*/ 
        	}
        }
        else 
        {
        	try
        	{
        		if (gridProxy.saveProxy() == false)
        			SimpleDialog.displayError(null,"Could not save proxy");
            }
            catch (VlException e2)
            {
               ExceptionForm.show(this.proxyDialog,e2,true);
            }<commit_msg>nl.uva.vlet.gui.utils:  - GridProxuDailogController: removes exception dialog when import new root CA certificates. <commit_after>/*
 * Copyright 2006-2011 The Virtual Laboratory for e-Science (VL-e) 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License").  
 * You may not use this file except in compliance with the License. 
 * For details, see the LICENCE.txt file location in the root directory of this 
 * distribution or obtain the Apache Licence at the following location: 
 *     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.
 * 
 * See: http://www.vl-e.nl/ 
 * See: LICENCE.txt (located in the root folder of this distribution). 
 * ---
 * $Id: GridProxyDialogController.java,v 1.12 2011-05-11 14:34:02 ptdeboer Exp $  
 * $Date: 2011-05-11 14:34:02 $
 */ 
// source: 

package nl.uva.vlet.gui.util.proxy;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.logging.Level;

import javax.swing.SwingUtilities;
import javax.swing.Timer;

import nl.uva.vlet.ClassLogger;
import nl.uva.vlet.Global;
import nl.uva.vlet.data.StringUtil;
import nl.uva.vlet.exception.VlException;
import nl.uva.vlet.grid.voms.VO;
import nl.uva.vlet.gui.GuiSettings;
import nl.uva.vlet.gui.UIGlobal;
import nl.uva.vlet.gui.dialog.ExceptionForm;
import nl.uva.vlet.gui.dialog.SimpleDialog;
import nl.uva.vlet.tasks.ActionTask;
import nl.uva.vlet.util.cog.GridProxy;
import nl.uva.vlet.vrs.VRSContext;

/**
 *  Proxy Dailog Controller
 */
class GridProxyDialogController implements ActionListener, WindowListener, FocusListener
{
    private GridProxyDialog proxyDialog = null;

    GridProxy gridProxy = null;

    /** optionally save new nl.uva.vlet.gui.utils.proxy 
     * @param context */

    public GridProxyDialogController(VRSContext context, GridProxyDialog proxyDialog)
    {
        this.proxyDialog = proxyDialog;

        if (context==null)
        	gridProxy = UIGlobal.getVRSContext().getGridProxy();
        else
        	gridProxy = context.getGridProxy();
    }
    
    ActionTask proxyCreationTask;

    boolean isOk=false;
    
    Runnable runProxyCreated=new Runnable()
        {
            public void run()
            {
                proxyCreated(); // call proxy created;
            }
        };

	private Timer timer=null;
    
    public void createProxy()
    {
    	debugPrintf("createProxy()\n"); 
    	
        final char[] chars = proxyDialog.passwordTextField.getPassword();

        proxyDialog.setCursor(GuiSettings.getBusyCursor());
        
        proxyDialog.createButton.setEnabled(false);
       
        // run creation of proxy in background
        proxyCreationTask = new ActionTask(null, "Proxy Creation Task")
        {
            protected void doTask() 
            {
               try
               {
            	   //Debug("GridProxy.createWithPasswd"); 
                   boolean result=gridProxy.createWithPassword(new String(chars));
                   // clear password chars from memory 
                   for (int i=0;i<chars.length;i++) 
                	   chars[i]=0; 
                   
                   //Debug("result="+result);
                   //Debug("proxy path="+gridProxy.getProxyFilename()); 
                   //Debug("isValid()="+gridProxy.isValid()); 
                   //
                   // When credentials are expired, globus still creates a zero time proxy.
                   // Which was ofcourse discovered after MY credentials expired :-).
                   //
                   if (gridProxy.getTimeLeft()<=0) 
                   {
                	   throw new VlException(
                			   "GlobusCredentialException",
                			   "Zero proxy lifetime detected after succesful proxy creation\n"+
                			   "Are the credentials still valid ?\n"+
                			   "(Proxy Created="+result+",Timeleft="+gridProxy.getTimeLeft()+")");
                   }
                   
               }
               catch (VlException e)
               {
                  this.setException(e); 
                  Global.debugPrintStacktrace(e);
                  debugPrintf("Exception:%s\n",e);  
               }
               // calback:
               SwingUtilities.invokeLater(runProxyCreated);  
            }
            
            @Override
            public void stopTask()
            {
            }
         };
         
         proxyCreationTask.startTask(); 
    }
    
    /** Is called after background task has created the proxy */ 
    protected void proxyCreated()
    {
    	debugPrintf("proxyCreated\n");
        proxyDialog.setCursor(GuiSettings.getDefaultCursor());

        Exception ex=proxyCreationTask.getException();
        VO vo=null;
        
        try 
        {
			vo=this.gridProxy.getProvider().getVO(gridProxy.getDefaultVOName()); 
		} 
        catch (VlException e) 
        {
        	logException("Couldn't get default VO information.",e);
		} 
        
        if (ex!=null)
        {
        	//currently only happens when VOMS server isn't known.  
        	if ((ex instanceof nl.uva.vlet.exception.VlUnknownCAException) && (vo!=null)) 
        	{
        		// ExceptionForm.show(this.proxyDialog,ex,true);
        		// String msg=ex.getMessage(); 
        		
        		Boolean val=SimpleDialog.askConfirmation("The Certificate Authority (CA) doesn't seem to be recognized for:"+vo.getDefaultHost()
        				//+"\nError message:\n-------\n"
        				//+msg+"\n------"
        				+"\nDo you want to import the CA certificate ?\nAfter importing the new certificate, you have to restart the VBrowser.", true);
        	
        		if (val)
        		{
        			checkImportVomsCA(true);
        			this.gridProxy.loadCertificates(); 
        			// still need to restart after loadCertificates. 
        		}
        	}
        	else
        	{
        		// MODALITY CHAINING: when showing a modal dialog DURING another
        		// modal dialog, the parent dialog MUST be supplied :
        		ExceptionForm.show(this.proxyDialog,ex,true);
        		// use JOptionPane during MODAL dialog 
       	 		/* JOptionPane.showMessageDialog(this.proxyDialog,
         		    ex.getMessage(),
         		    ex.getName(),
         		    JOptionPane.ERROR_MESSAGE);*/ 
        	}
        }
        else 
        {
        	try
        	{
        		if (gridProxy.saveProxy() == false)
        			SimpleDialog.displayError(null,"Could not save proxy");
            }
            catch (VlException e2)
            {
               ExceptionForm.show(this.proxyDialog,e2,true);
            }
{'fraction_non_alphanumeric': 0.09928005759539237, 'fraction_numerical': 0.006911447084233261, 'mean_word_length': 1.8939583333333334, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1924125', 'n_tokens_mistral': 4036, 'n_tokens_neox': 3805, 'n_words': 902}