1 22 23 package org.cofax.cms; 24 25 import java.io.File ; 26 import java.io.IOException ; 27 import java.io.PrintWriter ; 28 import java.util.Enumeration ; 29 import java.util.HashMap ; 30 import java.util.Locale ; 31 32 import javax.servlet.ServletConfig ; 33 import javax.servlet.ServletContext ; 34 import javax.servlet.ServletException ; 35 import javax.servlet.http.HttpServlet ; 36 import javax.servlet.http.HttpServletRequest ; 37 import javax.servlet.http.HttpServletResponse ; 38 import javax.servlet.http.HttpSession ; 39 40 import org.cofax.DataStore; 41 import org.cofax.XMLConfig; 42 import org.cofax.cms.login.ILoginHandler; 43 44 54 55 public class CofaxToolsServlet extends HttpServlet { 56 60 public static String toolsPath ; 61 62 public static String staticFolder; 63 64 public static String articleRessourceFolder; 66 67 public static String userImageFolder; 68 69 public static HashMap configGlossary = new HashMap (); 70 71 static XMLConfig configFile; 73 74 static String contextPath; 76 77 public static String contextUrl; 79 80 static String configLocation; 82 83 static String dataStoreName; 85 86 static DataStore dataStore; 88 89 static String adminGroupID; 90 91 public static String templatePath; 93 94 static String log; 96 97 static String logLocation; 99 100 static long logMaxSize = 0; 102 103 static String dataLog; 105 106 static String dataLogLocation; 108 109 static long dataLogMaxSize = 0; 111 112 static String removePageCache; 114 115 static String removeTemplateCache; 117 118 static String dataStoreClass; 120 121 static String dbProperties; 123 124 static String lookInContext; 126 127 static boolean hasPackageTags = false; 129 130 static boolean betaServer = false; 132 133 137 public static String fileTransferFolder; 138 139 142 public String servletPath; 143 144 147 public String pathInfo; 148 149 152 public String queryString; 153 154 157 public Object tmp; 158 159 162 public String hostName; 163 164 167 public HttpSession session; 168 169 172 public static String hilite = "CCCCCC"; 173 174 177 public static String bodyColor = "FFFFFF"; 178 179 182 public static String tableColor = "FFFFFF"; 183 184 187 public static String tabColor = "0066CC"; 188 189 public static String adminEmail = ""; 190 191 public static String aliasPath = ""; 192 193 public static Locale lcl; 194 195 public static int numrows = 10; 196 197 public static String logFolder = ""; 198 199 public static int uploadMaxSize = 2; 201 211 public final void init(ServletConfig config) throws ServletException { 212 super.init(config); 213 214 ServletContext context = getServletContext(); 216 contextPath = context.getRealPath("/"); 217 218 staticFolder = contextPath + "static"; 225 fileTransferFolder = staticFolder + File.separator + "tmp"; 226 227 templatePath = "/" + config.getInitParameter("templatePath"); 231 if (templatePath != null && !templatePath.endsWith("/")) 232 templatePath = templatePath + "/"; 234 logFolder = contextPath; 236 try { 237 logFolder = config.getInitParameter("logFolder"); 238 if (!(logFolder.endsWith(File.separator))) 239 logFolder += File.separator; 240 } catch (Exception e) { 241 logFolder = contextPath; 242 System.err.println("[WARNING] logFolder parameter not initialized. 'contextPath' value used instead"); 243 } 244 245 try { 247 String strUploadMaxSize = config.getInitParameter("uploadMaxSize"); 248 uploadMaxSize = Integer.parseInt(strUploadMaxSize); 249 } catch (Exception e) { 250 uploadMaxSize = 2; 251 System.err.println("[WARNING] uploadMaxSize parameter not initialized. Default value is 2Mo."); 252 } 253 254 adminGroupID = config.getInitParameter("adminGroupID"); 255 removePageCache = config.getInitParameter("removePageCache"); 256 removeTemplateCache = config.getInitParameter("removeTemplateCache"); 257 log = config.getInitParameter("log"); 258 logLocation = logFolder + "CofaxTools.log"; 259 260 userImageFolder = config.getInitParameter("configGlossary:userImageFolder"); 262 if (userImageFolder == null) 263 userImageFolder = "images"; 264 265 articleRessourceFolder = config.getInitParameter("configGlossary:articleRessourceFolder"); 266 if (articleRessourceFolder == null) 267 articleRessourceFolder = "images"; 268 269 for (Enumeration params = config.getInitParameterNames(); params.hasMoreElements();) { 270 String paramName = (String ) params.nextElement(); 271 if (paramName.indexOf("configGlossary:") > -1) { 272 configGlossary.put(paramName, config.getInitParameter(paramName)); 273 } 274 } 275 277 if (!config.getInitParameter("logMaxSize").equals("")) { 278 logMaxSize = Long.parseLong(config.getInitParameter("logMaxSize")); 279 } 280 dataLog = config.getInitParameter("dataLog"); 281 dataLogLocation = logFolder + "dataLog.log"; 282 283 if (!config.getInitParameter("dataLogMaxSize").equals("")) { 284 logMaxSize = Long.parseLong(config.getInitParameter("dataLogMaxSize")); 285 } 286 287 dataStoreName = (String ) getServletContext().getAttribute("dataStoreName"); 288 dataStoreClass = (String ) getServletContext().getAttribute("dataStoreClass"); 289 dataStore = (DataStore) getServletContext().getAttribute("dataStore"); 290 291 CofaxToolsLogin.setLoginHandler((ILoginHandler) getServletContext().getAttribute("loginHandler")); 293 294 adminEmail = (String ) getServletContext().getAttribute("adminEmail"); 295 getServletContext().setAttribute("toolsPath", toolsPath); 296 297 try { 299 CofaxToolsLifeCycleThread th = new CofaxToolsLifeCycleThread(); 300 th.setDataStore(dataStore); 301 th.setLogFile(logFolder + "CofaxToolsLifeCycle.log"); 302 th.start(); 303 } catch (Exception e) { 304 System.err.println("[ERROR] LifeCycleThread : Exception :" + e); 305 } 306 307 } 308 309 323 public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException , IOException { 324 325 res.setContentType("text/html"); 326 327 PrintWriter out = res.getWriter(); 329 servletPath = req.getServletPath(); 330 contextUrl = req.getContextPath(); pathInfo = req.getPathInfo(); 332 aliasPath = req.getContextPath(); 333 if (!(aliasPath.startsWith("/"))) 334 aliasPath = "/" + aliasPath; 335 toolsPath = aliasPath + "/tools/"; 336 CofaxToolsUtil.log("toolsPath :"+toolsPath); 337 lcl = req.getLocale(); 338 queryString = req.getQueryString(); 339 tmp = req.getHeader("host"); 340 session = req.getSession(true); 341 String page = ""; 342 DataStore db = null; 343 344 try { 345 db = (DataStore) (Class.forName(dataStoreClass)).newInstance(); 346 } catch (ClassNotFoundException e) { 347 CofaxToolsUtil.log("CofaxToolsServlet doGet ERROR: Data store class not found: " + e); 348 } catch (InstantiationException e) { 349 CofaxToolsUtil.log("CofaxToolsServlet doGet ERROR loading data store class.: " + e); 350 } catch (IllegalAccessException e) { 351 CofaxToolsUtil.log("CofaxToolsServlet doGet ERROR loading data store class.: " + e); 352 } 353 354 if (db == null) { 355 CofaxToolsUtil.log("CofaxToolsServlet doGet ERROR " + "instantiating dataStore: DataStore is null."); 356 } 357 if (hasPackageTags == false) { 362 if (db.connect()) { 363 hasPackageTags = true; 364 } else { 365 if (db.getError() == true) { 366 CofaxToolsUtil.log("CofaxToolsServlet doGet ERROR: " + "can't connect to db " + dataStoreName + "at CofaxToolsServlet: " 367 + db.getLastError()); 368 } 369 } 370 371 DataStore.dataLog = dataLog; 373 DataStore.dataLogLocation = dataLogLocation; 374 DataStore.dataLogMaxSize = dataLogMaxSize; 375 376 CofaxToolsDbUtils.st_selectPackagetags = db.selectPackagetags(); 377 CofaxToolsDbUtils.st_selectPackagetagsCache = db.selectPackagetagsCache(); 378 379 dataStore.setCache(CofaxToolsDbUtils.getValueValuePackageHash(db, CofaxToolsDbUtils.st_selectPackagetags, "TAG_NAME", "TAG_VALUE", true), 380 DataStore.PACKAGE_TAG_PROCESSED); 381 dataStore.setCache(CofaxToolsDbUtils.getValueValuePackageHash(db, CofaxToolsDbUtils.st_selectPackagetagsCache, "TAG_VALUE", "CACHE", true), 382 DataStore.PACKAGE_TAG_CACHE_COMMAND); 383 384 if (db.disConnect() != true) { 385 CofaxToolsUtil.log("CofaxToolsServlet doGet ERROR: " + "cannot disconnect from Servlet database " + dataStoreName); 386 } 387 } 388 389 if (db.connect()) { 390 391 try { 392 page = CofaxToolsNavigation.navigate(db, pathInfo, queryString, session, req, res); 393 } catch (Exception e) { 394 if (db.getError() == true) 395 { 396 page = "<html><body></pre>"; 397 page += "CofaxToolsServlet doGet ERROR: " + "occurred during a database transaction\n"; 398 page += db.getLastError(); 399 page += "<html><body></pre>"; 400 e.printStackTrace(System.err); 401 } else { 402 page = ("CofaxToolsServlet doGet ERROR : " + e + "<BR>Please report this error and the actions that led to it."); 403 e.printStackTrace(System.err); 404 } 405 } finally { 406 if (db.disConnect() != true) { 407 CofaxToolsUtil.log("CofaxToolsServlet doGet ERROR: " + "cannot disconnect from Servlet database " + dataStoreName); 408 } 409 } 410 411 } else { 412 CofaxToolsUtil.log("CofaxToolsServlet doGet ERROR: " + "can't connect to db " + dataStoreName + " at CofaxToolsServlet: " + db.getLastError()); 413 } 414 boolean transferFolder = CofaxToolsUtil.checkFileTransferFolder(fileTransferFolder); 415 if (transferFolder == false) { 416 page = ("The admin of this servlet has not set the file transfer folder and " 417 + "this servlet will not function correctly until it is set. Please have the admin read tools.xml " 418 + "and set the appropriate folder with permissions of 777."); 419 } 420 421 out.println(page); 422 423 } 424 425 437 public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException , IOException { 438 doGet(req, res); 439 } 440 441 447 public static DataStore connect() { 448 449 DataStore db = null; 451 452 try { 453 db = (DataStore) (Class.forName(dataStoreClass)).newInstance(); 454 } catch (ClassNotFoundException e) { 455 CofaxToolsUtil.log("CofaxToolsServlet connect ERROR: " + "Data store class not found.: " + e); 456 } catch (InstantiationException e) { 457 CofaxToolsUtil.log("CofaxToolsServlet connect ERROR: " + "loading data store class.: " + e); 458 } catch (IllegalAccessException e) { 459 CofaxToolsUtil.log("CofaxToolsServlet connect ERROR: " + "loading data store class.: " + e); 460 } 461 462 if (db == null) { 463 CofaxToolsUtil.log("CofaxToolsServlet connect ERROR: " + "Could not create DataStore object db."); 464 } 465 return db; 466 } 467 468 } 469 | Popular Tags |