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_ID, new Long(userId));
1032 session.setAttribute(Globals.LOCALE_KEY, user.getLocale());
1033
1034 EventsProcessorUtil.process(
1035 PropsKeys.LOGIN_EVENTS_POST, PropsValues.LOGIN_EVENTS_POST,
1036 request, response);
1037
1038 return userId;
1039 }
1040
1041 protected boolean processCompanyInactiveRequest(
1042 HttpServletRequest request, HttpServletResponse response,
1043 long companyId)
1044 throws IOException {
1045
1046 if (PortalInstances.isCompanyActive(companyId)) {
1047 return false;
1048 }
1049
1050 processInactiveRequest(
1051 request, response,
1052 "this-instance-is-inactive-please-contact-the-administrator");
1053
1054 return true;
1055 }
1056
1057 protected boolean processGroupInactiveRequest(
1058 HttpServletRequest request, HttpServletResponse response)
1059 throws IOException, PortalException, SystemException {
1060
1061 long plid = ParamUtil.getLong(request, "p_l_id");
1062
1063 if (plid <= 0) {
1064 return false;
1065 }
1066
1067 Layout layout = LayoutLocalServiceUtil.getLayout(plid);
1068
1069 Group group = layout.getGroup();
1070
1071 if (group.isActive()) {
1072 return false;
1073 }
1074
1075 processInactiveRequest(
1076 request, response,
1077 "this-site-is-inactive-please-contact-the-administrator");
1078
1079 return true;
1080 }
1081
1082 protected void processGlobalShutdownEvents() throws Exception {
1083 EventsProcessorUtil.process(
1084 PropsKeys.GLOBAL_SHUTDOWN_EVENTS,
1085 PropsValues.GLOBAL_SHUTDOWN_EVENTS);
1086
1087 super.destroy();
1088 }
1089
1090 protected void processGlobalStartupEvents() throws Exception {
1091 EventsProcessorUtil.process(
1092 PropsKeys.GLOBAL_STARTUP_EVENTS, PropsValues.GLOBAL_STARTUP_EVENTS);
1093 }
1094
1095 protected void processInactiveRequest(
1096 HttpServletRequest request, HttpServletResponse response,
1097 String messageKey)
1098 throws IOException {
1099
1100 response.setContentType(ContentTypes.TEXT_HTML_UTF8);
1101
1102 Locale locale = LocaleUtil.getDefault();
1103
1104 String message = LanguageUtil.get(locale, messageKey);
1105
1106 String html = ContentUtil.get(
1107 "com/liferay/portal/dependencies/inactive.html");
1108
1109 html = StringUtil.replace(html, "[$MESSAGE$]", message);
1110
1111 ServletOutputStream servletOutputStream = response.getOutputStream();
1112
1113 servletOutputStream.print(html);
1114 }
1115
1116 protected boolean processMaintenanceRequest(
1117 HttpServletRequest request, HttpServletResponse response)
1118 throws IOException, ServletException {
1119
1120 if (!MaintenanceUtil.isMaintaining()) {
1121 return false;
1122 }
1123
1124 RequestDispatcher requestDispatcher = request.getRequestDispatcher(
1125 "/html/portal/maintenance.jsp");
1126
1127 requestDispatcher.include(request, response);
1128
1129 return true;
1130 }
1131
1132 protected void processServicePost(
1133 HttpServletRequest request, HttpServletResponse response) {
1134
1135 try {
1136 EventsProcessorUtil.process(
1137 PropsKeys.SERVLET_SERVICE_EVENTS_POST,
1138 PropsValues.SERVLET_SERVICE_EVENTS_POST, request, response);
1139 }
1140 catch (Exception e) {
1141 _log.error(e, e);
1142 }
1143
1144 if (_HTTP_HEADER_VERSION_VERBOSITY_DEFAULT) {
1145 }
1146 else if (_HTTP_HEADER_VERSION_VERBOSITY_PARTIAL) {
1147 response.addHeader(
1148 _LIFERAY_PORTAL_REQUEST_HEADER, ReleaseInfo.getName());
1149 }
1150 else {
1151 response.addHeader(
1152 _LIFERAY_PORTAL_REQUEST_HEADER, ReleaseInfo.getReleaseInfo());
1153 }
1154
1155 ThreadLocalCacheManager.clearAll(Lifecycle.REQUEST);
1156 }
1157
1158 protected boolean processServicePre(
1159 HttpServletRequest request, HttpServletResponse response,
1160 long userId)
1161 throws IOException, ServletException {
1162
1163 try {
1164 EventsProcessorUtil.process(
1165 PropsKeys.SERVLET_SERVICE_EVENTS_PRE,
1166 PropsValues.SERVLET_SERVICE_EVENTS_PRE, request, response);
1167 }
1168 catch (Exception e) {
1169 Throwable cause = e.getCause();
1170
1171 if (cause instanceof NoSuchLayoutException) {
1172 sendError(
1173 HttpServletResponse.SC_NOT_FOUND, cause, request, response);
1174
1175 return true;
1176 }
1177 else if (cause instanceof PrincipalException) {
1178 processServicePrePrincipalException(
1179 cause, userId, request, response);
1180
1181 return true;
1182 }
1183
1184 _log.error(e, e);
1185
1186 request.setAttribute(PageContext.EXCEPTION, e);
1187
1188 ServletContext servletContext = getServletContext();
1189
1190 StrutsUtil.forward(
1191 PropsValues.SERVLET_SERVICE_EVENTS_PRE_ERROR_PAGE,
1192 servletContext, request, response);
1193
1194 return true;
1195 }
1196
1197 return false;
1198 }
1199
1200 protected void processServicePrePrincipalException(
1201 Throwable t, long userId, HttpServletRequest request,
1202 HttpServletResponse response)
1203 throws IOException, ServletException {
1204
1205 if (userId > 0) {
1206 sendError(
1207 HttpServletResponse.SC_UNAUTHORIZED, t, request, response);
1208
1209 return;
1210 }
1211
1212 String redirect = PortalUtil.getPathMain().concat("/portal/login");
1213
1214 String currentURL = PortalUtil.getCurrentURL(request);
1215
1216 redirect = HttpUtil.addParameter(redirect, "redirect", currentURL);
1217
1218 long plid = ParamUtil.getLong(request, "p_l_id");
1219
1220 if (plid > 0) {
1221 try {
1222 Layout layout = LayoutLocalServiceUtil.getLayout(plid);
1223
1224 if (layout.getGroup().isStagingGroup()) {
1225 Group group = GroupLocalServiceUtil.getGroup(
1226 layout.getCompanyId(), GroupConstants.GUEST);
1227
1228 plid = group.getDefaultPublicPlid();
1229 }
1230 else if (layout.isPrivateLayout()) {
1231 plid = LayoutLocalServiceUtil.getDefaultPlid(
1232 layout.getGroupId(), false);
1233 }
1234
1235 redirect = HttpUtil.addParameter(redirect, "p_l_id", plid);
1236 }
1237 catch (Exception e) {
1238 }
1239 }
1240
1241 response.sendRedirect(redirect);
1242 }
1243
1244 protected boolean processShutdownRequest(
1245 HttpServletRequest request, HttpServletResponse response)
1246 throws IOException {
1247
1248 if (!ShutdownUtil.isShutdown()) {
1249 return false;
1250 }
1251
1252 String messageKey = ShutdownUtil.getMessage();
1253
1254 if (Validator.isNull(messageKey)) {
1255 messageKey = "the-system-is-shutdown-please-try-again-later";
1256 }
1257
1258 processInactiveRequest(request, response, messageKey);
1259
1260 return true;
1261 }
1262
1263 protected void processStartupEvents() throws Exception {
1264 StartupAction startupAction = new StartupAction();
1265
1266 startupAction.run(null);
1267 }
1268
1269 protected HttpServletRequest protectRequest(
1270 HttpServletRequest request, String remoteUser) {
1271
1272
1273
1274
1275
1276
1277
1278 return new ProtectedServletRequest(request, remoteUser);
1279 }
1280
1281 protected void sendError(
1282 int status, Throwable t, HttpServletRequest request,
1283 HttpServletResponse response)
1284 throws IOException, ServletException {
1285
1286 DynamicServletRequest dynamicRequest = new DynamicServletRequest(
1287 request);
1288
1289
1290
1291 dynamicRequest.setParameter("p_l_id", StringPool.BLANK);
1292
1293 dynamicRequest.setParameter("groupId", StringPool.BLANK);
1294 dynamicRequest.setParameter("layoutId", StringPool.BLANK);
1295 dynamicRequest.setParameter("privateLayout", StringPool.BLANK);
1296
1297 PortalUtil.sendError(status, (Exception)t, dynamicRequest, response);
1298 }
1299
1300 protected void setPortalPort(HttpServletRequest request) {
1301 PortalUtil.setPortalPort(request);
1302 }
1303
1304 protected void setPrincipal(
1305 long userId, String remoteUser, String password) {
1306
1307 if ((userId == 0) && (remoteUser == null)) {
1308 return;
1309 }
1310
1311 String name = String.valueOf(userId);
1312
1313 if (!PropsValues.PORTAL_JAAS_ENABLE) {
1314 if (remoteUser != null) {
1315 name = remoteUser;
1316 }
1317 }
1318
1319 PrincipalThreadLocal.setName(name);
1320
1321 PrincipalThreadLocal.setPassword(password);
1322 }
1323
1324 private static final boolean _HTTP_HEADER_VERSION_VERBOSITY_DEFAULT =
1325 PropsValues.HTTP_HEADER_VERSION_VERBOSITY.equalsIgnoreCase(
1326 ReleaseInfo.getName());
1327
1328 private static final boolean _HTTP_HEADER_VERSION_VERBOSITY_PARTIAL =
1329 PropsValues.HTTP_HEADER_VERSION_VERBOSITY.equalsIgnoreCase("partial");
1330
1331 private static final String _LIFERAY_PORTAL_REQUEST_HEADER =
1332 "Liferay-Portal";
1333
1334 private static Log _log = LogFactoryUtil.getLog(MainServlet.class);
1335
1336 }