Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: info/crossbar/model/sitemap/Menu.java


1   /*
2    *  @(#)Menu.java $Revision: 1.1.1.1 $ $Date: 2003/05/20 06:34:48 $
3    *
4    *  Copyright 2002 by Daniel Kehoe <kehoe@fortuity.com>
5    *  All Rights Reserved
6    *
7    *  Redistribution and use in source and binary forms, with or without
8    *  modification, are permitted provided that the following conditions
9    *  are met:
10   *  1. Redistributions of source code must retain the above copyright
11   *  notice, this list of conditions and the following disclaimer.
12   *  2. Redistributions in binary form must reproduce the above copyright
13   *  notice, this list of conditions and the following disclaimer in the
14   *  documentation and/or other materials provided with the distribution.
15   *
16   *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17   *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18   *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19   *  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20   *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21   *  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22   *  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23   *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24   *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25   *  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26   *  SUCH DAMAGE.
27   */
28  package info.crossbar.model.sitemap;
29  
30  import java.util.logging.Logger;
31  import java.util.logging.Level;
32  import java.util.*;
33  
34  import javax.servlet.http.HttpServletRequest;
35  
36  /**
37   * Class used only within the Sitemap class.
38   *
39   */
40  public class Menu { 
41      
42    /**
43     * Set up logging.
44     *
45     */
46    private static Logger log = Logger.getLogger(Menu.class.getName());
47      
48    String name;
49    
50    HashMap displayNames = new HashMap();
51    
52    TreeMap sortOrdering = new TreeMap();
53    
54    ArrayList roles = new ArrayList();
55    
56    public String toString() {
57      return name;
58    }
59    
60    public String getName() {
61      return name;
62    }
63    public void setName(String value) {
64      name = value;
65    }
66    
67    public String getDisplayName(String locale) {
68      return (String) displayNames.get(locale);
69    }
70    public void setDisplayName(String locale, String displayName) {
71      displayNames.put(locale, displayName);
72    }
73    
74    public String getSortOrder(String sortOrder) {
75      return (String) sortOrdering.get(sortOrder);
76    }
77    public void setSortOrder(String sortOrder, String name) {
78      sortOrdering.put(sortOrder, name);
79    }
80    
81    public ArrayList getRoles() {
82      return roles;
83    }
84    public void setRole(String role) {
85      roles.add(role);
86    }
87    
88    public ArrayList getItems() {
89      return new ArrayList(sortOrdering.values());
90    }
91    
92    /**
93     * Return a list of menu categories.
94     * By default, the HttpServletRequest will be null and the user 
95     * will not be allowed to see any categories meant for users in a
96     * specific role.
97     *
98     * @param  request  HttpServletRequest needed to determine the user's role(s)
99     * @param  categories  A look-up dictionary of menu categories
100    */    
101   public ArrayList getItems(HttpServletRequest request, HashMap categories) {
102     ArrayList allowed = new ArrayList();
103     for (Iterator it= sortOrdering.values().iterator(); it.hasNext(); ) {
104       String itemName = (String) it.next();
105       ArrayList roles = ((MenuCategory) categories.get(itemName)).getRoles();
106       if (accessAllowed(request, roles)) allowed.add(itemName);
107     }
108     return allowed;
109   }
110   
111   /**
112    * Checks if the user is in a role allowed access to an element, unless the
113    * element is designated as accessible to a &quot;public&quot; role.
114    *
115    * @param request   the HttpServletRequest is needed to determine what role the user is in
116    * @param roles   a List of roles
117    * @return allowed   a boolean true if access is allowed
118    */
119   private static boolean accessAllowed(HttpServletRequest request, List roles) {
120     log.finest("ENTER");
121     boolean allowed = false;
122     if (!roles.contains("public")) {
123       if (request != null) {
124         // check each role allowed access to the page to see if the user is in that role:
125         Iterator j = roles.iterator();
126         while (j.hasNext()) {
127           String roleToCheck = (String) j.next();
128           // if the user is in one of the roles allowed access, return true:
129           if (request.isUserInRole(roleToCheck)) allowed = true;
130           log.finest("RemoteUser \"" + request.getRemoteUser() 
131             + "\" in role \"" + roleToCheck + "\": " + allowed);
132         }
133       }
134     } else allowed = true;
135     log.finest("RETURN");
136     return allowed;
137   }
138 }