| 1 28 package net.sf.jguard.example.swing.dialog; 29 30 import java.awt.BorderLayout ; 31 import java.awt.Dimension ; 32 import java.awt.Frame ; 33 import java.awt.GridLayout ; 34 import java.awt.event.ActionEvent ; 35 import java.awt.event.ActionListener ; 36 import java.io.BufferedReader ; 37 import java.io.FileReader ; 38 import java.io.IOException ; 39 import java.security.AccessController ; 40 import java.security.PrivilegedActionException ; 41 import java.security.PrivilegedExceptionAction ; 42 43 import javax.security.auth.Subject ; 44 import javax.swing.JButton ; 45 import javax.swing.JDialog ; 46 import javax.swing.JFileChooser ; 47 import javax.swing.JLabel ; 48 import javax.swing.JOptionPane ; 49 import javax.swing.JPanel ; 50 import javax.swing.JTextField ; 51 52 import net.sf.jguard.core.authorization.policy.AccessControlContextUtils; 53 54 import org.apache.log4j.Logger; 55 56 61 public class CheckPermissionDialog extends JDialog { 62 63 private static final Logger logger = Logger 64 .getLogger(CheckPermissionDialog.class); 65 66 private static final long serialVersionUID = -7641435710653804135L; 67 68 private Subject subject; 69 70 private JTextField fileTxtField; 71 72 public CheckPermissionDialog(Frame parentFrame,Subject subject){ 73 super(parentFrame,"Check Permission Dialog",true); 74 this.subject = subject; 75 76 this.setLayout(new BorderLayout ()); 77 78 JPanel informationPanel = new JPanel (); 79 GridLayout informationLayout = new GridLayout (1,3); 80 informationPanel.setLayout(informationLayout); 81 82 JLabel fileLabel = new JLabel (" filepath :"); 83 fileTxtField = new JTextField (100); 84 fileTxtField.setColumns(100); 85 86 JButton browseButton = new JButton ("browse ..."); 87 browseButton.addActionListener(new ActionListener (){ 88 public void actionPerformed(ActionEvent evt){ 89 handleBrowse(); 90 } 91 }); 92 informationPanel.add(fileLabel); 93 informationPanel.add(fileTxtField); 94 informationPanel.add(browseButton); 95 96 informationPanel.setPreferredSize(new Dimension (450,20)); 97 98 JPanel validationPanel=new JPanel (); 99 100 JButton tryButton = new JButton ("try"); 101 tryButton.addActionListener(new ActionListener (){ 102 public void actionPerformed(ActionEvent e){ 103 handleTry(); 104 } 105 }); 106 107 validationPanel.add(tryButton); 108 109 110 this.add(informationPanel,BorderLayout.CENTER); 111 this.add(validationPanel,BorderLayout.SOUTH); 112 113 this.pack(); 114 this.setResizable(false); 115 this.setLocation(parentFrame.getLocation().x,parentFrame.getLocation().y); 116 this.setVisible(true); 117 } 118 119 120 private void handleTry(){ 121 122 final String filePath = fileTxtField.getText(); 123 try{ 124 AccessController.doPrivileged(new PrivilegedExceptionAction () { 127 public Object run() throws SecurityException , IOException { 128 129 BufferedReader br = new BufferedReader (new FileReader (filePath)); 130 br.readLine(); 131 br.close(); 132 133 return null; 134 } 135 },AccessControlContextUtils.getStackSubjectAccessControlContext(subject)); 136 137 JOptionPane.showMessageDialog(this,"You have the permissions to do that","permission allowed",JOptionPane.INFORMATION_MESSAGE); 138 139 }catch(SecurityException e){ 140 logger.error("Logged subject has no permission to read this file",e); 141 JOptionPane.showMessageDialog(this,"You don't have the permissions to do that ","permission denied",JOptionPane.ERROR_MESSAGE); 142 } catch (PrivilegedActionException e) { 143 logger.error("PrivilegedActionException ioException",e); 144 JOptionPane.showMessageDialog(this,"You don't have the permissions to do that ","permission denied",JOptionPane.ERROR_MESSAGE); 145 } 146 } 147 148 149 private void handleBrowse(){ 150 151 JFileChooser fileChooser = new JFileChooser (); 152 int state = fileChooser.showOpenDialog(this); 153 if(state == JFileChooser.APPROVE_OPTION){ 154 String selectedPath = fileChooser.getSelectedFile().getAbsolutePath(); 155 fileTxtField.setText(selectedPath); 156 } 157 158 } 159 } 160 | Popular Tags |