001
014
015 package com.liferay.portal.servlet;
016
017 import com.liferay.portal.NoSuchLayoutException;
018 import com.liferay.portal.deploy.hot.PluginPackageHotDeployListener;
019 import com.liferay.portal.events.EventsProcessorUtil;
020 import com.liferay.portal.events.StartupAction;
021 import com.liferay.portal.kernel.cache.Lifecycle;
022 import com.liferay.portal.kernel.cache.ThreadLocalCacheManager;
023 import com.liferay.portal.kernel.deploy.hot.HotDeployUtil;
024 import com.liferay.portal.kernel.exception.PortalException;
025 import com.liferay.portal.kernel.exception.SystemException;
026 import com.liferay.portal.kernel.log.Log;
027 import com.liferay.portal.kernel.log.LogFactoryUtil;
028 import com.liferay.portal.kernel.plugin.PluginPackage;
029 import com.liferay.portal.kernel.scheduler.SchedulerEngineUtil;
030 import com.liferay.portal.kernel.scheduler.SchedulerEntry;
031 import com.liferay.portal.kernel.scheduler.SchedulerEntryImpl;
032 import com.liferay.portal.kernel.scheduler.TimeUnit;
033 import com.liferay.portal.kernel.scheduler.TriggerType;
034 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
035 import com.liferay.portal.kernel.servlet.PortletSessionTracker;
036 import com.liferay.portal.kernel.servlet.ProtectedServletRequest;
037 import com.liferay.portal.kernel.servlet.ServletContextPool;
038 import com.liferay.portal.kernel.util.ContentTypes;
039 import com.liferay.portal.kernel.util.GetterUtil;
040 import com.liferay.portal.kernel.util.HttpUtil;
041 import com.liferay.portal.kernel.util.ParamUtil;
042 import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
043 import com.liferay.portal.kernel.util.PortalInitableUtil;
044 import com.liferay.portal.kernel.util.PropsKeys;
045 import com.liferay.portal.kernel.util.ReleaseInfo;
046 import com.liferay.portal.kernel.util.StringPool;
047 import com.liferay.portal.kernel.xml.Document;
048 import com.liferay.portal.kernel.xml.DocumentException;
049 import com.liferay.portal.kernel.xml.Element;
050 import com.liferay.portal.kernel.xml.SAXReaderUtil;
051 import com.liferay.portal.model.Company;
052 import com.liferay.portal.model.Group;
053 import com.liferay.portal.model.GroupConstants;
054 import com.liferay.portal.model.Layout;
055 import com.liferay.portal.model.Portlet;
056 import com.liferay.portal.model.PortletApp;
057 import com.liferay.portal.model.PortletFilter;
058 import com.liferay.portal.model.PortletURLListener;
059 import com.liferay.portal.model.User;
060 import com.liferay.portal.plugin.PluginPackageIndexer;
061 import com.liferay.portal.security.auth.PrincipalException;
062 import com.liferay.portal.security.auth.PrincipalThreadLocal;
063 import com.liferay.portal.security.permission.ResourceActionsUtil;
064 import com.liferay.portal.service.CompanyLocalServiceUtil;
065 import com.liferay.portal.service.GroupLocalServiceUtil;
066 import com.liferay.portal.service.LayoutLocalServiceUtil;
067 import com.liferay.portal.service.LayoutTemplateLocalServiceUtil;
068 import com.liferay.portal.service.PortletLocalServiceUtil;
069 import com.liferay.portal.service.ResourceActionLocalServiceUtil;
070 import com.liferay.portal.service.ResourceCodeLocalServiceUtil;
071 import com.liferay.portal.service.ThemeLocalServiceUtil;
072 import com.liferay.portal.service.UserLocalServiceUtil;
073 import com.liferay.portal.servlet.filters.i18n.I18nFilter;
074 import com.liferay.portal.struts.PortletRequestProcessor;
075 import com.liferay.portal.struts.StrutsUtil;
076 import com.liferay.portal.util.ContentUtil;
077 import com.liferay.portal.util.ExtRegistry;
078 import com.liferay.portal.util.MaintenanceUtil;
079 import com.liferay.portal.util.Portal;
080 import com.liferay.portal.util.PortalInstances;
081 import com.liferay.portal.util.PortalUtil;
082 import com.liferay.portal.util.PropsUtil;
083 import com.liferay.portal.util.PropsValues;
084 import com.liferay.portal.util.ShutdownUtil;
085 import com.liferay.portal.util.WebKeys;
086 import com.liferay.portlet.PortletBagFactory;
087 import com.liferay.portlet.PortletConfigFactoryUtil;
088 import com.liferay.portlet.PortletFilterFactory;
089 import com.liferay.portlet.PortletInstanceFactoryUtil;
090 import com.liferay.portlet.PortletURLListenerFactory;
091 import com.liferay.portlet.social.messaging.CheckEquityLogMessageListener;
092 import com.liferay.util.servlet.DynamicServletRequest;
093 import com.liferay.util.servlet.EncryptedServletRequest;
094
095 import java.io.IOException;
096
097 import java.util.Iterator;
098 import java.util.List;
099 import java.util.Set;
100
101 import javax.portlet.PortletConfig;
102 import javax.portlet.PortletContext;
103 import javax.portlet.PortletException;
104
105 import javax.servlet.RequestDispatcher;
106 import javax.servlet.ServletContext;
107 import javax.servlet.ServletException;
108 import javax.servlet.http.HttpServletRequest;
109 import javax.servlet.http.HttpServletResponse;
110 import javax.servlet.http.HttpSession;
111 import javax.servlet.jsp.PageContext;
112
113 import org.apache.struts.Globals;
114 import org.apache.struts.action.ActionServlet;
115 import org.apache.struts.action.RequestProcessor;
116 import org.apache.struts.config.ControllerConfig;
117 import org.apache.struts.config.ModuleConfig;
118 import org.apache.struts.tiles.TilesUtilImpl;
119
120
125 public class MainServlet extends ActionServlet {
126
127 public void destroy() {
128 List<Portlet> portlets = PortletLocalServiceUtil.getPortlets();
129
130 if (_log.isDebugEnabled()) {
131 _log.debug("Destroy schedulers");
132 }
133
134 try {
135 destroySchedulers(portlets);
136 }
137 catch (Exception e) {
138 _log.error(e, e);
139 }
140
141 if (_log.isDebugEnabled()) {
142 _log.debug("Destroy portlets");
143 }
144
145 try {
146 destroyPortlets(portlets);
147 }
148 catch (Exception e) {
149 _log.error(e, e);
150 }
151
152 if (_log.isDebugEnabled()) {
153 _log.debug("Destroy companies");
154 }
155
156 try {
157 destroyCompanies();
158 }
159 catch (Exception e) {
160 _log.error(e, e);
161 }
162
163 if (_log.isDebugEnabled()) {
164 _log.debug("Process global shutdown events");
165 }
166
167 try {
168 processGlobalShutdownEvents();
169 }
170 catch (Exception e) {
171 _log.error(e, e);
172 }
173
174 if (_log.isDebugEnabled()) {
175 _log.debug("Destroy");
176 }
177
178 callParentDestroy();
179 }
180
181 public void init() throws ServletException {
182 if (_log.isDebugEnabled()) {
183 _log.debug("Initialize");
184 }
185
186 callParentInit();
187
188 if (_log.isDebugEnabled()) {
189 _log.debug("Process startup events");
190 }
191
192 try {
193 processStartupEvents();
194 }
195 catch (Exception e) {
196 _log.error(e, e);
197
198 System.out.println(
199 "Stopping the server due to unexpected startup errors");
200
201 System.exit(0);
202 }
203
204 if (_log.isDebugEnabled()) {
205 _log.debug("Initialize servlet context pool");
206 }
207
208 try {
209 initServletContextPool();
210 }
211 catch (Exception e) {
212 _log.error(e, e);
213 }
214
215 if (_log.isDebugEnabled()) {
216 _log.debug("Initialize plugin package");
217 }
218
219 PluginPackage pluginPackage = null;
220
221 try {
222 pluginPackage = initPluginPackage();
223 }
224 catch (Exception e) {
225 _log.error(e, e);
226 }
227
228 if (_log.isDebugEnabled()) {
229 _log.debug("Initialize portlets");
230 }
231
232 List<Portlet> portlets = null;
233
234 try {
235 portlets = initPortlets(pluginPackage);
236 }
237 catch (Exception e) {
238 _log.error(e, e);
239 }
240
241 if (_log.isDebugEnabled()) {
242 _log.debug("Initialize layout templates");
243 }
244
245 try {
246 initLayoutTemplates(pluginPackage, portlets);
247 }
248 catch (Exception e) {
249 _log.error(e, e);
250 }
251
252 if (_log.isDebugEnabled()) {
253 _log.debug("Initialize themes");
254 }
255
256 try {
257 initThemes(pluginPackage, portlets);
258 }
259 catch (Exception e) {
260 _log.error(e, e);
261 }
262
263 if (_log.isDebugEnabled()) {
264 _log.debug("Initialize social log scheduler");
265 }
266
267 try {
268 initSocialEquityLogScheduler();
269 }
270 catch (Exception e) {
271 _log.error(e, e);
272 }
273
274 if (_log.isDebugEnabled()) {
275 _log.debug("Initialize web settings");
276 }
277
278 try {
279 initWebSettings();
280 }
281 catch (Exception e) {
282 _log.error(e, e);
283 }
284
285 if (_log.isDebugEnabled()) {
286 _log.debug("Initialize extension environment");
287 }
288
289 try {
290 initExt();
291 }
292 catch (Exception e) {
293 _log.error(e, e);
294 }
295
296 if (_log.isDebugEnabled()) {
297 _log.debug("Process global startup events");
298 }
299
300 try {
301 processGlobalStartupEvents();
302 }
303 catch (Exception e) {
304 _log.error(e, e);
305 }
306
307 if (_log.isDebugEnabled()) {
308 _log.debug("Initialize resource actions");
309 }
310
311 try {
312 initResourceActions(portlets);
313 }
314 catch (Exception e) {
315 _log.error(e, e);
316 }
317
318 if (_log.isDebugEnabled()) {
319 _log.debug("Initialize resource codes");
320 }
321
322 try {
323 initResourceCodes(portlets);
324 }
325 catch (Exception e) {
326 _log.error(e, e);
327 }
328
329 if (_log.isDebugEnabled()) {
330 _log.debug("Initialize companies");
331 }
332
333 try {
334 initCompanies();
335 }
336 catch (Exception e) {
337 _log.error(e, e);
338 }
339
340 if (_log.isDebugEnabled()) {
341 _log.debug("Initialize message resources");
342 }
343
344 if (_log.isDebugEnabled()) {
345 _log.debug("Initialize plugins");
346 }
347
348 try {
349 initPlugins();
350 }
351 catch (Exception e) {
352 _log.error(e, e);
353 }
354 }
355
356 public void service(
357 HttpServletRequest request, HttpServletResponse response)
358 throws IOException, ServletException {
359
360 if (_log.isDebugEnabled()) {
361 _log.debug("Process service request");
362 }
363
364 if (processShutdownRequest(request, response)) {
365 if (_log.isDebugEnabled()) {
366 _log.debug("Processed shutdown request");
367 }
368
369 return;
370 }
371
372 if (processMaintenanceRequest(request, response)) {
373 if (_log.isDebugEnabled()) {
374 _log.debug("Processed maintenance request");
375 }
376
377 return;
378 }
379
380 if (_log.isDebugEnabled()) {
381 _log.debug("Get company id");
382 }
383
384 long companyId = getCompanyId(request);
385
386 if (_log.isDebugEnabled()) {
387 _log.debug("Set portal port");
388 }
389
390 setPortalPort(request);
391
392 if (_log.isDebugEnabled()) {
393 _log.debug("Check variables");
394 }
395
396 checkServletContext(request);
397 checkPortletSessionTracker(request);
398 checkPortletRequestProcessor(request);
399 checkTilesDefinitionsFactory();
400
401 if (_log.isDebugEnabled()) {
402 _log.debug("Encrypt request");
403 }
404
405 request = encryptRequest(request, companyId);
406
407 long userId = getUserId(request);
408 String remoteUser = getRemoteUser(request, userId);
409
410 if (_log.isDebugEnabled()) {
411 _log.debug("Protect request");
412 }
413
414 request = protectRequest(request, remoteUser);
415
416 if (_log.isDebugEnabled()) {
417 _log.debug("Set principal");
418 }
419
420 setPrincipalName(userId, remoteUser);
421
422 try {
423 if (_log.isDebugEnabled()) {
424 _log.debug(
425 "Authenticate user id " + userId + " and remote user " +
426 remoteUser);
427 }
428
429 userId = loginUser(request, response, userId, remoteUser);
430
431 if (_log.isDebugEnabled()) {
432 _log.debug("Authenticated user id " + userId);
433 }
434 }
435 catch (Exception e) {
436 _log.error(e, e);
437 }
438
439 if (_log.isDebugEnabled()) {
440 _log.debug("Process service pre events");
441 }
442
443 if (processServicePre(request, response, userId)) {
444 if (_log.isDebugEnabled()) {
445 _log.debug("Processing service pre events has errors");
446 }
447
448 return;
449 }
450
451 if (hasAbsoluteRedirect(request)) {
452 if (_log.isDebugEnabled()) {
453 String currentURL = PortalUtil.getCurrentURL(request);
454
455 _log.debug(
456 "Current URL " + currentURL + " has absolute redirect");
457 }
458
459 return;
460 }
461
462 if (!hasThemeDisplay(request)) {
463 if (_log.isDebugEnabled()) {
464 String currentURL = PortalUtil.getCurrentURL(request);
465
466 _log.debug(
467 "Current URL " + currentURL +
468 " does not have a theme display");
469 }
470
471 return;
472 }
473
474 try {
475 if (_log.isDebugEnabled()) {
476 _log.debug("Call parent service");
477 }
478
479 callParentService(request, response);
480 }
481 finally {
482 if (_log.isDebugEnabled()) {
483 _log.debug("Process service post events");
484 }
485
486 processServicePost(request, response);
487 }
488 }
489
490 protected void callParentDestroy() {
491 super.destroy();
492 }
493
494 protected void callParentInit() throws ServletException {
495 super.init();
496 }
497
498 protected void callParentService(
499 HttpServletRequest request, HttpServletResponse response)
500 throws IOException, ServletException {
501
502 super.service(request, response);
503 }
504
505 protected void checkPortletRequestProcessor(HttpServletRequest request)
506 throws ServletException {
507
508 ServletContext servletContext = getServletContext();
509
510 PortletRequestProcessor portletReqProcessor =
511 (PortletRequestProcessor)servletContext.getAttribute(
512 WebKeys.PORTLET_STRUTS_PROCESSOR);
513
514 if (portletReqProcessor == null) {
515 ModuleConfig moduleConfig = getModuleConfig(request);
516
517 portletReqProcessor =
518 PortletRequestProcessor.getInstance(this, moduleConfig);
519
520 servletContext.setAttribute(
521 WebKeys.PORTLET_STRUTS_PROCESSOR, portletReqProcessor);
522 }
523 }
524
525 protected void checkPortletSessionTracker(HttpServletRequest request) {
526 HttpSession session = request.getSession();
527
528 if (session.getAttribute(WebKeys.PORTLET_SESSION_TRACKER) != null) {
529 return;
530 }
531
532 session.setAttribute(
533 WebKeys.PORTLET_SESSION_TRACKER,
534 PortletSessionTracker.getInstance());
535 }
536
537 protected void checkServletContext(HttpServletRequest request) {
538 ServletContext servletContext = getServletContext();
539
540 request.setAttribute(WebKeys.CTX, servletContext);
541 }
542
543 protected void checkTilesDefinitionsFactory() {
544 ServletContext servletContext = getServletContext();
545
546 if (servletContext.getAttribute(
547 TilesUtilImpl.DEFINITIONS_FACTORY) != null) {
548
549 return;
550 }
551
552 servletContext.setAttribute(
553 TilesUtilImpl.DEFINITIONS_FACTORY,
554 servletContext.getAttribute(TilesUtilImpl.DEFINITIONS_FACTORY));
555 }
556
557 protected void checkWebSettings(String xml) throws DocumentException {
558 Document doc = SAXReaderUtil.read(xml);
559
560 Element root = doc.getRootElement();
561
562 int timeout = PropsValues.SESSION_TIMEOUT;
563
564 Element sessionConfig = root.element("session-config");
565
566 if (sessionConfig != null) {
567 String sessionTimeout = sessionConfig.elementText(
568 "session-timeout");
569
570 timeout = GetterUtil.getInteger(sessionTimeout, timeout);
571 }
572
573 PropsUtil.set(PropsKeys.SESSION_TIMEOUT, String.valueOf(timeout));
574
575 PropsValues.SESSION_TIMEOUT = timeout;
576
577 I18nServlet.setLanguageIds(root);
578 I18nFilter.setLanguageIds(I18nServlet.getLanguageIds());
579 }
580
581 protected void destroyCompanies() throws Exception {
582 long[] companyIds = PortalInstances.getCompanyIds();
583
584 for (int i = 0; i < companyIds.length; i++) {
585 destroyCompany(companyIds[i]);
586 }
587 }
588
589 protected void destroyCompany(long companyId) {
590 if (_log.isDebugEnabled()) {
591 _log.debug("Process shutdown events");
592 }
593
594 try {
595 EventsProcessorUtil.process(
596 PropsKeys.APPLICATION_SHUTDOWN_EVENTS,
597 PropsValues.APPLICATION_SHUTDOWN_EVENTS,
598 new String[] {String.valueOf(companyId)});
599 }
600 catch (Exception e) {
601 _log.error(e, e);
602 }
603 }
604
605 protected void destroyPortlets(List<Portlet> portlets) throws Exception {
606 Iterator<Portlet> itr = portlets.iterator();
607
608 while (itr.hasNext()) {
609 Portlet portlet = itr.next();
610
611 PortletInstanceFactoryUtil.destroy(portlet);
612 }
613 }
614
615 protected void destroySchedulers(List<Portlet> portlets) throws Exception {
616 if (!PropsValues.SCHEDULER_ENABLED) {
617 return;
618 }
619
620 for (Portlet portlet : portlets) {
621 if (!portlet.isActive()) {
622 continue;
623 }
624
625 List<SchedulerEntry> schedulerEntries =
626 portlet.getSchedulerEntries();
627
628 if ((schedulerEntries == null) || schedulerEntries.isEmpty()) {
629 continue;
630 }
631
632 for (SchedulerEntry schedulerEntry : schedulerEntries) {
633 SchedulerEngineUtil.unschedule(schedulerEntry);
634 }
635 }
636 }
637
638 protected HttpServletRequest encryptRequest(
639 HttpServletRequest request, long companyId) {
640
641 boolean encryptRequest = ParamUtil.getBoolean(request, WebKeys.ENCRYPT);
642
643 if (!encryptRequest) {
644 return request;
645 }
646
647 try {
648 Company company = CompanyLocalServiceUtil.getCompanyById(
649 companyId);
650
651 request = new EncryptedServletRequest(
652 request, company.getKeyObj());
653 }
654 catch (Exception e) {
655 }
656
657 return request;
658 }
659
660 protected long getCompanyId(HttpServletRequest request) {
661 return PortalInstances.getCompanyId(request);
662 }
663
664 protected String getRemoteUser(
665 HttpServletRequest request, long userId) {
666
667 String remoteUser = request.getRemoteUser();
668
669 if (!PropsValues.PORTAL_JAAS_ENABLE) {
670 HttpSession session = request.getSession();
671
672 String jRemoteUser = (String)session.getAttribute("j_remoteuser");
673
674 if (jRemoteUser != null) {
675 remoteUser = jRemoteUser;
676
677 session.removeAttribute("j_remoteuser");
678 }
679 }
680
681 if ((userId > 0) && (remoteUser == null)) {
682 remoteUser = String.valueOf(userId);
683 }
684
685 return remoteUser;
686 }
687
688 protected synchronized RequestProcessor getRequestProcessor(
689 ModuleConfig moduleConfig)
690 throws ServletException {
691
692 ServletContext servletContext = getServletContext();
693
694 String key = Globals.REQUEST_PROCESSOR_KEY + moduleConfig.getPrefix();
695
696 RequestProcessor processor =
697 (RequestProcessor)servletContext.getAttribute(key);
698
699 if (processor == null) {
700 ControllerConfig controllerConfig =
701 moduleConfig.getControllerConfig();
702
703 String processorClass = controllerConfig.getProcessorClass();
704
705 ClassLoader classLoader = PortalClassLoaderUtil.getClassLoader();
706
707 try {
708 processor = (RequestProcessor)classLoader.loadClass(
709 processorClass).newInstance();
710 }
711 catch (Exception e) {
712 throw new ServletException(e);
713 }
714
715 processor.init(this, moduleConfig);
716
717 servletContext.setAttribute(key, processor);
718 }
719
720 return processor;
721 }
722
723 protected long getUserId(HttpServletRequest request) {
724 return PortalUtil.getUserId(request);
725 }
726
727 protected boolean hasAbsoluteRedirect(HttpServletRequest request) {
728 if (request.getAttribute(
729 AbsoluteRedirectsResponse.class.getName()) == null) {
730
731 return false;
732 }
733 else {
734 return true;
735 }
736 }
737
738 protected boolean hasThemeDisplay(HttpServletRequest request) {
739 if (request.getAttribute(WebKeys.THEME_DISPLAY) == null) {
740 return false;
741 }
742 else {
743 return true;
744 }
745 }
746
747 protected void initCompanies() throws Exception {
748 ServletContext servletContext = getServletContext();
749
750 String[] webIds = PortalInstances.getWebIds();
751
752 for (int i = 0; i < webIds.length; i++) {
753 PortalInstances.initCompany(servletContext, webIds[i]);
754 }
755 }
756
757 protected void initExt() throws Exception {
758 ServletContext servletContext = getServletContext();
759
760 ExtRegistry.registerPortal(servletContext);
761 }
762
763 protected void initLayoutTemplates(
764 PluginPackage pluginPackage, List<Portlet> portlets)
765 throws Exception {
766
767 ServletContext servletContext = getServletContext();
768
769 String[] xmls = new String[] {
770 HttpUtil.URLtoString(
771 servletContext.getResource(
772 "/WEB-INF/liferay-layout-templates.xml")),
773 HttpUtil.URLtoString(
774 servletContext.getResource(
775 "/WEB-INF/liferay-layout-templates-ext.xml"))
776 };
777
778 LayoutTemplateLocalServiceUtil.init(
779 servletContext, xmls, pluginPackage);
780 }
781
782 protected PluginPackage initPluginPackage() throws Exception {
783 ServletContext servletContext = getServletContext();
784
785 IndexerRegistryUtil.register(new PluginPackageIndexer());
786
787 return PluginPackageHotDeployListener.readPluginPackage(servletContext);
788 }
789
790 protected void initPlugins() throws Exception {
791
792
793
794
795 HotDeployUtil.flushPrematureEvents();
796 PortalInitableUtil.flushInitables();
797 }
798
799 protected void initPortletApp(
800 Portlet portlet, ServletContext servletContext)
801 throws PortletException {
802
803 PortletApp portletApp = portlet.getPortletApp();
804
805 PortletConfig portletConfig = PortletConfigFactoryUtil.create(
806 portlet, servletContext);
807
808 PortletContext portletContext = portletConfig.getPortletContext();
809
810 Set<PortletFilter> portletFilters = portletApp.getPortletFilters();
811
812 for (PortletFilter portletFilter : portletFilters) {
813 PortletFilterFactory.create(portletFilter, portletContext);
814 }
815
816 Set<PortletURLListener> portletURLListeners =
817 portletApp.getPortletURLListeners();
818
819 for (PortletURLListener portletURLListener : portletURLListeners) {
820 PortletURLListenerFactory.create(portletURLListener);
821 }
822 }
823
824 protected List<Portlet> initPortlets(PluginPackage pluginPackage)
825 throws Exception {
826
827 ServletContext servletContext = getServletContext();
828
829 String[] xmls = new String[] {
830 HttpUtil.URLtoString(
831 servletContext.getResource(
832 "/WEB-INF/" + Portal.PORTLET_XML_FILE_NAME_CUSTOM)),
833 HttpUtil.URLtoString(
834 servletContext.getResource("/WEB-INF/portlet-ext.xml")),
835 HttpUtil.URLtoString(
836 servletContext.getResource("/WEB-INF/liferay-portlet.xml")),
837 HttpUtil.URLtoString(
838 servletContext.getResource("/WEB-INF/liferay-portlet-ext.xml")),
839 HttpUtil.URLtoString(
840 servletContext.getResource("/WEB-INF/web.xml"))
841 };
842
843 PortletLocalServiceUtil.initEAR(servletContext, xmls, pluginPackage);
844
845 PortletBagFactory portletBagFactory = new PortletBagFactory();
846
847 portletBagFactory.setClassLoader(
848 PortalClassLoaderUtil.getClassLoader());
849 portletBagFactory.setServletContext(servletContext);
850 portletBagFactory.setWARFile(false);
851
852 List<Portlet> portlets = PortletLocalServiceUtil.getPortlets();
853
854 for (int i = 0; i < portlets.size(); i++) {
855 Portlet portlet = portlets.get(i);
856
857 portletBagFactory.create(portlet);
858
859 if (i == 0) {
860 initPortletApp(portlet, servletContext);
861 }
862 }
863
864 return portlets;
865 }
866
867 protected void initResourceActions(List<Portlet> portlets)
868 throws Exception {
869
870 Iterator<Portlet> itr = portlets.iterator();
871
872 while (itr.hasNext()) {
873 Portlet portlet = itr.next();
874
875 List<String> portletActions =
876 ResourceActionsUtil.getPortletResourceActions(
877 portlet.getPortletId());
878
879 ResourceActionLocalServiceUtil.checkResourceActions(
880 portlet.getPortletId(), portletActions);
881
882 List<String> modelNames =
883 ResourceActionsUtil.getPortletModelResources(
884 portlet.getPortletId());
885
886 for (String modelName : modelNames) {
887 List<String> modelActions =
888 ResourceActionsUtil.getModelResourceActions(modelName);
889
890 ResourceActionLocalServiceUtil.checkResourceActions(
891 modelName, modelActions);
892 }
893 }
894 }
895
896 protected void initResourceCodes(List<Portlet> portlets) throws Exception {
897 long[] companyIds = PortalInstances.getCompanyIdsBySQL();
898
899 Iterator<Portlet> itr = portlets.iterator();
900
901 while (itr.hasNext()) {
902 Portlet portlet = itr.next();
903
904 List<String> modelNames =
905 ResourceActionsUtil.getPortletModelResources(
906 portlet.getPortletId());
907
908 for (long companyId : companyIds) {
909 ResourceCodeLocalServiceUtil.checkResourceCodes(
910 companyId, portlet.getPortletId());
911
912 for (String modelName : modelNames) {
913 ResourceCodeLocalServiceUtil.checkResourceCodes(
914 companyId, modelName);
915 }
916 }
917 }
918 }
919
920 protected void initServletContextPool() throws Exception {
921 ServletContext servletContext = getServletContext();
922
923 String contextPath = PortalUtil.getPathContext();
924
925 ServletContextPool.put(contextPath, servletContext);
926 }
927
928 protected void initSocialEquityLogScheduler() throws Exception {
929 SchedulerEntry schedulerEntry = new SchedulerEntryImpl();
930
931 schedulerEntry.setEventListenerClass(
932 CheckEquityLogMessageListener.class.getName());
933 schedulerEntry.setTimeUnit(TimeUnit.MINUTE);
934 schedulerEntry.setTriggerType(TriggerType.SIMPLE);
935 schedulerEntry.setTriggerValue(
936 PropsValues.SOCIAL_EQUITY_EQUITY_LOG_CHECK_INTERVAL);
937
938 SchedulerEngineUtil.schedule(
939 schedulerEntry, PortalClassLoaderUtil.getClassLoader());
940 }
941
942 protected void initThemes(
943 PluginPackage pluginPackage, List<Portlet> portlets)
944 throws Exception {
945
946 ServletContext servletContext = getServletContext();
947
948 String[] xmls = new String[] {
949 HttpUtil.URLtoString(
950 servletContext.getResource(
951 "/WEB-INF/liferay-look-and-feel.xml")),
952 HttpUtil.URLtoString(
953 servletContext.getResource(
954 "/WEB-INF/liferay-look-and-feel-ext.xml"))
955 };
956
957 ThemeLocalServiceUtil.init(
958 servletContext, null, true, xmls, pluginPackage);
959 }
960
961 protected void initWebSettings() throws Exception {
962 ServletContext servletContext = getServletContext();
963
964 String xml = HttpUtil.URLtoString(
965 servletContext.getResource("/WEB-INF/web.xml"));
966
967 checkWebSettings(xml);
968 }
969
970 protected long loginUser(
971 HttpServletRequest request, HttpServletResponse response,
972 long userId, String remoteUser)
973 throws PortalException, SystemException {
974
975 if ((userId > 0) || (remoteUser == null)) {
976 return userId;
977 }
978
979 userId = GetterUtil.getLong(remoteUser);
980
981 EventsProcessorUtil.process(
982 PropsKeys.LOGIN_EVENTS_PRE, PropsValues.LOGIN_EVENTS_PRE, request,
983 response);
984
985 User user = UserLocalServiceUtil.getUserById(userId);
986
987 if (PropsValues.USERS_UPDATE_LAST_LOGIN) {
988 UserLocalServiceUtil.updateLastLogin(
989 userId, request.getRemoteAddr());
990 }
991
992 HttpSession session = request.getSession();
993
994 session.setAttribute(WebKeys.USER_ID, new Long(userId));
995 session.setAttribute(Globals.LOCALE_KEY, user.getLocale());
996
997 EventsProcessorUtil.process(
998 PropsKeys.LOGIN_EVENTS_POST, PropsValues.LOGIN_EVENTS_POST,
999 request, response);
1000
1001 return userId;
1002 }
1003
1004 protected void processGlobalShutdownEvents() throws Exception {
1005 EventsProcessorUtil.process(
1006 PropsKeys.GLOBAL_SHUTDOWN_EVENTS,
1007 PropsValues.GLOBAL_SHUTDOWN_EVENTS);
1008
1009 super.destroy();
1010 }
1011
1012 protected void processGlobalStartupEvents() throws Exception {
1013 EventsProcessorUtil.process(
1014 PropsKeys.GLOBAL_STARTUP_EVENTS, PropsValues.GLOBAL_STARTUP_EVENTS);
1015 }
1016
1017 protected boolean processMaintenanceRequest(
1018 HttpServletRequest request, HttpServletResponse response)
1019 throws IOException, ServletException {
1020
1021 if (!MaintenanceUtil.isMaintaining()) {
1022 return false;
1023 }
1024
1025 RequestDispatcher requestDispatcher = request.getRequestDispatcher(
1026 "/html/portal/maintenance.jsp");
1027
1028 requestDispatcher.include(request, response);
1029
1030 return true;
1031 }
1032
1033 protected void processServicePost(
1034 HttpServletRequest request, HttpServletResponse response) {
1035
1036 try {
1037 EventsProcessorUtil.process(
1038 PropsKeys.SERVLET_SERVICE_EVENTS_POST,
1039 PropsValues.SERVLET_SERVICE_EVENTS_POST, request, response);
1040 }
1041 catch (Exception e) {
1042 _log.error(e, e);
1043 }
1044
1045 response.addHeader(
1046 _LIFERAY_PORTAL_REQUEST_HEADER, ReleaseInfo.getReleaseInfo());
1047
1048 ThreadLocalCacheManager.clearAll(Lifecycle.REQUEST);
1049 }
1050
1051 protected boolean processServicePre(
1052 HttpServletRequest request, HttpServletResponse response,
1053 long userId)
1054 throws IOException, ServletException {
1055
1056 try {
1057 EventsProcessorUtil.process(
1058 PropsKeys.SERVLET_SERVICE_EVENTS_PRE,
1059 PropsValues.SERVLET_SERVICE_EVENTS_PRE, request, response);
1060 }
1061 catch (Exception e) {
1062 Throwable cause = e.getCause();
1063
1064 if (cause instanceof NoSuchLayoutException) {
1065 sendError(
1066 HttpServletResponse.SC_NOT_FOUND, cause, request, response);
1067
1068 return true;
1069 }
1070 else if (cause instanceof PrincipalException) {
1071 processServicePrePrincipalException(
1072 cause, userId, request, response);
1073
1074 return true;
1075 }
1076
1077 _log.error(e, e);
1078
1079 request.setAttribute(PageContext.EXCEPTION, e);
1080
1081 ServletContext servletContext = getServletContext();
1082
1083 StrutsUtil.forward(
1084 PropsValues.SERVLET_SERVICE_EVENTS_PRE_ERROR_PAGE,
1085 servletContext, request, response);
1086
1087 return true;
1088 }
1089
1090 return false;
1091 }
1092
1093 protected void processServicePrePrincipalException(
1094 Throwable t, long userId, HttpServletRequest request,
1095 HttpServletResponse response)
1096 throws IOException, ServletException {
1097
1098 if (userId > 0) {
1099 sendError(
1100 HttpServletResponse.SC_UNAUTHORIZED, t, request, response);
1101
1102 return;
1103 }
1104
1105 String redirect =
1106 request.getContextPath() + Portal.PATH_MAIN + "/portal/login";
1107
1108 String currentURL = PortalUtil.getCurrentURL(request);
1109
1110 redirect = HttpUtil.addParameter(redirect, "redirect", currentURL);
1111
1112 long plid = ParamUtil.getLong(request, "p_l_id");
1113
1114 if (plid > 0) {
1115 try {
1116 Layout layout = LayoutLocalServiceUtil.getLayout(plid);
1117
1118 if (layout.getGroup().isStagingGroup()) {
1119 Group group = GroupLocalServiceUtil.getGroup(
1120 layout.getCompanyId(), GroupConstants.GUEST);
1121
1122 plid = group.getDefaultPublicPlid();
1123 }
1124 else if (layout.isPrivateLayout()) {
1125 plid = LayoutLocalServiceUtil.getDefaultPlid(
1126 layout.getGroupId(), false);
1127 }
1128
1129 redirect = HttpUtil.addParameter(redirect, "p_l_id", plid);
1130 }
1131 catch (Exception e) {
1132 }
1133 }
1134
1135 response.sendRedirect(redirect);
1136 }
1137
1138 protected boolean processShutdownRequest(
1139 HttpServletRequest request, HttpServletResponse response)
1140 throws IOException {
1141
1142 if (!ShutdownUtil.isShutdown()) {
1143 return false;
1144 }
1145
1146 response.setContentType(ContentTypes.TEXT_HTML_UTF8);
1147
1148 String html = ContentUtil.get(
1149 "com/liferay/portal/dependencies/shutdown.html");
1150
1151 response.getOutputStream().print(html);
1152
1153 return true;
1154 }
1155
1156 protected void processStartupEvents() throws Exception {
1157 StartupAction startupAction = new StartupAction();
1158
1159 startupAction.run(null);
1160 }
1161
1162 protected HttpServletRequest protectRequest(
1163 HttpServletRequest request, String remoteUser) {
1164
1165
1166
1167
1168
1169
1170
1171 return new ProtectedServletRequest(request, remoteUser);
1172 }
1173
1174 protected void sendError(
1175 int status, Throwable t, HttpServletRequest request,
1176 HttpServletResponse response)
1177 throws IOException, ServletException {
1178
1179 DynamicServletRequest dynamicRequest = new DynamicServletRequest(
1180 request);
1181
1182
1183
1184 dynamicRequest.setParameter("p_l_id", StringPool.BLANK);
1185
1186 PortalUtil.sendError(status, (Exception)t, dynamicRequest, response);
1187 }
1188
1189 protected void setPortalPort(HttpServletRequest request) {
1190 PortalUtil.setPortalPort(request);
1191 }
1192
1193 protected void setPrincipalName(long userId, String remoteUser) {
1194 if ((userId == 0) && (remoteUser == null)) {
1195 return;
1196 }
1197
1198 String name = String.valueOf(userId);
1199
1200 if (remoteUser != null) {
1201 name = remoteUser;
1202 }
1203
1204 PrincipalThreadLocal.setName(name);
1205 }
1206
1207 private static final String _LIFERAY_PORTAL_REQUEST_HEADER =
1208 "Liferay-Portal";
1209
1210 private static Log _log = LogFactoryUtil.getLog(MainServlet.class);
1211
1212 }