KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > Yasna > forum > ForumProxy


1 /**
2  * Copyright (C) 2001 Yasna.com. All rights reserved.
3  *
4  * ===================================================================
5  * The Apache Software License, Version 1.1
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  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution,
20  * if any, must include the following acknowledgment:
21  * "This product includes software developed by
22  * Yasna.com (http://www.yasna.com)."
23  * Alternately, this acknowledgment may appear in the software itself,
24  * if and wherever such third-party acknowledgments normally appear.
25  *
26  * 4. The names "Yazd" and "Yasna.com" must not be used to
27  * endorse or promote products derived from this software without
28  * prior written permission. For written permission, please
29  * contact yazd@yasna.com.
30  *
31  * 5. Products derived from this software may not be called "Yazd",
32  * nor may "Yazd" appear in their name, without prior written
33  * permission of Yasna.com.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL YASNA.COM OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of Yasna.com. For more information
51  * on Yasna.com, please see <http://www.yasna.com>.
52  */

53
54 /**
55  * Copyright (C) 2000 CoolServlets.com. All rights reserved.
56  *
57  * ===================================================================
58  * The Apache Software License, Version 1.1
59  *
60  * Redistribution and use in source and binary forms, with or without
61  * modification, are permitted provided that the following conditions
62  * are met:
63  *
64  * 1. Redistributions of source code must retain the above copyright
65  * notice, this list of conditions and the following disclaimer.
66  *
67  * 2. Redistributions in binary form must reproduce the above copyright
68  * notice, this list of conditions and the following disclaimer in
69  * the documentation and/or other materials provided with the
70  * distribution.
71  *
72  * 3. The end-user documentation included with the redistribution,
73  * if any, must include the following acknowledgment:
74  * "This product includes software developed by
75  * CoolServlets.com (http://www.coolservlets.com)."
76  * Alternately, this acknowledgment may appear in the software itself,
77  * if and wherever such third-party acknowledgments normally appear.
78  *
79  * 4. The names "Jive" and "CoolServlets.com" must not be used to
80  * endorse or promote products derived from this software without
81  * prior written permission. For written permission, please
82  * contact webmaster@coolservlets.com.
83  *
84  * 5. Products derived from this software may not be called "Jive",
85  * nor may "Jive" appear in their name, without prior written
86  * permission of CoolServlets.com.
87  *
88  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
89  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
90  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
91  * DISCLAIMED. IN NO EVENT SHALL COOLSERVLETS.COM OR
92  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
93  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
94  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
95  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
96  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
97  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
98  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
99  * SUCH DAMAGE.
100  * ====================================================================
101  *
102  * This software consists of voluntary contributions made by many
103  * individuals on behalf of CoolServlets.com. For more information
104  * on CoolServlets.com, please see <http://www.coolservlets.com>.
105  */

106
107 package com.Yasna.forum;
108
109 import com.Yasna.forum.util.ClientIP;
110 import com.Yasna.forum.Exceptions.RapidPostingException;
111 import com.Yasna.forum.Exceptions.UserBlackListedException;
112
113 import java.util.Date JavaDoc;
114 import java.util.Iterator JavaDoc;
115 import java.util.Enumeration JavaDoc;
116
117 /**
118  * A protection proxy for Forums. A proxy has a set of permissions that are
119  * specified at creation time of the proxy. Subsequently, those permissions
120  * are use to restrict access to protected Forum methods. If a user does
121  * not have the right to execute a particular method, and UnauthorizedException
122  * is thrown.
123  *
124  * @see Forum
125  * @see ForumPermissions
126  * @see UnauthorizedException
127  */

