You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
173 lines
5.6 KiB
173 lines
5.6 KiB
package de.superx.servlet; |
|
|
|
import java.io.IOException; |
|
import java.io.PrintWriter; |
|
import java.io.UnsupportedEncodingException; |
|
import java.util.Enumeration; |
|
|
|
import jakarta.servlet.RequestDispatcher; |
|
import jakarta.servlet.ServletConfig; |
|
import jakarta.servlet.ServletException; |
|
import jakarta.servlet.http.HttpServlet; |
|
import jakarta.servlet.http.HttpServletRequest; |
|
import jakarta.servlet.http.HttpServletResponse; |
|
import jakarta.servlet.http.HttpSession; |
|
|
|
import org.apache.log4j.Logger; |
|
import org.saiku.web.service.SessionService; |
|
import org.springframework.context.ApplicationContext; |
|
import org.springframework.security.web.csrf.CsrfToken; |
|
import org.springframework.web.context.WebApplicationContext; |
|
|
|
import de.memtext.util.ServletHelper; |
|
import de.superx.common.SxUser; |
|
import de.superx.util.HtmlUtils; |
|
import de.superx.util.SqlStringUtils; |
|
|
|
public abstract class AbstractSuperXServlet extends HttpServlet { |
|
|
|
static Logger logger = Logger.getLogger(AbstractSuperXServlet.class); |
|
|
|
ApplicationContext springApplicationContext; |
|
|
|
/** |
|
* |
|
*/ |
|
private static final long serialVersionUID = 1L; |
|
|
|
public AbstractSuperXServlet() { |
|
super(); |
|
} |
|
|
|
@Override |
|
public void init(ServletConfig config) throws ServletException { |
|
super.init(config); |
|
this.springApplicationContext = (ApplicationContext) config.getServletContext() |
|
.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); |
|
} |
|
|
|
public String getTid(HttpServletRequest request) { |
|
String tidString = ServletHelper.getParameter(request, RequestParameter.tid); |
|
if (tidString == null || tidString.equals("")) |
|
throw new IllegalArgumentException("tid Argument fehlt"); |
|
return tidString; |
|
} |
|
|
|
public boolean getShowNavigation(HttpServletRequest request) { |
|
String naviParam = ServletHelper.getParameter(request, RequestParameter.navi); |
|
String naviAttr = (String) request.getAttribute(SessionAttribute.navi.toString()); |
|
if (naviAttr != null && !naviAttr.isEmpty()) { |
|
naviParam = naviAttr; |
|
} |
|
boolean showNavi = ((naviParam != null && naviParam.equals("true")) ? true : false); |
|
return showNavi; |
|
|
|
} |
|
|
|
public void checkCacheClearing(HttpServletRequest request) throws ServletException { |
|
String p = ServletHelper.getParameter(request, RequestParameter.cachingcontrol); |
|
if (p != null) { |
|
if (p.equalsIgnoreCase("clearstylesheets") || p.equalsIgnoreCase("s")) { |
|
try { |
|
SuperXManager.clearTransletStylesheets(); |
|
} catch (Exception e) { |
|
throw new ServletException(e.toString()); |
|
} |
|
} |
|
if (p.equalsIgnoreCase("clearall") || p.equalsIgnoreCase("a")) { |
|
|
|
try { |
|
SuperXManager.initPool(request, ServletUtils.getMandantenID(request)); |
|
} catch (Exception e) { |
|
e.printStackTrace(); |
|
throw new IllegalArgumentException("Kann Cache nicht neu laden:" + e); |
|
|
|
} |
|
} |
|
} |
|
} |
|
|
|
protected void forward(HttpServletRequest request, HttpServletResponse response, String uri) |
|
throws IOException, ServletException { |
|
String url = response.encodeRedirectURL("/servlet/" + uri); |
|
|
|
RequestDispatcher rd = getServletContext().getRequestDispatcher(url); |
|
if (rd == null) |
|
throw new IOException("Konnte keinen RequestDispatch aufbauen für /servlet/" + uri); |
|
else |
|
rd.forward(request, response); |
|
} |
|
|
|
protected void infoNeuAnmelden(HttpServletResponse response) throws IOException |
|
{ |
|
response.setContentType("text/html"); |
|
PrintWriter writer=response.getWriter(); |
|
writer.append("<html><body>Bitte melden Sie sich (neu) am System an</body></html>"); |
|
writer.flush(); |
|
} |
|
|
|
protected boolean hasProperSession(HttpServletRequest request) { |
|
boolean result = false; |
|
HttpSession s = request.getSession(false); |
|
|
|
if (s != null) { |
|
logger.debug("** Checking Session: ** -> " + s.getId()); |
|
String type = (String) s.getAttribute(SessionAttribute.sessiontype.toString()); |
|
logger.debug("Session Type: " + type); |
|
SxUser user = (SxUser) s.getAttribute(SessionAttribute.user.toString()); |
|
logger.debug("Session User: " + user); |
|
result = type != null && type.equals("superx") && user != null; |
|
} |
|
return result; |
|
} |
|
|
|
|
|
|
|
protected boolean isJSONRequest(HttpServletRequest request) { |
|
boolean result = false; |
|
for (Enumeration en = request.getParameterNames(); en.hasMoreElements();) { |
|
String aName = (String) en.nextElement(); |
|
if (aName.startsWith("getJSON")) |
|
result = true; |
|
} |
|
return result; |
|
} |
|
|
|
protected static void setEncoding(HttpServletRequest request) throws UnsupportedEncodingException { |
|
String ie = SqlStringUtils.getEncoding(); |
|
;// ServletHelper.getParameter(request, "ie"); |
|
// System.out.println(DateUtils.getNowString() + " " + ie); |
|
request.setCharacterEncoding(ie); |
|
|
|
} |
|
|
|
public static String insertCsrfToken(HttpServletRequest request, String html) throws IOException { |
|
CsrfToken token = SuperXServletHelper.getCsRfToken(request); |
|
// -> introduce more testable signature |
|
String result = HtmlUtils.insertCsrfToken(token, html); |
|
return result; |
|
} |
|
|
|
/** |
|
* wird analog in UserInitialiser.initSession aufgerufen |
|
*/ |
|
protected void logoutOfSaiku(HttpServletRequest request) { |
|
if (springApplicationContext != null) { |
|
SessionService sessionService = (SessionService) this.springApplicationContext.getBean("sessionService"); |
|
sessionService.logout(request); |
|
} |
|
} |
|
|
|
/** |
|
* wird analog in UserInitialiser.initSession aufgerufen |
|
*/ |
|
protected void loginToSaiku(HttpServletRequest request, String username) { |
|
if (springApplicationContext != null) { |
|
SessionService sessionService = (SessionService) this.springApplicationContext.getBean("sessionService"); |
|
sessionService.authenticate(request, username, null); |
|
} |
|
|
|
} |
|
|
|
} |
|
// Created on 20.08.2007 at 14:00:28
|
|
|