1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.context;
35
36 import info.magnolia.cms.core.HierarchyManager;
37 import info.magnolia.cms.core.search.QueryManager;
38 import info.magnolia.cms.i18n.Messages;
39 import info.magnolia.cms.i18n.MessagesManager;
40 import info.magnolia.cms.security.AccessManager;
41 import info.magnolia.cms.security.AccessManagerImpl;
42 import info.magnolia.cms.security.Permission;
43 import info.magnolia.cms.security.PermissionUtil;
44 import info.magnolia.cms.security.Security;
45 import info.magnolia.cms.security.User;
46 import info.magnolia.cms.util.HierarchyManagerUtil;
47 import info.magnolia.exception.RuntimeRepositoryException;
48
49 import java.io.Serializable;
50 import java.util.Collection;
51 import java.util.HashMap;
52 import java.util.Iterator;
53 import java.util.List;
54 import java.util.Locale;
55 import java.util.Map;
56 import java.util.Set;
57
58 import javax.jcr.LoginException;
59 import javax.jcr.RepositoryException;
60 import javax.jcr.Session;
61 import javax.security.auth.Subject;
62
63 import org.slf4j.Logger;
64 import org.slf4j.LoggerFactory;
65
66
67
68
69
70
71
72 public abstract class AbstractContext implements Context, Serializable {
73 private static final Logger log = LoggerFactory.getLogger(AbstractContext.class);
74
75 @Override
76 public User getUser() {
77 return Security.getSystemUser();
78 }
79
80 @Override
81 public Subject getSubject() {
82 return Security.getSystemSubject();
83 }
84
85
86
87
88 protected Locale locale;
89
90 private AttributeStrategy attributeStrategy;
91
92 private JCRSessionStrategy repositoryStrategy;
93
94 public AttributeStrategy getAttributeStrategy() {
95 return attributeStrategy;
96 }
97
98 public void setAttributeStrategy(AttributeStrategy strategy) {
99 this.attributeStrategy = strategy;
100 }
101
102 public JCRSessionStrategy getRepositoryStrategy() {
103 return repositoryStrategy;
104 }
105
106 public void setRepositoryStrategy(JCRSessionStrategy strategy) {
107 this.repositoryStrategy = strategy;
108 }
109
110 @Override
111 public Object getAttribute(String name, int scope) {
112 return getAttributeStrategy().getAttribute(name, scope);
113 }
114
115 @Override
116 public Map<String, Object> getAttributes(int scope) {
117 return getAttributeStrategy().getAttributes(scope);
118 }
119
120 @Override
121 public void removeAttribute(String name, int scope) {
122 getAttributeStrategy().removeAttribute(name, scope);
123 }
124
125 @Override
126 public void setAttribute(String name, Object value, int scope) {
127 getAttributeStrategy().setAttribute(name, value, scope);
128 }
129
130 @Override
131 public AccessManager getAccessManager(String name) {
132 Subject subject = getSubject();
133 List<Permission> availablePermissions = PermissionUtil.getPermissions(subject, name);
134 if (availablePermissions == null) {
135 log.warn("no permissions found for " + getUser().getName());
136 }
137
138 AccessManagerImpl ami = new AccessManagerImpl();
139 ami.setPermissionList(availablePermissions);
140 return ami;
141 }
142
143 @Override
144 public Session getJCRSession(String workspaceName) throws LoginException, RepositoryException {
145 return getRepositoryStrategy().getSession(workspaceName);
146 }
147
148
149
150
151
152
153 @Override
154 public Object getAttribute(String name) {
155 Object value = this.getAttribute(name, Context.LOCAL_SCOPE);
156 if (null == value) {
157 value = this.getAttribute(name, Context.SESSION_SCOPE);
158 }
159 if (null == value) {
160 value = this.getAttribute(name, Context.APPLICATION_SCOPE);
161 }
162 return value;
163 }
164
165
166
167
168 @Override
169 public Map<String, Object> getAttributes() {
170 final Map<String, Object> map = new HashMap<String, Object>();
171 map.putAll(this.getAttributes(Context.LOCAL_SCOPE));
172 map.putAll(this.getAttributes(Context.SESSION_SCOPE));
173 map.putAll(this.getAttributes(Context.APPLICATION_SCOPE));
174 return map;
175 }
176
177
178
179
180
181 @Override
182 public Locale getLocale() {
183 if (locale == null) {
184 final SystemContext sysctx = MgnlContext.getSystemContext();
185 if (this == sysctx) {
186
187 return null;
188 }
189 locale = sysctx.getLocale();
190 }
191 return locale;
192 }
193
194 @Override
195 public void setLocale(Locale locale) {
196 this.locale = locale;
197 }
198
199
200
201
202 @Override
203 public Messages getMessages() {
204 return getMessages(MessagesManager.DEFAULT_BASENAME);
205 }
206
207
208
209
210 @Override
211 public Messages getMessages(String basename) {
212 return MessagesManager.getMessages(basename, getLocale());
213 }
214
215 @Override
216 public HierarchyManager getHierarchyManager(String workspaceName) {
217 try {
218 return HierarchyManagerUtil.asHierarchyManager(getJCRSession(workspaceName));
219 } catch (RepositoryException e) {
220 throw new RuntimeRepositoryException(e);
221 }
222 }
223
224 @Override
225 public QueryManager getQueryManager(String workspaceName) {
226 return this.getHierarchyManager(workspaceName).getQueryManager();
227 }
228
229
230
231
232
233 @Override
234 public Object get(Object key) {
235 return this.getAttribute(key.toString());
236 }
237
238
239
240
241 @Override
242 public Object put(Object key, Object value) {
243 this.setAttribute(key.toString(), value, Context.LOCAL_SCOPE);
244 return value;
245 }
246
247
248
249
250 @Override
251 public void clear() {
252 for (String key : this.getAttributes().keySet()) {
253 this.removeAttribute(key, Context.LOCAL_SCOPE);
254 }
255 throw new UnsupportedOperationException("you can not clear a magnolia context");
256 }
257
258
259
260
261 @Override
262 public boolean containsValue(Object value) {
263 return this.getAttributes().containsValue(value);
264 }
265
266
267
268
269 @Override
270 public Set<Entry<String, Object>> entrySet() {
271 return this.getAttributes().entrySet();
272 }
273
274
275
276
277 @Override
278 public boolean isEmpty() {
279 return this.getAttributes().isEmpty();
280 }
281
282
283
284
285 @Override
286 public Set<String> keySet() {
287 return this.getAttributes().keySet();
288 }
289
290
291
292
293 @Override
294 public void putAll(Map map) {
295 for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) {
296 Entry entry = (Entry) iter.next();
297 this.setAttribute(entry.getKey().toString(), entry.getValue(), Context.LOCAL_SCOPE);
298 }
299 }
300
301
302
303
304 @Override
305 public Object remove(Object key) {
306 Object obj = this.getAttribute(key.toString());
307 this.removeAttribute(key.toString(), Context.LOCAL_SCOPE);
308 return obj;
309 }
310
311
312
313
314 @Override
315 public Collection<Object> values() {
316 return this.getAttributes().values();
317 }
318
319
320
321
322 @Override
323 public boolean containsKey(Object arg0) {
324 return this.getAttributes().containsKey(arg0);
325 }
326
327
328
329
330 @Override
331 public int size() {
332 return this.getAttributes().size();
333 }
334
335
336
337
338 @Override
339 public void release() {
340 getRepositoryStrategy().release();
341 }
342 }