KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > security > SecureURI


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.security;
14
15 import info.magnolia.cms.util.SimpleUrlPattern;
16 import info.magnolia.cms.util.UrlPattern;
17
18 import java.util.Hashtable JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
21
22
23 /**
24  * @author Sameer Charles
25  */

26 public final class SecureURI {
27
28     /**
29      * Map of protected url patterns.
30      */

31     private static Map JavaDoc secureURIs = new Hashtable JavaDoc();
32
33     private static Map JavaDoc unsecureURIs = new Hashtable JavaDoc();
34
35     /**
36      * Utility class, don't instantiate.
37      */

38     private SecureURI() {
39         // unused
40
}
41
42     public static Map JavaDoc listSecureURIs() {
43         Hashtable JavaDoc copy = new Hashtable JavaDoc();
44         copy.putAll(secureURIs);
45         return copy;
46     }
47
48     public static Map JavaDoc listUnsecureURIs() {
49         Hashtable JavaDoc copy = new Hashtable JavaDoc();
50         copy.putAll(unsecureURIs);
51         return copy;
52     }
53
54     /**
55      * Initialize the Secure URI list.
56      */

57     public static void init() {
58         SecureURI.secureURIs.clear();
59         SecureURI.unsecureURIs.clear();
60
61     }
62
63     /**
64      * Reload the secure URI list.
65      */

66     public static void reload() {
67         init();
68     }
69
70     /**
71      * @param handle
72      */

73     private static synchronized void addToList(String JavaDoc handle, Map JavaDoc map) {
74         UrlPattern pattern1 = new SimpleUrlPattern(handle);
75         map.put(handle, pattern1);
76     }
77
78     /**
79      * @param handle
80      */

81     private static synchronized void deleteFromList(String JavaDoc handle, Map JavaDoc map) {
82         map.remove(handle);
83     }
84
85     /**
86      * @param handle
87      */

88     public static void addSecure(String JavaDoc handle) {
89         SecureURI.addToList(handle, SecureURI.secureURIs);
90     }
91
92     /**
93      * @param handle
94      */

95     public static void deleteSecure(String JavaDoc handle) {
96         SecureURI.deleteFromList(handle, SecureURI.secureURIs);
97     }
98
99     public static void addUnsecure(String JavaDoc handle) {
100         SecureURI.addToList(handle, SecureURI.unsecureURIs);
101     }
102
103     /**
104      * @param handle
105      */

106     public static void deleteUnsecure(String JavaDoc handle) {
107         SecureURI.deleteFromList(handle, SecureURI.unsecureURIs);
108     }
109
110     /**
111      * Check if a request URI matches a configured pattern.
112      * @param uri request URI to check
113      * @return <code>true</code> if the given uri matches one of the configured patterns.
114      */

115     public static boolean isProtected(String JavaDoc uri) {
116         return hasPattern(uri, SecureURI.secureURIs);
117     }
118
119     public static boolean isUnsecure(String JavaDoc uri) {
120         return hasPattern(uri, SecureURI.unsecureURIs);
121     }
122
123     /**
124      * Check if the uri is a pattern that has a mapping in the given map
125      * @param uri the uri to search for
126      * @param map the map where the <code>UrlPattern</code> are stored
127      * @return <code>true</code> if the uri has match one of the pattern stored in the map, false otherwise
128      */

129     public static boolean hasPattern(String JavaDoc uri, Map JavaDoc map) {
130         Iterator JavaDoc e = map.keySet().iterator();
131         while (e.hasNext()) {
132             UrlPattern p = (UrlPattern) map.get(e.next());
133             if (p.match(uri)) {
134                 return true;
135             }
136         }
137         return false;
138     }
139 }
140
Popular Tags