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