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.
539 lines
20 KiB
539 lines
20 KiB
package de.superx.servlet; |
|
|
|
import static de.superx.servlet.SxSQL_Server.DEFAULT_MANDANTEN_ID; |
|
|
|
import java.io.File; |
|
import java.io.IOException; |
|
import java.io.PrintWriter; |
|
import java.sql.Connection; |
|
import java.sql.SQLException; |
|
import java.text.ParseException; |
|
import java.text.SimpleDateFormat; |
|
import java.util.Enumeration; |
|
import java.util.Hashtable; |
|
import java.util.Iterator; |
|
import java.util.List; |
|
import java.util.Locale; |
|
import java.util.ResourceBundle; |
|
|
|
import jakarta.servlet.http.HttpServletRequest; |
|
import jakarta.servlet.http.HttpServletResponse; |
|
|
|
import org.apache.commons.fileupload.FileItem; |
|
import org.apache.log4j.Level; |
|
import org.apache.log4j.Logger; |
|
|
|
import de.memtext.util.DateUtils; |
|
import de.memtext.util.ServletHelper; |
|
import de.memtext.util.StringUtils; |
|
import de.superx.common.DBServletException; |
|
import de.superx.common.SuperX_el; |
|
import de.superx.common.SxResultSet; |
|
import de.superx.common.TranslationContainer; |
|
import de.superx.util.SqlStringUtils; |
|
|
|
/** |
|
* @author Daniel Quathamer Projektgruppe SuperX ServletUtils.java @ Diese |
|
* Java-Anwendung liefert zentrale Utilities für Servlets |
|
* |
|
*/ |
|
public class ServletUtils { |
|
// boolean der angibt, ob außerhalb von Anmeldeservlet, auch noch einmal |
|
// per Datenbankabfrage geprüft werden soll, ob userid und password_sha in |
|
// der |
|
// Session OK sind |
|
static final boolean isExtraAuthenticationCheckWanted = false; |
|
|
|
private static String path; |
|
|
|
public static void setPath(String path) { |
|
ServletUtils.path = path; |
|
} |
|
|
|
public static String getPath() { |
|
return path; |
|
} |
|
|
|
public static String getMandantenID(HttpServletRequest request) { |
|
String mandantenID = DEFAULT_MANDANTEN_ID; |
|
if (ServletHelper.getParameter(request, RequestParameter.MandantenID) != null) mandantenID = ServletHelper.getParameter(request, RequestParameter.MandantenID); |
|
// SuperXmlAnmeldung könnte als attribut default hinterlegt haben |
|
if (request.getAttribute(SessionAttribute.MandantenID.toString()) != null) mandantenID = (String) request.getAttribute("MandantenID"); |
|
if (request.getSession() != null && request.getSession().getAttribute(SessionAttribute.MandantenID.toString()) != null) { |
|
mandantenID = (String) request.getSession().getAttribute(SessionAttribute.MandantenID.toString()); |
|
|
|
} |
|
if (mandantenID == null) throw new IllegalStateException("Keine MandantenID gefunden. Muss bei einem Mandanten \"" + DEFAULT_MANDANTEN_ID + "\" sein"); |
|
return mandantenID; |
|
} |
|
|
|
public static Hashtable hash = new Hashtable(); |
|
|
|
/** |
|
* DQ für Internationalisierung wird für jedes Stylesheet eine |
|
* locale-spezifische Variante gesucht. Wenn die Locale anders als die |
|
* Standard-Locale ist, und wenn das Stylesheet mit dem Namen < |
|
* <original-Name>>_ < <language>>.xsl existiert, wird es zurückgegeben. |
|
* |
|
* @param original |
|
* -Stylesheet |
|
* @param aktuelle |
|
* Locale der Session |
|
* @return Lokalisiertes Stylesheet |
|
* @throws |
|
*/ |
|
static String getLocalizedStylesheet(String stil, Locale locale) |
|
|
|
{ |
|
if (locale.getLanguage() != "de") { |
|
int trunc_index = stil.lastIndexOf(".xsl"); |
|
if (trunc_index > 0) { |
|
String localized_stil = stil.substring(0, trunc_index) + "_" + locale.getLanguage() + ".xsl"; |
|
File f = new File(localized_stil); |
|
if (f.exists()) stil = localized_stil; |
|
} |
|
} |
|
return stil; |
|
|
|
} |
|
|
|
static String getJSONErrorText(String user, Locale locale) { |
|
StringBuffer errorText = new StringBuffer(); |
|
if (user != null) { |
|
errorText.append(get("superx.denied", locale)); |
|
errorText.append(" '"); |
|
errorText.append(user); |
|
errorText.append("'."); |
|
} else { |
|
errorText.append(get("superx.deniednouser", locale)); |
|
} |
|
errorText.append("\n"); |
|
errorText.append(get("superx.logintext", locale)); |
|
|
|
String s = ""; |
|
// provisorisch |
|
// "alert(\""+get("superx.denied", locale) + " '" + user + "' "+ |
|
// get("superx.logintext", locale)+"\");"; |
|
s += "({error:\"" + StringUtils.jsonAdapt(errorText.toString(), true) + "\"})"; |
|
return s; |
|
|
|
} |
|
|
|
static String getXmlErrorText(String fehler, String zs, Locale locale) { |
|
String s = de.superx.servlet.SuperXManager.htmlPageHead("Transformation Problem") + "<h1>Ein Fehler ist bei der XML-Transformation aufgereten.</H1><p class=\"errmsg\">" |
|
+ fehler + "</p>"; |
|
s += "<p>Der XML-Code:<br /><TEXTAREA NAME=\"content\" ROWS=\"40\" COLS=\"80\" WRAP=\"on\" READONLY=\"true\">"; |
|
s += zs + "</TEXTAREA></p></body></html>"; |
|
return s; |
|
} |
|
|
|
public static String getLoginFailureXml(String user, String mandantenid, Locale locale) { |
|
|
|
StringBuffer failureXML = new StringBuffer(); |
|
|
|
failureXML.append("<nachricht><p class=\"errmsg\">"); |
|
|
|
if (user != null) { |
|
failureXML.append(get("superx.denied", locale)); |
|
failureXML.append(" "); |
|
failureXML.append(user); |
|
} else { |
|
failureXML.append(get("superx.deniednouser", locale)); |
|
} |
|
failureXML.append("</p><p>"); |
|
failureXML.append(get("superx.logintext", locale)); |
|
|
|
if (SuperXManager.isXMLReturnLinkWanted) { |
|
|
|
failureXML.append("<br /><a href=\".."); |
|
if (mandantenid != null && !mandantenid.equalsIgnoreCase("null") && !mandantenid.equalsIgnoreCase(DEFAULT_MANDANTEN_ID)) { |
|
failureXML.append("/"); |
|
failureXML.append(mandantenid); |
|
} |
|
failureXML.append("/xml/index.htm\" target=\"_parent\">"); |
|
failureXML.append(get("superx.login", locale)); |
|
failureXML.append("</a></p>"); |
|
|
|
} |
|
failureXML.append("</nachricht>"); |
|
return failureXML.toString(); |
|
} |
|
|
|
/** |
|
* |
|
* @param user |
|
* @param locale |
|
* @return |
|
* |
|
* static String getLoginFailureText(String user, Locale locale) { |
|
* String s = get("superx.denied", locale) + " '" + user + "'</br>"; |
|
* s += get("superx.logintext", locale); if (isXMLReturnLinkWanted) |
|
* s += "<br/> |
|
* <a href=\"../xml/index.htm\" target=\"_parent\">" + |
|
* get("superx.login", locale) + "</a></p>"; |
|
* |
|
* return s; } |
|
*/ |
|
static String get(String what, Locale locale) { |
|
if (locale == null) locale = TranslationContainer.defaultLocale; |
|
ResourceBundle bundle = null; |
|
if (hash != null) bundle = (ResourceBundle) hash.get(locale.getLanguage()); |
|
|
|
// Wenn es noch kein ResourceBundle für die Locale gibt, versucht er |
|
// eines zu laden: |
|
if (bundle == null) { |
|
// neues ResourceBundle für die Locale. |
|
bundle = ResourceBundle.getBundle("LocalStrings", locale); |
|
// speichern im statischen |
|
// hash table. |
|
hash.put(bundle, locale.getLanguage()); |
|
|
|
} |
|
String r = bundle.getString(what); |
|
if (r == null) r = "Unknown Local String: " + what; |
|
return r; |
|
} |
|
|
|
public static String escapeTimeFeld(String feldwert) throws ParseException { |
|
String charcode1; |
|
String charcode2; |
|
charcode1 = "'"; // "#";Access |
|
charcode2 = "'"; // "#";Access |
|
// feldwert="2000-12-12 10:10:10"; |
|
if (feldwert.startsWith(charcode1) && feldwert.endsWith(charcode1)) feldwert = feldwert.substring(2, feldwert.length() - 1); |
|
|
|
SimpleDateFormat in_df = new SimpleDateFormat("HH:mm:ss"); |
|
// "" |
|
java.util.Date feldwert_df = in_df.parse(feldwert); |
|
// DAs folgende Format ist kompatibel mit |
|
// DB-Datumsfeldern (Access) |
|
SimpleDateFormat out_df = new SimpleDateFormat("HH:mm:ss"); |
|
// (""); |
|
feldwert = out_df.format(feldwert_df); |
|
|
|
feldwert = charcode1 + feldwert + charcode2; |
|
return feldwert; |
|
} |
|
|
|
public static String escapeTimestampFeld(String feldwert) throws ParseException { |
|
String charcode1; |
|
String charcode2; |
|
charcode1 = "'"; // "#";Access |
|
charcode2 = "'"; // "#";Access |
|
if (feldwert.startsWith(charcode1) && feldwert.endsWith(charcode1)) feldwert = feldwert.substring(2, feldwert.length() - 1); |
|
// feldwert="2000-12-12 10:10:10"; |
|
|
|
SimpleDateFormat in_df = new SimpleDateFormat("d.M.yyyy HH:mm:ss"); |
|
// "" |
|
java.util.Date feldwert_df = in_df.parse(feldwert); |
|
// System.out.println("hihi"); |
|
// DAs folgende Format ist kompatibel mit |
|
// DB-Datumsfeldern (Access) |
|
SimpleDateFormat out_df = new SimpleDateFormat("d.M.yyyy HH:mm:ss"); |
|
// (""); |
|
feldwert = out_df.format(feldwert_df); |
|
|
|
if (feldwert.length() > 0) feldwert = charcode1 + feldwert + charcode2; |
|
return feldwert; |
|
} |
|
|
|
/** |
|
* MB parsing hier nicht mehr nötig |
|
* |
|
* @param feldwert |
|
* @return |
|
* @throws ParseException |
|
*/ |
|
public static String escapeDateFeld(String feldwert) throws ParseException { |
|
String charcode1; |
|
String charcode2; |
|
charcode1 = "'"; // "#";Access |
|
charcode2 = "'"; // "#";Access |
|
if (feldwert.startsWith(charcode1) && feldwert.endsWith(charcode1)) feldwert = feldwert.substring(1, feldwert.length() - 1); |
|
// feldwert="2000-12-12 10:10:10"; |
|
|
|
/* |
|
* SimpleDateFormat in_df = new SimpleDateFormat("d.M.yyyy"); |
|
* //"dd.MM.yyyy hh:mm:ss" java.util.Date feldwert_df = |
|
* in_df.parse(feldwert); //DAs folgende Format ist kompatibel mit // |
|
* DB-Datumsfeldern (Access) SimpleDateFormat out_df = new |
|
* SimpleDateFormat("d.M.yyyy"); //("yyyy-MM-dd hh:mm:ss"); feldwert = |
|
* out_df.format(feldwert_df); |
|
*/ |
|
if (feldwert.length() > 0) { |
|
if (feldwert.equals("--leer--")) |
|
feldwert = "''"; |
|
else |
|
feldwert = charcode1 + feldwert + charcode2; |
|
} |
|
return feldwert; |
|
} |
|
|
|
/** |
|
* MB Einschließen in ' ' führte dazu, dass passende SelectableItems nicht |
|
* gefunden wurden, weil der key eben z.B. 5100 und nicht '5100' ist daher |
|
* auskommentiert DQ Korrigiert. |
|
* |
|
* @param feldwert |
|
* @return |
|
*/ |
|
public static String escapeCharFeld(String feldwert) { |
|
String charcode1; |
|
String charcode2; |
|
charcode1 = "'"; |
|
charcode2 = "'"; |
|
// Im Hashtable formular sind char- und date-Felder |
|
// jeweils mit '' versehen, diese müssen für die Ergbnisdarstellung |
|
// entfernt werden. |
|
if (feldwert.startsWith(charcode1) && feldwert.endsWith(charcode1)) feldwert = feldwert.substring(2, feldwert.length() - 1); |
|
feldwert = SqlStringUtils.escapeString(feldwert); |
|
if (feldwert.length() > 0) { |
|
if (SqlStringUtils.unescapeString(feldwert).equals("--leer--")) |
|
// feldwert = charcode1 + charcode2; |
|
feldwert = ""; |
|
// else |
|
// feldwert = charcode1 + feldwert + charcode2; |
|
} |
|
|
|
return feldwert; |
|
} |
|
|
|
/** |
|
* Liefert SxResultset zurück |
|
* |
|
* @param task |
|
* @param query |
|
* @param mandantenID |
|
* |
|
* @return |
|
* @throws SQLException |
|
* @throws DBServletException |
|
*/ |
|
public static SxResultSet execute(String task, String query, String mandantenID) throws SQLException, DBServletException { |
|
return execute_el(task, query, true, mandantenID).getResultSet(); |
|
} |
|
|
|
public static boolean isExternalDbWanted(String sql) { |
|
return sql.indexOf("--#dbid:") > -1; |
|
} |
|
|
|
public static String getExternalDbID(String sql) { |
|
String result = ""; |
|
int index1 = sql.indexOf("--#dbid:"); |
|
int index2 = sql.indexOf("#", index1 + 8); |
|
result = sql.substring(index1 + 8, index2); |
|
return result; |
|
} |
|
|
|
/** |
|
* Führt Abfrage durch und liefert ein SuperX_el Objekt, mit dem Ergebnis |
|
* des ersten selects zurück |
|
* |
|
* Achtung! Wenn mehrere SQL-Statements übergeben werden, die einen |
|
* Ergebniswert zurückliefern, kommen alle Ergebnisse in den Result- sehr |
|
* fies: execute procedure sp_user_orga(...); select .. from |
|
* tmp_organigramm;drop table tmp_ die Prozedur lieferte 318 zurück, war als |
|
* Eintrag im Result! |
|
* |
|
* @param task |
|
* @param query |
|
* @param isSingleQuery |
|
* TODO |
|
* |
|
* @return |
|
* @throws SQLException |
|
* @throws DBServletException |
|
*/ |
|
public static SuperX_el execute_el(String task, String query, boolean isSingleQuery, String mandantenID) throws SQLException, DBServletException { |
|
SuperX_el result_el = new SuperX_el(); |
|
Connection dbConnection = SxPools.getConnection(mandantenID); |
|
try { |
|
SxSQL_Server sxServer = new SxSQL_Server(mandantenID); |
|
|
|
if (isSingleQuery) |
|
sxServer.executeQuery(query, dbConnection, result_el); |
|
else |
|
sxServer.execute(query, dbConnection, result_el); |
|
// alles OK, Rückgabe an Pool |
|
dbConnection.close(); |
|
} catch (SQLException e) { |
|
String msg = "Problem bei:" + task + " DETAILS:" + e.getMessage() + " sql:" + query; |
|
Logger.getLogger("superx_" + mandantenID).log(Level.ERROR, msg); |
|
SxPools.invalidate(mandantenID, dbConnection); |
|
|
|
throw new SQLException(msg); |
|
} |
|
|
|
if (result_el.getError_String() != null && !result_el.getError_String().trim().equals("")) |
|
throw new SQLException("\nProblem bei:" + task + "\n\n DETAILS:" + result_el.getError_String() + "\n sql:" + query); |
|
return result_el; |
|
} |
|
|
|
/** |
|
* Führt Abfrage durch und liefert ein SuperX_el Objekt mit dem Ergebnis des |
|
* letzten selects zurück |
|
* |
|
* @param externalPool |
|
* normalerweise null, außerwenn Verbindung zu externer Datenbank |
|
* laufen soll |
|
* @param dbCon |
|
* es kann eine Connection übergeben werden, wenn null wird eine |
|
* neue geholt aus der SuperXDB, wohl nur von |
|
* SuperXmlTabelle.csvUploadhelper genutzt |
|
* @param task |
|
* @param query |
|
* @param mandantenID |
|
* @param isStacktraceWanted |
|
* TODO |
|
* |
|
* @return |
|
* @throws SQLException |
|
* @throws DBServletException |
|
*/ |
|
public static SuperX_el executeALL_el(ExternalPool externalPool, Connection dbCon, String task, String query, String mandantenID, boolean isStacktraceWanted) |
|
throws SQLException, DBServletException { |
|
if (externalPool != null) { |
|
task = task + " DatenbankID:" + externalPool.getName(); |
|
query = "-- DatenbankID:" + externalPool.getName() + "\n" + query; |
|
} |
|
if (task.startsWith("Abfrage Maske")) { |
|
SuperXManager.setLastMaskenSql("--" + task + " " |
|
+ DateUtils.getNowString() + "\n" + query); |
|
SuperXManager.setLastMaskenSqlMandantid(mandantenID); |
|
|
|
} |
|
SuperX_el result_el = new SuperX_el(); |
|
Connection dbConnection = null; |
|
if (dbCon == null) { |
|
if (externalPool == null) |
|
dbConnection = SxPools.getConnection(mandantenID); |
|
else |
|
dbConnection = externalPool.getConnection(); |
|
|
|
} else |
|
dbConnection = dbCon; |
|
try { |
|
|
|
SxSQL_Server sxServer = new SxSQL_Server(mandantenID); |
|
|
|
sxServer.executeALL(query, dbConnection, result_el); |
|
// alles OK, Rückgabe an Pool |
|
if (dbCon == null) dbConnection.close(); |
|
} catch (SQLException e) { |
|
if (externalPool == null) { |
|
SxPools.invalidate(mandantenID, dbConnection); |
|
} else { |
|
externalPool.invalidate(dbConnection); |
|
} |
|
String msg = "Problem bei:" + task + "\nDETAILS:" + e.getMessage(); |
|
Logger.getLogger("superx_" + mandantenID).log(Level.ERROR, msg); |
|
if (isStacktraceWanted) e.printStackTrace(); |
|
throw new SQLException(msg); |
|
} catch (Exception e) { |
|
if (externalPool == null) { |
|
SxPools.invalidate(mandantenID, dbConnection); |
|
} else { |
|
externalPool.invalidate(dbConnection); |
|
} |
|
String msg = "Problem bei:" + task + " DETAILS:" + e.getMessage() + " sql:" + query; |
|
Logger.getLogger("superx_" + mandantenID).log(Level.ERROR, msg); |
|
if (isStacktraceWanted) e.printStackTrace(); |
|
throw new DBServletException(msg); |
|
} |
|
|
|
if (result_el.getError_String() != null && !result_el.getError_String().trim().equals("")) |
|
throw new SQLException("\nProblem bei:" + task + "\n\n DETAILS:" + result_el.getError_String() + "\n sql:" + query); |
|
|
|
return result_el; |
|
} |
|
|
|
/** |
|
* wird derzeit nicht verwendet, könnte auch noch mit |
|
* StringUtils.containsTags() prüfen,ob unerlaubte Tags vorhanden sind |
|
* |
|
* @param request |
|
*/ |
|
public static void checkParams(HttpServletRequest request) { |
|
StringBuffer test = new StringBuffer(); |
|
for (Enumeration en = request.getParameterNames(); en.hasMoreElements();) { |
|
String element = (String) en.nextElement(); |
|
test.append(element + " - " + request.getParameter(element) + "\n"); |
|
if (request.getParameter(element) != null && request.getParameter(element).indexOf("'") > -1) { |
|
int i = 1; |
|
} |
|
} |
|
// System.out.println(test.toString()); |
|
} |
|
|
|
/** |
|
* Kopiert Parameter aus einem Request in ein Hashtable Formular. |
|
* Schutz gegen Sql-Injection: ; wird zu ^ nur von |
|
* Maske.setFieldDefaults aufgerufen |
|
* |
|
* @param request |
|
* @return Hashtable formular |
|
*/ |
|
/* 'addToFormular' entfernt #279826 */ |
|
public static void addParamToFormular(HttpServletRequest request, Hashtable formular, String aName) { |
|
String val = ServletHelper.getParameterString(request, aName); |
|
if (val != null && val.length() > 1) { |
|
val = val.replace(';', '^'); |
|
} |
|
if (val == null || val.equals("--leer--")) { |
|
val = ""; |
|
} |
|
formular.put(aName, val); |
|
} |
|
|
|
public static void sendBackHtml(HttpServletResponse response, String info) throws IOException { |
|
PrintWriter output = response.getWriter(); |
|
response.setContentType("text/html; charset=" + SqlStringUtils.getEncoding()); |
|
output.write(info); |
|
output.flush(); |
|
output.close(); |
|
} |
|
|
|
/** |
|
* |
|
* @param mandantenID |
|
* @param sql |
|
* @return null if no externe Datenbankverbindung angegeben |
|
* @throws DBServletException |
|
*/ |
|
public static ExternalPool getExternalPoolIfSpecified(String mandantenID, Integer maskennummer, String sql) throws DBServletException { |
|
ExternalPool ep = null; |
|
if (isExternalDbWanted(sql)) { |
|
String epID = getExternalDbID(sql); |
|
SxPool sxPool = SxPools.get(mandantenID); |
|
if (sxPool.hasExternalPool(mandantenID + "-" + epID + "_" + maskennummer)) { |
|
ep = sxPool.getExternalPool(mandantenID + "-" + epID + "_" + maskennummer); |
|
|
|
} else if (sxPool.hasExternalPool(mandantenID + "-" + epID)) { |
|
ep = sxPool.getExternalPool(mandantenID + "-" + epID); |
|
|
|
} else |
|
throw new DBServletException("Externe Datenverbindung von Mandant :" + mandantenID + ":" + epID + " konnte nicht aufgebaut werden"); |
|
|
|
} |
|
if (ep != null && !ep.isOK()) throw new DBServletException(ep.getExceptionMsg()); |
|
return ep; |
|
} |
|
|
|
public static String getParamValue(List<FileItem> multipartData, String paramName) { |
|
String result = null; |
|
Iterator iter = multipartData.iterator(); |
|
while (iter.hasNext()) { |
|
FileItem item = (FileItem) iter.next(); |
|
if (item.isFormField()) { |
|
String fieldName = item.getFieldName(); |
|
if (fieldName != null && fieldName.equals(paramName)) { |
|
result = item.getString(); |
|
break; |
|
} |
|
} |
|
} |
|
return result; |
|
} |
|
} |
|
|
|
// Created on 15.01.2004
|
|
|