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