1
14
15 package com.liferay.portal.servlet;
16
17 import com.liferay.portal.NoSuchLayoutException;
18 import com.liferay.portal.deploy.hot.PluginPackageHotDeployListener;
19 import com.liferay.portal.events.EventsProcessorUtil;
20 import com.liferay.portal.events.StartupAction;
21 import com.liferay.portal.kernel.deploy.hot.HotDeployUtil;
22 import com.liferay.portal.kernel.exception.PortalException;
23 import com.liferay.portal.kernel.exception.SystemException;
24 import com.liferay.portal.kernel.job.Scheduler;
25 import com.liferay.portal.kernel.log.Log;
26 import com.liferay.portal.kernel.log.LogFactoryUtil;
27 import com.liferay.portal.kernel.messaging.DestinationNames;
28 import com.liferay.portal.kernel.messaging.MessageBusUtil;
29 import com.liferay.portal.kernel.messaging.MessageListener;
30 import com.liferay.portal.kernel.plugin.PluginPackage;
31 import com.liferay.portal.kernel.poller.PollerProcessor;
32 import com.liferay.portal.kernel.scheduler.SchedulerEngineUtil;
33 import com.liferay.portal.kernel.scheduler.SchedulerEntry;
34 import com.liferay.portal.kernel.scheduler.messaging.SchedulerEventMessageListenerWrapper;
35 import com.liferay.portal.kernel.search.Indexer;
36 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
37 import com.liferay.portal.kernel.servlet.PortletSessionTracker;
38 import com.liferay.portal.kernel.servlet.ProtectedServletRequest;
39 import com.liferay.portal.kernel.util.ContentTypes;
40 import com.liferay.portal.kernel.util.GetterUtil;
41 import com.liferay.portal.kernel.util.HttpUtil;
42 import com.liferay.portal.kernel.util.InstancePool;
43 import com.liferay.portal.kernel.util.ParamUtil;
44 import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
45 import com.liferay.portal.kernel.util.PortalInitableUtil;
46 import com.liferay.portal.kernel.util.PropsKeys;
47 import com.liferay.portal.kernel.util.ReleaseInfo;
48 import com.liferay.portal.kernel.util.StringPool;
49 import com.liferay.portal.kernel.util.Validator;
50 import com.liferay.portal.kernel.workflow.WorkflowHandler;
51 import com.liferay.portal.kernel.workflow.WorkflowHandlerRegistryUtil;
52 import com.liferay.portal.kernel.xml.Document;
53 import com.liferay.portal.kernel.xml.DocumentException;
54 import com.liferay.portal.kernel.xml.Element;
55 import com.liferay.portal.kernel.xml.SAXReaderUtil;
56 import com.liferay.portal.kernel.xmlrpc.Method;
57 import com.liferay.portal.language.LanguageResources;
58 import com.liferay.portal.model.Company;
59 import com.liferay.portal.model.Group;
60 import com.liferay.portal.model.GroupConstants;
61 import com.liferay.portal.model.Layout;
62 import com.liferay.portal.model.Portlet;
63 import com.liferay.portal.model.PortletApp;
64 import com.liferay.portal.model.PortletFilter;
65 import com.liferay.portal.model.PortletURLListener;
66 import com.liferay.portal.model.User;
67 import com.liferay.portal.plugin.PluginPackageIndexer;
68 import com.liferay.portal.poller.PollerProcessorUtil;
69 import com.liferay.portal.pop.POPServerUtil;
70 import com.liferay.portal.security.auth.PrincipalException;
71 import com.liferay.portal.security.auth.PrincipalThreadLocal;
72 import com.liferay.portal.security.permission.ResourceActionsUtil;
73 import com.liferay.portal.service.CompanyLocalServiceUtil;
74 import com.liferay.portal.service.GroupLocalServiceUtil;
75 import com.liferay.portal.service.LayoutLocalServiceUtil;
76 import com.liferay.portal.service.LayoutTemplateLocalServiceUtil;
77 import com.liferay.portal.service.PortletLocalServiceUtil;
78 import com.liferay.portal.service.ResourceActionLocalServiceUtil;
79 import com.liferay.portal.service.ResourceCodeLocalServiceUtil;
80 import com.liferay.portal.service.ThemeLocalServiceUtil;
81 import com.liferay.portal.service.UserLocalServiceUtil;
82 import com.liferay.portal.servlet.filters.i18n.I18nFilter;
83 import com.liferay.portal.struts.MultiMessageResources;
84 import com.liferay.portal.struts.PortletRequestProcessor;
85 import com.liferay.portal.struts.StrutsUtil;
86 import com.liferay.portal.util.ContentUtil;
87 import com.liferay.portal.util.ExtRegistry;
88 import com.liferay.portal.util.MaintenanceUtil;
89 import com.liferay.portal.util.Portal;
90 import com.liferay.portal.util.PortalInstances;
91 import com.liferay.portal.util.PortalUtil;
92 import com.liferay.portal.util.PropsUtil;
93 import com.liferay.portal.util.PropsValues;
94 import com.liferay.portal.util.ShutdownUtil;
95 import com.liferay.portal.util.WebKeys;
96 import com.liferay.portal.velocity.VelocityContextPool;
97 import com.liferay.portal.webdav.WebDAVStorage;
98 import com.liferay.portal.webdav.WebDAVUtil;
99 import com.liferay.portal.xmlrpc.XmlRpcServlet;
100 import com.liferay.portlet.PortletConfigFactory;
101 import com.liferay.portlet.PortletFilterFactory;
102 import com.liferay.portlet.PortletInstanceFactoryUtil;
103 import com.liferay.portlet.PortletURLListenerFactory;
104 import com.liferay.portlet.asset.AssetRendererFactoryRegistryUtil;
105 import com.liferay.portlet.asset.model.AssetRendererFactory;
106 import com.liferay.portlet.social.model.SocialActivityInterpreter;
107 import com.liferay.portlet.social.model.SocialRequestInterpreter;
108 import com.liferay.portlet.social.model.impl.SocialActivityInterpreterImpl;
109 import com.liferay.portlet.social.model.impl.SocialRequestInterpreterImpl;
110 import com.liferay.portlet.social.service.SocialActivityInterpreterLocalServiceUtil;
111 import com.liferay.portlet.social.service.SocialRequestInterpreterLocalServiceUtil;
112 import com.liferay.util.servlet.DynamicServletRequest;
113 import com.liferay.util.servlet.EncryptedServletRequest;
114
115 import java.io.IOException;
116
117 import java.util.Iterator;
118 import java.util.List;
119 import java.util.Set;
120
121 import javax.portlet.PortletConfig;
122 import javax.portlet.PortletContext;
123 import javax.portlet.PortletException;
124
125 import javax.servlet.RequestDispatcher;
126 import javax.servlet.ServletContext;
127 import javax.servlet.ServletException;
128 import javax.servlet.http.HttpServletRequest;
129 import javax.servlet.http.HttpServletResponse;
130 import javax.servlet.http.HttpSession;
131 import javax.servlet.jsp.PageContext;
132
133 import org.apache.struts.Globals;
134 import org.apache.struts.action.ActionServlet;
135 import org.apache.struts.action.RequestProcessor;
136 import org.apache.struts.config.ControllerConfig;
137 import org.apache.struts.config.ModuleConfig;
138 import org.apache.struts.tiles.TilesUtilImpl;
139
140
147 public class MainServlet extends ActionServlet {
148
149 public void destroy() {
150 List<Portlet> portlets = PortletLocalServiceUtil.getPortlets();
151
152 if (_log.isDebugEnabled()) {
153 _log.debug("Destroy schedulers");
154 }
155
156 try {
157 destroySchedulers(portlets);
158 }
159 catch (Exception e) {
160 _log.error(e, e);
161 }
162
163 if (_log.isDebugEnabled()) {
164 _log.debug("Destroy portlets");
165 }
166
167 try {
168 destroyPortlets(portlets);
169 }
170 catch (Exception e) {
171 _log.error(e, e);
172 }
173
174 if (_log.isDebugEnabled()) {
175 _log.debug("Destroy companies");
176 }
177
178 try {
179 destroyCompanies();
180 }
181 catch (Exception e) {
182 _log.error(e, e);
183 }
184
185 if (_log.isDebugEnabled()) {
186 _log.debug("Process global shutdown events");
187 }
188
189 try {
190 processGlobalShutdownEvents();
191 }
192 catch (Exception e) {
193 _log.error(e, e);
194 }
195
196 if (_log.isDebugEnabled()) {
197 _log.debug("Destroy");
198 }
199
200 callParentDestroy();
201 }
202
203 public void init() throws ServletException {
204 if (_log.isDebugEnabled()) {
205 _log.debug("Initialize");
206 }
207
208 callParentInit();
209
210 if (_log.isDebugEnabled()) {
211 _log.debug("Process startup events");
212 }
213
214 try {
215 processStartupEvents();
216 }
217 catch (Exception e) {
218 _log.error(e, e);
219
220 System.out.println(
221 "Stopping the server due to unexpected startup errors");
222
223 System.exit(0);
224 }
225
226 if (_log.isDebugEnabled()) {
227 _log.debug("Initialize velocity");
228 }
229
230 try {
231 initVelocity();
232 }
233 catch (Exception e) {
234 _log.error(e, e);
235 }
236
237 if (_log.isDebugEnabled()) {
238 _log.debug("Initialize plugin package");
239 }
240
241 PluginPackage pluginPackage = null;
242
243 try {
244 pluginPackage = initPluginPackage();
245 }
246 catch (Exception e) {
247 _log.error(e, e);
248 }
249
250 if (_log.isDebugEnabled()) {
251 _log.debug("Initialize portlets");
252 }
253
254 List<Portlet> portlets = null;
255
256 try {
257 portlets = initPortlets(pluginPackage);
258 }
259 catch (Exception e) {
260 _log.error(e, e);
261 }
262
263 if (_log.isDebugEnabled()) {
264 _log.debug("Initialize layout templates");
265 }
266
267 try {
268 initLayoutTemplates(pluginPackage, portlets);
269 }
270 catch (Exception e) {
271 _log.error(e, e);
272 }
273
274 if (_log.isDebugEnabled()) {
275 _log.debug("Initialize themes");
276 }
277
278 try {
279 initThemes(pluginPackage, portlets);
280 }
281 catch (Exception e) {
282 _log.error(e, e);
283 }
284
285 if (_log.isDebugEnabled()) {
286 _log.debug("Initialize indexers");
287 }
288
289 try {
290 initIndexers(portlets);
291 }
292 catch (Exception e) {
293 _log.error(e, e);
294 }
295
296 if (_log.isDebugEnabled()) {
297 _log.debug("Initialize schedulers");
298 }
299
300 try {
301 initSchedulers(portlets);
302 }
303 catch (Exception e) {
304 _log.error(e, e);
305 }
306
307 if (_log.isDebugEnabled()) {
308 _log.debug("Initialize poller processors");
309 }
310
311 try {
312 initPollerProcessors(portlets);
313 }
314 catch (Exception e) {
315 _log.error(e, e);
316 }
317
318 if (_log.isDebugEnabled()) {
319 _log.debug("Initialize POP message listeners");
320 }
321
322 try {
323 initPOPMessageListeners(portlets);
324 }
325 catch (Exception e) {
326 _log.error(e, e);
327 }
328
329 if (_log.isDebugEnabled()) {
330 _log.debug("Initialize social activity interpreters");
331 }
332
333 try {
334 initSocialActivityInterpreters(portlets);
335 }
336 catch (Exception e) {
337 _log.error(e, e);
338 }
339
340 if (_log.isDebugEnabled()) {
341 _log.debug("Initialize social request interpreters");
342 }
343
344 try {
345 initSocialRequestInterpreters(portlets);
346 }
347 catch (Exception e) {
348 _log.error(e, e);
349 }
350
351 if (_log.isDebugEnabled()) {
352 _log.debug("Initialize WebDAV storages");
353 }
354
355 try {
356 initWebDAVStorages(portlets);
357 }
358 catch (Exception e) {
359 _log.error(e, e);
360 }
361
362 if (_log.isDebugEnabled()) {
363 _log.debug("Initialize XML-RPC methods");
364 }
365
366 try {
367 initXmlRpcMethods(portlets);
368 }
369 catch (Exception e) {
370 _log.error(e, e);
371 }
372
373 if (_log.isDebugEnabled()) {
374 _log.debug("Initialize asset renderer factories");
375 }
376
377 try {
378 initAssetRendererFactories(portlets);
379 }
380 catch (Exception e) {
381 _log.error(e, e);
382 }
383
384 if (_log.isDebugEnabled()) {
385 _log.debug("Initialize workflow handlers");
386 }
387
388 try {
389 initWorkflowHandlers(portlets);
390 }
391 catch (Exception e) {
392 _log.error(e, e);
393 }
394
395 if (_log.isDebugEnabled()) {
396 _log.debug("Initialize web settings");
397 }
398
399 try {
400 initWebSettings();
401 }
402 catch (Exception e) {
403 _log.error(e, e);
404 }
405
406 if (_log.isDebugEnabled()) {
407 _log.debug("Initialize extension environment");
408 }
409
410 try {
411 initExt();
412 }
413 catch (Exception e) {
414 _log.error(e, e);
415 }
416
417 if (_log.isDebugEnabled()) {
418 _log.debug("Process global startup events");
419 }
420
421 try {
422 processGlobalStartupEvents();
423 }
424 catch (Exception e) {
425 _log.error(e, e);
426 }
427
428 if (_log.isDebugEnabled()) {
429 _log.debug("Initialize resource actions");
430 }
431
432 try {
433 initResourceActions(portlets);
434 }
435 catch (Exception e) {
436 _log.error(e, e);
437 }
438
439 if (_log.isDebugEnabled()) {
440 _log.debug("Initialize resource codes");
441 }
442
443 try {
444 initResourceCodes(portlets);
445 }
446 catch (Exception e) {
447 _log.error(e, e);
448 }
449
450 if (_log.isDebugEnabled()) {
451 _log.debug("Initialize companies");
452 }
453
454 try {
455 initCompanies();
456 }
457 catch (Exception e) {
458 _log.error(e, e);
459 }
460
461 if (_log.isDebugEnabled()) {
462 _log.debug("Initialize message resources");
463 }
464
465 try {
466 initMessageResources();
467 }
468 catch (Exception e) {
469 _log.error(e, e);
470 }
471
472 if (_log.isDebugEnabled()) {
473 _log.debug("Initialize plugins");
474 }
475
476 try {
477 initPlugins();
478 }
479 catch (Exception e) {
480 _log.error(e, e);
481 }
482 }
483
484 public void service(
485 HttpServletRequest request, HttpServletResponse response)
486 throws IOException, ServletException {
487
488 if (_log.isDebugEnabled()) {
489 _log.debug("Process service request");
490 }
491
492 if (processShutdownRequest(request, response)) {
493 if (_log.isDebugEnabled()) {
494 _log.debug("Processed shutdown request");
495 }
496
497 return;
498 }
499
500 if (processMaintenanceRequest(request, response)) {
501 if (_log.isDebugEnabled()) {
502 _log.debug("Processed maintenance request");
503 }
504
505 return;
506 }
507
508 if (_log.isDebugEnabled()) {
509 _log.debug("Get company id");
510 }
511
512 long companyId = getCompanyId(request);
513
514 if (_log.isDebugEnabled()) {
515 _log.debug("Set portal port");
516 }
517
518 setPortalPort(request);
519
520 if (_log.isDebugEnabled()) {
521 _log.debug("Check variables");
522 }
523
524 checkServletContext(request);
525 checkPortletSessionTracker(request);
526 checkPortletRequestProcessor(request);
527 checkTilesDefinitionsFactory();
528
529 if (_log.isDebugEnabled()) {
530 _log.debug("Encrypt request");
531 }
532
533 request = encryptRequest(request, companyId);
534
535 long userId = getUserId(request);
536 String remoteUser = getRemoteUser(request, userId);
537
538 if (_log.isDebugEnabled()) {
539 _log.debug("Protect request");
540 }
541
542 request = protectRequest(request, remoteUser);
543
544 if (_log.isDebugEnabled()) {
545 _log.debug("Set principal");
546 }
547
548 setPrincipalName(userId, remoteUser);
549
550 try {
551 if (_log.isDebugEnabled()) {
552 _log.debug(
553 "Authenticate user id " + userId + " and remote user " +
554 remoteUser);
555 }
556
557 userId = loginUser(request, response, userId, remoteUser);
558
559 if (_log.isDebugEnabled()) {
560 _log.debug("Authenticated user id " + userId);
561 }
562 }
563 catch (Exception e) {
564 _log.error(e, e);
565 }
566
567 if (_log.isDebugEnabled()) {
568 _log.debug("Process service pre events");
569 }
570
571 if (processServicePre(request, response, userId)) {
572 if (_log.isDebugEnabled()) {
573 _log.debug("Processing service pre events has errors");
574 }
575
576 return;
577 }
578
579 if (hasAbsoluteRedirect(request)) {
580 if (_log.isDebugEnabled()) {
581 String currentURL = PortalUtil.getCurrentURL(request);
582
583 _log.debug(
584 "Current URL " + currentURL + " has absolute redirect");
585 }
586
587 return;
588 }
589
590 if (!hasThemeDisplay(request)) {
591 if (_log.isDebugEnabled()) {
592 String currentURL = PortalUtil.getCurrentURL(request);
593
594 _log.debug(
595 "Current URL " + currentURL +
596 " does not have a theme display");
597 }
598
599 return;
600 }
601
602 try {
603 if (_log.isDebugEnabled()) {
604 _log.debug("Call parent service");
605 }
606
607 callParentService(request, response);
608 }
609 finally {
610 if (_log.isDebugEnabled()) {
611 _log.debug("Process service post events");
612 }
613
614 processServicePost(request, response);
615 }
616 }
617
618 protected void callParentDestroy() {
619 super.destroy();
620 }
621
622 protected void callParentInit() throws ServletException {
623 super.init();
624 }
625
626 protected void callParentService(
627 HttpServletRequest request, HttpServletResponse response)
628 throws IOException, ServletException {
629
630 super.service(request, response);
631 }
632
633 protected void checkPortletRequestProcessor(HttpServletRequest request)
634 throws ServletException {
635
636 ServletContext servletContext = getServletContext();
637
638 PortletRequestProcessor portletReqProcessor =
639 (PortletRequestProcessor)servletContext.getAttribute(
640 WebKeys.PORTLET_STRUTS_PROCESSOR);
641
642 if (portletReqProcessor == null) {
643 ModuleConfig moduleConfig = getModuleConfig(request);
644
645 portletReqProcessor =
646 PortletRequestProcessor.getInstance(this, moduleConfig);
647
648 servletContext.setAttribute(
649 WebKeys.PORTLET_STRUTS_PROCESSOR, portletReqProcessor);
650 }
651 }
652
653 protected void checkPortletSessionTracker(HttpServletRequest request) {
654 HttpSession session = request.getSession();
655
656 if (session.getAttribute(WebKeys.PORTLET_SESSION_TRACKER) != null) {
657 return;
658 }
659
660 session.setAttribute(
661 WebKeys.PORTLET_SESSION_TRACKER,
662 PortletSessionTracker.getInstance());
663 }
664
665 protected void checkServletContext(HttpServletRequest request) {
666 ServletContext servletContext = getServletContext();
667
668 request.setAttribute(WebKeys.CTX, servletContext);
669 }
670
671 protected void checkTilesDefinitionsFactory() {
672 ServletContext servletContext = getServletContext();
673
674 if (servletContext.getAttribute(
675 TilesUtilImpl.DEFINITIONS_FACTORY) != null) {
676
677 return;
678 }
679
680 servletContext.setAttribute(
681 TilesUtilImpl.DEFINITIONS_FACTORY,
682 servletContext.getAttribute(TilesUtilImpl.DEFINITIONS_FACTORY));
683 }
684
685 protected void checkWebSettings(String xml) throws DocumentException {
686 Document doc = SAXReaderUtil.read(xml);
687
688 Element root = doc.getRootElement();
689
690 int timeout = PropsValues.SESSION_TIMEOUT;
691
692 Element sessionConfig = root.element("session-config");
693
694 if (sessionConfig != null) {
695 String sessionTimeout = sessionConfig.elementText(
696 "session-timeout");
697
698 timeout = GetterUtil.getInteger(sessionTimeout, timeout);
699 }
700
701 PropsUtil.set(PropsKeys.SESSION_TIMEOUT, String.valueOf(timeout));
702
703 PropsValues.SESSION_TIMEOUT = timeout;
704
705 I18nServlet.setLanguageIds(root);
706 I18nFilter.setLanguageIds(I18nServlet.getLanguageIds());
707 }
708
709 protected void destroyCompanies() throws Exception {
710 long[] companyIds = PortalInstances.getCompanyIds();
711
712 for (int i = 0; i < companyIds.length; i++) {
713 destroyCompany(companyIds[i]);
714 }
715 }
716
717 protected void destroyCompany(long companyId) {
718 if (_log.isDebugEnabled()) {
719 _log.debug("Process shutdown events");
720 }
721
722 try {
723 EventsProcessorUtil.process(
724 PropsKeys.APPLICATION_SHUTDOWN_EVENTS,
725 PropsValues.APPLICATION_SHUTDOWN_EVENTS,
726 new String[] {String.valueOf(companyId)});
727 }
728 catch (Exception e) {
729 _log.error(e, e);
730 }
731 }
732
733 protected void destroyPortlets(List<Portlet> portlets) throws Exception {
734 Iterator<Portlet> itr = portlets.iterator();
735
736 while (itr.hasNext()) {
737 Portlet portlet = itr.next();
738
739 PortletInstanceFactoryUtil.destroy(portlet);
740 }
741 }
742
743 protected void destroySchedulerEntry(SchedulerEntry schedulerEntry)
744 throws Exception {
745
746 MessageListener schedulerEventListener =
747 schedulerEntry.getEventListener();
748
749 MessageBusUtil.unregisterMessageListener(
750 DestinationNames.SCHEDULER_DISPATCH, schedulerEventListener);
751
752 SchedulerEngineUtil.unschedule(schedulerEntry.getTrigger());
753 }
754
755 protected void destroySchedulers(List<Portlet> portlets) throws Exception {
756 if (!PropsValues.SCHEDULER_ENABLED) {
757 return;
758 }
759
760 for (String className : PropsValues.SCHEDULER_CLASSES) {
761 Scheduler scheduler = (Scheduler)InstancePool.get(className, false);
762
763 if (scheduler != null) {
764 scheduler.unschedule();
765 }
766 }
767
768 Iterator<Portlet> itr = portlets.iterator();
769
770 while (itr.hasNext()) {
771 Portlet portlet = itr.next();
772
773 String className = portlet.getSchedulerClass();
774
775 if (!portlet.isActive() || Validator.isNull(className)) {
776 continue;
777 }
778
779 Scheduler scheduler = (Scheduler)InstancePool.get(className, false);
780
781 if (scheduler != null) {
782 scheduler.unschedule();
783 }
784 }
785
786 for (Portlet portlet : portlets) {
787 if (!portlet.isActive()) {
788 continue;
789 }
790
791 List<SchedulerEntry> schedulerEntries =
792 portlet.getSchedulerEntries();
793
794 if ((schedulerEntries == null) || schedulerEntries.isEmpty()) {
795 continue;
796 }
797
798 for (SchedulerEntry schedulerEntry : schedulerEntries) {
799 destroySchedulerEntry(schedulerEntry);
800 }
801 }
802 }
803
804 protected HttpServletRequest encryptRequest(
805 HttpServletRequest request, long companyId) {
806
807 boolean encryptRequest = ParamUtil.getBoolean(request, WebKeys.ENCRYPT);
808
809 if (!encryptRequest) {
810 return request;
811 }
812
813 try {
814 Company company = CompanyLocalServiceUtil.getCompanyById(
815 companyId);
816
817 request = new EncryptedServletRequest(
818 request, company.getKeyObj());
819 }
820 catch (Exception e) {
821 }
822
823 return request;
824 }
825
826 protected long getCompanyId(HttpServletRequest request) {
827 return PortalInstances.getCompanyId(request);
828 }
829
830 protected String getRemoteUser(
831 HttpServletRequest request, long userId) {
832
833 String remoteUser = request.getRemoteUser();
834
835 if (!PropsValues.PORTAL_JAAS_ENABLE) {
836 HttpSession session = request.getSession();
837
838 String jRemoteUser = (String)session.getAttribute("j_remoteuser");
839
840 if (jRemoteUser != null) {
841 remoteUser = jRemoteUser;
842
843 session.removeAttribute("j_remoteuser");
844 }
845 }
846
847 if ((userId > 0) && (remoteUser == null)) {
848 remoteUser = String.valueOf(userId);
849 }
850
851 return remoteUser;
852 }
853
854 protected synchronized RequestProcessor getRequestProcessor(
855 ModuleConfig moduleConfig)
856 throws ServletException {
857
858 ServletContext servletContext = getServletContext();
859
860 String key = Globals.REQUEST_PROCESSOR_KEY + moduleConfig.getPrefix();
861
862 RequestProcessor processor =
863 (RequestProcessor)servletContext.getAttribute(key);
864
865 if (processor == null) {
866 ControllerConfig controllerConfig =
867 moduleConfig.getControllerConfig();
868
869 String processorClass = controllerConfig.getProcessorClass();
870
871 ClassLoader classLoader = PortalClassLoaderUtil.getClassLoader();
872
873 try {
874 processor = (RequestProcessor)classLoader.loadClass(
875 processorClass).newInstance();
876 }
877 catch (Exception e) {
878 throw new ServletException(e);
879 }
880
881 processor.init(this, moduleConfig);
882
883 servletContext.setAttribute(key, processor);
884 }
885
886 return processor;
887 }
888
889 protected long getUserId(HttpServletRequest request) {
890 return PortalUtil.getUserId(request);
891 }
892
893 protected boolean hasAbsoluteRedirect(HttpServletRequest request) {
894 if (request.getAttribute(
895 AbsoluteRedirectsResponse.class.getName()) == null) {
896
897 return false;
898 }
899 else {
900 return true;
901 }
902 }
903
904 protected boolean hasThemeDisplay(HttpServletRequest request) {
905 if (request.getAttribute(WebKeys.THEME_DISPLAY) == null) {
906 return false;
907 }
908 else {
909 return true;
910 }
911 }
912
913 protected void initAssetRendererFactories(List<Portlet> portlets)
914 throws Exception {
915
916 Iterator<Portlet> itr = portlets.iterator();
917
918 while (itr.hasNext()) {
919 Portlet portlet = itr.next();
920
921 List<AssetRendererFactory> assetRendererFactories =
922 portlet.getAssetRendererFactoryInstances();
923
924 if (assetRendererFactories == null) {
925 continue;
926 }
927
928 AssetRendererFactoryRegistryUtil.register(assetRendererFactories);
929 }
930 }
931
932 protected void initCompanies() throws Exception {
933 ServletContext servletContext = getServletContext();
934
935 String[] webIds = PortalInstances.getWebIds();
936
937 for (int i = 0; i < webIds.length; i++) {
938 PortalInstances.initCompany(servletContext, webIds[i]);
939 }
940 }
941
942 protected void initExt() throws Exception {
943 ServletContext servletContext = getServletContext();
944
945 ExtRegistry.registerPortal(servletContext);
946 }
947
948 protected void initIndexers(List<Portlet> portlets) throws Exception {
949 IndexerRegistryUtil.register(new PluginPackageIndexer());
950
951 Iterator<Portlet> itr = portlets.iterator();
952
953 while (itr.hasNext()) {
954 Portlet portlet = itr.next();
955
956 String indexerClass = portlet.getIndexerClass();
957
958 if (!portlet.isActive() || Validator.isNull(indexerClass)) {
959 continue;
960 }
961
962 Indexer indexer = (Indexer)InstancePool.get(indexerClass);
963
964 IndexerRegistryUtil.register(indexer);
965 }
966 }
967
968 protected void initLayoutTemplates(
969 PluginPackage pluginPackage, List<Portlet> portlets)
970 throws Exception {
971
972 ServletContext servletContext = getServletContext();
973
974 String[] xmls = new String[] {
975 HttpUtil.URLtoString(
976 servletContext.getResource(
977 "/WEB-INF/liferay-layout-templates.xml")),
978 HttpUtil.URLtoString(
979 servletContext.getResource(
980 "/WEB-INF/liferay-layout-templates-ext.xml"))
981 };
982
983 LayoutTemplateLocalServiceUtil.init(
984 servletContext, xmls, pluginPackage);
985 }
986
987 protected void initMessageResources() throws Exception {
988 ServletContext servletContext = getServletContext();
989
990 MultiMessageResources messageResources =
991 (MultiMessageResources)servletContext.getAttribute(
992 Globals.MESSAGES_KEY);
993
994 LanguageResources.init(messageResources);
995 }
996
997 protected PluginPackage initPluginPackage() throws Exception {
998 ServletContext servletContext = getServletContext();
999
1000 return PluginPackageHotDeployListener.readPluginPackage(servletContext);
1001 }
1002
1003 protected void initPlugins() throws Exception {
1004
1005
1008 PortalInitableUtil.flushInitables();
1009 HotDeployUtil.flushPrematureEvents();
1010 }
1011
1012 protected void initPollerProcessors(List<Portlet> portlets)
1013 throws Exception {
1014
1015 Iterator<Portlet> itr = portlets.iterator();
1016
1017 while (itr.hasNext()) {
1018 Portlet portlet = itr.next();
1019
1020 PollerProcessor pollerProcessor =
1021 portlet.getPollerProcessorInstance();
1022
1023 if (!portlet.isActive() || (pollerProcessor == null)) {
1024 continue;
1025 }
1026
1027 PollerProcessorUtil.addPollerProcessor(
1028 portlet.getPortletId(), pollerProcessor);
1029 }
1030 }
1031
1032 protected void initPOPMessageListeners(List<Portlet> portlets)
1033 throws Exception {
1034
1035 Iterator<Portlet> itr = portlets.iterator();
1036
1037 while (itr.hasNext()) {
1038 Portlet portlet = itr.next();
1039
1040 com.liferay.portal.kernel.pop.MessageListener popMessageListener =
1041 portlet.getPopMessageListenerInstance();
1042
1043 if (!portlet.isActive() || (popMessageListener == null)) {
1044 continue;
1045 }
1046
1047 POPServerUtil.addListener(popMessageListener);
1048 }
1049 }
1050
1051 protected void initPortletApp(
1052 Portlet portlet, ServletContext servletContext)
1053 throws PortletException {
1054
1055 PortletApp portletApp = portlet.getPortletApp();
1056
1057 PortletConfig portletConfig = PortletConfigFactory.create(
1058 portlet, servletContext);
1059
1060 PortletContext portletContext = portletConfig.getPortletContext();
1061
1062 Set<PortletFilter> portletFilters = portletApp.getPortletFilters();
1063
1064 for (PortletFilter portletFilter : portletFilters) {
1065 PortletFilterFactory.create(portletFilter, portletContext);
1066 }
1067
1068 Set<PortletURLListener> portletURLListeners =
1069 portletApp.getPortletURLListeners();
1070
1071 for (PortletURLListener portletURLListener : portletURLListeners) {
1072 PortletURLListenerFactory.create(portletURLListener);
1073 }
1074 }
1075
1076 protected List<Portlet> initPortlets(PluginPackage pluginPackage)
1077 throws Exception {
1078
1079 ServletContext servletContext = getServletContext();
1080
1081 String[] xmls = new String[] {
1082 HttpUtil.URLtoString(
1083 servletContext.getResource(
1084 "/WEB-INF/" + Portal.PORTLET_XML_FILE_NAME_CUSTOM)),
1085 HttpUtil.URLtoString(
1086 servletContext.getResource("/WEB-INF/portlet-ext.xml")),
1087 HttpUtil.URLtoString(
1088 servletContext.getResource("/WEB-INF/liferay-portlet.xml")),
1089 HttpUtil.URLtoString(
1090 servletContext.getResource("/WEB-INF/liferay-portlet-ext.xml")),
1091 HttpUtil.URLtoString(
1092 servletContext.getResource("/WEB-INF/web.xml"))
1093 };
1094
1095 PortletLocalServiceUtil.initEAR(servletContext, xmls, pluginPackage);
1096
1097 List<Portlet> portlets = PortletLocalServiceUtil.getPortlets();
1098
1099 for (int i = 0; i < portlets.size(); i++) {
1100 Portlet portlet = portlets.get(i);
1101
1102 if (i == 0) {
1103 initPortletApp(portlet, servletContext);
1104 }
1105
1106 PortletInstanceFactoryUtil.create(portlet, servletContext);
1107 }
1108
1109 return portlets;
1110 }
1111
1112 protected void initResourceActions(List<Portlet> portlets)
1113 throws Exception {
1114
1115 Iterator<Portlet> itr = portlets.iterator();
1116
1117 while (itr.hasNext()) {
1118 Portlet portlet = itr.next();
1119
1120 List<String> portletActions =
1121 ResourceActionsUtil.getPortletResourceActions(
1122 portlet.getPortletId());
1123
1124 ResourceActionLocalServiceUtil.checkResourceActions(
1125 portlet.getPortletId(), portletActions);
1126
1127 List<String> modelNames =
1128 ResourceActionsUtil.getPortletModelResources(
1129 portlet.getPortletId());
1130
1131 for (String modelName : modelNames) {
1132 List<String> modelActions =
1133 ResourceActionsUtil.getModelResourceActions(modelName);
1134
1135 ResourceActionLocalServiceUtil.checkResourceActions(
1136 modelName, modelActions);
1137 }
1138 }
1139 }
1140
1141 protected void initResourceCodes(List<Portlet> portlets) throws Exception {
1142 long[] companyIds = PortalInstances.getCompanyIdsBySQL();
1143
1144 Iterator<Portlet> itr = portlets.iterator();
1145
1146 while (itr.hasNext()) {
1147 Portlet portlet = itr.next();
1148
1149 List<String> modelNames =
1150 ResourceActionsUtil.getPortletModelResources(
1151 portlet.getPortletId());
1152
1153 for (long companyId : companyIds) {
1154 ResourceCodeLocalServiceUtil.checkResourceCodes(
1155 companyId, portlet.getPortletId());
1156
1157 for (String modelName : modelNames) {
1158 ResourceCodeLocalServiceUtil.checkResourceCodes(
1159 companyId, modelName);
1160 }
1161 }
1162 }
1163 }
1164
1165 protected void initSchedulerEntry(SchedulerEntry schedulerEntry)
1166 throws Exception {
1167
1168 MessageListener schedulerEventListener =
1169 (MessageListener)InstancePool.get(
1170 schedulerEntry.getEventListenerClass());
1171
1172 schedulerEventListener = new SchedulerEventMessageListenerWrapper(
1173 schedulerEventListener);
1174
1175 schedulerEntry.setEventListener(schedulerEventListener);
1176
1177 MessageBusUtil.registerMessageListener(
1178 DestinationNames.SCHEDULER_DISPATCH, schedulerEventListener);
1179
1180 SchedulerEngineUtil.schedule(
1181 schedulerEntry.getTrigger(), schedulerEntry.getDescription(),
1182 DestinationNames.SCHEDULER_DISPATCH, null);
1183 }
1184
1185 protected void initSchedulers(List<Portlet> portlets) throws Exception {
1186 if (!PropsValues.SCHEDULER_ENABLED) {
1187 return;
1188 }
1189
1190 for (String className : PropsValues.SCHEDULER_CLASSES) {
1191 Scheduler scheduler = (Scheduler)InstancePool.get(className);
1192
1193 scheduler.schedule();
1194 }
1195
1196 Iterator<Portlet> itr = portlets.iterator();
1197
1198 while (itr.hasNext()) {
1199 Portlet portlet = itr.next();
1200
1201 String className = portlet.getSchedulerClass();
1202
1203 if (!portlet.isActive() || Validator.isNull(className)) {
1204 continue;
1205 }
1206
1207 Scheduler scheduler = (Scheduler)InstancePool.get(className);
1208
1209 scheduler.schedule();
1210 }
1211
1212 for (Portlet portlet : portlets) {
1213 if (!portlet.isActive()) {
1214 continue;
1215 }
1216
1217 List<SchedulerEntry> schedulerEntries =
1218 portlet.getSchedulerEntries();
1219
1220 if ((schedulerEntries == null) || schedulerEntries.isEmpty()) {
1221 continue;
1222 }
1223
1224 for (SchedulerEntry schedulerEntry : schedulerEntries) {
1225 initSchedulerEntry(schedulerEntry);
1226 }
1227 }
1228 }
1229
1230 protected void initSocialActivityInterpreters(List<Portlet> portlets)
1231 throws Exception {
1232
1233 Iterator<Portlet> itr = portlets.iterator();
1234
1235 while (itr.hasNext()) {
1236 Portlet portlet = itr.next();
1237
1238 SocialActivityInterpreter socialActivityInterpreter =
1239 portlet.getSocialActivityInterpreterInstance();
1240
1241 if (!portlet.isActive() ||
1242 (socialActivityInterpreter == null)) {
1243
1244 continue;
1245 }
1246
1247 socialActivityInterpreter = new SocialActivityInterpreterImpl(
1248 portlet.getPortletId(), socialActivityInterpreter);
1249
1250 SocialActivityInterpreterLocalServiceUtil.addActivityInterpreter(
1251 socialActivityInterpreter);
1252 }
1253 }
1254
1255 protected void initSocialRequestInterpreters(List<Portlet> portlets)
1256 throws Exception {
1257
1258 Iterator<Portlet> itr = portlets.iterator();
1259
1260 while (itr.hasNext()) {
1261 Portlet portlet = itr.next();
1262
1263 SocialRequestInterpreter socialRequestInterpreter =
1264 portlet.getSocialRequestInterpreterInstance();
1265
1266 if (!portlet.isActive() || (socialRequestInterpreter == null)) {
1267 continue;
1268 }
1269
1270 socialRequestInterpreter = new SocialRequestInterpreterImpl(
1271 portlet.getPortletId(), socialRequestInterpreter);
1272
1273 SocialRequestInterpreterLocalServiceUtil.addRequestInterpreter(
1274 socialRequestInterpreter);
1275 }
1276 }
1277
1278 protected void initThemes(
1279 PluginPackage pluginPackage, List<Portlet> portlets)
1280 throws Exception {
1281
1282 ServletContext servletContext = getServletContext();
1283
1284 String[] xmls = new String[] {
1285 HttpUtil.URLtoString(
1286 servletContext.getResource(
1287 "/WEB-INF/liferay-look-and-feel.xml")),
1288 HttpUtil.URLtoString(
1289 servletContext.getResource(
1290 "/WEB-INF/liferay-look-and-feel-ext.xml"))
1291 };
1292
1293 ThemeLocalServiceUtil.init(
1294 servletContext, null, true, xmls, pluginPackage);
1295 }
1296
1297 protected void initVelocity() throws Exception {
1298 ServletContext servletContext = getServletContext();
1299
1300 String contextPath = PortalUtil.getPathContext();
1301
1302 VelocityContextPool.put(contextPath, servletContext);
1303 }
1304
1305 protected void initWebDAVStorages(List<Portlet> portlets) throws Exception {
1306 Iterator<Portlet> itr = portlets.iterator();
1307
1308 while (itr.hasNext()) {
1309 Portlet portlet = itr.next();
1310
1311 WebDAVStorage webDAVStorage = portlet.getWebDAVStorageInstance();
1312
1313 if (!portlet.isActive() || (webDAVStorage == null)) {
1314 continue;
1315 }
1316
1317 webDAVStorage.setToken(portlet.getWebDAVStorageToken());
1318
1319 WebDAVUtil.addStorage(webDAVStorage);
1320 }
1321 }
1322
1323 protected void initWebSettings() throws Exception {
1324 ServletContext servletContext = getServletContext();
1325
1326 String xml = HttpUtil.URLtoString(
1327 servletContext.getResource("/WEB-INF/web.xml"));
1328
1329 checkWebSettings(xml);
1330 }
1331
1332 protected void initWorkflowHandlers(List<Portlet> portlets)
1333 throws Exception {
1334
1335 Iterator<Portlet> itr = portlets.iterator();
1336
1337 while (itr.hasNext()) {
1338 Portlet portlet = itr.next();
1339
1340 List<WorkflowHandler> workflowHandlers =
1341 portlet.getWorkflowHandlerInstances();
1342
1343 if (workflowHandlers == null) {
1344 continue;
1345 }
1346
1347 WorkflowHandlerRegistryUtil.register(workflowHandlers);
1348 }
1349 }
1350
1351 protected void initXmlRpcMethods(List<Portlet> portlets) throws Exception {
1352 Iterator<Portlet> itr = portlets.iterator();
1353
1354 while (itr.hasNext()) {
1355 Portlet portlet = itr.next();
1356
1357 Method method = portlet.getXmlRpcMethodInstance();
1358
1359 if (!portlet.isActive() || (method == null)) {
1360 continue;
1361 }
1362
1363 XmlRpcServlet.registerMethod(method);
1364 }
1365 }
1366
1367 protected long loginUser(
1368 HttpServletRequest request, HttpServletResponse response,
1369 long userId, String remoteUser)
1370 throws PortalException, SystemException {
1371
1372 if ((userId > 0) || (remoteUser == null)) {
1373 return userId;
1374 }
1375
1376 userId = GetterUtil.getLong(remoteUser);
1377
1378 EventsProcessorUtil.process(
1379 PropsKeys.LOGIN_EVENTS_PRE, PropsValues.LOGIN_EVENTS_PRE, request,
1380 response);
1381
1382 User user = UserLocalServiceUtil.getUserById(userId);
1383
1384 if (PropsValues.USERS_UPDATE_LAST_LOGIN) {
1385 UserLocalServiceUtil.updateLastLogin(
1386 userId, request.getRemoteAddr());
1387 }
1388
1389 HttpSession session = request.getSession();
1390
1391 session.setAttribute(WebKeys.USER_ID, new Long(userId));
1392 session.setAttribute(Globals.LOCALE_KEY, user.getLocale());
1393
1394 EventsProcessorUtil.process(
1395 PropsKeys.LOGIN_EVENTS_POST, PropsValues.LOGIN_EVENTS_POST,
1396 request, response);
1397
1398 return userId;
1399 }
1400
1401 protected void processGlobalShutdownEvents() throws Exception {
1402 EventsProcessorUtil.process(
1403 PropsKeys.GLOBAL_SHUTDOWN_EVENTS,
1404 PropsValues.GLOBAL_SHUTDOWN_EVENTS);
1405
1406 super.destroy();
1407 }
1408
1409 protected void processGlobalStartupEvents() throws Exception {
1410 EventsProcessorUtil.process(
1411 PropsKeys.GLOBAL_STARTUP_EVENTS, PropsValues.GLOBAL_STARTUP_EVENTS);
1412 }
1413
1414 protected boolean processMaintenanceRequest(
1415 HttpServletRequest request, HttpServletResponse response)
1416 throws IOException, ServletException {
1417
1418 if (!MaintenanceUtil.isMaintaining()) {
1419 return false;
1420 }
1421
1422 RequestDispatcher requestDispatcher = request.getRequestDispatcher(
1423 "/html/portal/maintenance.jsp");
1424
1425 requestDispatcher.include(request, response);
1426
1427 return true;
1428 }
1429
1430 protected void processServicePost(
1431 HttpServletRequest request, HttpServletResponse response) {
1432
1433 try {
1434 EventsProcessorUtil.process(
1435 PropsKeys.SERVLET_SERVICE_EVENTS_POST,
1436 PropsValues.SERVLET_SERVICE_EVENTS_POST, request, response);
1437 }
1438 catch (Exception e) {
1439 _log.error(e, e);
1440 }
1441
1442 response.addHeader(
1443 _LIFERAY_PORTAL_REQUEST_HEADER, ReleaseInfo.getReleaseInfo());
1444 }
1445
1446 protected boolean processServicePre(
1447 HttpServletRequest request, HttpServletResponse response,
1448 long userId)
1449 throws IOException, ServletException {
1450
1451 try {
1452 EventsProcessorUtil.process(
1453 PropsKeys.SERVLET_SERVICE_EVENTS_PRE,
1454 PropsValues.SERVLET_SERVICE_EVENTS_PRE, request, response);
1455 }
1456 catch (Exception e) {
1457 Throwable cause = e.getCause();
1458
1459 if (cause instanceof NoSuchLayoutException) {
1460 sendError(
1461 HttpServletResponse.SC_NOT_FOUND, cause, request, response);
1462
1463 return true;
1464 }
1465 else if (cause instanceof PrincipalException) {
1466 processServicePrePrincipalException(
1467 cause, userId, request, response);
1468
1469 return true;
1470 }
1471
1472 _log.error(e, e);
1473
1474 request.setAttribute(PageContext.EXCEPTION, e);
1475
1476 ServletContext servletContext = getServletContext();
1477
1478 StrutsUtil.forward(
1479 PropsValues.SERVLET_SERVICE_EVENTS_PRE_ERROR_PAGE,
1480 servletContext, request, response);
1481
1482 return true;
1483 }
1484
1485 return false;
1486 }
1487
1488 protected void processServicePrePrincipalException(
1489 Throwable t, long userId, HttpServletRequest request,
1490 HttpServletResponse response)
1491 throws IOException, ServletException {
1492
1493 if (userId > 0) {
1494 sendError(
1495 HttpServletResponse.SC_UNAUTHORIZED, t, request, response);
1496
1497 return;
1498 }
1499
1500 String redirect =
1501 request.getContextPath() + Portal.PATH_MAIN + "/portal/login";
1502
1503 String currentURL = PortalUtil.getCurrentURL(request);
1504
1505 redirect = HttpUtil.addParameter(redirect, "redirect", currentURL);
1506
1507 long plid = ParamUtil.getLong(request, "p_l_id");
1508
1509 if (plid > 0) {
1510 try {
1511 Layout layout = LayoutLocalServiceUtil.getLayout(plid);
1512
1513 if (layout.getGroup().isStagingGroup()) {
1514 Group group = GroupLocalServiceUtil.getGroup(
1515 layout.getCompanyId(), GroupConstants.GUEST);
1516
1517 plid = group.getDefaultPublicPlid();
1518 }
1519 else if (layout.isPrivateLayout()) {
1520 plid = LayoutLocalServiceUtil.getDefaultPlid(
1521 layout.getGroupId(), false);
1522 }
1523
1524 redirect = HttpUtil.addParameter(redirect, "p_l_id", plid);
1525 }
1526 catch (Exception e) {
1527 }
1528 }
1529
1530 response.sendRedirect(redirect);
1531 }
1532
1533 protected boolean processShutdownRequest(
1534 HttpServletRequest request, HttpServletResponse response)
1535 throws IOException {
1536
1537 if (!ShutdownUtil.isShutdown()) {
1538 return false;
1539 }
1540
1541 response.setContentType(ContentTypes.TEXT_HTML_UTF8);
1542
1543 String html = ContentUtil.get(
1544 "com/liferay/portal/dependencies/shutdown.html");
1545
1546 response.getOutputStream().print(html);
1547
1548 return true;
1549 }
1550
1551 protected void processStartupEvents() throws Exception {
1552 StartupAction startupAction = new StartupAction();
1553
1554 startupAction.run(null);
1555 }
1556
1557 protected HttpServletRequest protectRequest(
1558 HttpServletRequest request, String remoteUser) {
1559
1560
1566 return new ProtectedServletRequest(request, remoteUser);
1567 }
1568
1569 protected void sendError(
1570 int status, Throwable t, HttpServletRequest request,
1571 HttpServletResponse response)
1572 throws IOException, ServletException {
1573
1574 DynamicServletRequest dynamicRequest = new DynamicServletRequest(
1575 request);
1576
1577
1579 dynamicRequest.setParameter("p_l_id", StringPool.BLANK);
1580
1581 PortalUtil.sendError(status, (Exception)t, dynamicRequest, response);
1582 }
1583
1584 protected void setPortalPort(HttpServletRequest request) {
1585 PortalUtil.setPortalPort(request);
1586 }
1587
1588 protected void setPrincipalName(long userId, String remoteUser) {
1589 if ((userId == 0) && (remoteUser == null)) {
1590 return;
1591 }
1592
1593 String name = String.valueOf(userId);
1594
1595 if (remoteUser != null) {
1596 name = remoteUser;
1597 }
1598
1599 PrincipalThreadLocal.setName(name);
1600 }
1601
1602 private static final String _LIFERAY_PORTAL_REQUEST_HEADER =
1603 "Liferay-Portal";
1604
1605 private static Log _log = LogFactoryUtil.getLog(MainServlet.class);
1606
1607}