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