[jboss-cvs] jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/ui ...

Christian Bauer christian at hibernate.org
Sun Mar 18 11:44:37 EDT 2007


  User: cbauer  
  Date: 07/03/18 11:44:37

  Modified:    examples/wiki/src/main/org/jboss/seam/wiki/core/ui   
                        FileServlet.java WikiUtil.java Converters.java
  Log:
  Basic access level/role security, automatic home page for activated users
  
  Revision  Changes    Path
  1.3       +5 -9      jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/ui/FileServlet.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: FileServlet.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/ui/FileServlet.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -b -r1.2 -r1.3
  --- FileServlet.java	8 Mar 2007 10:44:15 -0000	1.2
  +++ FileServlet.java	18 Mar 2007 15:44:37 -0000	1.3
  @@ -1,10 +1,6 @@
   package org.jboss.seam.wiki.core.ui;
   
   import org.jboss.seam.wiki.core.model.File;
  -import org.jboss.seam.util.Transactions;
  -import org.jboss.seam.core.Expressions;
  -import org.jboss.seam.contexts.Contexts;
  -import org.jboss.seam.annotations.In;
   
   import javax.imageio.ImageIO;
   import javax.servlet.http.HttpServletRequest;
  @@ -12,8 +8,6 @@
   import javax.servlet.http.HttpServlet;
   import javax.servlet.ServletException;
   import javax.swing.*;
  -import javax.transaction.UserTransaction;
  -import javax.persistence.EntityManager;
   import java.awt.*;
   import java.awt.image.BufferedImage;
   import java.io.IOException;
  @@ -58,9 +52,10 @@
           if (DOWNLOAD_PATH.equals(request.getPathInfo())) {
   
               String id = request.getParameter("fileId");
  -            File file;
  +            File file = null;
   
               // TODO: Seam should use its transaction interceptor for java beans: http://jira.jboss.com/jira/browse/JBSEAM-957
  +            /* Disabled because I can't get the restrictedEntityManager here, need to implement workaround
               UserTransaction userTx = null;
               boolean startedTx = false;
               try {
  @@ -70,7 +65,7 @@
                       userTx.begin();
                   }
   
  -                EntityManager em = ((EntityManager)org.jboss.seam.Component.getInstance("entityManager"));
  +                EntityManager em = ((EntityManager)org.jboss.seam.Component.getInstance("restrictedEntityManager"));
                   em.joinTransaction();
   
                   file = (!"".equals(id)) ? em.find(File.class, Long.parseLong(id)) : null;
  @@ -84,6 +79,7 @@
                   }
                   throw new RuntimeException(ex);
               }
  +            */
   
               String contentType = null;
               byte[] data = null;
  @@ -97,7 +93,7 @@
                   data = noImage;
               }
   
  -            if (data != null) {
  +            if (file != null && data != null) {
                   response.setContentType(contentType);
   
                   boolean rescale = false;
  
  
  
  1.3       +40 -3     jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/ui/WikiUtil.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: WikiUtil.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/ui/WikiUtil.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -b -r1.2 -r1.3
  --- WikiUtil.java	8 Mar 2007 17:50:58 -0000	1.2
  +++ WikiUtil.java	18 Mar 2007 15:44:37 -0000	1.3
  @@ -7,6 +7,8 @@
   import javax.faces.component.UIData;
   import javax.faces.context.FacesContext;
   import java.util.Collection;
  +import java.util.List;
  +import java.util.StringTokenizer;
   
   /**
    * Adds stuff to and for JSF that should be there but isn't. Also stuff that is exposed
  @@ -16,8 +18,6 @@
   @Name("wikiUtil")
   public class WikiUtil {
   
  -    // Used against page names, simply remove everything that is not alphanumeric, should do for most strings
  -    public static final String WIKINAME_REMOVECHARACTERS = "[^\\p{Alnum}]+";
   
       // Replacement for missing instaceOf in EL (can't use string comparison, might be proxy)
       public static boolean isDirectory(Node node) {
  @@ -32,6 +32,24 @@
           return node != null && File.class.isAssignableFrom(node.getClass());
       }
   
  +    // EL is weak
  +    public static String truncateString(String string, int length, String appendString) {
  +        if (string.length() <= length) return string;
  +        return string.substring(0, length-1) + appendString;
  +    }
  +
  +    public static String concat(String a, String b) {
  +        return a + b;
  +    }
  +
  +    // Display all roles for a particular access level
  +    public static Role.AccessLevel resolveAccessLevel(Integer accessLevel) {
  +        List<Role.AccessLevel> accessLevels = (List<Role.AccessLevel>)Component.getInstance("accessLevelsList");
  +        return accessLevels.get(
  +                accessLevels.indexOf(new Role.AccessLevel(accessLevel, null))
  +               );
  +    }
  +    
       // Allow calling this as a Facelets function in pages
       public static String renderURL(Node node) {
           GlobalPreferences globalPrefs = (GlobalPreferences) Component.getInstance("globalPrefs");
  @@ -50,8 +68,27 @@
           }
       }
   
  +    // Creates clean alphanumeric UpperCaseCamelCase
       public static String convertToWikiName(String realName) {
  -        return realName.replaceAll(WIKINAME_REMOVECHARACTERS, "");
  +        StringBuilder wikiName = new StringBuilder();
  +        // Remove everything that is not alphanumeric or whitespace, then split on word boundaries
  +        String[] tokens = realName.replaceAll("[^\\p{Alnum}|\\s]+", "").split("\\s");
  +        for (String token : tokens) {
  +            // Append word, uppercase first letter of word
  +            if (token.length() > 1) {
  +                wikiName.append(token.substring(0,1).toUpperCase());
  +                wikiName.append(token.substring(1));
  +            } else {
  +                wikiName.append(token.toUpperCase());
  +            }
  +        }
  +        return wikiName.toString();
  +    }
  +
  +    public static String renderHomeURL(User user) {
  +        String contextPath = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath();
  +        return contextPath + "/" + user.getMemberHome().getParent().getWikiname() + "/" + user.getMemberHome().getWikiname();
  +
       }
   
       /**
  
  
  
  1.3       +16 -12    jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/ui/Converters.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: Converters.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/ui/Converters.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -b -r1.2 -r1.3
  --- Converters.java	8 Mar 2007 10:44:15 -0000	1.2
  +++ Converters.java	18 Mar 2007 15:44:37 -0000	1.3
  @@ -3,6 +3,10 @@
   import org.jboss.seam.annotations.Name;
   import org.jboss.seam.annotations.Transactional;
   import org.jboss.seam.wiki.core.model.Role;
  +import org.jboss.seam.wiki.core.model.Node;
  +import org.jboss.seam.Component;
  +import org.jboss.seam.core.Expressions;
  +import org.jboss.seam.ui.EntityConverter;
   
   import javax.faces.component.UIComponent;
   import javax.faces.context.FacesContext;
  @@ -10,12 +14,13 @@
   import javax.faces.convert.ConverterException;
   import javax.persistence.EntityManager;
   import java.io.Serializable;
  +import java.util.List;
   
   public class Converters {
   
  -    @Name("roleConverter")
  -    @org.jboss.seam.annotations.jsf.Converter(forClass = Role.class)
  -    public static class RoleConverter implements Converter, Serializable {
  +    @Name("accessLevelConverter")
  +    @org.jboss.seam.annotations.jsf.Converter(forClass = Role.AccessLevel.class)
  +    public static class AccessLevelConverter implements Converter, Serializable {
   
           @Transactional
           public Object getAsObject(FacesContext arg0,
  @@ -23,23 +28,22 @@
                                     String arg2) throws ConverterException {
               if (arg2 == null) return null;
               try {
  -                EntityManager em = ((EntityManager)org.jboss.seam.Component.getInstance("entityManager"));
  -                em.joinTransaction();
  -
  -                return arg2 != null ? em.find(Role.class, Long.valueOf(arg2)) : null;
  +                List<Role.AccessLevel> accessLevels = (List<Role.AccessLevel>)Component.getInstance("accessLevelsList");
  +                return accessLevels.get(accessLevels.indexOf(new Role.AccessLevel(Integer.valueOf(arg2), null)));
               } catch (NumberFormatException e) {
  -                throw new ConverterException("Cannot find selected role", e);
  +                throw new ConverterException("Cannot find selected access level", e);
               }
           }
   
           public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) throws ConverterException {
  -            if (arg2 instanceof Role) {
  -                Role role = (Role) arg2;
  -                return role.getId().toString();
  +            if (arg2 instanceof Role.AccessLevel) {
  +                Role.AccessLevel accessLevel = (Role.AccessLevel)arg2;
  +                return accessLevel.getAccessLevel().toString();
               } else {
                   return null;
               }
           }
       }
   
  +
   }
  
  
  



More information about the jboss-cvs-commits mailing list