001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.serializer.objects;
018:
019: import java.util.ArrayList;
020: import java.util.Iterator;
021:
022: import javolution.xml.XMLFormat;
023: import javolution.xml.stream.XMLStreamException;
024:
025: import org.apache.commons.lang.StringEscapeUtils;
026: import org.apache.jetspeed.security.FolderPermission;
027: import org.apache.jetspeed.security.FragmentPermission;
028: import org.apache.jetspeed.security.PagePermission;
029: import org.apache.jetspeed.security.PortalResourcePermission;
030: import org.apache.jetspeed.security.PortletPermission;
031:
032: /**
033: * Serialized Permission <permission type='folder' resource='/' actions='view,
034: * edit'> <roles>admin, user</roles> <groups>dev</groups> <users>joe</users>
035: * </permission>
036: *
037: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
038: * @version $Id: $
039: */
040: public class JSPermission {
041:
042: private String type;
043:
044: private String resource;
045:
046: private String actions;
047:
048: private long id;
049:
050: private ArrayList roles = null;
051:
052: private ArrayList groups = null;
053:
054: private ArrayList users = null;
055:
056: private JSUserRoles roleString;
057:
058: private JSUserGroups groupString;
059:
060: private JSUserUsers userString;
061:
062: public static final String TYPE_FOLDER = "folder".intern();
063:
064: public static final String TYPE_FRAGMENT = "fragment".intern();
065:
066: public static final String TYPE_PAGE = "page".intern();
067:
068: public static final String TYPE_PORTALRESOURCE = "portalResource"
069: .intern();
070:
071: public static final String TYPE_PORTALRESOURCECOLLECTION = "portalResource"
072: .intern();
073:
074: public static final String TYPE_PORTAL = "portal".intern();
075:
076: public static final String TYPE_UNKNOWN = "unknown".intern();
077:
078: public String getClassForType(String type) {
079: if ((type == null) || (type.length() == 0)
080: || (type.equals(TYPE_UNKNOWN)))
081: return "";
082: if (type.equals(TYPE_FOLDER))
083: return "org.apache.jetspeed.security.FolderPermission";
084: if (type.equals(TYPE_FRAGMENT))
085: return "org.apache.jetspeed.security.FragmentPermission";
086: if (type.equals(TYPE_PAGE))
087: return "org.apache.jetspeed.security.PagePermission";
088: if (type.equals(TYPE_PORTALRESOURCE))
089: return "org.apache.jetspeed.security.PortalResourcePermission";
090: if (type.equals(TYPE_PORTALRESOURCECOLLECTION))
091: return "org.apache.jetspeed.security.PortalResourcePermissionCollection";
092: if (type.equals(TYPE_PORTAL))
093: return "org.apache.jetspeed.security.PortletPermission";
094: return "";
095: }
096:
097: public String getTypeForClass(String className) {
098: if ((className == null) || (className.length() == 0))
099: return TYPE_UNKNOWN;
100: if (className
101: .equals("org.apache.jetspeed.security.FolderPermission"))
102: return TYPE_FOLDER;
103:
104: if (className
105: .equals("org.apache.jetspeed.security.FragmentPermission"))
106: return TYPE_FRAGMENT;
107: if (className
108: .equals("org.apache.jetspeed.security.PagePermission"))
109: return TYPE_PAGE;
110: if (className
111: .equals("org.apache.jetspeed.security.PortletPermission"))
112: return TYPE_PORTAL;
113:
114: if (className
115: .equals("org.apache.jetspeed.security.PortalResourcePermission"))
116: return TYPE_PORTALRESOURCE;
117: if (className
118: .equals("org.apache.jetspeed.security.PortalResourcePermissionCollection"))
119: return TYPE_PORTALRESOURCECOLLECTION;
120: return TYPE_UNKNOWN;
121:
122: }
123:
124: public PortalResourcePermission getPermissionForType() {
125: PortalResourcePermission newPermission = null;
126: if ((this .type == null) || (this .type.equals(TYPE_UNKNOWN)))
127: return null;
128: try {
129: if (type.equals(TYPE_FOLDER))
130: newPermission = new FolderPermission(this .resource,
131: this .actions);
132: else if (type.equals(TYPE_FRAGMENT))
133: newPermission = new FragmentPermission(this .resource,
134: this .actions);
135: else if (type.equals(TYPE_PAGE))
136: newPermission = new PagePermission(this .resource,
137: this .actions);
138: else if (type.equals(TYPE_PORTAL))
139: newPermission = new PortletPermission(this .resource,
140: this .actions);
141: else
142: return null;
143: return newPermission;
144: } catch (Exception e) {
145: e.printStackTrace();
146: return null;
147: }
148:
149: }
150:
151: public JSPermission() {
152: }
153:
154: private String append(JSRole rule) {
155: return rule.getName();
156: }
157:
158: private String append(JSGroup group) {
159: return group.getName();
160: }
161:
162: private String append(JSUser user) {
163: return user.getName();
164: }
165:
166: private String append(Object s) {
167: if (s instanceof JSRole)
168: return append((JSRole) s);
169: if (s instanceof JSGroup)
170: return append((JSGroup) s);
171: if (s instanceof JSUser)
172: return append((JSUser) s);
173:
174: return s.toString();
175: }
176:
177: private String putTokens(ArrayList _list) {
178: if ((_list == null) || (_list.size() == 0))
179: return "";
180: boolean _start = true;
181: Iterator _it = _list.iterator();
182: StringBuffer _sb = new StringBuffer();
183: while (_it.hasNext()) {
184: if (!_start)
185: _sb.append(',');
186: else
187: _start = false;
188:
189: _sb.append(append(_it.next()));
190: }
191: return _sb.toString();
192: }
193:
194: /**
195: * @return Returns the actions.
196: */
197: public String getActions() {
198: return actions;
199: }
200:
201: /**
202: * @param actions
203: * The actions to set.
204: */
205: public void setActions(String actions) {
206: this .actions = actions;
207: }
208:
209: /**
210: * @return Returns the groups.
211: */
212: public ArrayList getGroups() {
213: return groups;
214: }
215:
216: /**
217: * @param groups
218: * The groups to set.
219: */
220: public void setGroups(ArrayList groups) {
221: this .groups = groups;
222: }
223:
224: /**
225: * @return Returns the resource.
226: */
227: public String getResource() {
228: return resource;
229: }
230:
231: /**
232: * @param resource
233: * The resource to set.
234: */
235: public void setResource(String resource) {
236: this .resource = resource;
237: }
238:
239: /**
240: * @return Returns the roles.
241: */
242: public ArrayList getRoles() {
243: return roles;
244: }
245:
246: /**
247: * @param roles
248: * The roles to set.
249: */
250: public void setRoles(ArrayList roles) {
251: this .roles = roles;
252: }
253:
254: /**
255: * @return Returns the type.
256: */
257: public String getType() {
258: return type;
259: }
260:
261: /**
262: * @param type
263: * The type to set.
264: */
265: public void setType(String type) {
266: this .type = type;
267: }
268:
269: /**
270: * @return Returns the users.
271: */
272: public ArrayList getUsers() {
273: return users;
274: }
275:
276: /**
277: * @param users
278: * The users to set.
279: */
280: public void setUsers(ArrayList users) {
281: this .users = users;
282: }
283:
284: /**
285: * @return Returns the id.
286: */
287: public long getId() {
288: return id;
289: }
290:
291: /**
292: * @param id
293: * The id to set.
294: */
295: public void setId(long id) {
296: this .id = id;
297: }
298:
299: public void addGroup(JSGroup group) {
300: if (groups == null)
301: groups = new ArrayList();
302: groups.add(group);
303: }
304:
305: public void addRole(JSRole role) {
306: if (roles == null)
307: roles = new ArrayList();
308: roles.add(role);
309: }
310:
311: public void addUser(JSUser user) {
312: if (users == null)
313: users = new ArrayList();
314: users.add(user);
315: }
316:
317: /***************************************************************************
318: * SERIALIZER
319: */
320: private static final XMLFormat XML = new XMLFormat(
321: JSPermission.class) {
322: public void write(Object o, OutputElement xml)
323: throws XMLStreamException {
324: try {
325: JSPermission g = (JSPermission) o;
326: xml.setAttribute("type", g.getType());
327: xml.setAttribute("resource", g.getResource());
328: xml.setAttribute("actions", g.getActions());
329: g.groupString = new JSUserGroups(g.putTokens(g
330: .getGroups()));
331: g.roleString = new JSUserRoles(g
332: .putTokens(g.getRoles()));
333: g.userString = new JSUserUsers(g
334: .putTokens(g.getUsers()));
335: xml.add(g.roleString);
336: xml.add(g.groupString);
337: xml.add(g.userString);
338:
339: } catch (Exception e) {
340: e.printStackTrace();
341: }
342: }
343:
344: public void read(InputElement xml, Object o) {
345: try {
346: JSPermission g = (JSPermission) o;
347: g.type = StringEscapeUtils.unescapeHtml(xml
348: .getAttribute("type", "type_unknown"));
349: g.resource = StringEscapeUtils.unescapeHtml(xml
350: .getAttribute("resource", "resource_unknown"));
351: g.actions = StringEscapeUtils.unescapeHtml(xml
352: .getAttribute("actions", "unknown_actions"));
353:
354: while (xml.hasNext()) {
355: Object o1 = xml.getNext(); // mime
356:
357: if (o1 instanceof JSUserGroups)
358: g.groupString = (JSUserGroups) o1;
359: else if (o1 instanceof JSUserUsers)
360: g.userString = (JSUserUsers) o1;
361: else if (o1 instanceof JSUserRoles)
362: g.roleString = (JSUserRoles) o1;
363: }
364: } catch (Exception e) {
365: e.printStackTrace();
366: }
367: }
368:
369: };
370:
371: public JSUserGroups getGroupString() {
372: return groupString;
373: }
374:
375: public JSUserRoles getRoleString() {
376: return roleString;
377: }
378:
379: public JSUserUsers getUserString() {
380: return userString;
381: }
382:
383: }
|