BrightSide Workbench Full Report + Source Code
ZkossElephant.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.zkoss;
19 
20 import java.io.IOException;
21 import java.util.logging.Level;
22 import java.util.logging.Logger;
23 import javax.servlet.http.HttpServletRequest;
24 import javax.servlet.http.HttpServletResponse;
25 import javax.servlet.http.HttpSession;
26 import org.turro.string.Strings;
27 import org.turro.action.Contacts;
28 import org.turro.auth.Authentication;
29 import org.turro.elephant.ApplicationFramework;
30 import org.turro.elephant.context.Application;
31 import org.turro.elephant.context.ElephantContext;
32 import org.turro.elephant.context.History;
33 import org.turro.elephant.context.IConstructor;
34 import org.turro.elephant.security.IUser;
35 import org.turro.log.SystemLogType;
36 import org.turro.log.SystemLogger;
37 import org.turro.plugin.contacts.IContact;
38 import org.zkoss.web.fn.ServletFns;
39 import org.zkoss.zk.ui.Execution;
40 import org.zkoss.zk.ui.Executions;
41 import org.zkoss.zk.ui.Sessions;
42 
47 public class ZkossElephant extends Application {
48 
49  private ZkossElephant() {}
50 
51  public ZkossElephant(HttpServletRequest request, HttpServletResponse response) {
52  super();
53  this.request = request;
54  this.response = response;
55  request.setAttribute("el_app", this);
56  ApplicationFramework.setLocale((HttpServletRequest) request);
57  }
58 
59  public static Application getCurrent() {
60  Application application = app.get();
61  if(application == null) {
62  try {
63  application = (Application) ServletFns.getCurrentRequest().getAttribute("el_app");
64  if(application == null) {
65  application = new ZkossElephant((HttpServletRequest) ServletFns.getCurrentRequest(), (HttpServletResponse) ServletFns.getCurrentResponse());
66  ServletFns.getCurrentRequest().setAttribute("el_app", application);
67  }
68  app.set(application);
69  } catch(Exception ex) {
70  return null;
71  }
72  }
73  return application;
74  }
75 
76  @Override
81  constructor.setRequestAttribute("el_cons", constructor);
82  return constructor;
83  }
84 
85  @Override
86  public HttpServletRequest getHttpServletRequest() {
87  Execution execution = Executions.getCurrent();
88  if(execution != null) {
89  return (HttpServletRequest) execution.getNativeRequest(); //ServletFns.getCurrentRequest();
90  } else {
91  return request;
92  }
93  }
94 
95  @Override
96  public HttpServletResponse getHttpServletResponse() {
97  Execution execution = Executions.getCurrent();
98  if(execution != null) {
99  return (HttpServletResponse) execution.getNativeResponse(); //ServletFns.getCurrentResponse();
100  } else {
101  return response;
102  }
103  }
104 
105  @Override
106  public HttpSession getHttpSession(boolean create) {
107  HttpSession ses = super.getHttpSession(create);
108  if(ses == null) {
109  ses = (HttpSession) Sessions.getCurrent(create).getNativeSession();
110  }
111  return ses;
112  }
113 
114  public static void doWebAdmin(boolean value, String redir) throws IOException {
116  getCurrent().getHttpSession(false).setMaxInactiveInterval(3 * 60 * 60);
117  if(value) {
118  SystemLogger.getInstance().doLog(SystemLogType.LOG_INFO, "/log/webadmin", null, null);
119  }
120  Executions.sendRedirect(redir == null ? "" : redir);
121  }
122 
123  public static boolean doSendReminder(String login) throws IOException {
124  IConstructor constructor = getCurrent().getConstructor();
125  IContact user = Contacts.getEmpty();
126  if(user.impersonate(login)) {
127  return Authentication.sendReminder(constructor, user.getName(), user.getProperty(IUser.CONNECTOR_EMAIL));
128  } else if(user.impersonateByEmail(login)) {
129  return Authentication.sendReminder(constructor, user.getName(), user.getProperty(IUser.CONNECTOR_EMAIL));
130  }
131  return false;
132  }
133 
134  @Override
135  public void sendRedirect(String uri) {
136  if(Executions.getCurrent() != null) {
137  Executions.sendRedirect(uri);
138  } else {
139  try {
140  getConstructor().redirect(uri);
141  } catch (IOException ex) {
142  Logger.getLogger(ZkossElephant.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
143  }
144  }
145  }
146 
147  @Override
148  public void navigateBack() {
149  IConstructor constructor = getConstructor();
150  if(constructor != null) {
151  if(Executions.getCurrent() != null) {
152  Executions.sendRedirect(Strings.isBlank(History.back(constructor), "/"));
153  } else {
154  try {
155  constructor.redirect(Strings.isBlank(History.back(constructor), "/"));
156  } catch (IOException ex) {
157  Logger.getLogger(ZkossElephant.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
158  }
159  }
160  }
161  }
162 
163  /* Factory */
164 
165  public static ZkossElephant instance(HttpServletRequest request, HttpServletResponse response) {
166  return new ZkossElephant(request, response);
167  }
168 
169 }
static IContact getEmpty()
Definition: Contacts.java:56
static boolean sendReminder(IConstructor constructor, String name, String email)
static void setLocale(HttpServletRequest request)
HttpSession getHttpSession(boolean create)
static ThreadLocal< Application > app
static IConstructor getConstructor(HttpServletRequest request, HttpServletResponse response)
static String back(IConstructor constructor)
Definition: History.java:46
static boolean doSendReminder(String login)
HttpSession getHttpSession(boolean create)
ZkossElephant(HttpServletRequest request, HttpServletResponse response)
static void doWebAdmin(boolean value, String redir)
HttpServletResponse getHttpServletResponse()
HttpServletRequest getHttpServletRequest()
static ZkossElephant instance(HttpServletRequest request, HttpServletResponse response)
static ISystemLogger getInstance()
void setRequestAttribute(String key, Object value)
static final String CONNECTOR_EMAIL
Definition: IUser.java:27
boolean impersonate(String login)
String getProperty(String key)
boolean impersonateByEmail(String email)
void doLog(SystemLogType type, Object entity, String comment, Serializable data)