19 package org.turro.action;
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;
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;
57 private static final Charset charset = StandardCharsets.UTF_8;
59 private static final String
60 ACTION_PAR =
"exacton",
61 RIGHTNOW_PAR =
"exrino",
62 DVALID_PAR =
"dvalid",
65 public static final String
68 COMMAND_PAR =
"command";
72 if(!Strings.isBlank(exacton)) {
79 }
catch (IOException | ParserException ex) {
93 return ACTION_PAR +
"=" + exacton;
98 public static String
createAction(Map<String, String> values,
int daysValid,
boolean ajax) {
100 if(exacton !=
null) {
101 return ACTION_PAR +
"=" + exacton;
106 public static String
createAction(Map<String, String> values,
boolean ajax) {
108 if(exacton !=
null) {
109 return ACTION_PAR +
"=" + exacton;
116 if(exacton !=
null) {
117 return ACTION_PAR +
"=" + exacton;
124 HashMap<String, String> values =
new HashMap<>();
126 values.put(
Actions.REDIR_PAR, URLEncoder.encode(redir, charset.name()));
128 }
catch (UnsupportedEncodingException ex) {
129 Logger.getLogger(
Actions.class.getName()).log(Level.SEVERE,
null, ex);
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));
141 values.put(AJAX_PAR,
"true");
147 String action = KeyValueMap.format(values);
151 }
catch (UnsupportedEncodingException ex) {
162 }
catch (UnsupportedEncodingException ex) {
174 boolean authenticated =
false;
176 String email = values.get(
USER_PAR);
177 if(!Strings.isBlank(email) && logged !=
null) {
181 Date valid = (Date) ObjectString.parseString(values.get(DVALID_PAR), ObjectString.COMPRESSED_DATE_PATTERN, Date.class,
false);
183 if(valid.before(
new Date())) {
187 if(!Strings.isBlank(email)) {
206 String redir = values.get(REDIR_PAR);
207 if(!Strings.isBlank(redir)) {
208 HttpServletResponse response = constructor.getResponse();
209 response.setCharacterEncoding(charset.name());
211 encodePars(URLDecoder.decode(redir, charset.name()), charset.name() + addParameters(values)));
214 String ajax = values.get(AJAX_PAR);
215 if(
"true".equals(ajax)) {
221 private static String addParameters(KeyValueMap values) {
223 for(String key : values.keySet()) {
224 if(!internalPar(key)) {
225 if(Strings.isBlank(result)) {
226 result =
"?" + key +
"=" + values.get(key);
228 result =
"&" + key +
"=" + values.get(key);
235 private static boolean internalPar(String par) {
237 DVALID_PAR.equals(par) ||
238 AJAX_PAR.equals(par) ||
239 REDIR_PAR.equals(par);
246 .stream().anyMatch((iAction) -> (iAction.execute(constructor, values)))) {
253 String exacton = constructor.
getParameter(ACTION_PAR,
false);
254 if(!Strings.isBlank(exacton)) {
257 Date valid = (Date) ObjectString.parseString(values.get(DVALID_PAR), ObjectString.COMPRESSED_DATE_PATTERN, Date.class,
false);
259 if(valid.before(
new Date())) {
266 }
catch (ParserException ex) {
273 private static boolean isReminder(KeyValueMap values) {
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);
289 return !Strings.isBlank(constructor.
getParameter(RIGHTNOW_PAR,
true));
295 }
catch (ParserException ex) {
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));
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));
315 }
catch (ParserException ex) {
316 Logger.getLogger(
Actions.class.getName()).log(Level.SEVERE,
null, ex);
325 return RIGHTNOW_PAR +
"=" + URLEncoder.encode(exrino, charset.name());
326 }
catch (UnsupportedEncodingException ex) {
342 String exrino = constructor.
getParameter(RIGHTNOW_PAR,
true);
343 if(!Strings.isBlank(exrino)) {
346 Date valid = (Date) ObjectString.parseString(values.get(DVALID_PAR), ObjectString.COMPRESSED_DATE_PATTERN, Date.class,
false);
348 if(valid.before(
new Date())) {
355 }
catch (ParserException ex) {
356 Logger.getLogger(
Actions.class.getName()).log(Level.SEVERE,
null, ex);
362 private static String charset(String value) {
366 private static String encodePars(String url, String charset) {
368 int p = url.indexOf(
"?");
370 KeyValueMap map =
new KeyValueMap(url.substring(p + 1),
"&",
"=");
371 url = url.substring(0, p + 1) + map.getEncoded(charset);
374 }
catch (UnsupportedEncodingException | ParserException ex) {
375 Logger.getLogger(Actions.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(
null), ex);
static String createAction(String email, String redir)
static String createRightNowParameter(String values)
static boolean executeAction(IConstructor constructor)
static boolean isRightNowAction(IConstructor constructor)
static String createActionFromElephant(String email, String redir)
static String createParameter(String email, String redir)
static String createParameter(Map< String, String > values, boolean ajax)
static String createAction(Map< String, String > values)
static final String USER_PAR
static String createRightNowParameter(Map< String, String > values)
static boolean executeExternalActions(IConstructor constructor, KeyValueMap values)
static KeyValueMap getRightNowAction(IConstructor constructor)
static String createPars(String values, int days)
static boolean checkEnding(IConstructor constructor, KeyValueMap values)
static String createPars(Map< String, String > values, int days)
static KeyValueMap getAction(IConstructor constructor)
static Map< String, String > addEmailAndDays(Map< String, String > values, String email, int days)
static String createParameter(String action)
static String createParameter(Map< String, String > values)
static String createAction(Map< String, String > values, boolean ajax)
static String createRightNowAction(String values)
static String createParameter(Map< String, String > values, int daysValid, boolean ajax)
static boolean prepareActions(IConstructor constructor, KeyValueMap values)
static Charset getCharset()
static String createAction(Map< String, String > values, int daysValid, boolean ajax)
static String createRightNowAction(Map< String, String > values)
static Application getApplication()
static String getRootWebPath()
static String logMsg(String msg)
static boolean getLiveLinks()
static byte[] encrypt(byte[] value)
static String getEncoding()
static byte[] decrypt(byte[] value)
static ISystemLogger getInstance()
static IElephantSSO getSSO()
HttpServletResponse getResponse()
HttpServletRequest getRequest()
String getParameter(String param)
void setSessionAttribute(String key, Object value)
Object getSessionAttribute(String key)
static final String INTERNAL_SIGNIN
static final String CONNECTOR_EMAIL
String getProperty(String key)
static final String LOGGED_USER
boolean impersonateByEmail(String email)
void createAssertion(HttpServletRequest request, HttpServletResponse response, IContact contact)
void doLog(SystemLogType type, Object entity, String comment, Serializable data)