BrightSide Workbench Full Report + Source Code
Application.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.elephant.context;
19 
20 import java.io.IOException;
21 import java.util.List;
22 import java.util.Locale;
23 import java.util.Map;
24 import java.util.ResourceBundle;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27 import javax.servlet.http.Cookie;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
30 import javax.servlet.http.HttpSession;
31 import org.turro.annotation.ElephantSessionResolver;
32 import org.turro.elephant.impl.context.ContextFactory;
33 import org.turro.elephant.impl.security.UserPreferences;
34 import org.turro.elephant.impl.util.CookieUtil;
35 import org.turro.elephant.sitemap.SitemapGenerator;
36 import org.turro.elephant.web.ElContextMap;
37 import org.turro.i18n.I_;
38 import org.turro.i18n.Labels;
39 import org.turro.i18n.locale.I18nLocales;
40 import org.turro.plugin.contacts.IContact;
41 import org.turro.reflection.Instances;
42 import org.turro.security.SecurityGroups;
43 import org.turro.string.Strings;
44 
49 public abstract class Application {
50 
51  public static final String[] BUNDLES = {
52  "lang",
53  "elephant",
54  "labels",
55  "documentation",
56  "label-site"
57  };
58  public static final String REGEXP_NO_RESOURCES = "(\\/zkau.*|\\/app.*)";
59 
60  /*
61  Create 2 app: elapp and bsapp.
62  ElephantApplication will use with preference elapp and when not available bsapp.
63  BrightSideApplication will use with preference bsapp, checking isAvailable() using Executions.getCurrent() != null.
64  */
65  protected static ThreadLocal<Application> app = new ThreadLocal<>();
66 
68  protected HttpServletRequest request;
69  protected HttpServletResponse response;
70 
71  public static Application getApplication() {
72  Application application = app.get();
73  try {
74  if(application == null) {
75  HttpServletRequest request = getResolverRequest();
76  if(request != null) {
77  if(application == null) {
78  application = (Application) request.getAttribute("el_app");
79  }
80  if(application == null) {
81  application = new ElephantApplication(request, getResolverResponse());
82  request.setAttribute("el_app", application);
83  }
84  }
85  }
86  if(application != null) {
87  app.set(application);
88  return application;
89  } else {
91  }
92  } catch(Exception ex) {
94  }
95  }
96 
97  public static Application getFromConstructor(IConstructor constructor) {
98  return (Application) constructor.getRequest().getAttribute("el_app");
99  }
100 
101  public static boolean loadResources(String path) {
102  return !Strings.isBlank(path) && !path.matches(REGEXP_NO_RESOURCES);
103  }
104 
105  public static void resetSettings() {
110  Labels.resetMaps();
111  I_.resetMaps();
114  }
115 
116  public static void resetSchemes() {
118  }
119 
120  public Application() {
121  // move request and response here
122  /*if(app.get() == null)*/
123  if(!(this instanceof HeadlessApplication)) {
124  app.set(this);
125  }
126  }
127 
129  return new ApplicationContext(
132  }
133 
134  public boolean hasAnyRoleKey(String role) {
135  IConstructor constructor = getConstructor();
136  return constructor.hasAnyRoleKey(role);
137  }
138 
139  public boolean isInRole(String role) {
140  try {
141  return isInRole(role, null);
142  } catch (IOException ex) {
143  Logger.getLogger(Application.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
144  }
145  return false;
146  }
147 
148  public boolean isInRole(String role, String redirect) throws IOException {
149  IConstructor constructor = getConstructor();
150  boolean result, isWebAdministering = constructor.isWebAdministering();
151  if(!isWebAdministering) {
152  constructor.setWebAdministering(true);
153  }
154  result = constructor.isInRole(role);
155  if(!isWebAdministering) {
156  constructor.setWebAdministering(false);
157  }
158  if(!result && redirect != null) {
159  constructor.redirect(redirect);
160  }
161  return result;
162  }
163 
164  public static IContact getUser() {
166  }
167 
168  public IContact checkUser(boolean doLogin) throws IOException {
169  IConstructor constructor = getConstructor();
170  IContact user = constructor.getUser();
171  if(user == null && doLogin) {
172  ElephantLogin.redirectToLogin(constructor);
173  }
174  return user;
175  }
176 
177  public Object getPreference(String key) {
178  try {
179  IContact user = checkUser(false);
180  if (user != null) {
181  if (preferences == null || !preferences.isEqual(user.getId())) {
183  }
184  return preferences.get(key);
185  }
186  } catch (IOException ex) {
187  Logger.getLogger(Application.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
188  }
189  return null;
190  }
191 
192  public void putPreference(String key, Object value) {
193  try {
194  IContact user = checkUser(false);
195  if (user != null) {
196  if (preferences == null || !preferences.isEqual(user.getId())) {
198  }
199  preferences.put(key, value);
200  }
201  } catch (IOException ex) {
202  Logger.getLogger(Application.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
203  }
204  }
205 
207  IConstructor elcons = null;
208  if(getHttpServletRequest() != null) {
209  elcons = (IConstructor) getHttpServletRequest().getAttribute("el_cons");
210  }
211  if(elcons == null) {
212  elcons = doConstructor();
213  }
214  return elcons;
215  }
216 
217  public void invalidateSession() {
218  // check why Application.get() request has no session
219  try {
220  getHttpSession(false).invalidate();
221  } catch(Exception ex) {
222  getResolverSession(true).invalidate();
223  }
224  }
225 
226  public HttpSession getHttpSession(boolean create) {
227  if(getHttpServletRequest() != null) {
228  return getHttpServletRequest().getSession(create);
229  }
230  return null;
231  }
232 
233  public IImplementation getImplementation(String name) {
235  }
236 
237  public List getImplementationNamesByType(String type) {
239  }
240 
241  public abstract HttpServletRequest getHttpServletRequest();
242  public abstract HttpServletResponse getHttpServletResponse();
243  public abstract void sendRedirect(String uri);
244  public abstract void navigateBack();
245  protected abstract IConstructor doConstructor();
246 
247  // Cookies
248 
249  public static Cookie getCookie(String name) {
251  }
252 
253  public static String getCookieValue(String name) {
254  Cookie c = getCookie(name);
255  return c != null ? c.getValue() : null;
256  }
257 
258  public static void setCookie(String name, String value, String path, int age) {
259  CookieUtil.setCookie(getApplication().getHttpServletResponse(), name, value, path, age);
260  }
261 
262  public static void encryptCookie(String name, String value, String path, int age) {
263  try {
264  byte[] b = ElephantContext.encrypt(value.getBytes());
266  } catch (Exception ex) {
267  Logger.getLogger(Application.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
268  }
269  }
270 
271  public static String decryptCookieValue(String name) {
272  try {
273  Cookie cookie = getCookie(name);
274  if(cookie != null) {
275  byte[] v = CookieUtil.decryptCookieValue(cookie);
276  if(v != null) return new String(ElephantContext.decrypt(v));
277  }
278  } catch (Exception ex) {
279  Logger.getLogger(Application.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
280  }
281  return null;
282  }
283 
284  public static void deleteCookie(String name, String path) {
286  }
287 
288  /* Labels */
289 
290  public static Locale getUsedLocale() {
291  return I_.api().used();
292  }
293 
294  public static Map getStringMap() {
295  return I_.compatibilityMap();
296  }
297 
298  public static Map getStringMap(Locale locale, String configured) {
299  return I_.compatibilityMap(locale);
300  }
301 
302  public static String getString(String key) {
303  return I_.compatibility().get(key);
304  }
305 
306  public static String getString(String key, String locale) {
307  return I_.compatibility().get(I18nLocales.createLocale(locale), key);
308  }
309 
310  public static String getString(String key, Locale locale) {
311  return I_.compatibility().get(locale, key);
312  }
313 
314  public static List<String> getAllLocales(String key) {
315  return I_.compatibility().getInLocales(key);
316  }
317 
318  public static ResourceBundle getBundle(String bundle, Locale locale, String configured) {
319  return I_.compatibilityMap(locale).asBundle();
320  }
321 
322  /* Context delegate */
323 
324  public String getServerBase() {
325  return ElephantContext.getServerBase("http");
326  }
327 
328  public String getServerUrl() {
329  return ElephantContext.getServerUrl("http");
330  }
331 
332  /* Session Resolvers */
333 
334  public static HttpServletRequest getResolverRequest() {
335  for(ISessionResolver iSessionResolver : Instances.cached().byAnnotation(ElephantSessionResolver.class, ISessionResolver.class)) {
336  HttpServletRequest request = iSessionResolver.getRequest();
337  if(request != null) {
338  return request;
339  }
340  }
341  return null;
342  }
343 
344  public static HttpServletResponse getResolverResponse() {
345  for(ISessionResolver iSessionResolver : Instances.cached().byAnnotation(ElephantSessionResolver.class, ISessionResolver.class)) {
346  HttpServletResponse response = iSessionResolver.getResponse();
347  if(response != null) {
348  return response;
349  }
350  }
351  return null;
352  }
353 
354  public static HttpSession getResolverSession(boolean create) {
355  for(ISessionResolver iSessionResolver : Instances.cached().byAnnotation(ElephantSessionResolver.class, ISessionResolver.class)) {
356  HttpSession session = iSessionResolver.getSession(create);
357  if(session != null) {
358  return session;
359  }
360  }
361  return null;
362  }
363 
364 }
static String decryptCookieValue(String name)
static HttpSession getResolverSession(boolean create)
IImplementation getImplementation(String name)
abstract IConstructor doConstructor()
static ResourceBundle getBundle(String bundle, Locale locale, String configured)
abstract HttpServletResponse getHttpServletResponse()
static String getCookieValue(String name)
static List< String > getAllLocales(String key)
static HttpServletRequest getResolverRequest()
static String getString(String key, Locale locale)
abstract void sendRedirect(String uri)
static Application getFromConstructor(IConstructor constructor)
static String getString(String key, String locale)
static boolean loadResources(String path)
void putPreference(String key, Object value)
static void encryptCookie(String name, String value, String path, int age)
static HttpServletResponse getResolverResponse()
boolean isInRole(String role, String redirect)
HttpSession getHttpSession(boolean create)
static Cookie getCookie(String name)
static Map getStringMap(Locale locale, String configured)
List getImplementationNamesByType(String type)
static String getString(String key)
static void deleteCookie(String name, String path)
static void setCookie(String name, String value, String path, int age)
abstract HttpServletRequest getHttpServletRequest()
IContact checkUser(boolean doLogin)
static ThreadLocal< Application > app
static String getServerBase(String scheme)
static String getServerUrl(String scheme)
static void redirectToLogin(IConstructor constructor)
static Object getImplementation(IElement iel, String name)
static void resetImplementationsStartingWith(String name)
static UserPreferences getInstance(String id)
static Cookie getCookie(HttpServletRequest request, String name)
Definition: CookieUtil.java:36
static void setCookie(HttpServletResponse response, String name, String value, String path, int age)
Definition: CookieUtil.java:45
static byte[] decryptCookieValue(Cookie cookie)
Definition: CookieUtil.java:69
static void encryptCookie(HttpServletResponse response, String name, byte[] value, String path, int age)
Definition: CookieUtil.java:60
static void deleteCookie(HttpServletResponse response, String name, String path)
Definition: CookieUtil.java:53
static I18nApiWrapper api()
Definition: I_.java:65
static I18nCompatibilityWrapper compatibility()
Definition: I_.java:109
static void resetMaps()
Definition: I_.java:196
static I18nCompatibilityMap compatibilityMap()
Definition: I_.java:117