View Javadoc
1   /**
2    * This file Copyright (c) 2003-2017 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.context;
35  
36  import info.magnolia.cms.i18n.Messages;
37  import info.magnolia.cms.i18n.MessagesManager;
38  import info.magnolia.cms.security.AccessManager;
39  import info.magnolia.cms.security.PermissionUtil;
40  import info.magnolia.cms.security.Security;
41  import info.magnolia.cms.security.User;
42  
43  import java.io.Serializable;
44  import java.util.Collection;
45  import java.util.HashMap;
46  import java.util.Iterator;
47  import java.util.Locale;
48  import java.util.Map;
49  import java.util.Set;
50  
51  import javax.jcr.LoginException;
52  import javax.jcr.RepositoryException;
53  import javax.jcr.Session;
54  import javax.security.auth.Subject;
55  
56  
57  /**
58   * Default implementation of the Context interface.
59   */
60  public abstract class AbstractContext implements Context, Serializable {
61  
62      @Override
63      public User getUser() {
64          return Security.getSystemUser();
65      }
66  
67      @Override
68      public Subject getSubject() {
69          return Security.getSystemSubject();
70      }
71  
72      /**
73       * The locale for this context.
74       */
75      protected Locale locale;
76  
77      private AttributeStrategy attributeStrategy;
78  
79      private JCRSessionStrategy repositoryStrategy;
80  
81      public AttributeStrategy getAttributeStrategy() {
82          return attributeStrategy;
83      }
84  
85      public void setAttributeStrategy(AttributeStrategy strategy) {
86          this.attributeStrategy = strategy;
87      }
88  
89      public JCRSessionStrategy getRepositoryStrategy() {
90          return repositoryStrategy;
91      }
92  
93      public void setRepositoryStrategy(JCRSessionStrategy strategy) {
94          this.repositoryStrategy = strategy;
95      }
96  
97      @Override
98      public Object getAttribute(String name, int scope) {
99          return getAttributeStrategy().getAttribute(name, scope);
100     }
101 
102     @Override
103     public Map<String, Object> getAttributes(int scope) {
104         return getAttributeStrategy().getAttributes(scope);
105     }
106 
107     @Override
108     public void removeAttribute(String name, int scope) {
109         getAttributeStrategy().removeAttribute(name, scope);
110     }
111 
112     @Override
113     public void setAttribute(String name, Object value, int scope) {
114         getAttributeStrategy().setAttribute(name, value, scope);
115     }
116 
117     @Override
118     public AccessManager getAccessManager(String workspace) {
119         return PermissionUtil.getAccessManager(workspace, getSubject());
120     }
121 
122     @Override
123     public Session getJCRSession(String workspaceName) throws LoginException, RepositoryException {
124         return getRepositoryStrategy().getSession(workspaceName);
125     }
126 
127     /**
128      * Get the attribute value.
129      *
130      * @param name to which value is associated to
131      * @return attribute value
132      */
133     @Override
134     public Object getAttribute(String name) {
135         Object value = this.getAttribute(name, Context.LOCAL_SCOPE);
136         if (null == value) {
137             value = this.getAttribute(name, Context.SESSION_SCOPE);
138         }
139         if (null == value) {
140             value = this.getAttribute(name, Context.APPLICATION_SCOPE);
141         }
142         return value;
143     }
144 
145     /**
146      * Merge the scopes maps.
147      */
148     @Override
149     public Map<String, Object> getAttributes() {
150         final Map<String, Object> map = new HashMap<String, Object>();
151         map.putAll(this.getAttributes(Context.LOCAL_SCOPE));
152         map.putAll(this.getAttributes(Context.SESSION_SCOPE));
153         map.putAll(this.getAttributes(Context.APPLICATION_SCOPE));
154         return map;
155     }
156 
157     /**
158      * If not yet set try to get the locale of the user. Else use the locale of the system context.
159      *
160      * @see Context#getLocale()
161      */
162     @Override
163     public Locale getLocale() {
164         if (locale == null) {
165             final SystemContext sysctx = MgnlContext.getSystemContext();
166             if (this == sysctx) {
167                 // do not fall in the endless loop
168                 return null;
169             }
170             locale = sysctx.getLocale();
171         }
172         return locale;
173     }
174 
175     @Override
176     public void setLocale(Locale locale) {
177         this.locale = locale;
178     }
179 
180     /**
181      * TODO: This duplicates methods from MessagesManager : remove either.
182      */
183     @Override
184     public Messages getMessages() {
185         return getMessages(MessagesManager.DEFAULT_BASENAME);
186     }
187 
188     /**
189      * TODO: This duplicates methods from MessagesManager : remove either.
190      */
191     @Override
192     public Messages getMessages(String basename) {
193         return MessagesManager.getMessages(basename, getLocale());
194     }
195 
196     // ------ Map interface methods -------
197 
198     /**
199      * {@inheritDoc}
200      */
201     @Override
202     public Object get(Object key) {
203         return this.getAttribute(key.toString());
204     }
205 
206     /**
207      * {@inheritDoc}
208      */
209     @Override
210     public Object put(Object key, Object value) {
211         this.setAttribute(key.toString(), value, Context.LOCAL_SCOPE);
212         return value;
213     }
214 
215     /**
216      * {@inheritDoc}
217      */
218     @Override
219     public void clear() {
220         for (String key : this.getAttributes().keySet()) {
221             this.removeAttribute(key, Context.LOCAL_SCOPE);
222         }
223         throw new UnsupportedOperationException("you can not clear a magnolia context");
224     }
225 
226     /**
227      * This implementation is very slow!
228      */
229     @Override
230     public boolean containsValue(Object value) {
231         return this.getAttributes().containsValue(value);
232     }
233 
234     /**
235      * {@inheritDoc}
236      */
237     @Override
238     public Set<Entry<String, Object>> entrySet() {
239         return this.getAttributes().entrySet();
240     }
241 
242     /**
243      * {@inheritDoc}
244      */
245     @Override
246     public boolean isEmpty() {
247         return this.getAttributes().isEmpty();
248     }
249 
250     /**
251      * {@inheritDoc}
252      */
253     @Override
254     public Set<String> keySet() {
255         return this.getAttributes().keySet();
256     }
257 
258     /**
259      * {@inheritDoc}
260      */
261     @Override
262     public void putAll(Map map) {
263         for (Iterator iter = map.entrySet().iterator(); iter.hasNext(); ) {
264             Entry entry = (Entry) iter.next();
265             this.setAttribute(entry.getKey().toString(), entry.getValue(), Context.LOCAL_SCOPE);
266         }
267     }
268 
269     /**
270      * {@inheritDoc}
271      */
272     @Override
273     public Object remove(Object key) {
274         Object obj = this.getAttribute(key.toString());
275         this.removeAttribute(key.toString(), Context.LOCAL_SCOPE);
276         return obj;
277     }
278 
279     /**
280      * {@inheritDoc}
281      */
282     @Override
283     public Collection<Object> values() {
284         return this.getAttributes().values();
285     }
286 
287     /**
288      * {@inheritDoc}
289      */
290     @Override
291     public boolean containsKey(Object arg0) {
292         return this.getAttributes().containsKey(arg0);
293     }
294 
295     /**
296      * {@inheritDoc}
297      */
298     @Override
299     public int size() {
300         return this.getAttributes().size();
301     }
302 
303     /**
304      * {@inheritDoc}
305      */
306     @Override
307     public void release() {
308         getRepositoryStrategy().release();
309     }
310 }