1 package org.jahia.webapps.fichotheque; 2 3 import java.io.*; 4 import java.util.*; 5 import java.sql.*; 6 import javax.servlet.*; 7 import javax.servlet.http.*; 8 9 import org.jahia.tools.*; 10 import org.jahia.tools.db.*; 11 import org.jahia.webapps.util.*; 12 13 import org.apache.ojb.broker.PBFactoryException; 15 import org.apache.ojb.broker.PersistenceBroker; 16 import org.apache.ojb.broker.PersistenceBrokerFactory; 17 import org.apache.ojb.broker.util.ui.AsciiSplash; 18 import org.odmg.*; 19 import org.apache.ojb.odmg.*; 20 21 28 public class FichothequeServlet extends HttpServlet 29 { 30 private static org.apache.log4j.Logger logger = 31 org.apache.log4j.Logger.getLogger(FichothequeServlet.class); 32 33 35 37 private final static String SC_KEY_PROPERTIES = "properties"; 38 39 private final static String SC_KEY_METHODS = "methods"; 40 41 private final static String SC_KEY_USEMODE = "useMode"; 42 43 private final static String SC_KEY_CLASSNAME = "classname"; 44 45 private final static String PROP_CARD_LIBRARY_NAME = "cardLibrary.name"; 46 private final static String PROP_NB_CARD_BY_PAGE = "nbCardByPage"; 47 48 private final static String PROP_DB_DRIVER = "database.driver"; 49 50 private final static String PROP_DB_URL = "database.url"; 51 52 private final static String PROP_DB_USERNAME = "database.username"; 53 54 private final static String PROP_DB_PASSWORD = "database.password"; 55 56 private final static String PROP_DB_INITIALCONNECTIONS = "database.initialConnections"; 57 58 private final static String PROP_DB_MAXCONNECTIONS = "database.maxConnections"; 59 60 private final static String CONTEXT_ROOT_REAL_PATH = "$contextRootRealPath"; 61 62 private final static String JAHIA_LICENSE_TYPE = "com.xo3.jahia.license.licenseType"; 63 64 private final static int LICENSE_TYPE = 2; 65 66 private final static String NO_LICENSE_PAGE = "/jsp/no_license.jsp"; 67 68 private static String m_ContextRootRealPath = ""; 69 70 71 private String CARD_LIBRARY_NAME; 72 73 private Card CARD_FIELDS_NAMES; 74 75 private String NB_CARD_BY_PAGE; 76 77 private String CURRENT_ACTION; 78 79 private int CURRENT_CARD_ID; 80 81 private String EDITOR_ROLE = "manager"; 82 83 private String useModePath; 84 85 86 88 private String m_DB_DRIVER; 89 90 private String m_DB_URL; 91 92 private String m_DB_USERNAME; 93 94 private String m_DB_PASSWORD; 95 96 private int m_DB_MAXCONNECTIONS; 97 98 private int m_DB_INITIALCONNECTIONS; 99 100 private PersistenceBroker broker = null; 101 102 103 104 private DBServices m_DBServices; 105 106 private ActionHandler actionHandler; 107 108 private String next_page = "default.html"; 109 110 111 public FichothequeServlet() 112 { 113 } 114 115 116 123 public void init(ServletConfig config) throws ServletException 124 { 125 logger.debug("FichothequeServlet: init: begin"); 126 super.init(config); 127 128 useModePath = getServletContext().getRealPath(config.getInitParameter(SC_KEY_USEMODE)); 129 130 m_ContextRootRealPath = getServletContext().getRealPath(File.separator); 131 132 134 String props = config.getInitParameter(SC_KEY_PROPERTIES); 135 if ( props != null ) 136 { 137 String realPath = getServletContext().getRealPath(props); 138 if ( realPath != null ) 139 { 140 props = realPath; 141 if (loadProperties(props)) 142 { 143 144 try { 145 151 logger.debug("AbsenceRequest::init: stage 1"); 152 if (broker != null) broker.close(); 153 broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 154 AccessDB.init(broker); 155 logger.debug("AbsenceRequest::init: stage 2"); 156 157 158 } catch (Throwable t) 159 { 160 logger.error("AbsenceRequestServlet::init: error initializing PersistenceBroker"); 161 throw new ServletException("error initializing database via OJB", t); 162 } 163 164 } 165 else 166 { 167 logger.debug("FichothequeServlet: init: Error Loading Properties"); 168 } 169 } 170 } 171 172 String methodsPath = getServletContext().getRealPath(config.getInitParameter(SC_KEY_METHODS)); 174 175 if (methodsPath == null) 176 { 177 throw new ServletException("Methods file not specified in the servlet config - please specify init-param \""+SC_KEY_METHODS+"\""); 178 } 179 actionHandler = new ActionHandler(methodsPath, config.getInitParameter(SC_KEY_CLASSNAME)); 180 181 loadFieldsNames(); 183 184 logger.debug("FichothequeServlet: init: end"); 185 } 186 187 188 193 protected void loadFieldsNames() 194 { 195 196 Card crd = new Card(0,CARD_LIBRARY_NAME); 197 CARD_FIELDS_NAMES = crd; 198 } 199 200 201 207 protected boolean loadProperties(String file) 208 { 209 Properties Props = new Properties(); 210 try 211 { 212 FileInputStream fis = new FileInputStream(file); 213 Props.load(fis); 214 fis.close(); 215 216 CARD_LIBRARY_NAME = Props.getProperty( PROP_CARD_LIBRARY_NAME ); 217 NB_CARD_BY_PAGE = Props.getProperty( PROP_NB_CARD_BY_PAGE ); 218 223 226 229 233 } 234 catch (java.io.IOException ex) 235 { 236 ex.printStackTrace(); 237 return false; 238 } 239 return true; 240 } 241 242 243 250 public void doGet(HttpServletRequest request, HttpServletResponse response) 251 throws IOException, ServletException 252 { 253 doPost(request, response); 254 } 255 256 257 263 public void doPost(HttpServletRequest request, HttpServletResponse response) 264 throws IOException, ServletException 265 { 266 String inPortal = (String )request.getAttribute("org.portletapi.portal"); 267 boolean run = true; 268 269 if ("true".equals(inPortal)) 270 { 271 Integer licenseType = (Integer )request.getAttribute(JAHIA_LICENSE_TYPE); 272 if (licenseType != null && (licenseType.intValue() < LICENSE_TYPE) ) 273 { 274 logger.debug("AbsenceRequestServlet - doPost: Insufficient jahia version"); 275 run = false; 276 } 277 } 278 279 if (run) 280 { 281 setParameters(request); 283 logger.debug("FichothequeServlet: doPost: mode=*"+ request.getParameter("mode") + "*"); 284 try 286 { 287 logger.debug("currentAction " + CURRENT_ACTION); 288 actionHandler.call(CURRENT_ACTION,request); 289 } 290 catch (NullPointerException npe) 291 { 292 logger.error("FichothequeServlet: doPost: Couldn't perform action " + 293 CURRENT_ACTION); 294 request.setAttribute("next_page","display_list.jsp"); 295 request.setAttribute("menu_page","menuList.jsp"); 296 request.setAttribute("bottom_page","bottomList.jsp"); 297 } 298 } 302 try 303 { 304 RequestDispatcher rd; 305 if (!run) 306 { 307 rd = getServletContext().getRequestDispatcher(NO_LICENSE_PAGE); 308 } 309 else 310 { 311 String next_page = (String )request.getAttribute("next_page"); 313 logger.debug("FichothequeServlet: doPost: forward to "+next_page); 314 rd = getServletContext().getRequestDispatcher("/jsp/index.jsp"); 315 } 316 rd.forward(request, response); 317 } 318 catch (Exception e) 319 { 320 logger.error("FichothequeServlet: doPost",e); 321 } 322 323 logger.debug("FichothequeServlet: doPost: end"); 324 } 325 326 327 332 protected void setParameters(HttpServletRequest request) 333 { 334 HttpSession session = request.getSession(true); 335 session.setMaxInactiveInterval(7200); 336 int t = session.getMaxInactiveInterval(); 337 logger.debug("FichothequeServlet: setParameters: Session Max Inactive Time: "+ Integer.toString(t)); 338 logger.debug("FichothequeServlet: setParameters: Context Id: "+ (String )request.getAttribute("org.portletapi.contextid")); 339 340 if (request.getAttribute("org.portletapi.contextid") == null) 342 { 343 request.setAttribute("org.portletapi.contextid", ""); 344 } 345 if (request.getAttribute("org.portletapi.portal") == null) 346 { 347 request.setAttribute("org.portletapi.portal", ""); 348 } 349 350 CURRENT_ACTION = request.getParameter("current_action"); 352 if ((CURRENT_ACTION == null)||(CURRENT_ACTION == "")) 353 { 354 CURRENT_ACTION = "display_ListCards"; 356 } 357 358 if (request.getParameter("card_id") != null) 360 { 361 CURRENT_CARD_ID = Integer.parseInt(request.getParameter("card_id")); 362 } 363 else 364 { 365 CURRENT_CARD_ID = 0; 367 } 368 request.setAttribute("card_id",Integer.toString(CURRENT_CARD_ID)); 369 370 371 String sortedBy = (String )request.getParameter("sort"); 373 if (sortedBy == null) 374 { 375 sortedBy = "CARD_ID"; 376 } 377 session.setAttribute(CARD_LIBRARY_NAME + "_sort", sortedBy); 378 379 String sortFlag = (String )request.getParameter("sortFlag"); 381 if (sortFlag == null) 382 { 383 sortFlag = "ASC"; 384 } 385 session.setAttribute(CARD_LIBRARY_NAME + "_sortFlag", sortFlag); 386 387 388 String userName = request.getParameter("username"); 390 if (userName == null) 391 { 392 userName = ""; 394 } 395 request.setAttribute("username",userName); 396 397 String userRight = (String )session.getAttribute(CARD_LIBRARY_NAME 399 + "_userRight"); 400 if (userRight == null) 401 { 402 userRight = ""; 404 session.setAttribute(CARD_LIBRARY_NAME + "_userRight",userRight ); 405 } 406 407 String contextId = (String )request.getAttribute("org.portletapi.contextid"); 409 logger.debug("FichothequeServlet: setParameters: useMode(session)=" + (String )session.getAttribute(CARD_LIBRARY_NAME + "_useMode")); 410 logger.debug("FichothequeServlet: setParameters: sessionId=" + (String )session.getId()); 411 if (session.getAttribute(CARD_LIBRARY_NAME + "_useMode") == null) 412 { 413 try 414 { 415 Properties Prop_useMode = new Properties(); 416 FileInputStream fis = new FileInputStream(useModePath); 417 Prop_useMode.load(fis); 418 fis.close(); 419 420 String useMode = Prop_useMode.getProperty( "useMode"+ contextId ); 421 logger.debug("FichothequeServlet: setParameters: useMode=*" + useMode + contextId +"*"); 422 423 if (useMode == null || useMode.equals("")) 424 { 425 logger.debug("FichothequeServlet: setParameters: action=useMode"); 426 CURRENT_ACTION = "displayAdminMode"; 428 session.setAttribute(CARD_LIBRARY_NAME + "_useModePath", useModePath); 429 session.setAttribute(CARD_LIBRARY_NAME + "_useMode", ""); 430 431 } 432 else 433 { 434 logger.debug("FichothequeServlet: setParameters: set useMode"); 435 session.setAttribute(CARD_LIBRARY_NAME + "_useMode", useMode); 436 if (useMode.equals("user")) 437 { 438 session.setAttribute(CARD_LIBRARY_NAME + "_userRight", EDITOR_ROLE); 439 } 440 } 441 442 } 443 catch (java.io.IOException ex) 444 { 445 logger.error("FichothequeServlet: setParameters: Exception: " ,ex); 446 ex.printStackTrace(); 447 } 448 } 449 logger.debug("FichothequeServlet: setParameters: end useMode"); 450 451 452 Query query = new Query(); 454 if (session.getAttribute(CARD_LIBRARY_NAME + "_query") == null) 455 { 456 session.setAttribute(CARD_LIBRARY_NAME + "_query", query); 457 } 458 459 if ( !(((String )session.getAttribute(CARD_LIBRARY_NAME + "_useMode")).equals("")) ) 460 { 461 463 String oldContextId = (String )session.getAttribute(CARD_LIBRARY_NAME + "_oldContextId"); 464 465 if ((session.getAttribute(CARD_LIBRARY_NAME + "_listCards") == null) || (!contextId.equals(oldContextId)) ) 466 { 467 String inPortal = (String )request.getAttribute("org.portletapi.portal"); 468 String useMode = (String )session.getAttribute(CARD_LIBRARY_NAME + "_useMode"); 469 String remoteUser = (String )request.getRemoteUser(); 470 471 ListCards currentList = new ListCards(); 472 currentList.loadList(contextId, inPortal, useMode, remoteUser, userRight, sortFlag, sortedBy); 474 session.setAttribute(CARD_LIBRARY_NAME + "_listCards",currentList); 475 session.setAttribute(CARD_LIBRARY_NAME + "_oldContextId", contextId); 476 } 477 } 478 479 request.setAttribute("card", CARD_FIELDS_NAMES); 481 482 request.setAttribute("cardLibraryName", CARD_LIBRARY_NAME); 484 485 request.setAttribute("nbCardByPage", NB_CARD_BY_PAGE); 487 488 session.setAttribute(CARD_LIBRARY_NAME + "_editorRole", EDITOR_ROLE); 490 491 492 session.setAttribute(CARD_LIBRARY_NAME + "_servletURI", request.getRequestURI()); 494 495 496 497 498 logger.debug("FichothequeServlet: setParameters: action= " 499 + CURRENT_ACTION + ", user= " + userName + ", userRight= " 500 + userRight + ", card_id = " + CURRENT_CARD_ID); 501 502 } 503 504 505 512 protected String replace(String str, String token, String replaceValue) 513 { 514 String result = ""; 515 int i = str.indexOf(token); 516 while (i != -1) { 517 result += str.substring(0,i) + replaceValue; 518 str = str.substring(i + token.length(), str.length()); 519 i = str.indexOf(token); 520 } 521 return result + str; 522 } 523 524 525 } 526 527 528 529 | Popular Tags |