1 18 package org.jahia.admin.search; 19 20 import java.io.IOException ; 21 import java.util.Hashtable ; 22 import java.util.Locale ; 23 24 import javax.servlet.ServletContext ; 25 import javax.servlet.ServletException ; 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.http.HttpServletResponse ; 28 import javax.servlet.http.HttpSession ; 29 30 import org.jahia.bin.JahiaAdministration; 31 import org.jahia.data.JahiaData; 32 import org.jahia.exceptions.JahiaException; 33 import org.jahia.params.ParamBean; 34 import org.jahia.registries.ServicesRegistry; 35 import org.jahia.resourcebundle.JahiaResourceBundle; 36 import org.jahia.services.search.JahiaSearchService; 37 import org.jahia.services.sites.JahiaSite; 38 import org.jahia.services.sites.JahiaSiteTools; 39 import org.jahia.services.sites.JahiaSitesService; 40 import org.jahia.services.usermanager.JahiaUser; 41 42 43 52 public class ManageSearch { 53 54 private static final org.apache.log4j.Logger logger = 55 org.apache.log4j.Logger.getLogger (ManageSearch.class.getName ()); 56 57 private static final String CLASS_NAME = ManageSearch.class.getName (); 58 private static final String JSP_PATH = JahiaAdministration.JSP_PATH; 59 private static final String HTTP_FILES = "/jsp/jahia/engines"; 60 private static final String JSP = "/jsp/jahia/administration/search/main.jsp?includejsp="; 61 62 private static ServletContext context; 63 64 private ServicesRegistry sReg; 65 private JahiaSitesService siteServ; 66 private JahiaSearchService searchServ; 67 68 private JahiaSite site; 69 private JahiaUser user; 70 private Hashtable values; 71 72 private String operation = ""; 74 75 83 public ManageSearch (HttpServletRequest request, 84 HttpServletResponse response, 85 HttpSession session, 86 ServletContext context) 87 throws Throwable { 88 ManageSearch.context = context; 89 JahiaData jData = (JahiaData)request.getAttribute ("org.jahia.data.JahiaData"); 90 ParamBean jParams = null; 91 if (jData != null) { 92 jParams = jData.params (); 93 } 94 95 sReg = ServicesRegistry.getInstance (); 96 97 if (sReg == null) { 98 String dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.warningMsg.serviceRegistyNotAvailable.label", 99 jParams, jParams.getLocale ()); 100 displayError (request, response, dspMsg); 101 return; 102 } 103 104 siteServ = sReg.getJahiaSitesService (); 105 searchServ = sReg.getJahiaSearchService (); 106 107 if (siteServ == null || searchServ == null) { 108 String dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.warningMsg.requestServiceNotAvailable.label", 109 jParams, jParams.getLocale ()); 110 displayError (request, response, dspMsg); 111 return; 112 } 113 114 userRequestDispatcher (request, response, session); 115 } 116 117 118 125 private void userRequestDispatcher (HttpServletRequest request, 126 HttpServletResponse response, 127 HttpSession session) 128 throws IOException , ServletException { 129 130 user = (JahiaUser)session.getAttribute (ParamBean.SESSION_USER); 132 site = (JahiaSite)session.getAttribute (ParamBean.SESSION_SITE); 133 JahiaData jData = (JahiaData)request.getAttribute ("org.jahia.data.JahiaData"); 134 ParamBean jParams = null; 135 if (jData != null) { 136 jParams = jData.params (); 137 } 138 139 if (site == null || user == null || sReg == null) { 140 String dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.warningMsg.requestInfoNotAvailable.label", 141 jParams, jParams.getLocale ()); 142 displayError (request, response, dspMsg); 143 return; 144 } 145 146 values = (Hashtable )session.getAttribute ("org.jahia.adminsearch.values." + site.getSiteKey ()); 148 if (values == null) { 149 Hashtable values = new Hashtable (); 150 session.setAttribute ("org.jahia.adminsearch.values." + site.getSiteKey (), values); 151 this.values = values; 152 } 153 154 try { 155 156 if (JahiaSiteTools.getAdminGroup (site).isMember (user)) { 157 158 operation = request.getParameter ("sub"); 159 160 if (operation.equals ("display")) { 161 displayMain (request, response, session); 162 } else if (operation.equals ("chooseoperation")) { 163 processChooseOperation (request, response, session); 164 } else if (operation.equals ("doindex")) { 165 processDoIndex (request, response, session); 166 } else if (operation.equals ("abortIndexation")) { 167 processAbortIndexation (request, response, session); 168 } 169 } else { 170 String dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.warningMsg.noAdminAcessSite.label", 171 jParams, jParams.getLocale ()); 172 displayError (request, response, dspMsg); 173 } 174 175 } catch (Throwable t) { 176 t.printStackTrace (); 177 String dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.warningMsg.processingRequestError.label", 178 jParams, jParams.getLocale ()); 179 displayError (request, response, dspMsg); 180 } 181 } 182 183 194 private void doRedirect (HttpServletRequest request, 195 HttpServletResponse response, 196 String target) 197 throws IOException , ServletException { 198 try { 199 request.setAttribute ("currentSite", site); 200 request.setAttribute ("user", user); 201 202 if (request.getAttribute ("operation") == null) { 204 request.setAttribute ("operation", operation); 205 } 206 207 if (request.getAttribute ("warningMsg") == null) { 209 request.setAttribute ("warningMsg", ""); 210 } 211 212 if (request.getAttribute ("msg") == null) { 214 JahiaData jData = (JahiaData)request.getAttribute ("org.jahia.data.JahiaData"); 215 ParamBean jParams = null; 216 if (jData != null) { 217 jParams = jData.params (); 218 } 219 request.setAttribute ("msg", 220 JahiaResourceBundle.getAdminResource("org.jahia.admin.copyright", 221 jParams, jParams.getLocale())); 222 223 } 224 225 if (request.getAttribute ("focus") == null) { 226 request.setAttribute ("focus", "-none-"); 227 } 228 229 if (request.getAttribute ("title") == null) { 231 request.setAttribute ("title", "Manage Search Engine"); 232 } 233 234 request.setAttribute ("values", values); 236 237 request.setAttribute ("url", request.getContextPath () + HTTP_FILES); 239 240 String userAgent = request.getHeader ("user-agent"); 242 243 Integer navigator = (userAgent.indexOf ("IE")) 244 != -1 ? new Integer (0) : new Integer (1); 245 request.setAttribute ("navigator", navigator); 246 247 boolean isLynx = false; 248 249 if (userAgent.indexOf ("Lynx") != -1) { 250 isLynx = true; 251 } 252 request.setAttribute ("isLynx", new Boolean (isLynx)); 253 254 HttpSession session = request.getSession (); 256 JahiaData jData = (JahiaData)request.getAttribute ("org.jahia.data.JahiaData"); 257 ParamBean jParams = null; 258 if (jData != null) { 259 jParams = jData.params (); 260 } 261 Locale currentLocale = (Locale )session.getAttribute (ParamBean.SESSION_LOCALE); 262 if (currentLocale == null) { 263 currentLocale = jParams.getLocale (); 264 } 265 266 String contentTypeStr = "text/html;charset="; 267 contentTypeStr = contentTypeStr + "ISO8859-1"; 268 request.setAttribute ("content-type", contentTypeStr); 269 270 context.getRequestDispatcher (target).forward (request, response); 272 273 } catch (IOException ie) { 274 logger.error (ie.getMessage ()); 275 } catch (ServletException se) { 276 logger.error (se.getMessage ()); 277 } 278 } 279 280 287 private void displayMain (HttpServletRequest request, 288 HttpServletResponse response, 289 HttpSession session) 290 throws IOException , ServletException , JahiaException { 291 292 request.setAttribute ("subAction", "chooseoperation"); 294 request.setAttribute ("go", "change"); 296 297 String operation = "doindex"; 299 300 String val = null; 301 val = (String )values.get ("operation"); 302 if (val != null) { 303 operation = val; 304 } 305 306 String indexPath = searchServ.getSiteIndex (site.getID ()); 308 boolean indexExists = (indexPath != null); 309 310 values.put ("operation", operation); 311 312 request.setAttribute ("operation", operation); 313 request.setAttribute ("indexExists", new Boolean (indexExists)); 314 request.setAttribute ("nbDocsInMainQueue", new Integer (searchServ.getNbDocumentsInQueue())); 315 request.setAttribute ("nbFilesInMainQueue", new Integer (searchServ.getNbOfFilesInQueue())); 316 request.setAttribute ("siteIndexationStatus", searchServ.getSiteIndexationStatus(site.getID())); 317 318 doRedirect (request, response, JSP + "operations"); 319 } 320 321 328 private void processChooseOperation (HttpServletRequest request, 329 HttpServletResponse response, 330 HttpSession session) 331 throws IOException , ServletException , JahiaException { 332 333 String operation = request.getParameter ("operation"); 335 if (operation == null) 336 displayMain (request, response, session); 337 338 values.put ("operation", operation); 339 340 if (operation.equals ("doindex")) { 341 displayIndexForm (request, response, session); 342 } else if (operation.equals ("abortIndexation")) { 343 displayAbortIndexationForm (request, response, session); 344 } else { 345 displayMain (request, response, session); 346 } 347 } 348 349 356 private void processDoIndex (HttpServletRequest request, 357 HttpServletResponse response, 358 HttpSession session) 359 throws IOException , ServletException , JahiaException { 360 361 JahiaData jData = (JahiaData)request.getAttribute ("org.jahia.data.JahiaData"); 362 ParamBean jParams = null; 363 if (jData != null) { 364 jParams = jData.params (); 365 } 366 if (!user.isAdminMember(site.getID()) && !user.isRoot ()) { 367 String dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.warningMsg.noAdminAcessSite.label", 368 jParams, jParams.getLocale ()); 369 displayError (request, response, dspMsg); 370 return; 371 } 372 373 375 String go = request.getParameter ("go"); 377 if (go == null) 378 displayMain (request, response, session); 379 380 if (go.equals ("ok")) { 381 382 boolean result = searchServ.indexSite (site.getID (), jData.params (), false, true); 384 386 request.setAttribute ("result", new Boolean (result)); 387 388 request.setAttribute ("subAction", "display"); 390 request.setAttribute ("go", "change"); 392 393 doRedirect (request, response, JSP + "indexresult"); 394 395 } else { 396 displayMain (request, response, session); 397 } 398 } 399 400 407 private void displayIndexForm (HttpServletRequest request, 408 HttpServletResponse response, 409 HttpSession session) 410 throws IOException , ServletException , JahiaException { 411 412 request.setAttribute ("subAction", "doindex"); 414 request.setAttribute ("go", "change"); 416 417 doRedirect (request, response, JSP + "index"); 418 } 419 420 427 private void processAbortIndexation (HttpServletRequest request, 428 HttpServletResponse response, 429 HttpSession session) 430 throws IOException , ServletException , JahiaException { 431 432 JahiaData jData = (JahiaData)request.getAttribute ("org.jahia.data.JahiaData"); 433 ParamBean jParams = null; 434 if (jData != null) { 435 jParams = jData.params (); 436 } 437 if (!user.isAdminMember(site.getID()) && !user.isRoot ()) { 438 String dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.warningMsg.noAdminAcessSite.label", 439 jParams, jParams.getLocale ()); 440 displayError (request, response, dspMsg); 441 return; 442 } 443 444 446 String go = request.getParameter ("go"); 448 if (go == null) 449 displayMain (request, response, session); 450 451 if (go.equals ("ok")) { 452 453 boolean result = searchServ.abortSiteIndexation(site.getID ()); 455 request.setAttribute ("result", new Boolean (result)); 456 request.setAttribute ("subAction", "display"); 458 request.setAttribute ("go", "change"); 460 461 doRedirect (request, response, JSP + "abortindexationresult"); 462 463 } else { 464 displayMain (request, response, session); 465 } 466 } 467 468 475 private void displayAbortIndexationForm (HttpServletRequest request, 476 HttpServletResponse response, 477 HttpSession session) 478 throws IOException , ServletException , JahiaException { 479 480 request.setAttribute ("subAction", "abortIndexation"); 482 request.setAttribute ("go", "change"); 484 485 doRedirect (request, response, JSP + "abortindexation"); 486 } 487 488 495 private void displayError (HttpServletRequest request, 496 HttpServletResponse response, 497 String msg) 498 throws IOException , ServletException { 499 request.setAttribute ("warningMsg", msg); 500 doRedirect (request, response, JSP + "error"); 501 } 502 503 504 } | Popular Tags |