View Javadoc

1   package org.eparapher.rcp.actions;
2   
3   import java.io.FileNotFoundException;
4   import java.io.IOException;
5   import java.security.NoSuchAlgorithmException;
6   import java.security.NoSuchProviderException;
7   import java.util.Iterator;
8   
9   import org.apache.log4j.Logger;
10  import org.bouncycastle.cms.CMSException;
11  import org.eclipse.core.runtime.IProgressMonitor;
12  import org.eclipse.core.runtime.IStatus;
13  import org.eclipse.core.runtime.Status;
14  import org.eclipse.core.runtime.jobs.IJobChangeEvent;
15  import org.eclipse.core.runtime.jobs.Job;
16  import org.eclipse.core.runtime.jobs.JobChangeAdapter;
17  import org.eclipse.jface.action.Action;
18  import org.eclipse.jface.viewers.ISelection;
19  import org.eclipse.jface.viewers.IStructuredSelection;
20  import org.eclipse.jface.viewers.TreeViewer;
21  import org.eclipse.swt.widgets.Display;
22  import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
23  import org.eparapher.core.crypto.EPKeystoreManager;
24  import org.eparapher.core.encryption.CMSEncryption;
25  import org.eparapher.core.encryption.CMSEncryptionParameters;
26  import org.eparapher.rcp.tools.GUIIcons;
27  import org.eparapher.rcp.tools.eParapherTools;
28  import org.eparapher.rcp.views.documents.SecuredDocumentsView.TreeObject;
29  import org.eparapher.rcp.views.documents.SecuredDocumentsView.TreeParent;
30  
31  
32  public class EncryptAction extends Action implements IWorkbenchAction {
33  
34  	private static Logger log = Logger.getLogger(EncryptAction.class);
35  	
36  	private TreeViewer viewer;
37  	private boolean usewizard;
38  	
39  	public EncryptAction() {
40  		this.setId("EncryptAction");
41  		this.setText("Encrypt");
42  		this.setToolTipText("Encrypt your files");
43  		this.setImageDescriptor(GUIIcons.ENCRYPTION_ICON);
44  	}
45  
46  	public EncryptAction(TreeViewer mviewer, boolean museWizard) {
47  		this();
48  		viewer = mviewer;
49  		usewizard = museWizard;
50  		if (usewizard) {
51  			this.setText("Encrypt (wizard)");
52  			this.setToolTipText("Encrypt your file(s) in CMS with a wizard");
53  		}
54  	}
55  
56  	public void run() {
57  		if (viewer.getSelection()!=null) {
58  			if (!viewer.getSelection().isEmpty()) {
59  				CMSEncryptionJob job = new CMSEncryptionJob();
60  				if (job.isEncryptionPrivateKeyLoaded) {
61  					job.setUser(true);
62  					job.schedule();
63  				}
64  			}
65  		}
66  	}
67  	
68  	private CMSEncryptionParameters getEncryptionParameters() {
69  
70  		CMSEncryptionParameters cmsencparams = new CMSEncryptionParameters();
71  		cmsencparams.setCMSEncryptionParamsFromPreferences();
72  		/* TODO : Encryption Wizard GUI
73  		if (usewizard) {
74  			//Launch Wizard to get PDF Signature Parameters
75  			CMSEncryptWizard cmsEncWizard = new CMSEncryptWizard();
76  			WizardDialog dialog = new WizardDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), cmsEncWizard);
77  			if ( dialog.open() != Window.OK ) {
78  				log.info("CMS encryption wizard cancelled");
79  				return null;
80  			}
81  			//recover params from wizard
82  			cmsencparams = cmsWizard.getCMSEncryptionParams();
83  		}
84  		*/
85  		return cmsencparams;
86  	}
87  	
88  	public void dispose() {}
89  
90  	class CMSEncryptionJob extends Job {
91  
92  		private CMSEncryptionParameters cmsencryptparams;
93  		private CMSEncryption cmsencrypt;
94  
95  		private IStructuredSelection structselection;
96  		protected boolean isEncryptionPrivateKeyLoaded;
97  
98  		protected CMSEncryptionJob() {
99  
100 			super("CMS Encryption");
101 			// Use Wizard if needed
102 			cmsencryptparams = getEncryptionParameters();
103 			if (cmsencryptparams != null ) {
104 				
105 				//Ask PIN or Passphrase with CMSSigner Constructor
106 				//TODO : use encryption private key
107 				isEncryptionPrivateKeyLoaded = EPKeystoreManager.getInstance().getUserkeystore().loadPrivateKey();
108 				
109 				if (isEncryptionPrivateKeyLoaded) {
110 					cmsencrypt = new CMSEncryption();
111 					
112 					// Iterate over selected file in view
113 					ISelection selection = viewer.getSelection();
114 					structselection = (IStructuredSelection) selection;
115 					
116 					//End Job Listener
117 					this.addJobChangeListener(new JobChangeAdapter() {
118 						public void done(IJobChangeEvent event) {
119 							if (event.getResult().isOK())
120 								log.info("CMS Encryption Job completed successfully");
121 							else
122 								log.warn("CMS Encryption Job did not complete successfully");
123 						}
124 					});
125 				}
126 			} else
127 				log.warn("CMS Encryption cancelled because default successfully");
128 		}
129 		
130 		@SuppressWarnings("unchecked")
131 		@Override
132 		protected IStatus run(IProgressMonitor monitor) {
133 			
134 			if (cmsencryptparams != null ) {
135 				monitor.beginTask("CMS Encrypt", structselection.size() );
136 				for (Iterator<Object> iterator = structselection.iterator(); iterator.hasNext();) {
137 					Object obj =  iterator.next();
138 					if (obj instanceof TreeParent)
139 						eParapherTools.debugMessage("Cannot encrypt a directory!");
140 					else {
141 						TreeObject to = (TreeObject) obj;
142 	
143 						monitor.subTask("Encrypt "+to.getName());
144 						try {
145 							cmsencrypt.encrypt( to.getFilePath(), cmsencryptparams);
146 						} catch (FileNotFoundException e) {
147 							log.warn("File" + to.getFilePath() + " not found. Encrypting operation cancelled",e);
148 						} catch (NoSuchAlgorithmException e) {
149 							log.warn("Error while encrypting " + to.getFilePath() ,e);
150 						} catch (NoSuchProviderException e) {
151 							log.warn("Error while encrypting " + to.getFilePath() ,e);
152 						} catch (IOException e) {
153 							log.warn("Error while encrypting " + to.getFilePath() ,e);
154 						} catch (CMSException e) {
155 							log.warn("Error while encrypting " + to.getFilePath() ,e);
156 						}
157 						monitor.worked(1);
158 						
159 						//refresh the view
160 						Display.getDefault().asyncExec(new Runnable() {
161 							public void run() {	viewer.refresh(); }
162 						});
163 					}
164 				}
165 				monitor.done();
166 			}
167 			return Status.OK_STATUS;
168 		}
169 	}
170 	
171 }