View Javadoc

1   /**
2    *
3    */
4   package org.chenillekit.access.internal;
5   
6   import org.apache.tapestry5.EventContext;
7   import org.apache.tapestry5.annotations.OnEvent;
8   import org.apache.tapestry5.services.ContextValueEncoder;
9   import org.apache.tapestry5.services.TransformMethodSignature;
10  import org.chenillekit.access.ChenilleKitAccessConstants;
11  
12  /**
13   *	@version $Id: ChenillekitAccessInternalUtils.java 374 2008-12-12 16:47:49Z mlusetti $
14   */
15  public class ChenillekitAccessInternalUtils
16  {
17  	private static final String[] NO_GROUPS = new String[0];
18  
19  	private ChenillekitAccessInternalUtils() {}
20  
21  	/**
22  	 * Build a string representing the context to be stored inside a cookie.
23  	 *
24  	 * @param context the context to be stored in the cookie
25  	 * @return a String rapresentation of the {@link EventContext}
26  	 */
27  	public static final String getContextAsString(EventContext context)
28  	{
29  		String res = "";
30  
31  		for (int i = 0; i < context.getCount(); i++)
32  		{
33  			res = res + context.get(String.class, i) + "####";
34  		}
35  
36  		return res;
37  	}
38  
39  	/**
40  	 *
41  	 * @param valueEncoder
42  	 * @param contextString
43  	 * @return
44  	 */
45  	public static final EventContext getContextFromString(ContextValueEncoder valueEncoder,
46  							String contextString)
47  	{
48  		String[] contextElements = contextString.split("####");
49  
50  		return new CKUrlEventContext(valueEncoder, contextElements);
51  	}
52  
53  	/**
54  	 * check if user has required role to access page/component/event.
55  	 *
56  	 * @param userRoleWeigh     role weigh the user have
57  	 * @param requiredRoleWeigh role weigh required for page/component/event access
58  	 *
59  	 * @return true if user fulfill the required role
60  	 */
61  	public static final boolean hasUserRequiredRole(int userRoleWeigh, int requiredRoleWeigh)
62  	{
63  		return userRoleWeigh >= requiredRoleWeigh;
64  	}
65  
66  	/**
67  	 * check if user has required group to access page/component/event.
68  	 *
69  	 * @param userGroups     groups the user have
70  	 * @param requiredGroups groups required for page/component/event access
71  	 *
72  	 * @return true if user has the required group
73  	 */
74  	public static final boolean hasUserRequiredGroup(String[] userGroups, String[] requiredGroups)
75  	{
76  		boolean hasGroup = false;
77  
78  		/**
79  		 * if no group required
80  		 */
81  		if (requiredGroups.length == 0)
82  			return true;
83  
84  		for (String requiredGroup : requiredGroups)
85  		{
86  			for (String userGroup : userGroups)
87  			{
88  				if (userGroup.equalsIgnoreCase(requiredGroup))
89  				{
90  					hasGroup = true;
91  					break;
92  				}
93  			}
94  
95  			if (hasGroup)
96  				break;
97  		}
98  
99  		return hasGroup;
100 	}
101 
102 	/**
103 	 *
104 	 * @param componentId
105 	 * @param eventType
106 	 * @param suffix
107 	 * @return
108 	 */
109 	public static final String buildMetaForHandlerMethod(String componentId, String eventType, String suffix)
110 	{
111 		return ChenilleKitAccessConstants.RESTRICTED_EVENT_HANDLER_PREFIX
112 					+ "-" + componentId + "-" + eventType + "-" + suffix;
113 
114 	}
115 
116 	/**
117 	 * build an CSV string from group array.
118 	 *
119 	 * @return CSV string
120 	 */
121 	public static final String getStringArrayAsString(String[] groups)
122 	{
123 		String csvString = "";
124 
125 		for (int i = 0; i < groups.length; i++)
126 		{
127 			if (i == 0)
128 				csvString += groups[i];
129 			else
130 				csvString += "," + groups[i];
131 		}
132 
133 		return csvString;
134 	}
135 
136 	/**
137 	 *
138 	 * @param groups
139 	 * @return
140 	 */
141 	public static final String[] getStringAsStringArray(String groups)
142 	{
143 		if ( ChenilleKitAccessConstants.NO_RESTRICTION.equals(groups)) return NO_GROUPS;
144 		return groups.split(",");
145 	}
146 
147 	/**
148 	 * This code is taken deliberatly from
149 	 * http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/OnEventWorker.java?view=log
150 	 */
151 	public static final String extractComponentId(TransformMethodSignature method, OnEvent annotation)
152 	{
153 		if (annotation != null) return annotation.component();
154 
155 		// Method name started with "on". Extract the component id, if present.
156 
157 		String name = method.getMethodName();
158 
159 		int fromx = name.indexOf("From");
160 
161 		if (fromx < 0) return "";
162 
163 		return name.substring(fromx + 4);
164 	}
165 
166 	/**
167 	 * This code is taken deliberatly from:
168 	 * http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/OnEventWorker.java?view=log
169 	 */
170 	public static final String extractEventType(TransformMethodSignature method, OnEvent annotation)
171 	{
172 		if (annotation != null) return annotation.value();
173 
174 		// Method name started with "on". Extract the event type.
175 
176 		String name = method.getMethodName();
177 
178 		int fromx = name.indexOf("From");
179 
180 		return fromx == -1 ? name.substring(2) : name.substring(2, fromx);
181 	}
182 
183 }