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