128 public class ForumProxy implements Forum {
129
130     private Forum forum;
131     private Authorization authorization;
132     private ForumPermissions permissions;
133
134     /**
135      * Creates a new ForumProxy object.
136      *
137      * @param forum the forum to protect by proxy
138      * @param authorization the user's authorization token
139      * @param permissions the permissions to use with this proxy.
140      */

141     public ForumProxy(Forum forum, Authorization authorization,
142             ForumPermissions permissions)
143     {
144         this.forum = forum;
145         this.authorization = authorization;
146         this.permissions = permissions;
147     }
148
149     //** Methods from interface below**//
150

151     public int getID() {
152         return forum.getID();
153     }
154
155     public String JavaDoc getName() {
156         return forum.getName();
157     }
158
159     public void setName(String JavaDoc name) throws UnauthorizedException,
160             ForumAlreadyExistsException
161     {
162         if (permissions.isSystemOrForumAdmin()) {
163             forum.setName(name);
164         }
165         else {
166             throw new UnauthorizedException();
167         }
168     }
169
170     public String JavaDoc getDescription() {
171         return forum.getDescription();
172     }
173
174     public void setDescription(String JavaDoc description)
175             throws UnauthorizedException
176     {
177         if (permissions.isSystemOrForumAdmin()) {
178             forum.setDescription(description);
179         }
180         else {
181             throw new UnauthorizedException();
182         }
183     }
184
185     public Date JavaDoc getCreationDate() {
186         return forum.getCreationDate();
187     }
188
189     public void setCreationDate(Date JavaDoc creationDate)
190             throws UnauthorizedException
191     {
192         if (permissions.isSystemOrForumAdmin()) {
193             forum.setCreationDate(creationDate);
194         }
195         else {
196             throw new UnauthorizedException();
197         }
198     }
199
200     public Date JavaDoc getModifiedDate() {
201         return forum.getModifiedDate();
202     }
203
204     public void setModifiedDate(Date JavaDoc modifiedDate)
205             throws UnauthorizedException
206     {
207         if (permissions.isSystemOrForumAdmin()) {
208             forum.setModifiedDate(modifiedDate);
209         }
210         else {
211             throw new UnauthorizedException();
212         }
213     }
214
215     public String JavaDoc getProperty(String JavaDoc name) {
216         return forum.getProperty(name);
217     }
218
219     public void setProperty(String JavaDoc name, String JavaDoc value)
220             throws UnauthorizedException
221     {
222         if (permissions.isSystemOrForumAdmin()) {
223             forum.setProperty(name, value);
224         }
225         else {
226             throw new UnauthorizedException();
227         }
228     }
229
230     public Enumeration JavaDoc propertyNames() {
231         return forum.propertyNames();
232     }
233
234     public ForumThread createThread(ForumMessage rootMessage,ThreadType type)
235             throws UnauthorizedException
236     {
237         if (permissions.get(ForumPermissions.CREATE_THREAD))
238         {
239             ForumThread thread = forum.createThread(rootMessage,type);
240             return new ForumThreadProxy(thread, authorization, permissions);
241         }
242         else {
243             throw new UnauthorizedException();
244         }
245     }
246
247     public ForumMessage createMessage(User user,ClientIP clientIP)
248             throws UnauthorizedException,RapidPostingException, UserBlackListedException
249     {
250         if (permissions.get(ForumPermissions.CREATE_MESSAGE) ||
251                 permissions.get(ForumPermissions.CREATE_THREAD) )
252         {
253             //The user must be anonymous or the actual user in order to post as
254
//that user. Otherwise, throw an exception.
255
if (user.hasPermission(ForumPermissions.USER_ADMIN) ||
256                 user.isAnonymous() )
257             {
258                 ForumMessage message = forum.createMessage(user,clientIP);
259                 return new ForumMessageProxy(message, authorization, permissions);
260             }
261             else {
262                 throw new UnauthorizedException();
263             }
264
265         }
266         else {
267             throw new UnauthorizedException();
268         }
269     }
270     public ForumMessage createDummyMessage(User user)
271             throws UnauthorizedException
272     {
273         if (permissions.get(ForumPermissions.CREATE_MESSAGE) ||
274                 permissions.get(ForumPermissions.CREATE_THREAD) )
275         {
276             //The user must be anonymous or the actual user in order to post as
277
//that user. Otherwise, throw an exception.
278
if (user.hasPermission(ForumPermissions.USER_ADMIN) ||
279                 user.isAnonymous() )
280             {
281                 ForumMessage message = forum.createDummyMessage(user);
282                 return new ForumMessageProxy(message, authorization, permissions);
283             }
284             else {
285                 throw new UnauthorizedException();
286             }
287
288         }
289         else {
290             throw new UnauthorizedException();
291         }
292     }
293
294     public void deleteThread(ForumThread thread) throws UnauthorizedException
295     {
296         if (permissions.isSystemOrForumAdmin() || permissions.get(ForumPermissions.MODERATOR)) {
297             forum.deleteThread(thread);
298         }
299         else {
300             throw new UnauthorizedException();
301         }
302     }
303
304     public void moveThread(ForumThread thread, Forum newForum) throws
305             UnauthorizedException, IllegalArgumentException JavaDoc
306     {
307         //If the user is an amdin of both forums
308
if (permissions.isSystemOrForumAdmin() && (
309                 newForum.hasPermission(ForumPermissions.SYSTEM_ADMIN) ||
310                 newForum.hasPermission(ForumPermissions.FORUM_ADMIN)))
311         {
312             forum.moveThread(thread, newForum);
313         }
314         else {
315             throw new UnauthorizedException();
316         }
317     }
318
319     public void addThread(ForumThread thread) throws UnauthorizedException
320     {
321         if (permissions.get(ForumPermissions.CREATE_THREAD)) {
322             forum.addThread(thread);
323         }
324         else {
325             throw new UnauthorizedException();
326         }
327     }
328
329     public ForumThread getThread(int threadID) throws ForumThreadNotFoundException
330     {
331         ForumThread thread = forum.getThread(threadID);
332         //Apply protection proxy and return.
333
return new ForumThreadProxy(thread, authorization, permissions);
334     }
335
336     public Iterator JavaDoc threads() {
337         Iterator JavaDoc iterator = forum.threads();
338         return new ThreadIteratorProxy(iterator, authorization, permissions);
339     }
340
341     public Iterator JavaDoc threads(int startIndex, int numResults, int sortBy) {
342         Iterator JavaDoc iterator = forum.threads(startIndex, numResults, sortBy);
343         return new ThreadIteratorProxy(iterator, authorization, permissions);
344     }
345
346     public int getThreadCount() {
347         return forum.getThreadCount();
348     }
349
350     public int getMessageCount() {
351         return forum.getMessageCount();
352     }
353
354     public Query createQuery() {
355         return new QueryProxy(forum.createQuery(), authorization, permissions);
356     }
357
358     public void addUserPermission(User user, int permissionType)
359             throws UnauthorizedException
360     {
361         //Don't let someone become a System Admin through this method.
362
//The ForumPermissions class probably needs to be changed.
363
if (permissionType == ForumPermissions.SYSTEM_ADMIN) {
364             throw new UnauthorizedException();
365         }
366         if (permissions.isSystemOrForumAdmin()) {
367             forum.addUserPermission(user, permissionType);
368         }
369         else {
370             throw new UnauthorizedException();
371         }
372     }
373
374     public void removeUserPermission(User user, int permissionType)
375             throws UnauthorizedException
376     {
377         if (permissions.isSystemOrForumAdmin()) {
378             forum.removeUserPermission(user, permissionType);
379         }
380         else {
381             throw new UnauthorizedException();
382         }
383     }
384
385     public int [] usersWithPermission(int permissionType)
386             throws UnauthorizedException
387     {
388         if (permissions.isSystemOrForumAdmin()) {
389             return forum.usersWithPermission(permissionType);
390         }
391         else {
392             throw new UnauthorizedException();
393         }
394     }
395
396     public void addGroupPermission(Group group, int permissionType)
397             throws UnauthorizedException
398     {
399         //Don't let someone become a System Admin through this method.
400
//The ForumPermissions class probably needs to be changed.
401
if (permissionType == ForumPermissions.SYSTEM_ADMIN) {
402             throw new UnauthorizedException();
403         }
404         if (permissions.isSystemOrForumAdmin()) {
405             forum.addGroupPermission(group, permissionType);
406         }
407         else {
408             throw new UnauthorizedException();
409         }
410     }
411
412     public void removeGroupPermission(Group group, int permissionType)
413             throws UnauthorizedException
414     {
415         if (permissions.isSystemOrForumAdmin()) {
416             forum.removeGroupPermission(group, permissionType);
417         }
418         else {
419             throw new UnauthorizedException();
420         }
421     }
422
423     public int[] groupsWithPermission(int permissionType)
424             throws UnauthorizedException
425     {
426         if (permissions.isSystemOrForumAdmin()) {
427             return forum.groupsWithPermission(permissionType);
428         }
429         else {
430             throw new UnauthorizedException();
431         }
432     }
433
434     public ForumMessageFilter[] getForumMessageFilters()
435             throws UnauthorizedException
436     {
437         if (permissions.isSystemOrForumAdmin()) {
438             return forum.getForumMessageFilters();
439         }
440         else {
441             throw new UnauthorizedException();
442         }
443     }
444
445     public void addForumMessageFilter(ForumMessageFilter filter)
446             throws UnauthorizedException
447     {
448         if (permissions.isSystemOrForumAdmin()) {
449             forum.addForumMessageFilter(filter);
450         }
451         else {
452             throw new UnauthorizedException();
453         }
454     }
455
456     public void addForumMessageFilter(ForumMessageFilter filter, int index)
457             throws UnauthorizedException
458     {
459         if (permissions.isSystemOrForumAdmin()) {
460             forum.addForumMessageFilter(filter, index);
461         }
462         else {
463             throw new UnauthorizedException();
464         }
465     }
466
467     public void removeForumMessageFilter(int index)
468             throws UnauthorizedException
469     {
470         if (permissions.isSystemOrForumAdmin()) {
471             forum.removeForumMessageFilter(index);
472         }
473         else {
474             throw new UnauthorizedException();
475         }
476     }
477
478     public ForumMessage applyFilters(ForumMessage message) {
479         return forum.applyFilters(message);
480     }
481
482     public ForumPermissions getPermissions(Authorization authorization) {
483         return forum.getPermissions(authorization);
484     }
485
486     public boolean hasPermission(int type) {
487         return permissions.get(type);
488     }
489
490     public boolean isModerated() {
491         return forum.isModerated();
492     }
493
494     public void setModerated(boolean moderated) throws UnauthorizedException {
495         if (permissions.isSystemOrForumAdmin()) {
496             forum.setModerated(moderated);
497         }
498         else {
499             throw new UnauthorizedException();
500         }
501     }
502
503     public String JavaDoc toString() {
504         return forum.toString();
505     }
506     public boolean isArticleForum(){
507         return forum.isArticleForum();
508     }
509     public void addArticleMap(String JavaDoc pageKey,ForumThread thread) throws UnauthorizedException{
510         forum.addArticleMap(pageKey,thread);
511     }
512     public int forumOrder(){
513         return forum.forumOrder();
514     }
515     public void setForumOrder(int param) throws UnauthorizedException{
516         if (permissions.isSystemOrForumAdmin()) {
517             forum.setForumOrder(param);
518         }
519         else {
520             throw new UnauthorizedException();
521         }
522     }
523
524 }
525
526
Popular Tags