19 package org.turro.elephant.context;
22 import java.io.IOException;
24 import java.net.URISyntaxException;
25 import java.util.ArrayList;
26 import java.util.Currency;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Locale;
30 import java.util.logging.Level;
31 import java.util.logging.Logger;
32 import javax.servlet.ServletContext;
33 import javax.servlet.ServletContextEvent;
34 import javax.servlet.ServletContextListener;
35 import javax.servlet.annotation.WebListener;
36 import javax.servlet.http.HttpServletRequest;
37 import javax.servlet.http.HttpServletResponse;
38 import org.turro.string.Strings;
39 import org.apache.commons.codec.binary.Base64;
40 import org.jdom.Document;
41 import org.jdom.Element;
42 import org.jdom.JDOMException;
43 import org.jdom.input.SAXBuilder;
44 import org.jdom.output.XMLOutputter;
45 import org.turro.annotation.ElephantConstructor;
46 import org.turro.collections.CollectionUtil;
47 import org.turro.config.Configurator;
48 import org.turro.elephant.impl.security.Cipher;
49 import org.turro.file.Folder;
50 import org.turro.json.Jsons;
51 import org.turro.path.Path;
52 import org.turro.reflection.Instances;
61 private static ServletContext _context;
62 private static ArrayList<String> _metas;
77 _context =
event.getServletContext();
78 loadSiteConfiguration();
109 if(!Strings.isBlank(
remove) && path.startsWith(
remove)) {
110 return path.substring(
remove.length());
118 real = real.replaceAll(
"\\\\",
"/");
119 if(real.endsWith(
"/"))
120 real = real.substring(0, real.length() - 1);
127 if(path.startsWith(
remove)) {
128 return path.substring(
remove.length());
138 return _context.getContextPath();
146 return _context.getAttribute(key);
150 _context.setAttribute(key, value);
154 _context.removeAttribute(key);
214 return (String)
getAttribute(
"site_directory-qualification");
222 return "true".equals((String)
getAttribute(
"site_use-ssl"));
226 return "true".equals((String)
getAttribute(
"site_use-sso"));
230 return "true".equals((String)
getAttribute(
"site_live-links"));
238 return Currency.getInstance((String)
getAttribute(
"contact_currency"));
243 "true".equals((String)
getAttribute(
"site_has_signup"));
247 if(scheme.equals(
"http") &&
getUseSSL()) {
250 String server = schemes.get(scheme);
261 public static URI
toUri(String localPath)
throws URISyntaxException {
270 return (value !=
null && ((value.length % 8) == 0)) ?
275 return !Strings.isBlank(value) ?
new String(
276 new Base64().encode(
encrypt(value.getBytes()))) :
"";
280 if(!Strings.isBlank(value)) {
281 byte[] bytes =
decrypt(
new Base64().decode(value.getBytes()));
282 return bytes !=
null ?
new String(bytes) :
"";
287 public static File
file(String path) {
291 public static String
logMsg(String msg) {
292 return "[" +
getSiteName() +
"]" + (Strings.isBlank(msg) ?
"" :
": " + msg);
296 loadSiteConfiguration();
303 private final static String
304 SITE_FILE =
"/WEB-INF/elephant/conf/site.xml";
306 private static void loadSiteConfiguration() {
308 SAXBuilder builder =
new SAXBuilder();
312 if(confFile.exists()) {
313 doc = builder.build(confFile);
314 conf = doc.getRootElement();
317 setAttribute(
"site_name", readAttribute(conf.getChild(
"name"),
"value",
"Elephant"));
318 setAttribute(
"site_forced-domain", conf.getChildText(
"force-domain"));
319 setAttribute(
"site_description", conf.getChildText(
"description"));
320 setAttribute(
"site_keywords", conf.getChildText(
"keywords"));
323 setAttribute(
"site_welcome", conf.getChildText(
"welcome"));
324 setAttribute(
"site_internal-files", conf.getChildText(
"internalFiles"));
325 setAttribute(
"site_encoding", readAttribute(conf.getChild(
"encoding"),
"value",
"ISO-8859-1"));
326 setAttribute(
"site_context-load", readAttribute(conf.getChild(
"context-load"),
"value",
"none"));
327 setAttribute(
"site_locales", readAttribute(conf.getChild(
"locales"),
"value",
"_en,_ca"));
328 setAttribute(
"site_use-ssl", conf.getChildText(
"use-ssl"));
329 setAttribute(
"site_use-sso", Strings.isBlank(conf.getChildText(
"use-sso"),
"true"));
330 setAttribute(
"site_live-links", Strings.isBlank(conf.getChildText(
"live-links"),
"true"));
331 String gs = conf.getChildText(
"syndicate");
332 setAttribute(
"site_syndicate", Strings.isBlank(gs) ?
"Guests" : gs);
333 String qualification = conf.getChildText(
"directory-qualification");
334 setAttribute(
"site_directory-qualification", Strings.isBlank(qualification) ?
"any,!guest" : qualification);
336 String tmplRoot = conf.getChildText(
"templates-root");
337 setAttribute(
"site_templatesRoot", Strings.isBlank(tmplRoot) ?
"templates" : tmplRoot);
338 setAttribute(
"site_has_signup", Strings.isBlank(conf.getChildText(
"has-signup"),
"true"));
339 String signin = conf.getChildText(
"signin");
341 if(Strings.isBlank(signin) ||
"all".equals(signin)) {
344 }
else if(
"persona".equals(signin)) {
347 }
else if(
"internal".equals(signin)) {
351 _metas =
new ArrayList<>();
352 for(Element meta : (List<Element>) conf.getChildren(
"meta")) {
353 _metas.add(
new XMLOutputter().outputString(meta));
355 setAttribute(
"contact_language", readAttribute(conf.getChild(
"contact-defaults"),
"language",
"ca"));
356 setAttribute(
"contact_country", readAttribute(conf.getChild(
"contact-defaults"),
"country",
"ES"));
357 setAttribute(
"contact_currency", readAttribute(conf.getChild(
"contact-defaults"),
"currency",
"EUR"));
359 }
catch (IOException | JDOMException ex) {
364 private static String readAttribute(Element child, String attr, String def) {
365 return child !=
null ? child.getAttributeValue(attr) : def;
368 private static final HashMap<String, String> schemes =
new HashMap<>();
371 if(!schemes.containsKey(request.getScheme())) {
372 schemes.put(request.getScheme(), request.getScheme() +
"://" +
373 request.getServerName() + getServerPort(request));
377 private static String getServerPort(HttpServletRequest request) {
378 String scheme = request.getScheme();
379 int port = request.getServerPort();
380 if(
"http".equals(scheme) && port == 80) {
382 }
else if(
"https".equals(scheme) && port == 443) {
384 }
else if(
"ftp".equals(scheme) && port == 21) {
392 private static final String ENTITY_WEB_CONTEXT =
"entity-web-context";
402 String[] paths = path.split(
",");
403 for(String p : paths) {
405 if(!Strings.isBlank(ep)) {
void contextInitialized(ServletContextEvent event)
static boolean hasSignup()
static String getRelativePath(String path)
static void setAttribute(String key, Object value)
static String getDirectoryQualification()
static String getRealPath(String path)
static String getSiteSyndicate()
static List< String > getMetas()
static String getRootResourcePath()
static boolean getUseSSL()
static String encrypt(String value)
static ServletContext getCurrent()
static String getServerBase(String scheme)
static String getEntityWebContext(Path path)
static String getSiteFonts()
static boolean isValidLocale(Locale locale)
static String getSiteName()
static Currency getContactCurrency()
static void initServerSchemas(HttpServletRequest request)
static IConstructor getConstructor(HttpServletRequest request, HttpServletResponse response)
static String getContextLoad()
static String getSiteIcon()
static String getSiteDescription()
static String decrypt(String value)
static String getServerUrl(String scheme)
static String getRootWebPath()
static String logMsg(String msg)
static String getSiteForcedDomain()
static boolean getLiveLinks()
static String getSiteLocales()
static byte[] encrypt(byte[] value)
static File file(String path)
static String getEncoding()
static String getTemplatesRoot()
void contextDestroyed(ServletContextEvent event)
static Object getAttribute(String key)
static String getSiteSignin()
static URI toUri(String localPath)
static String getSiteKeywords()
static String getContextVariable(IConstructor constructor)
static byte[] decrypt(byte[] value)
static void resetSchemes()
static Locale getContactLocale()
static void removeAttribute(String key)
static String getSiteInternalFiles()
static String removeWebContext(String path)
static IConstructor createConstructor()
static String getEntityWebContext(String path)
static String getSiteWelcome()
static boolean getUseSSO()
static String getContextProperty(String context, String property)
static byte[] symetricEncrypt(byte[] inpBytes)
static byte[] symetricDecrypt(byte[] inpBytes)
void setRequest(ServletRequest request)
ElContext getCurrentContext()
void setResponse(ServletResponse response)