View Javadoc

1   package org.eparapher.rcp.views;
2   
3   import java.io.FileNotFoundException;
4   import java.io.FileReader;
5   import java.io.IOException;
6   import java.security.cert.CRLException;
7   import java.security.cert.CertificateEncodingException;
8   import java.util.ArrayList;
9   
10  import org.apache.log4j.Logger;
11  import org.bouncycastle.asn1.ASN1InputStream;
12  import org.bouncycastle.asn1.DERApplicationSpecific;
13  import org.bouncycastle.asn1.DEREnumerated;
14  import org.bouncycastle.asn1.DERObject;
15  import org.bouncycastle.asn1.DERUnknownTag;
16  import org.bouncycastle.asn1.pkcs.ContentInfo;
17  import org.bouncycastle.asn1.util.ASN1Dump;
18  import org.bouncycastle.jce.PKCS10CertificationRequest;
19  import org.bouncycastle.jce.provider.X509CRLObject;
20  import org.bouncycastle.jce.provider.X509CertificateObject;
21  import org.bouncycastle.openssl.PEMReader;
22  import org.bouncycastle.x509.X509AttributeCertificate;
23  import org.eclipse.core.runtime.IAdaptable;
24  import org.eclipse.jface.viewers.ISelection;
25  import org.eclipse.jface.viewers.IStructuredContentProvider;
26  import org.eclipse.jface.viewers.IStructuredSelection;
27  import org.eclipse.jface.viewers.ITreeContentProvider;
28  import org.eclipse.jface.viewers.LabelProvider;
29  import org.eclipse.jface.viewers.Viewer;
30  import org.eclipse.jface.viewers.ViewerSorter;
31  import org.eclipse.swt.SWT;
32  import org.eclipse.swt.graphics.Image;
33  import org.eclipse.swt.widgets.Composite;
34  import org.eclipse.swt.widgets.Text;
35  import org.eclipse.ui.ISelectionListener;
36  import org.eclipse.ui.ISelectionService;
37  import org.eclipse.ui.ISharedImages;
38  import org.eclipse.ui.IWorkbenchPart;
39  import org.eclipse.ui.PlatformUI;
40  import org.eclipse.ui.part.ViewPart;
41  import org.eparapher.core.tools.FileUtil;
42  import org.eparapher.rcp.views.documents.SecuredDocumentsView;
43  
44  
45  /**
46   * TODO : evolve with a tree viewer.
47   * <p>
48   * 
49   * <p>
50   */
51  
52  public class ASN1Viewer extends ViewPart {
53  	
54  	public static final String ID = "org.eparapher.rcp.views.ASN1Viewer";
55  	private static Logger log = Logger.getLogger(ASN1Viewer.class);
56  	
57  	private Text textdump;
58  	//private TreeViewer viewer;
59  	//private DrillDownAdapter drillDownAdapter;
60  	//private Action action1;
61  	//private Action action2;
62  	//private Action doubleClickAction;
63  	private ISelectionListener mylistener = new ISelectionListener() {
64          public void selectionChanged(IWorkbenchPart sourcepart, ISelection selection) {
65          	//log.debug("selection changed on " + sourcepart + ". new selection is : "+selection);
66          	if (sourcepart instanceof SecuredDocumentsView &&
67          			selection instanceof IStructuredSelection) {
68          		IStructuredSelection is = (IStructuredSelection) selection;
69          		textdump.setText("Please select only one item in the document view");
70          		if (is.size()==1) {
71          			SecuredDocumentsView.TreeObject to = (SecuredDocumentsView.TreeObject) is.getFirstElement();
72          			if (to instanceof SecuredDocumentsView.TreeParent )
73          				textdump.setText("directory doesn't have an ASN1 structure.");
74          			else {
75          				String path = to.getFilePath();
76          				ASN1InputStream asn1is = null;
77          				DERObject der = null;
78          				//First : try with PEMReader
79  						try {
80  							PEMReader pr = new PEMReader(new FileReader(path));
81  							Object obj = pr.readObject();
82  							if (obj instanceof X509CertificateObject)
83  								asn1is = new ASN1InputStream(((X509CertificateObject)obj).getEncoded());
84  							if (obj instanceof X509CRLObject)
85  								asn1is = new ASN1InputStream(((X509CRLObject)obj).getEncoded());
86  							if (obj instanceof PKCS10CertificationRequest)
87  								asn1is = new ASN1InputStream(((PKCS10CertificationRequest)obj).getEncoded());
88  							if (obj instanceof ContentInfo)
89  								asn1is = new ASN1InputStream(((ContentInfo)obj).getEncoded());
90  							if (obj instanceof X509AttributeCertificate)
91  								asn1is = new ASN1InputStream(((X509AttributeCertificate)obj).getEncoded());
92  							//if (obj instanceof ECNamedCurveParameterSpec)
93  							//	asn1is = new ASN1InputStream(((ECNamedCurveParameterSpec)obj).getEncoded());
94  							if (asn1is!=null)
95  								der = asn1is.readObject();
96  						} catch (FileNotFoundException e) {
97  							log.debug(path + " is not found");
98  						} catch (IOException e) {
99  							log.debug(path + " is not an ASN1 structure");
100 						} catch (CertificateEncodingException e) {
101 							log.debug(path + " is not a valid X509 structure");
102 						} catch (CRLException e) {
103 							log.debug(path + " is not a valid X509 CRL structure");
104 						}
105 						//Then try to read it directly in ASN1
106 						if (asn1is==null) {
107 							try {
108 								asn1is = new ASN1InputStream(FileUtil.readFile(path));
109 								der = asn1is.readObject();
110 							} catch (IOException e) {
111 								log.debug(path + " is not a valid ASN1 structure");
112 							}
113 						}
114 						if ( der!=null &&
115 							 !( der instanceof DERUnknownTag) && 
116 							 !( der instanceof DEREnumerated) && 
117 							 !( der instanceof DERApplicationSpecific))
118 							textdump.setText(ASN1Dump.dumpAsString(der,true));
119 						else
120 							textdump.setText(path + " has not been detected as a valid ASN1 object/structure");
121         			}
122         		}
123             }
124         }
125     };
126 
127 	/*
128 	 * The content provider class is responsible for
129 	 * providing objects to the view. It can wrap
130 	 * existing objects in adapters or simply return
131 	 * objects as-is. These objects may be sensitive
132 	 * to the current input of the view, or ignore
133 	 * it and always show the same content 
134 	 * (like Task List, for example).
135 	 */
136 	 
137 	class TreeObject implements IAdaptable {
138 		private String name;
139 		private TreeParent parent;
140 		
141 		public TreeObject(String name) {
142 			this.name = name;
143 		}
144 		public String getName() {
145 			return name;
146 		}
147 		public void setParent(TreeParent parent) {
148 			this.parent = parent;
149 		}
150 		public TreeParent getParent() {
151 			return parent;
152 		}
153 		public String toString() {
154 			return getName();
155 		}
156 		public Object getAdapter(Class key) {
157 			return null;
158 		}
159 	}
160 	
161 	class TreeParent extends TreeObject {
162 		private ArrayList children;
163 		public TreeParent(String name) {
164 			super(name);
165 			children = new ArrayList();
166 		}
167 		public void addChild(TreeObject child) {
168 			children.add(child);
169 			child.setParent(this);
170 		}
171 		public void removeChild(TreeObject child) {
172 			children.remove(child);
173 			child.setParent(null);
174 		}
175 		public TreeObject [] getChildren() {
176 			return (TreeObject [])children.toArray(new TreeObject[children.size()]);
177 		}
178 		public boolean hasChildren() {
179 			return children.size()>0;
180 		}
181 	}
182 
183 	class ViewContentProvider implements IStructuredContentProvider, 
184 										   ITreeContentProvider {
185 		private TreeParent invisibleRoot;
186 
187 		public void inputChanged(Viewer v, Object oldInput, Object newInput) {
188 		}
189 		public void dispose() {
190 		}
191 		public Object[] getElements(Object parent) {
192 			if (parent.equals(getViewSite())) {
193 				if (invisibleRoot==null) initialize();
194 				return getChildren(invisibleRoot);
195 			}
196 			return getChildren(parent);
197 		}
198 		public Object getParent(Object child) {
199 			if (child instanceof TreeObject) {
200 				return ((TreeObject)child).getParent();
201 			}
202 			return null;
203 		}
204 		public Object [] getChildren(Object parent) {
205 			if (parent instanceof TreeParent) {
206 				return ((TreeParent)parent).getChildren();
207 			}
208 			return new Object[0];
209 		}
210 		public boolean hasChildren(Object parent) {
211 			if (parent instanceof TreeParent)
212 				return ((TreeParent)parent).hasChildren();
213 			return false;
214 		}
215 /*
216  * We will set up a dummy model to initialize tree heararchy.
217  * In a real code, you will connect to a real model and
218  * expose its hierarchy.
219  */
220 		private void initialize() {
221 			TreeObject to1 = new TreeObject("Leaf 1");
222 			TreeObject to2 = new TreeObject("Leaf 2");
223 			TreeObject to3 = new TreeObject("Leaf 3");
224 			TreeParent p1 = new TreeParent("Parent 1");
225 			p1.addChild(to1);
226 			p1.addChild(to2);
227 			p1.addChild(to3);
228 			
229 			TreeObject to4 = new TreeObject("Leaf 4");
230 			TreeParent p2 = new TreeParent("Parent 2");
231 			p2.addChild(to4);
232 			
233 			TreeParent root = new TreeParent("Root");
234 			root.addChild(p1);
235 			root.addChild(p2);
236 			
237 			invisibleRoot = new TreeParent("");
238 			invisibleRoot.addChild(root);
239 		}
240 	}
241 	class ViewLabelProvider extends LabelProvider {
242 
243 		public String getText(Object obj) {
244 			return obj.toString();
245 		}
246 		public Image getImage(Object obj) {
247 			String imageKey = ISharedImages.IMG_OBJ_ELEMENT;
248 			if (obj instanceof TreeParent)
249 			   imageKey = ISharedImages.IMG_OBJ_FOLDER;
250 			return PlatformUI.getWorkbench().getSharedImages().getImage(imageKey);
251 		}
252 	}
253 	class NameSorter extends ViewerSorter {
254 	}
255 
256 	/**
257 	 * The constructor.
258 	 */
259 	public ASN1Viewer() {
260 	}
261 
262 	/**
263 	 * This is a callback that will allow us
264 	 * to create the viewer and initialize it.
265 	 */
266 	public void createPartControl(Composite parent) {
267 		textdump = new Text(parent,SWT.BORDER | SWT.V_SCROLL);
268 		textdump.setText("ASN1 Dump Here");
269 		getSite().getWorkbenchWindow().getSelectionService().addSelectionListener(SecuredDocumentsView.ID,mylistener);
270 		/*
271 		viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
272 		drillDownAdapter = new DrillDownAdapter(viewer);
273 		viewer.setContentProvider(new ViewContentProvider());
274 		viewer.setLabelProvider(new ViewLabelProvider());
275 		viewer.setSorter(new NameSorter());
276 		viewer.setInput(getViewSite());
277 
278 		// Create the help context id for the viewer's control
279 		PlatformUI.getWorkbench().getHelpSystem().setHelp(viewer.getControl(), "eParapher.viewer");
280 		makeActions();
281 		hookContextMenu();
282 		hookDoubleClickAction();
283 		contributeToActionBars();*/
284 	}
285 
286 	/*
287 	private void hookContextMenu() {
288 		MenuManager menuMgr = new MenuManager("#PopupMenu");
289 		menuMgr.setRemoveAllWhenShown(true);
290 		menuMgr.addMenuListener(new IMenuListener() {
291 			public void menuAboutToShow(IMenuManager manager) {
292 				ASN1Viewer.this.fillContextMenu(manager);
293 			}
294 		});
295 		Menu menu = menuMgr.createContextMenu(viewer.getControl());
296 		viewer.getControl().setMenu(menu);
297 		getSite().registerContextMenu(menuMgr, viewer);
298 	}
299 
300 	private void contributeToActionBars() {
301 		IActionBars bars = getViewSite().getActionBars();
302 		fillLocalPullDown(bars.getMenuManager());
303 		fillLocalToolBar(bars.getToolBarManager());
304 	}
305 
306 	private void fillLocalPullDown(IMenuManager manager) {
307 		manager.add(action1);
308 		manager.add(new Separator());
309 		manager.add(action2);
310 	}
311 
312 	private void fillContextMenu(IMenuManager manager) {
313 		manager.add(action1);
314 		manager.add(action2);
315 		manager.add(new Separator());
316 		drillDownAdapter.addNavigationActions(manager);
317 		// Other plug-ins can contribute there actions here
318 		manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
319 	}
320 	
321 	private void fillLocalToolBar(IToolBarManager manager) {
322 		manager.add(action1);
323 		manager.add(action2);
324 		manager.add(new Separator());
325 		drillDownAdapter.addNavigationActions(manager);
326 	}
327 
328 	private void makeActions() {
329 		action1 = new Action() {
330 			public void run() {
331 				showMessage("Action 1 executed");
332 			}
333 		};
334 		action1.setText("Action 1");
335 		action1.setToolTipText("Action 1 tooltip");
336 		action1.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().
337 			getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
338 		
339 		action2 = new Action() {
340 			public void run() {
341 				showMessage("Action 2 executed");
342 			}
343 		};
344 		action2.setText("Action 2");
345 		action2.setToolTipText("Action 2 tooltip");
346 		action2.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().
347 				getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
348 		doubleClickAction = new Action() {
349 			public void run() {
350 				ISelection selection = viewer.getSelection();
351 				Object obj = ((IStructuredSelection)selection).getFirstElement();
352 				showMessage("Double-click detected on "+obj.toString());
353 			}
354 		};
355 	}
356 
357 	private void hookDoubleClickAction() {
358 		viewer.addDoubleClickListener(new IDoubleClickListener() {
359 			public void doubleClick(DoubleClickEvent event) {
360 				doubleClickAction.run();
361 			}
362 		});
363 	}
364 	private void showMessage(String message) {
365 		MessageDialog.openInformation(
366 			viewer.getControl().getShell(),
367 			"ASN1 Parser",
368 			message);
369 	}
370 	*/
371 
372 	/**
373 	 * Passing the focus request to the viewer's control.
374 	 */
375 	public void setFocus() {
376 		//viewer.getControl().setFocus();
377 	}
378 	public void dispose() {
379 		//Remove the Selection listner
380         ISelectionService s = getSite().getWorkbenchWindow().getSelectionService();
381         s.removeSelectionListener(mylistener);
382         super.dispose();
383     }
384 
385 }