18 package org.turro.elephant.impl.context;
21 import java.net.URLDecoder;
22 import java.net.URLEncoder;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26 import javax.servlet.ServletException;
27 import javax.servlet.ServletRequest;
28 import javax.servlet.ServletResponse;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
31 import javax.servlet.http.HttpSession;
32 import org.turro.string.Strings;
33 import org.jdom.Element;
34 import org.turro.action.Actions;
35 import org.turro.action.Parameters;
36 import org.turro.action.Secrets;
37 import org.turro.annotation.ElephantConstructor;
38 import org.turro.elephant.context.Application;
39 import org.turro.elephant.context.ElephantContext;
40 import org.turro.elephant.context.ElephantLogin;
41 import org.turro.elephant.context.History;
42 import org.turro.elephant.context.IAction;
43 import org.turro.elephant.context.IConstructor;
44 import org.turro.elephant.context.MetaList;
45 import org.turro.elephant.impl.util.CookieUtil;
46 import org.turro.elephant.impl.util.FileUtil;
47 import org.turro.elephant.impl.util.Localizer;
48 import org.turro.elephant.impl.util.Parser;
49 import org.turro.elephant.security.IUser;
50 import org.turro.elephant.web.ElContext;
51 import org.turro.elephant.web.ElContextMap;
52 import org.turro.html.HtmlContent;
53 import org.turro.i18n.I_;
54 import org.turro.marker.ElephantMarker;
55 import org.turro.plugin.contacts.IContact;
56 import org.turro.util.IdGenerator;
65 protected final static String
71 protected PrintWriter
out =
null;
73 protected ArrayList
metas =
new ArrayList(), headers =
new ArrayList(), footers =
new ArrayList();
74 protected boolean isAContext =
false, pageRendering =
true, nullOutput =
false;
78 private int uniqueSuffix = 0;
92 this.request = (HttpServletRequest)
request;
93 if(this.request !=
null) {
94 this.request.setAttribute(
"el_cons",
this);
97 this.request.setAttribute(
"i_",
I_.
api());
98 this.request.setAttribute(
"i",
I_.
map());
106 this.response = (HttpServletResponse)
response;
107 if(this.response !=
null)
123 if(currentContext ==
null)
125 return currentContext;
141 currentContext = context;
152 if(file ==
null)
return true;
153 String fileName = file.isFile() ? file.getName() : file.getAbsolutePath();
155 if(internals !=
null) {
156 String tmp[] = internals.split(
",");
157 for(String s : tmp) {
158 if(fileName.matches(Strings.convertToRegEx(s)))
return true;
161 return (fileName.matches(
"google.*\\.html")) ||
162 (fileName.matches(
"site.*\\.xml")) ||
163 (fileName.matches(
"robots\\.txt")) ||
164 (fileName.contains(
"_internal")) ||
165 (fileName.contains(
"_zul")) ||
166 (fileName.contains(
"resources")) ||
167 (fileName.contains(
"WEB-INF")) ||
168 (fileName.contains(
"META-INF"));
188 return request.getSession().getAttribute(key);
190 }
catch(Exception ex) {
198 HashMap<String, Object> map =
new HashMap<>();
201 HttpSession session =
request.getSession();
202 Iterator<String> it = session.getAttributeNames().asIterator();
203 while(it.hasNext()) {
204 String key = it.next();
205 if(Strings.isBlank(prefix) || key.startsWith(prefix)) {
206 map.put(Strings.killPrefix(key, prefix), session.getAttribute(key));
210 }
catch(Exception ex) {
230 }
catch(Exception ex) {
239 }
catch(Exception ex) {
253 }
catch(Exception ex) {
262 }
catch(Exception ex) {
269 boolean include =
request.getServletPath().startsWith(
"/xp"),
270 action =
request.getServletPath().startsWith(
"/xpaction");
274 Element incNode =
null;
281 request.setAttribute(
"el_cons",
this);
285 }
catch (Exception ex) {
286 throw new ServletException(ex);
289 request.setAttribute(
"el_cons",
this);
290 if(incNode !=
null) {
291 Iterator heads = incNode.getChildren(
"header").iterator();
293 while(heads.hasNext()) {
294 tmp = (Element) heads.next();
295 switch (tmp.getAttributeValue(
"type")) {
297 addCSS(tmp.getAttributeValue(
"content"));
305 if(incNode !=
null) {
339 if(internalSignIn !=
null && internalSignIn) {
366 return file.exists();
372 addCSS(
"https://fonts.googleapis.com/css?family=${site_fonts}");
379 marker.
put(
"lang",
I_.
api().used().getLanguage());
381 marker.
put(
"metas", confMetas);
385 marker.
put(
"headers", headers);
387 marker.
process(
"elephant",
"startHtml");
395 marker.
put(
"footers", footers);
396 marker.
process(
"elephant",
"endHtml");
403 if(path.matches(
".*\\.(htm|html)")) {
405 }
else if(path.matches(
".*\\.(txt)")) {
406 out.println(
"<pre>");
408 out.println(
"</pre>");
429 public void redirect(String path)
throws IOException {
430 if(Strings.isBlank(path)) path =
"/";
431 if(path.equals(
"*")) {
433 path = ctx !=
null ? ctx.
getPath() :
"/";
435 if(path.startsWith(
"http:") || path.startsWith(
"https:") || !path.startsWith(
"/") ||
436 (!Strings.isBlank(
request.getContextPath()) && path.startsWith(
request.getContextPath() +
"/"))) {
449 public void reload() throws IOException {
457 public PrintWriter
getOut() throws IOException {
458 if(
out ==
null && !nullOutput) {
460 if(
response.getContentType() ==
null && pageRendering) {
474 if(
out !=
null)
out.close();
479 pageRendering = value;
502 public void processOutput(String text)
throws ServletException, IOException {
507 public void processOutput(PrintWriter newOut, String text)
throws ServletException, IOException {
514 return URLEncoder.encode(url,
"UTF-8");
515 }
catch (UnsupportedEncodingException ex) {
525 }
catch (UnsupportedEncodingException ex) {
541 return request.getParameter(param);
546 return request.getParameterValues(param);
559 Enumeration pn =
getRequest().getParameterNames();
560 while(pn.hasMoreElements()) {
561 name = (String) pn.nextElement();
562 if(name.startsWith(
"action_") &&
getParameter(name) !=
null) {
563 value = name.substring(7);
581 private void includeFile(PrintWriter
out, String file)
throws IOException, ServletException {
585 while((str = br.readLine()) !=
null) {
598 return name.replaceAll(
"[^a-zA-Z0-9\\.\\-\\/]",
"_");
603 return name.replaceAll(
"[^a-zA-Z0-9\\.\\-]",
"_");
611 private String transformString(String
string,
int mode) {
615 }
else if(mode == 1) {
616 return string.substring(0, 1).toUpperCase() +
string.substring(1);
617 }
else if(mode == 2) {
618 return string.toUpperCase();
619 }
else if(mode == 3) {
620 return string.toLowerCase();
634 if(user !=
null && user.
isValid()) {
642 return "cid_" + IdGenerator.generate() + uniqueSuffix++;
647 if((
"lang:_" +
I_.
api().used().getLanguage()).equals(role)) {
652 if(
"user:in".equals(role))
return true;
689 if(cssFile.contains(
"${site_fonts}")) {
692 for(String font : fonts) {
694 "<link rel=\"stylesheet\" type=\"text/css\" href=\"" +
695 urlToFile(cssFile.replaceFirst(
"\\$\\{site\\_fonts\\}", font)) +
697 if(!headers.contains(result)) {
704 "<link rel=\"stylesheet\" type=\"text/css\" href=\"" +
707 if(!headers.contains(result)) {
716 "<script type=\"text/javascript\" src=\"" +
719 if(!headers.contains(result)) {
730 if(file.startsWith(
"http:") || file.startsWith(
"https:")) {
738 if(!footers.contains(script)) {
746 String content = noIndex ?
"NOINDEX" :
"";
747 content = content + (content.length() > 0 && noFollow ?
"," :
"");
748 content = content + (noFollow ?
"NOFOLLOW" :
"");
749 content = (content.length() == 0 ?
"ALL" : content);
750 String result =
"<META NAME=\"ROBOTS\" CONTENT=\"" + content +
"\">";
751 if(!headers.contains(result)) {
758 request.getSession().invalidate();
763 HttpSession session =
request.getSession();
764 if(session !=
null) {
765 session.setMaxInactiveInterval(interval);
772 for(String s : (List<String>) headers) {
773 headers.set(count, s.replace(from, to));
781 return user ==
null ? false :
796 private boolean isWebAdminRole(String role) {
797 return role.startsWith(
"context") ||
798 role.startsWith(
"default") ||
799 role.startsWith(
"control-version") ||
800 role.startsWith(
"maillist");
805 private String templateRoot =
null;
809 if(templateRoot ==
null) {
static boolean executeAction(IConstructor constructor)
static Parameters from(Map< String, String[]> parameterMap)
static String getSecret(String keys)
static boolean loadResources(String path)
static void setAttribute(String key, Object value)
static String getRealPath(String path)
static List< String > getMetas()
static ServletContext getCurrent()
static String getSiteFonts()
static String getSiteName()
static String getSiteIcon()
static String getSiteDescription()
static String getRootWebPath()
static String logMsg(String msg)
static String getEncoding()
static String getTemplatesRoot()
static Object getAttribute(String key)
static String getSiteKeywords()
static void removeAttribute(String key)
static String getSiteInternalFiles()
static void redirectToLogin(IConstructor constructor)
static String peek(IConstructor constructor)
static String back(IConstructor constructor)
static void add(IConstructor constructor, String path)
boolean addAll(Collection<? extends String > c)
static Element getInclude(IConstructor constructor)
static IAction getAction(IConstructor constructor)
void setSessionAttribute(String key, Object value)
ElContext getCurrentContext()
String encodeURL(String url)
void redirect(String path)
void renderContext(String path, String stopper)
HttpServletRequest getRequest()
String getValidFileName(String name)
ElContext getRenderingContext()
Object getRequestAttribute(String key)
Map< String, Object > getSessionAttributeMap(String prefix)
boolean canWebAdminister()
void addOnLoadedJavaScript(String script)
Object findAttribute(String key)
Parameters getParameters()
void setRenderingContext(ElContext context)
void setRequestAttribute(String key, Object value)
boolean fileExists(String filePath)
void removeSessionAttribute(String key)
HttpServletResponse response
Object getApplicationAttribute(String key)
void setWebAdministering(boolean value)
String getValidUrl(String name)
void addRobots(ElContext context)
void processOutput(PrintWriter newOut, String text)
String getParameter(String param)
void processOutput(String text)
Object getSessionAttribute(String key)
void addMeta(String meta)
void setNullOutput(boolean value)
boolean isInRole(String role)
String getLastReferringContext()
String decodeURL(String url)
void setStopper(String stopper)
String[] getParameterValues(String param)
StringWriter stringOutput
void addJavaScript(String jsFile)
boolean isWebAdministering()
String getActionParameter()
void setApplicationAttribute(String key, Object value)
boolean hasAnyRoleKey(String role)
static final String BUNDLE_LOCATION
void forceIsAContext(ElContext context)
void setRequest(ServletRequest request)
HttpServletResponse getResponse()
void addCSS(String cssFile)
void setMaxInactiveInterval(int interval)
void includeContent(String path)
String getContextParameter()
void removeApplicationAttribute(String key)
void changeCssHeaders(String from, String to)
String getParameter(String param, boolean decode)
void setResponse(ServletResponse response)
void addHeader(String header)
void setOut(PrintWriter out)
String getIdelParameter()
HttpServletRequest request
void setLastReferringContext(String context)
void setPageRendering(boolean value)
String urlToFile(String file)
void removeRequestAttribute(String key)
boolean isFileInternal(File file)
static void setCookie(HttpServletResponse response, String name, String value, String path, int age)
static boolean existsFile(String requestURI)
static BufferedReader getBufferedFile(String folder, String file)
static void processMacros(IConstructor constructor, PrintWriter out, String text)
static ElContext getCurrent()
static ElContext getContext(IConstructor constructor)
WebContext getWebContext()
boolean equals(Object obj)
void construct(IConstructor constructor)
static I18nByKey byKeyMap()
static I18nApiWrapper api()
static I18nMapWrapper map()
static I18nCompatibilityMap compatibilityMap()
void process(String rootTmpl, String tmpl)
Object put(Object key, Object value)
static final String INTERNAL_SIGNIN
static final String CONNECTOR_EMAIL
boolean isInRole(String role)
String getProperty(String key)
static final String LOGGED_USER
boolean hasAnyRoleKey(String role)