1 22 package org.jboss.tutorial.entity.security.client; 23 24 import org.jboss.tutorial.entity.security.bean.AllEntity; 25 import org.jboss.tutorial.entity.security.bean.SomeEntity; 26 import org.jboss.tutorial.entity.security.bean.StarEntity; 27 import org.jboss.tutorial.entity.security.bean.Stateless; 28 import org.jboss.security.SecurityAssociation; 29 import org.jboss.security.SimplePrincipal; 30 31 import javax.naming.Context ; 32 import javax.naming.InitialContext ; 33 import java.util.Properties ; 34 35 40 public class Client 41 { 42 43 public static void main(String [] args) throws Exception 44 { 45 testAllEntity(); 46 testSomeEntity(); 47 testStarEntity(); 48 } 49 50 public static InitialContext getInitialContext(String username, String password) throws Exception 51 { 52 Properties env = new Properties (); 53 env.setProperty(Context.SECURITY_PRINCIPAL, username); 54 env.setProperty(Context.SECURITY_CREDENTIALS, password); 55 env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.security.jndi.JndiLoginInitialContextFactory"); 56 return new InitialContext (env); 57 58 } 59 60 public static void testAllEntity()throws Exception 61 { 62 InitialContext ctx = getInitialContext("somebody", "password"); 63 Stateless stateless = (Stateless)ctx.lookup("StatelessBean/remote"); 64 65 System.out.println("Good role"); 66 System.out.println("Inserting..."); 67 AllEntity e = stateless.insertAllEntity(); 68 System.out.println("Reading..."); 69 e = stateless.readAllEntity(e.id); 70 e.val += "y"; 71 System.out.println("Updating..."); 72 stateless.updateAllEntity(e); 73 System.out.println("Deleting..."); 74 stateless.deleteAllEntity(e); 75 System.out.println("Inserting..."); 76 e = stateless.insertAllEntity(); 77 78 System.out.println("Bad role"); 79 getInitialContext("rolefail", "password"); 80 81 AllEntity ae2 = null; 82 try 83 { 84 System.out.println("Inserting..."); 85 ae2 = stateless.insertAllEntity(); 86 throw new RuntimeException ("security exception should have been thrown!"); 87 } 88 catch(Exception ex) 89 { 90 System.out.println("Expected failure: " + ex.getMessage()); 91 } 92 93 try 94 { 95 System.out.println("Reading..."); 96 ae2 = stateless.readAllEntity(e.id); 97 throw new RuntimeException ("security exception should have been thrown!"); 98 } 99 catch(Exception ex) 100 { 101 System.out.println("Expected failure: " + ex.getMessage()); 102 } 103 104 try 105 { 106 e.val += "y"; 107 stateless.updateAllEntity(e); 108 throw new RuntimeException ("security exception should have been thrown!"); 109 } 110 catch(Exception ex) 111 { 112 System.out.println("Expected failure: " + ex.getMessage()); 113 } 114 115 try 116 { 117 stateless.deleteAllEntity(e); 118 throw new RuntimeException ("security exception should have been thrown!"); 119 } 120 catch(Exception ex) 121 { 122 System.out.println("Expected failure: " + ex.getMessage()); 123 } 124 125 try 126 { 127 e = stateless.insertAllEntity(); 128 throw new RuntimeException ("security exception should have been thrown!"); 129 } 130 catch(Exception ex) 131 { 132 System.out.println("Expected failure: " + ex.getMessage()); 133 } 134 } 135 136 137 public static void testStarEntity()throws Exception 138 { 139 InitialContext ctx = getInitialContext("somebody", "password"); 140 Stateless stateless = (Stateless)ctx.lookup("StatelessBean/remote"); 141 142 System.out.println("Good role"); 143 System.out.println("Inserting..."); 144 StarEntity e = stateless.insertStarEntity(); 145 System.out.println("Reading..."); 146 e = stateless.readStarEntity(e.id); 147 e.val += "y"; 148 System.out.println("Updating..."); 149 stateless.updateStarEntity(e); 150 System.out.println("Deleting..."); 151 stateless.deleteStarEntity(e); 152 System.out.println("Inserting..."); 153 e = stateless.insertStarEntity(); 154 155 System.out.println("Bad role"); 156 getInitialContext("rolefail", "password"); 157 158 StarEntity ae2 = null; 159 try 160 { 161 System.out.println("Inserting..."); 162 ae2 = stateless.insertStarEntity(); 163 throw new RuntimeException ("security exception should have been thrown!"); 164 } 165 catch(Exception ex) 166 { 167 System.out.println("Expected failure: " + ex.getMessage()); 168 } 169 170 try 171 { 172 System.out.println("Reading..."); 173 ae2 = stateless.readStarEntity(e.id); 174 throw new RuntimeException ("security exception should have been thrown!"); 175 } 176 catch(Exception ex) 177 { 178 System.out.println("Expected failure: " + ex.getMessage()); 179 } 180 181 try 182 { 183 e.val += "y"; 184 stateless.updateStarEntity(e); 185 throw new RuntimeException ("security exception should have been thrown!"); 186 } 187 catch(Exception ex) 188 { 189 System.out.println("Expected failure: " + ex.getMessage()); 190 } 191 192 try 193 { 194 stateless.deleteStarEntity(e); 195 throw new RuntimeException ("security exception should have been thrown!"); 196 } 197 catch(Exception ex) 198 { 199 System.out.println("Expected failure: " + ex.getMessage()); 200 } 201 202 try 203 { 204 e = stateless.insertStarEntity(); 205 throw new RuntimeException ("security exception should have been thrown!"); 206 } 207 catch(Exception ex) 208 { 209 System.out.println("Expected failure: " + ex.getMessage()); 210 } 211 } 212 213 public static void testSomeEntity()throws Exception 214 { 215 InitialContext ctx = getInitialContext("somebody", "password"); 216 Stateless stateless = (Stateless)ctx.lookup("StatelessBean/remote"); 217 218 System.out.println("Good role"); 219 System.out.println("Inserting..."); 220 SomeEntity e = stateless.insertSomeEntity(); 221 222 try 223 { 224 System.out.println("Reading..."); 225 e = stateless.readSomeEntity(e.id); 226 throw new RuntimeException ("security exception should have been thrown!"); 227 } 228 catch(Exception ex) 229 { 230 System.out.println("Expected failure: " + ex.getMessage()); 231 } 232 233 try 234 { 235 e.val += "y"; 236 System.out.println("Updating..."); 237 stateless.updateSomeEntity(e); 238 throw new RuntimeException ("security exception should have been thrown!"); 239 } 240 catch(Exception ex) 241 { 242 System.out.println("Expected failure: " + ex.getMessage()); 243 } 244 245 246 System.out.println("Deleting..."); 247 stateless.deleteSomeEntity(e); 248 System.out.println("Inserting..."); 249 e = stateless.insertSomeEntity(); 250 251 System.out.println("Bad role"); 252 getInitialContext("rolefail", "password"); 253 254 SomeEntity ae2 = null; 255 try 256 { 257 System.out.println("Inserting..."); 258 ae2 = stateless.insertSomeEntity(); 259 throw new RuntimeException ("security exception should have been thrown!"); 260 } 261 catch(Exception ex) 262 { 263 System.out.println("Expected failure: " + ex.getMessage()); 264 } 265 266 try 267 { 268 stateless.deleteSomeEntity(e); 269 throw new RuntimeException ("security exception should have been thrown!"); 270 } 271 catch(Exception ex) 272 { 273 System.out.println("Expected failure: " + ex.getMessage()); 274 } 275 276 try 277 { 278 e = stateless.insertSomeEntity(); 279 throw new RuntimeException ("security exception should have been thrown!"); 280 } 281 catch(Exception ex) 282 { 283 System.out.println("Expected failure: " + ex.getMessage()); 284 } 285 } 286 } 287 | Popular Tags |