BrightSide Workbench Full Report + Source Code
Actions.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2014 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 
19 package org.turro.action;
20 
21 import java.io.IOException;
22 import java.io.UnsupportedEncodingException;
23 import java.net.URLDecoder;
24 import java.net.URLEncoder;
25 import java.nio.charset.Charset;
26 import java.nio.charset.StandardCharsets;
27 import java.util.Date;
28 import java.util.HashMap;
29 import java.util.Map;
30 import java.util.Objects;
31 import java.util.logging.Level;
32 import java.util.logging.Logger;
33 import javax.servlet.http.HttpServletResponse;
34 import org.amic.util.date.CheckDate;
35 import org.turro.collections.parser.ParserException;
36 import org.turro.string.ObjectString;
37 import org.turro.string.Strings;
38 import org.turro.annotation.ExternalAction;
39 import org.turro.collections.KeyValueMap;
40 import org.turro.elephant.context.Application;
41 import org.turro.elephant.context.ElephantContext;
42 import org.turro.elephant.context.IConstructor;
43 import org.turro.elephant.security.IUser;
44 import org.turro.log.SystemLogType;
45 import org.turro.log.SystemLogger;
46 import org.turro.plugin.contacts.IContact;
47 import org.turro.reflection.Instances;
48 import org.turro.sso.SSO;
49 import org.turro.util.CompareUtil;
50 
55 public class Actions {
56 
57  private static final Charset charset = StandardCharsets.UTF_8;
58 
59  private static final String
60  ACTION_PAR = "exacton",
61  RIGHTNOW_PAR = "exrino",
62  DVALID_PAR = "dvalid",
63  AJAX_PAR = "ajax";
64 
65  public static final String
66  USER_PAR = "user",
67  REDIR_PAR = "redir",
68  COMMAND_PAR = "command";
69 
70  public static boolean executeAction(IConstructor constructor) {
71  String exacton = constructor.getParameter(ACTION_PAR);
72  if(!Strings.isBlank(exacton)) {
73  try {
74  KeyValueMap kvm = new KeyValueMap(ElephantContext.decrypt(exacton));
75  if(prepareActions(constructor, kvm)) {
76  executeExternalActions(constructor, kvm);
77  return checkEnding(constructor, kvm);
78  }
79  } catch (IOException | ParserException ex) {
80  Logger.getLogger(Actions.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
81  }
82  }
83  return false;
84  }
85 
86  public static String createActionFromElephant(String email, String redir) {
87  return createAction(email, Strings.convert(redir, charset(redir), Actions.getCharset().name()));
88  }
89 
90  public static String createAction(String email, String redir) {
91  String exacton = createParameter(email, redir);
92  if(exacton != null) {
93  return ACTION_PAR + "=" + exacton;
94  }
95  return null;
96  }
97 
98  public static String createAction(Map<String, String> values, int daysValid, boolean ajax) {
99  String exacton = createParameter(values, daysValid, ajax);
100  if(exacton != null) {
101  return ACTION_PAR + "=" + exacton;
102  }
103  return null;
104  }
105 
106  public static String createAction(Map<String, String> values, boolean ajax) {
107  String exacton = createParameter(values, ajax);
108  if(exacton != null) {
109  return ACTION_PAR + "=" + exacton;
110  }
111  return null;
112  }
113 
114  public static String createAction(Map<String, String> values) {
115  String exacton = createParameter(values);
116  if(exacton != null) {
117  return ACTION_PAR + "=" + exacton;
118  }
119  return null;
120  }
121 
122  public static String createParameter(String email, String redir) {
123  try {
124  HashMap<String, String> values = new HashMap<>();
125  values.put(Actions.USER_PAR, email);
126  values.put(Actions.REDIR_PAR, URLEncoder.encode(redir, charset.name()));
127  return Actions.createParameter(values, 8, false);
128  } catch (UnsupportedEncodingException ex) {
129  Logger.getLogger(Actions.class.getName()).log(Level.SEVERE, null, ex);
130  }
131  return null;
132  }
133 
134  public static String createParameter(Map<String, String> values, int daysValid, boolean ajax) {
135  values.put(DVALID_PAR, ObjectString.formatObject(new CheckDate().addDays(daysValid).getDate(), ObjectString.COMPRESSED_DATE_PATTERN, false));
136  return createParameter(values, ajax);
137  }
138 
139  public static String createParameter(Map<String, String> values, boolean ajax) {
140  if(ajax) {
141  values.put(AJAX_PAR, "true");
142  }
143  return createParameter(values);
144  }
145 
146  public static String createParameter(Map<String, String> values) {
147  String action = KeyValueMap.format(values);
148  if(action != null) {
149  try {
150  return URLEncoder.encode(ElephantContext.encrypt(action), charset.name());
151  } catch (UnsupportedEncodingException ex) {
152  Logger.getLogger(Actions.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
153  }
154  }
155  return null;
156  }
157 
158  public static String createParameter(String action) {
159  if(action != null) {
160  try {
161  return URLEncoder.encode(ElephantContext.encrypt(action), charset.name());
162  } catch (UnsupportedEncodingException ex) {
163  Logger.getLogger(Actions.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
164  }
165  }
166  return null;
167  }
168 
169  public static Charset getCharset() {
170  return charset;
171  }
172 
173  public static boolean prepareActions(IConstructor constructor, KeyValueMap values) {
174  boolean authenticated = false;
175  IUser logged = constructor.getUser();
176  String email = values.get(USER_PAR);
177  if(!Strings.isBlank(email) && logged != null) {
178  authenticated = CompareUtil.compare(email, logged.getProperty(IUser.CONNECTOR_EMAIL)) == 0;
179  }
180  if(!authenticated && (isReminder(values) || ElephantContext.getLiveLinks())) {
181  Date valid = (Date) ObjectString.parseString(values.get(DVALID_PAR), ObjectString.COMPRESSED_DATE_PATTERN, Date.class, false);
182  if(valid != null) {
183  if(valid.before(new Date())) {
184  return false;
185  }
186  }
187  if(!Strings.isBlank(email)) {
188  IContact user = Contacts.getEmpty();
189  if(!user.impersonateByEmail(email)) {
190  return false;
191  }
192  constructor.setSessionAttribute(IUser.INTERNAL_SIGNIN, Boolean.TRUE);
193  constructor.setSessionAttribute(IUser.LOGGED_USER, user);
195  SystemLogger.getInstance().doLog(SystemLogType.LOG_INFO, "/log/in", "Live Link", values.toString());
196  if(SSO.hasSSO()) {
197  SSO.getSSO().createAssertion(constructor.getRequest(), constructor.getResponse(),
199  }
200  }
201  }
202  return true;
203  }
204 
205  public static boolean checkEnding(IConstructor constructor, KeyValueMap values) throws IOException {
206  String redir = values.get(REDIR_PAR);
207  if(!Strings.isBlank(redir)) {
208  HttpServletResponse response = constructor.getResponse();
209  response.setCharacterEncoding(charset.name());
210  response.sendRedirect(ElephantContext.getRootWebPath() +
211  encodePars(URLDecoder.decode(redir, charset.name()), charset.name() + addParameters(values)));
212  return true;
213  }
214  String ajax = values.get(AJAX_PAR);
215  if("true".equals(ajax)) {
216  return true;
217  }
218  return false;
219  }
220 
221  private static String addParameters(KeyValueMap values) {
222  String result = "";
223  for(String key : values.keySet()) {
224  if(!internalPar(key)) {
225  if(Strings.isBlank(result)) {
226  result = "?" + key + "=" + values.get(key);
227  } else {
228  result = "&" + key + "=" + values.get(key);
229  }
230  }
231  }
232  return result;
233  }
234 
235  private static boolean internalPar(String par) {
236  return USER_PAR.equals(par) ||
237  DVALID_PAR.equals(par) ||
238  AJAX_PAR.equals(par) ||
239  REDIR_PAR.equals(par);
240  }
241 
242  /* External Actions */
243 
244  public static boolean executeExternalActions(IConstructor constructor, KeyValueMap values) {
245  if (Instances.cached().byAnnotation(ExternalAction.class, IAction.class)
246  .stream().anyMatch((iAction) -> (iAction.execute(constructor, values)))) {
247  return true;
248  }
249  return false;
250  }
251 
252  public static KeyValueMap getAction(IConstructor constructor) {
253  String exacton = constructor.getParameter(ACTION_PAR, false);
254  if(!Strings.isBlank(exacton)) {
255  try {
256  KeyValueMap values = new KeyValueMap(ElephantContext.decrypt(exacton));
257  Date valid = (Date) ObjectString.parseString(values.get(DVALID_PAR), ObjectString.COMPRESSED_DATE_PATTERN, Date.class, false);
258  if(valid != null) {
259  if(valid.before(new Date())) {
260  return null;
261  }
262  } else {
263  return null;
264  }
265  return values;
266  } catch (ParserException ex) {
267  Logger.getLogger(Actions.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
268  }
269  }
270  return null;
271  }
272 
273  private static boolean isReminder(KeyValueMap values) {
274  try {
275  String redir = values.get(REDIR_PAR);
276  return Strings.isBlank(redir) ? false : Objects.equals("/user/changepass", URLDecoder.decode(redir, charset.name()));
277  } catch (UnsupportedEncodingException ex) {
278  Logger.getLogger(Actions.class.getName()).log(Level.SEVERE, null, ex);
279  }
280  return false;
281  }
282 
283  private Actions() {
284  }
285 
286  /* RigthNow Actions */
287 
288  public static boolean isRightNowAction(IConstructor constructor) {
289  return !Strings.isBlank(constructor.getParameter(RIGHTNOW_PAR, true));
290  }
291 
292  public static String createPars(String values, int days) {
293  try {
294  return createRightNowParameter(new KeyValueMap(values));
295  } catch (ParserException ex) {
296  Logger.getLogger(Actions.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
297  }
298  return null;
299  }
300 
301  public static String createPars(Map<String, String> values, int days) {
302  values.put(DVALID_PAR, ObjectString.formatObject(new CheckDate().addDays(days).getDate(), ObjectString.COMPRESSED_DATE_PATTERN, false));
303  return createParameter(values);
304  }
305 
306  public static Map<String, String> addEmailAndDays(Map<String, String> values, String email, int days) {
307  values.put(DVALID_PAR, ObjectString.formatObject(new CheckDate().addDays(days).getDate(), ObjectString.COMPRESSED_DATE_PATTERN, false));
308  values.put(USER_PAR, email);
309  return values;
310  }
311 
312  public static String createRightNowAction(String values) {
313  try {
314  return createRightNowAction(new KeyValueMap(values));
315  } catch (ParserException ex) {
316  Logger.getLogger(Actions.class.getName()).log(Level.SEVERE, null, ex);
317  }
318  return null;
319  }
320 
321  public static String createRightNowAction(Map<String, String> values) {
322  String exrino = createRightNowParameter(values);
323  if(exrino != null) {
324  try {
325  return RIGHTNOW_PAR + "=" + URLEncoder.encode(exrino, charset.name());
326  } catch (UnsupportedEncodingException ex) {
327  Logger.getLogger(Actions.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
328  }
329  }
330  return null;
331  }
332 
333  public static String createRightNowParameter(String values) {
334  return createPars(values, 1);
335  }
336 
337  public static String createRightNowParameter(Map<String, String> values) {
338  return createPars(values, 1);
339  }
340 
341  public static KeyValueMap getRightNowAction(IConstructor constructor) {
342  String exrino = constructor.getParameter(RIGHTNOW_PAR, true);
343  if(!Strings.isBlank(exrino)) {
344  try {
345  KeyValueMap values = new KeyValueMap(ElephantContext.decrypt(exrino));
346  Date valid = (Date) ObjectString.parseString(values.get(DVALID_PAR), ObjectString.COMPRESSED_DATE_PATTERN, Date.class, false);
347  if(valid != null) {
348  if(valid.before(new Date())) {
349  return null;
350  }
351  } else {
352  return null;
353  }
354  return values;
355  } catch (ParserException ex) {
356  Logger.getLogger(Actions.class.getName()).log(Level.SEVERE, null, ex);
357  }
358  }
359  return null;
360  }
361 
362  private static String charset(String value) {
363  return Strings.charset(value, new String[] { ElephantContext.getEncoding(), charset.name() });
364  }
365 
366  private static String encodePars(String url, String charset) {
367  try {
368  int p = url.indexOf("?");
369  if(p > 1) {
370  KeyValueMap map = new KeyValueMap(url.substring(p + 1), "&", "=");
371  url = url.substring(0, p + 1) + map.getEncoded(charset);
372  }
373  return url;
374  } catch (UnsupportedEncodingException | ParserException ex) {
375  Logger.getLogger(Actions.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
376  }
377  return null;
378  }
379 
380 }
static String createAction(String email, String redir)
Definition: Actions.java:90
static String createRightNowParameter(String values)
Definition: Actions.java:333
static boolean executeAction(IConstructor constructor)
Definition: Actions.java:70
static boolean isRightNowAction(IConstructor constructor)
Definition: Actions.java:288
static String createActionFromElephant(String email, String redir)
Definition: Actions.java:86
static String createParameter(String email, String redir)
Definition: Actions.java:122
static String createParameter(Map< String, String > values, boolean ajax)
Definition: Actions.java:139
static String createAction(Map< String, String > values)
Definition: Actions.java:114
static final String USER_PAR
Definition: Actions.java:66
static String createRightNowParameter(Map< String, String > values)
Definition: Actions.java:337
static boolean executeExternalActions(IConstructor constructor, KeyValueMap values)
Definition: Actions.java:244
static KeyValueMap getRightNowAction(IConstructor constructor)
Definition: Actions.java:341
static String createPars(String values, int days)
Definition: Actions.java:292
static boolean checkEnding(IConstructor constructor, KeyValueMap values)
Definition: Actions.java:205
static String createPars(Map< String, String > values, int days)
Definition: Actions.java:301
static KeyValueMap getAction(IConstructor constructor)
Definition: Actions.java:252
static Map< String, String > addEmailAndDays(Map< String, String > values, String email, int days)
Definition: Actions.java:306
static String createParameter(String action)
Definition: Actions.java:158
static String createParameter(Map< String, String > values)
Definition: Actions.java:146
static String createAction(Map< String, String > values, boolean ajax)
Definition: Actions.java:106
static String createRightNowAction(String values)
Definition: Actions.java:312
static String createParameter(Map< String, String > values, int daysValid, boolean ajax)
Definition: Actions.java:134
static boolean prepareActions(IConstructor constructor, KeyValueMap values)
Definition: Actions.java:173
static Charset getCharset()
Definition: Actions.java:169
static String createAction(Map< String, String > values, int daysValid, boolean ajax)
Definition: Actions.java:98
static String createRightNowAction(Map< String, String > values)
Definition: Actions.java:321
static IContact getEmpty()
Definition: Contacts.java:56
static IContact getLoggedIContact(Application app)
Definition: Contacts.java:122
static final String LOGGED_ICONTACT
Definition: Contacts.java:45
static ISystemLogger getInstance()
static IElephantSSO getSSO()
Definition: SSO.java:49
static boolean hasSSO()
Definition: SSO.java:34
void setSessionAttribute(String key, Object value)
static final String INTERNAL_SIGNIN
Definition: IUser.java:33
static final String CONNECTOR_EMAIL
Definition: IUser.java:27
String getProperty(String key)
static final String LOGGED_USER
Definition: IUser.java:32
boolean impersonateByEmail(String email)
void createAssertion(HttpServletRequest request, HttpServletResponse response, IContact contact)
void doLog(SystemLogType type, Object entity, String comment, Serializable data)