BrightSide Workbench Full Report + Source Code
MarkerHelper.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2016 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.marker;
20 
21 import java.io.File;
22 import java.io.IOException;
23 import java.io.PrintWriter;
24 import java.io.StringWriter;
25 import java.net.URLDecoder;
26 import java.net.URLEncoder;
27 import java.nio.charset.Charset;
28 import java.nio.charset.StandardCharsets;
29 import java.util.HashMap;
30 import java.util.Map;
31 import java.util.Optional;
32 import java.util.Set;
33 import java.util.UUID;
34 import java.util.logging.Level;
35 import java.util.logging.Logger;
36 import javax.servlet.ServletException;
37 import javax.servlet.http.HttpServletRequest;
38 import org.turro.collections.parser.ParserException;
39 import org.turro.string.Strings;
40 import org.turro.action.Actions;
41 import org.turro.action.ContentService;
42 import org.turro.action.EntityInfoContent;
43 import org.turro.action.EntityInfoType;
44 import org.turro.action.LinkType;
45 import org.turro.action.MailSenders;
46 import org.turro.action.Plugins;
47 import org.turro.collections.KeyValueMap;
48 import org.turro.elephant.context.Application;
49 import org.turro.elephant.context.ElephantContext;
50 import org.turro.elephant.context.IConstructor;
51 import org.turro.elephant.direct.DirectContents;
52 import org.turro.elephant.direct.IDirectContent;
53 import org.turro.elephant.impl.repository.Repository;
54 import org.turro.elephant.impl.util.Parser;
55 import org.turro.elephant.security.IUser;
56 import org.turro.elephant.user.menu.IUserMenu;
57 import org.turro.elephant.user.menu.UserMenus;
58 import org.turro.elephant.web.ElContext;
59 import org.turro.elephant.web.tags.SessionTags;
60 import org.turro.elephant.web.tags.TagsServlet;
61 import org.turro.file.Document;
62 import org.turro.help.HelpContext;
63 import org.turro.html.Colors;
64 import org.turro.html.HtmlContent;
65 import org.turro.log.WebLoggers;
66 import org.turro.plugin.contacts.IContact;
67 import org.turro.reflection.Reflections;
68 import org.turro.user.activity.ActivityType;
69 import org.turro.user.activity.UserActivityContent;
70 import org.turro.user.content.IElephantUserContent;
71 import org.turro.user.content.UserContents;
72 
77 public class MarkerHelper {
78 
79  private static final Charset charset = StandardCharsets.UTF_8;
80 
81  private final IConstructor constructor;
82 
83  public MarkerHelper(IConstructor constructor) {
84  this.constructor = constructor;
85  }
86 
87  public String getQRCodeFromCurrent() {
88  return getQRCodeFromUrl(constructor.getRequest().getRequestURL().toString());
89  }
90 
91  public String getQRCodeFromUrl(String url) {
92  HashMap map = new HashMap();
93  map.put("type", "qrcode");
94  map.put("data", url);
95  return getContent(new KeyValueMap(map));
96  }
97 
98  public String getImageFromUrl(String url) {
99  HashMap map = new HashMap();
100  map.put("type", "image");
101  map.put("data", url);
102  return getContent(new KeyValueMap(map));
103  }
104 
105  public String getContent(String values) {
106  return ContentService.createURL(constructor, values);
107  }
108 
109  public String getContent(KeyValueMap values) {
110  return ContentService.createURL(constructor, values);
111  }
112 
113  public String getEntryPoint(String entryPoint, String values) {
114  return ContentService.createEntryPoint(constructor, entryPoint, values);
115  }
116 
117  public String getEntryPoint(String entryPoint, KeyValueMap values) {
118  return ContentService.createEntryPoint(constructor, entryPoint, values);
119  }
120 
121  public Repository getRepository(String path) {
122  return new Repository(constructor, path);
123  }
124 
125  public String imageOrEmpty(String image) {
126  return imageExists(image) ? image : "/_zul/images/no_img.png";
127  }
128 
129  public String imageOrDefault(String image) {
130  return imageExists(image) ? image : "/_zul/images/noimg/noimage.png";
131  }
132 
133  public boolean imageExists(String image) {
134  if(Strings.isBlank(image)) return false;
135  image = image.contains("?") ? image.substring(0, image.indexOf("?")) : image;
136  return new File(ElephantContext.getRealPath(image)).exists();
137  }
138 
140  return DirectContents.getDirectContent("cookie-management");
141  }
142 
144  return DirectContents.getDirectContent("img-tk");
145  }
146 
148  return DirectContents.getDirectContent("push-notification");
149  }
150 
152  return DirectContents.getDirectContent("message-queue");
153  }
154 
155  public boolean needCookies() {
156  HttpServletRequest request = constructor.getRequest();
157  return request != null;
158  }
159 
160  public String processMacrosForWeb(String text) {
161  if(text.contains("{@")) {
162  StringWriter swriter = new StringWriter();
163  try(PrintWriter writer = new PrintWriter(swriter)) {
164  Parser.processMacros(constructor, writer, text);
165  text = swriter.toString();
166  } catch (IOException | ServletException ex) {
167  Logger.getLogger(MarkerHelper.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
168  }
169  }
170  return MailSenders.processMacros((IContact) constructor.getUser(), text, true);
171  }
172 
173  public Set<IUserMenu> getUserMenu() {
174  return UserMenus.getUserMenu(constructor);
175  }
176 
177  /* User Contents */
178 
179  public Set<IElephantUserContent> getUserContents() {
180  return UserContents.getContents(constructor, (IContact) constructor.getUser());
181  }
182 
183  public Set<String> getUserContentTags() {
184  return UserContents.getTags(constructor, (IContact) constructor.getUser());
185  }
186 
187  /* Anonymous */
188 
190  return Plugins.loadImplementation(IDirectContent.class, "anonymous-ctrl");
191  }
192 
193  /* Id's */
194 
195  public String getIdFrom(String value) {
196  return Strings.identifier(value);
197  }
198 
199  public String getUniqueId() {
200  return UUID.randomUUID().toString();
201  }
202 
203  /* Help */
204 
205  public String getHelpLink(ElContext context) {
206  return context != null ? HelpContext.helpLink(context.getPath()) : null;
207  }
208 
209  /* EntityInfo */
210 
211  public String getEntityInfoURL(String entityPath) {
212  return EntityInfoContent.createURL(EntityInfoType.FULL, LinkType.WEB) + "&path=" + entityPath;
213  }
214 
215  /* User activity */
216 
217  public String getUserActivityURL(IContact contact) {
218  return new UserActivityContent().createURL(contact, ActivityType.values());
219  }
220 
221  /* Plugins */
222 
223  public <T> T loadImplementation(String className) {
224  Class<T> jclass = Reflections.check(className);
225  return Plugins.loadImplementation(jclass);
226  }
227 
228  public <T> T loadImplementation(String className, String label) {
229  Class<T> jclass = Reflections.check(className);
230  return Plugins.loadImplementation(jclass, label);
231  }
232 
233  /* Web tags */
234 
235  public String webTags() {
236  return SessionTags.get(constructor).path();
237  }
238 
239  public String navigateToWebTag(String fromTag, String webTag) {
240  return TagsServlet.navigateFromTo(fromTag, webTag);
241  }
242 
243  public String navigateAndSetWebTag(String fromTag, String path) {
244  return TagsServlet.navigateAndSet(fromTag, path);
245  }
246 
247  /* Parameters */
248 
249  public static final String OBFUSCATED_PAR = "el_xpar";
250  private KeyValueMap _keyValueMap;
251 
252  public String obfuscateParameters(String parameters) {
253  return setObfuscatedPars(parameters);
254  }
255 
256  public String obfuscateRightNowPars(String parameters) {
257  return setObfuscatedRightNowPars(parameters);
258  }
259 
260  public String getObfuscatedParameter(String parameter) {
261  try {
262  return getMapFromObfuscated().get(parameter);
263  } catch (Exception ex) {
264  Logger.getLogger(MarkerHelper.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
265  }
266  return null;
267  }
268 
269  private KeyValueMap getMapFromObfuscated() {
270  if(_keyValueMap == null) {
271  String obfuscated = constructor.getParameter(OBFUSCATED_PAR, true);
272  if(!Strings.isBlank(obfuscated)) {
273  try {
274  _keyValueMap = new KeyValueMap(ElephantContext.decrypt(URLDecoder.decode(obfuscated, charset)));
275  } catch (Exception ex) {
276  WebLoggers.info(this).exception(ex).log();
277  }
278  }
279  }
280  return _keyValueMap;
281  }
282 
283  /* Trick */
284 
285  public String getTrick(int seconds) {
286  return FormTrick.createTrick(seconds);
287  }
288 
289  /* Helpers */
290 
291  public static String setObfuscatedParsPOST(String parameters) {
292  return OBFUSCATED_PAR + ":\"" + URLEncoder.encode(Actions.createPars(parameters, 30), charset) + "\"";
293  }
294 
295  public static String setObfuscatedPars(String parameters) {
296  return OBFUSCATED_PAR + "=" + URLEncoder.encode(Actions.createPars(parameters, 30), charset);
297  }
298 
299  public static String setObfuscatedPars(KeyValueMap parameters) {
300  return OBFUSCATED_PAR + "=" + URLEncoder.encode(Actions.createPars(parameters, 30), charset);
301  }
302 
303  public static String setObfuscatedRightNowPars(String parameters) {
304  return OBFUSCATED_PAR + "=" + URLEncoder.encode(Actions.createRightNowParameter(parameters), charset);
305  }
306 
307  public static String setObfuscatedRightNowPars(KeyValueMap parameters) {
308  return OBFUSCATED_PAR + "=" + URLEncoder.encode(Actions.createRightNowParameter(parameters), charset);
309  }
310 
311  public static KeyValueMap getObfuscatedParameters() {
313  }
314 
315  public static KeyValueMap getObfuscatedParameters(HttpServletRequest request) {
316  return getObfuscatedFrom(request.getParameter(OBFUSCATED_PAR));
317  }
318 
319  public static KeyValueMap getObfuscatedFrom(String obfuscated) {
320  if(!Strings.isBlank(obfuscated)) {
321  try {
322  return new KeyValueMap(ElephantContext.decrypt(URLDecoder.decode(obfuscated, charset)));
323  } catch (Exception ex) {
325  }
326  }
327  return null;
328  }
329 
330  public static KeyValueMap getAllParameters() {
331  HttpServletRequest request = Application.getApplication().getHttpServletRequest();
332  if(request != null) {
333  return getAllParameters(request.getParameterMap());
334  }
335  return new KeyValueMap(new HashMap());
336  }
337 
338  public static KeyValueMap getAllParameters(Map<String, String[]> parameterMap) {
339  final Map map = new HashMap();
340  if(parameterMap != null) {
341  parameterMap.keySet().forEach((key) -> {
342  if(OBFUSCATED_PAR.equals(key)) {
343  KeyValueMap kvm = MarkerHelper.getObfuscatedFrom(parameterMap.get(key)[0]);
344  if(kvm != null) {
345  map.putAll(kvm);
346  }
347  } else {
348  map.put(key, parameterMap.get(key)[0]);
349  }
350  });
351  }
352  return new KeyValueMap(map);
353  }
354 
355  public static String obsfuscatedPOST(String context, String values) {
356  return "$.post(\"" + context + "\", { " + setObfuscatedParsPOST(values) + " })";
357  }
358 
359  /* Direct Content URLs */
360 
361  public String getSiteAddress() {
362  return ElephantContext.getServerUrl("http");
363  }
364 
365  public static String createPOST(String id, String values) {
366  return DirectContents.doCreatePOST(id, values);
367  }
368 
369  public static String createPOST(String id, String domId, String values) {
370  return DirectContents.doCreatePOST(id, domId, values);
371  }
372 
373  public static String createURL(String id, String values) {
374  return DirectContents.doCreateURL(id, values);
375  }
376 
377  public static String createDirectURL(IConstructor constructor, IContact contact,
378  String directContentId, String values, int daysValid, boolean withDomain) {
379  String exrino = null;
380  try {
381  KeyValueMap kvm = new KeyValueMap(values);
383  kvm.put(Actions.REDIR_PAR, (withDomain ? ElephantContext.getServerBase("http") : "") + constructor.getCurrentContext().getWebPath());
384  exrino = Actions.createAction(kvm, daysValid, false);
385  } catch (ParserException ex) {
386  Logger.getLogger(MarkerHelper.class.getName()).log(Level.SEVERE, null, ex);
387  }
388  return (withDomain ? ElephantContext.getServerBase("http") : "") +
390  DirectContents.DIRECT_CONTENT_PATH + directContentId +
391  "?" + exrino;
392  }
393 
394  /* Truncate */
395 
396  public String truncate(String text, int max) {
397  return Strings.truncate(text, max);
398  }
399 
400  public String truncateHTML(String html, int max) {
401  return HtmlContent.truncate(html, max);
402  }
403 
404  public String truncateHTMLtoPlain(String html, int max) {
405  return HtmlContent.plain(html, max);
406  }
407 
408  /* Colors */
409 
410  public Colors.Color color(String color) {
411  return Optional.ofNullable(Colors.of(color)).map(c -> c.color()).orElse(null);
412  }
413 
414  /* Download */
415 
416  public String downloadPath(Document document) {
417  return ElephantContext.getRootWebPath() + "/down_" +
419  ElephantContext.getRelativePath(document.path().toString()));
420  }
421 
422 }
static String createAction(String email, String redir)
Definition: Actions.java:90
static String createRightNowParameter(String values)
Definition: Actions.java:333
static final String USER_PAR
Definition: Actions.java:66
static String createPars(String values, int days)
Definition: Actions.java:292
static String createEntryPoint(IConstructor constructor, String entryPoint, String valueStr)
static String createURL(IConstructor constructor, KeyValueMap values)
static String createURL(EntityInfoType type, LinkType link)
static String processMacros(IContact contact, String text, boolean web)
static< T > T loadImplementation(Class< T > jclass)
Definition: Plugins.java:57
abstract HttpServletRequest getHttpServletRequest()
static String getServerBase(String scheme)
static String getServerUrl(String scheme)
static String doCreateURL(String id, String values)
static IDirectContent getDirectContent(String id)
static String doCreatePOST(String id, String values)
static void processMacros(IConstructor constructor, PrintWriter out, String text)
Definition: Parser.java:200
static Set< IUserMenu > getUserMenu(IConstructor constructor)
Definition: UserMenus.java:42
static String navigateAndSet(String fromTag, String path)
static String navigateFromTo(String fromTag, String toTag)
static String helpLink(String current)
WebLoggers exception(Throwable throwable)
Definition: WebLoggers.java:29
static WebLoggers info(Object entity)
Definition: WebLoggers.java:43
static String createTrick(int seconds)
Definition: FormTrick.java:35
static String obsfuscatedPOST(String context, String values)
String getEntityInfoURL(String entityPath)
IDirectContent getPushNotification()
static String setObfuscatedPars(KeyValueMap parameters)
String truncateHTML(String html, int max)
String navigateAndSetWebTag(String fromTag, String path)
static final String OBFUSCATED_PAR
MarkerHelper(IConstructor constructor)
static KeyValueMap getObfuscatedParameters()
String processMacrosForWeb(String text)
Colors.Color color(String color)
String getUserActivityURL(IContact contact)
String obfuscateRightNowPars(String parameters)
String getHelpLink(ElContext context)
static String createPOST(String id, String domId, String values)
String getIdFrom(String value)
static String setObfuscatedRightNowPars(String parameters)
Set< IUserMenu > getUserMenu()
String imageOrDefault(String image)
boolean imageExists(String image)
static String createDirectURL(IConstructor constructor, IContact contact, String directContentId, String values, int daysValid, boolean withDomain)
String imageOrEmpty(String image)
static String setObfuscatedRightNowPars(KeyValueMap parameters)
static KeyValueMap getAllParameters()
String getTrick(int seconds)
static String createURL(String id, String values)
static String setObfuscatedPars(String parameters)
IDirectContent getMessageQueue()
String getContent(String values)
String truncate(String text, int max)
String getImageFromUrl(String url)
String getObfuscatedParameter(String parameter)
String getContent(KeyValueMap values)
String truncateHTMLtoPlain(String html, int max)
String navigateToWebTag(String fromTag, String webTag)
Set< IElephantUserContent > getUserContents()
String getEntryPoint(String entryPoint, KeyValueMap values)
String getEntryPoint(String entryPoint, String values)
Set< String > getUserContentTags()
static String setObfuscatedParsPOST(String parameters)
static KeyValueMap getAllParameters(Map< String, String[]> parameterMap)
static KeyValueMap getObfuscatedParameters(HttpServletRequest request)
static KeyValueMap getObfuscatedFrom(String obfuscated)
Repository getRepository(String path)
String obfuscateParameters(String parameters)
static String createPOST(String id, String values)
String getQRCodeFromUrl(String url)
String downloadPath(Document document)
String createURL(IContact contact, ActivityType... types)
static Set< IElephantUserContent > getContents(IConstructor constructor, IContact contact)
static Set< String > getTags(IConstructor constructor, IContact contact)
static final String CONNECTOR_EMAIL
Definition: IUser.java:27