Initialization of authorization service from the code is now working.
[photos] / src / main / java / org / wamblee / photos / model / plumbing / Producer.java
index a3d9c2a74f75525050cfc36887f674e36e4287b1..f14c748f4d79e980aaf96de925ff63ad0f3ff92e 100644 (file)
@@ -73,12 +73,10 @@ import org.wamblee.security.authorization.jpa.JpaAuthorizationService;
 
 /**
  * @author Erik Brakkee
- * 
  */
 public class Producer {
 
-    private static final Logger LOGGER = Logger.getLogger(Producer.class
-        .getName());
+    private static final Logger LOGGER = Logger.getLogger(Producer.class.getName());
 
     private static final String APP_CONFIG_RESOURCE = "META-INF/org.wamblee.photos.properties";
 
@@ -111,16 +109,15 @@ public class Producer {
     @PhotoCache
     private Cache<String, ArrayList<PhotoEntry>> photoCache;
 
-    private Configuration getCOnfiguration() {
+    private Configuration getConfiguration() {
         LOGGER.info("Initializing configuration");
         Configuration config;
         try {
-            config = new Configuration(new ClassPathResource(
-                APP_CONFIG_RESOURCE).getInputStream());
-        catch (IOException e) {
+            config = new Configuration(new ClassPathResource(APP_CONFIG_RESOURCE).getInputStream());
+        }
+        catch (IOException e) {
             throw new RuntimeException(
-                "Could not read application configuration property classpath resource " +
-                    APP_CONFIG_RESOURCE, e);
+                    "Could not read application configuration property classpath resource " + APP_CONFIG_RESOURCE, e);
         }
         return config;
     }
@@ -129,24 +126,20 @@ public class Producer {
     @ApplicationScoped
     public UserAdministration getUserAdmin() {
         LOGGER.info("Initializing user administration");
-        NameValidator passwordvalidator = new RegexpNameValidator(".{5,}",
-            "INVALID_PASSWORD", "Password must have at least 5 characters");
+        NameValidator passwordvalidator =
+                new RegexpNameValidator(".{5,}", "INVALID_PASSWORD", "Password must have at least 5 characters");
         MessageDigester passwordEncoder = new Md5HexMessageDigester();
-        UserSet userset = new JpaUserSet(userCache, passwordvalidator,
-            passwordEncoder, entityManager);
+        UserSet userset = new JpaUserSet(userCache, passwordvalidator, passwordEncoder, entityManager);
         GroupSet groupset = new JpaGroupSet(entityManager);
-        NameValidator uservalidator = new RegexpNameValidator(
-            "[a-zA-Z]+[a-zA-Z0-9]*", "INVALID_USERNAME",
-            "User name must consist of alphanumeric characters only");
-        NameValidator groupvalidator = new RegexpNameValidator(
-            "[a-zA-Z]+[a-zA-Z0-9]*", "INVALID_GROUPNAME",
-            "Group name must consist of alphanumeric characters only");
-
-        UserAdministration admin = new UserAdministrationImpl(userset,
-            groupset, uservalidator, groupvalidator);
-        UserAdminInitializer initializer = new UserAdminInitializer(admin,
-            new String[] { "erik", "admin" }, new String[] { "users",
-                "administrators" }, new String[] { "abc123", "abc123" });
+        NameValidator uservalidator = new RegexpNameValidator("[a-zA-Z]+[a-zA-Z0-9]*", "INVALID_USERNAME",
+                "User name must consist of alphanumeric characters only");
+        NameValidator groupvalidator = new RegexpNameValidator("[a-zA-Z]+[a-zA-Z0-9]*", "INVALID_GROUPNAME",
+                "Group name must consist of alphanumeric characters only");
+
+        UserAdministration admin = new UserAdministrationImpl(userset, groupset, uservalidator, groupvalidator);
+        UserAdminInitializer initializer =
+                new UserAdminInitializer(admin, new String[]{"erik", "admin"}, new String[]{"users", "administrators"},
+                        new String[]{"abc123", "abc123"});
         return admin;
     }
 
@@ -155,10 +148,10 @@ public class Producer {
     @UserCache
     public Cache<String, User> getUserCache() {
         try {
-            InputResource cacheConfig = new ClassPathResource(
-                "META-INF/ehcache.xml");
+            InputResource cacheConfig = new ClassPathResource("META-INF/ehcache.xml");
             return new EhCache(cacheConfig, "users");
-        } catch (IOException e) {
+        }
+        catch (IOException e) {
             throw new RuntimeException("Could not create user cache", e);
         }
     }
@@ -168,11 +161,10 @@ public class Producer {
     @PhotoCache
     public Cache<String, ArrayList<PhotoEntry>> getPhotoCache() {
         try {
-            InputResource cacheConfig = new ClassPathResource(
-                "META-INF/ehcache.xml");
-            return new EhCache<String, ArrayList<PhotoEntry>>(cacheConfig,
-                "photos");
-        } catch (IOException e) {
+            InputResource cacheConfig = new ClassPathResource("META-INF/ehcache.xml");
+            return new EhCache<String, ArrayList<PhotoEntry>>(cacheConfig, "photos");
+        }
+        catch (IOException e) {
             throw new RuntimeException("Could not create photo cache", e);
         }
     }
@@ -180,10 +172,10 @@ public class Producer {
     @Produces
     @ApplicationScoped
     public AuthorizationService getAuthorizationService() {
+        LOGGER.info("Initializing authorization service");
         OperationRegistry registry = new DefaultOperationRegistry(
-            new Operation[] { new AllOperation(), new CreateOperation(),
-                new DeleteOperation(), new ReadOperation(),
-                new WriteOperation() });
+                new Operation[]{new AllOperation(), new CreateOperation(), new DeleteOperation(), new ReadOperation(),
+                        new WriteOperation()});
         UserAccessor userAccessor = new UserAccessor() {
 
             @Override
@@ -195,25 +187,23 @@ public class Producer {
                 return principal.getName();
             }
         };
-        AuthorizationService service = new JpaAuthorizationService("DEFAULT",
-            entityManager, userAccessor, userAdmin, 10000);
+        AuthorizationService service =
+                new JpaAuthorizationService("DEFAULT", entityManager, userAccessor, userAdmin, 10000);
 
         AnyUserCondition anyUserCondition = new AnyUserCondition();
-        GroupUserCondition adminUserCondition = new GroupUserCondition(
-            "administrators");
+        GroupUserCondition adminUserCondition = new GroupUserCondition("administrators");
 
-        PhotoAuthorizationRule photoEntryRule = new PhotoAuthorizationRule();
+        PhotoAuthorizationRule photoEntryRule = new PhotoAuthorizationRule(anyUserCondition);
 
         // Pages that allow access by any authenticated user
-        PageAuthorizationRule anyUserPageRule = new PageAuthorizationRule(
-            AuthorizationResult.GRANTED, anyUserCondition, HomePage.class);
+        PageAuthorizationRule anyUserPageRule =
+                new PageAuthorizationRule(AuthorizationResult.GRANTED, anyUserCondition, HomePage.class);
 
-        PageAuthorizationRule adminPageRule = new PageAuthorizationRule(
-            AuthorizationResult.GRANTED, adminUserCondition);
+        PageAuthorizationRule adminPageRule =
+                new PageAuthorizationRule(AuthorizationResult.GRANTED, adminUserCondition);
 
-        AuthorizationInitializer initializer = new AuthorizationInitializer(
-            service, new AbstractAuthorizationRule[] { photoEntryRule,
-                anyUserPageRule, adminPageRule });
+        AuthorizationInitializer initializer = new AuthorizationInitializer(service,
+                new AbstractAuthorizationRule[]{photoEntryRule, anyUserPageRule, adminPageRule});
 
         return service;
     }
@@ -225,12 +215,13 @@ public class Producer {
         LOGGER.info("Initializing photo album");
 
         try {
-            File dir = new File(getCOnfiguration().getPath());
+            File dir = new File(getConfiguration().getPath());
             Album fileSystemAlbum = new FileSystemAlbum(dir, "/", photoCache);
             Album concurrentAlbum = new ConcurrentAlbum(fileSystemAlbum);
 
             return concurrentAlbum;
-        } catch (IOException e) {
+        }
+        catch (IOException e) {
             throw new RuntimeException("Could not initialize photo album", e);
         }
     }
@@ -246,17 +237,15 @@ public class Producer {
             throw new RuntimeException("No authenticated user");
         }
         String username = userPrincipal.getName();
-        List<User> users = entityManager
-            .createNamedQuery(User.QUERY_FIND_BY_NAME)
-            .setParameter(User.NAME_PARAM, username).getResultList();
+        List<User> users =
+                entityManager.createNamedQuery(User.QUERY_FIND_BY_NAME).setParameter(User.NAME_PARAM, username)
+                        .getResultList();
         if (users.size() > 1) {
-            throw new RuntimeException("More than one user found for '" +
-                username + "'");
+            throw new RuntimeException("More than one user found for '" + username + "'");
         }
         if (users.isEmpty()) {
             throw new RuntimeException("No authenticated user");
         }
         return users.get(0);
     }
-
 }