001    /**
002     * Copyright (c) 2000-2013 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.struts;
016    
017    import com.liferay.portal.LayoutPermissionException;
018    import com.liferay.portal.PortletActiveException;
019    import com.liferay.portal.UserActiveException;
020    import com.liferay.portal.kernel.exception.SystemException;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.kernel.portlet.FriendlyURLMapper;
024    import com.liferay.portal.kernel.servlet.HttpMethods;
025    import com.liferay.portal.kernel.servlet.SessionErrors;
026    import com.liferay.portal.kernel.struts.LastPath;
027    import com.liferay.portal.kernel.util.CharPool;
028    import com.liferay.portal.kernel.util.GetterUtil;
029    import com.liferay.portal.kernel.util.HttpUtil;
030    import com.liferay.portal.kernel.util.JavaConstants;
031    import com.liferay.portal.kernel.util.ParamUtil;
032    import com.liferay.portal.kernel.util.PropsKeys;
033    import com.liferay.portal.kernel.util.StringBundler;
034    import com.liferay.portal.kernel.util.StringPool;
035    import com.liferay.portal.kernel.util.Validator;
036    import com.liferay.portal.liveusers.LiveUsers;
037    import com.liferay.portal.model.Company;
038    import com.liferay.portal.model.Layout;
039    import com.liferay.portal.model.LayoutConstants;
040    import com.liferay.portal.model.Portlet;
041    import com.liferay.portal.model.PortletPreferencesIds;
042    import com.liferay.portal.model.User;
043    import com.liferay.portal.model.UserTracker;
044    import com.liferay.portal.model.UserTrackerPath;
045    import com.liferay.portal.security.auth.PrincipalException;
046    import com.liferay.portal.security.permission.ActionKeys;
047    import com.liferay.portal.security.permission.PermissionChecker;
048    import com.liferay.portal.service.LayoutLocalServiceUtil;
049    import com.liferay.portal.service.PortletLocalServiceUtil;
050    import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
051    import com.liferay.portal.service.permission.PortletPermissionUtil;
052    import com.liferay.portal.service.persistence.UserTrackerPathUtil;
053    import com.liferay.portal.setup.SetupWizardUtil;
054    import com.liferay.portal.theme.ThemeDisplay;
055    import com.liferay.portal.util.PortalUtil;
056    import com.liferay.portal.util.PrefsPropsUtil;
057    import com.liferay.portal.util.PropsUtil;
058    import com.liferay.portal.util.PropsValues;
059    import com.liferay.portal.util.WebKeys;
060    import com.liferay.portlet.InvokerPortlet;
061    import com.liferay.portlet.PortletConfigFactoryUtil;
062    import com.liferay.portlet.PortletInstanceFactoryUtil;
063    import com.liferay.portlet.PortletPreferencesFactoryUtil;
064    import com.liferay.portlet.PortletURLImpl;
065    import com.liferay.portlet.RenderRequestFactory;
066    import com.liferay.portlet.RenderRequestImpl;
067    import com.liferay.portlet.RenderResponseFactory;
068    import com.liferay.portlet.RenderResponseImpl;
069    
070    import java.io.IOException;
071    
072    import java.util.Date;
073    import java.util.HashSet;
074    import java.util.Iterator;
075    import java.util.Map.Entry;
076    import java.util.Map;
077    import java.util.Set;
078    
079    import javax.portlet.PortletConfig;
080    import javax.portlet.PortletContext;
081    import javax.portlet.PortletMode;
082    import javax.portlet.PortletPreferences;
083    import javax.portlet.PortletRequest;
084    import javax.portlet.WindowState;
085    
086    import javax.servlet.ServletContext;
087    import javax.servlet.ServletException;
088    import javax.servlet.http.HttpServletRequest;
089    import javax.servlet.http.HttpServletResponse;
090    import javax.servlet.http.HttpSession;
091    import javax.servlet.jsp.PageContext;
092    
093    import org.apache.struts.Globals;
094    import org.apache.struts.action.Action;
095    import org.apache.struts.action.ActionMapping;
096    import org.apache.struts.config.ActionConfig;
097    import org.apache.struts.config.ForwardConfig;
098    import org.apache.struts.tiles.TilesRequestProcessor;
099    import org.apache.struts.util.MessageResources;
100    
101    /**
102     * @author Brian Wing Shun Chan
103     * @author Jorge Ferrer
104     * @author Wesley Gong
105     * @author Mika Koivisto
106     */
107    public class PortalRequestProcessor extends TilesRequestProcessor {
108    
109            public PortalRequestProcessor() {
110    
111                    // auth.forward.last.path.
112    
113                    _lastPaths = new HashSet<String>();
114    
115                    _lastPaths.add(_PATH_PORTAL_LAYOUT);
116    
117                    addPaths(_lastPaths, PropsKeys.AUTH_FORWARD_LAST_PATHS);
118    
119                    // auth.public.path.
120    
121                    _publicPaths = new HashSet<String>();
122    
123                    _publicPaths.add(_PATH_C);
124                    _publicPaths.add(_PATH_PORTAL_API_JSONWS);
125                    _publicPaths.add(_PATH_PORTAL_FLASH);
126                    _publicPaths.add(_PATH_PORTAL_J_LOGIN);
127                    _publicPaths.add(_PATH_PORTAL_LAYOUT);
128                    _publicPaths.add(_PATH_PORTAL_LICENSE);
129                    _publicPaths.add(_PATH_PORTAL_LOGIN);
130                    _publicPaths.add(_PATH_PORTAL_RENDER_PORTLET);
131                    _publicPaths.add(_PATH_PORTAL_TCK);
132                    _publicPaths.add(_PATH_PORTAL_UPDATE_PASSWORD);
133                    _publicPaths.add(_PATH_PORTAL_VERIFY_EMAIL_ADDRESS);
134                    _publicPaths.add(PropsValues.AUTH_LOGIN_DISABLED_PATH);
135    
136                    _trackerIgnorePaths = new HashSet<String>();
137    
138                    addPaths(_trackerIgnorePaths, PropsKeys.SESSION_TRACKER_IGNORE_PATHS);
139            }
140    
141            @Override
142            public void process(
143                            HttpServletRequest request, HttpServletResponse response)
144                    throws IOException, ServletException {
145    
146                    HttpSession session = request.getSession();
147    
148                    Boolean basicAuthEnabled = (Boolean)session.getAttribute(
149                            WebKeys.BASIC_AUTH_ENABLED);
150    
151                    if (basicAuthEnabled != null) {
152                            session.removeAttribute(WebKeys.BASIC_AUTH_ENABLED);
153                    }
154    
155                    String path = super.processPath(request, response);
156    
157                    ActionMapping actionMapping =
158                            (ActionMapping)moduleConfig.findActionConfig(path);
159    
160                    Action action = StrutsActionRegistryUtil.getAction(path);
161    
162                    if (((basicAuthEnabled != null) && basicAuthEnabled.booleanValue()) ||
163                            ((actionMapping == null) && (action == null))) {
164    
165                            String lastPath = getLastPath(request);
166    
167                            if (_log.isDebugEnabled()) {
168                                    _log.debug("Last path " + lastPath);
169                            }
170    
171                            response.sendRedirect(lastPath);
172    
173                            return;
174                    }
175    
176                    super.process(request, response);
177    
178                    try {
179                            if (isPortletPath(path)) {
180                                    cleanUp(request);
181                            }
182                    }
183                    catch (Exception e) {
184                            _log.error(e, e);
185                    }
186            }
187    
188            protected void addPaths(Set<String> paths, String propsKey) {
189                    String[] pathsArray = PropsUtil.getArray(propsKey);
190    
191                    for (String path : pathsArray) {
192                            paths.add(path);
193                    }
194            }
195    
196            protected void callParentDoForward(
197                            String uri, HttpServletRequest request,
198                            HttpServletResponse response)
199                    throws IOException, ServletException {
200    
201                    super.doForward(uri, request, response);
202            }
203    
204            protected HttpServletRequest callParentProcessMultipart(
205                    HttpServletRequest request) {
206    
207                    return super.processMultipart(request);
208            }
209    
210            protected String callParentProcessPath(
211                            HttpServletRequest request, HttpServletResponse response)
212                    throws IOException {
213    
214                    return super.processPath(request, response);
215            }
216    
217            protected boolean callParentProcessRoles(
218                            HttpServletRequest request, HttpServletResponse response,
219                            ActionMapping actionMapping)
220                    throws IOException, ServletException {
221    
222                    return super.processRoles(request, response, actionMapping);
223            }
224    
225            protected void cleanUp(HttpServletRequest request) throws Exception {
226    
227                    // Clean up portlet objects that may have been created by defineObjects
228                    // for portlets that are called directly from a Struts path
229    
230                    RenderRequestImpl renderRequestImpl =
231                            (RenderRequestImpl)request.getAttribute(
232                                    JavaConstants.JAVAX_PORTLET_REQUEST);
233    
234                    if (renderRequestImpl != null) {
235                            renderRequestImpl.cleanUp();
236                    }
237            }
238    
239            protected void defineObjects(
240                            HttpServletRequest request, HttpServletResponse response,
241                            Portlet portlet)
242                    throws Exception {
243    
244                    String portletId = portlet.getPortletId();
245    
246                    ServletContext servletContext = (ServletContext)request.getAttribute(
247                            WebKeys.CTX);
248    
249                    InvokerPortlet invokerPortlet = PortletInstanceFactoryUtil.create(
250                            portlet, servletContext);
251    
252                    PortletPreferencesIds portletPreferencesIds =
253                            PortletPreferencesFactoryUtil.getPortletPreferencesIds(
254                                    request, portletId);
255    
256                    PortletPreferences portletPreferences =
257                            PortletPreferencesLocalServiceUtil.getStrictPreferences(
258                                    portletPreferencesIds);
259    
260                    PortletConfig portletConfig = PortletConfigFactoryUtil.create(
261                            portlet, servletContext);
262                    PortletContext portletContext = portletConfig.getPortletContext();
263    
264                    RenderRequestImpl renderRequestImpl = RenderRequestFactory.create(
265                            request, portlet, invokerPortlet, portletContext,
266                            WindowState.MAXIMIZED, PortletMode.VIEW, portletPreferences);
267    
268                    RenderResponseImpl renderResponseImpl = RenderResponseFactory.create(
269                            renderRequestImpl, response, portletId, portlet.getCompanyId());
270    
271                    renderRequestImpl.defineObjects(portletConfig, renderResponseImpl);
272    
273                    request.setAttribute(WebKeys.PORTLET_STRUTS_EXECUTE, Boolean.TRUE);
274            }
275    
276            @Override
277            protected void doForward(
278                            String uri, HttpServletRequest request,
279                            HttpServletResponse response)
280                    throws ServletException {
281    
282                    StrutsUtil.forward(uri, getServletContext(), request, response);
283            }
284    
285            @Override
286            protected void doInclude(
287                            String uri, HttpServletRequest request,
288                            HttpServletResponse response)
289                    throws ServletException {
290    
291                    StrutsUtil.include(uri, getServletContext(), request, response);
292            }
293    
294            protected String getFriendlyTrackerPath(
295                            String path, ThemeDisplay themeDisplay, HttpServletRequest request)
296                    throws Exception {
297    
298                    if (!path.equals(_PATH_PORTAL_LAYOUT)) {
299                            return null;
300                    }
301    
302                    long plid = ParamUtil.getLong(request, "p_l_id");
303    
304                    if (plid == 0) {
305                            return null;
306                    }
307    
308                    Layout layout = LayoutLocalServiceUtil.getLayout(plid);
309    
310                    String layoutFriendlyURL = PortalUtil.getLayoutFriendlyURL(
311                            layout, themeDisplay);
312    
313                    String portletId = ParamUtil.getString(request, "p_p_id");
314    
315                    if (Validator.isNull(portletId)) {
316                            return layoutFriendlyURL;
317                    }
318    
319                    long companyId = PortalUtil.getCompanyId(request);
320    
321                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
322                            companyId, portletId);
323    
324                    if (portlet == null) {
325                            String strutsPath = path.substring(
326                                    1, path.lastIndexOf(CharPool.SLASH));
327    
328                            portlet = PortletLocalServiceUtil.getPortletByStrutsPath(
329                                    companyId, strutsPath);
330                    }
331    
332                    if ((portlet == null) || !portlet.isActive()) {
333                            return layoutFriendlyURL.concat(StringPool.QUESTION).concat(
334                                    request.getQueryString());
335                    }
336    
337                    String namespace = PortalUtil.getPortletNamespace(portletId);
338    
339                    FriendlyURLMapper friendlyURLMapper =
340                            portlet.getFriendlyURLMapperInstance();
341    
342                    if (friendlyURLMapper == null) {
343                            return layoutFriendlyURL.concat(StringPool.QUESTION).concat(
344                                    request.getQueryString());
345                    }
346    
347                    PortletURLImpl portletURL = new PortletURLImpl(
348                            request, portletId, plid, PortletRequest.RENDER_PHASE);
349    
350                    Iterator<Map.Entry<String, String[]>> itr =
351                            request.getParameterMap().entrySet().iterator();
352    
353                    while (itr.hasNext()) {
354                            Entry<String, String[]> entry = itr.next();
355    
356                            String key = entry.getKey();
357    
358                            if (key.startsWith(namespace)) {
359                                    key = key.substring(namespace.length());
360    
361                                    portletURL.setParameter(key, entry.getValue());
362                            }
363                    }
364    
365                    String portletFriendlyURL = friendlyURLMapper.buildPath(portletURL);
366    
367                    if (portletFriendlyURL != null) {
368                            return layoutFriendlyURL.concat(portletFriendlyURL);
369                    }
370                    else {
371                            return layoutFriendlyURL.concat(StringPool.QUESTION).concat(
372                                    request.getQueryString());
373                    }
374            }
375    
376            protected String getLastPath(HttpServletRequest request) {
377                    HttpSession session = request.getSession();
378    
379                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
380                            WebKeys.THEME_DISPLAY);
381    
382                    Boolean httpsInitial = (Boolean)session.getAttribute(
383                            WebKeys.HTTPS_INITIAL);
384    
385                    String portalURL = null;
386    
387                    if (PropsValues.COMPANY_SECURITY_AUTH_REQUIRES_HTTPS &&
388                            !PropsValues.SESSION_ENABLE_PHISHING_PROTECTION &&
389                            (httpsInitial != null) && !httpsInitial.booleanValue()) {
390    
391                            portalURL = PortalUtil.getPortalURL(request, false);
392                    }
393                    else {
394                            portalURL = PortalUtil.getPortalURL(request);
395                    }
396    
397                    StringBundler sb = new StringBundler();
398    
399                    sb.append(portalURL);
400                    sb.append(themeDisplay.getPathMain());
401                    sb.append(_PATH_PORTAL_LAYOUT);
402    
403                    if (!PropsValues.AUTH_FORWARD_BY_LAST_PATH) {
404                            if (request.getRemoteUser() != null) {
405    
406                                    // If we do not forward by last path and the user is logged in,
407                                    // forward to the user's default layout to prevent a lagging
408                                    // loop
409    
410                                    sb.append(StringPool.QUESTION);
411                                    sb.append("p_l_id");
412                                    sb.append(StringPool.EQUAL);
413                                    sb.append(LayoutConstants.DEFAULT_PLID);
414                            }
415    
416                            return sb.toString();
417                    }
418    
419                    LastPath lastPath = (LastPath)session.getAttribute(WebKeys.LAST_PATH);
420    
421                    if (lastPath == null) {
422                            return sb.toString();
423                    }
424    
425                    Map<String, String[]> parameterMap = lastPath.getParameterMap();
426    
427                    // Only test for existing mappings for last paths that were set when the
428                    // user accessed a layout directly instead of through its friendly URL
429    
430                    if (lastPath.getContextPath().equals(themeDisplay.getPathMain())) {
431                            ActionMapping actionMapping =
432                                    (ActionMapping)moduleConfig.findActionConfig(
433                                            lastPath.getPath());
434    
435                            if ((actionMapping == null) || (parameterMap == null)) {
436                                    return sb.toString();
437                            }
438                    }
439    
440                    StringBundler lastPathSB = new StringBundler(4);
441    
442                    lastPathSB.append(portalURL);
443                    lastPathSB.append(lastPath.getContextPath());
444                    lastPathSB.append(lastPath.getPath());
445                    lastPathSB.append(HttpUtil.parameterMapToString(parameterMap));
446    
447                    return lastPathSB.toString();
448            }
449    
450            protected boolean isPortletPath(String path) {
451                    if ((path != null) &&
452                            !path.equals(_PATH_C) &&
453                            !path.startsWith(_PATH_COMMON) &&
454                            !path.contains(_PATH_J_SECURITY_CHECK) &&
455                            !path.startsWith(_PATH_PORTAL)) {
456    
457                            return true;
458                    }
459                    else {
460                            return false;
461                    }
462            }
463    
464            protected boolean isPublicPath(String path) {
465                    if ((path != null) &&
466                            (_publicPaths.contains(path) || path.startsWith(_PATH_COMMON) ||
467                             AuthPublicPathRegistry.contains(path))) {
468    
469                            return true;
470                    }
471                    else {
472                            return false;
473                    }
474            }
475    
476            @Override
477            protected Action processActionCreate(
478                            HttpServletRequest request, HttpServletResponse response,
479                            ActionMapping actionMapping)
480                    throws IOException {
481    
482                    ActionAdapter actionAdapter =
483                            (ActionAdapter)StrutsActionRegistryUtil.getAction(
484                                    actionMapping.getPath());
485    
486                    if (actionAdapter != null) {
487                            ActionConfig actionConfig = moduleConfig.findActionConfig(
488                                    actionMapping.getPath());
489    
490                            if (actionConfig != null) {
491                                    Action originalAction = super.processActionCreate(
492                                            request, response, actionMapping);
493    
494                                    actionAdapter.setOriginalAction(originalAction);
495                            }
496    
497                            return actionAdapter;
498                    }
499    
500                    return super.processActionCreate(request, response, actionMapping);
501            }
502    
503            @Override
504            protected ActionMapping processMapping(
505                            HttpServletRequest request, HttpServletResponse response,
506                            String path)
507                    throws IOException {
508    
509                    if (path == null) {
510                            return null;
511                    }
512    
513                    Action action = StrutsActionRegistryUtil.getAction(path);
514    
515                    if (action != null) {
516                            ActionMapping actionMapping =
517                                    (ActionMapping)moduleConfig.findActionConfig(path);
518    
519                            if (actionMapping == null) {
520                                    actionMapping = new ActionMapping();
521    
522                                    actionMapping.setModuleConfig(moduleConfig);
523                                    actionMapping.setPath(path);
524    
525                                    request.setAttribute(Globals.MAPPING_KEY, actionMapping);
526                            }
527    
528                            return actionMapping;
529                    }
530    
531                    ActionMapping actionMapping = super.processMapping(
532                            request, response, path);
533    
534                    if (actionMapping == null) {
535                            MessageResources messageResources = getInternal();
536    
537                            String msg = messageResources.getMessage("processInvalid");
538    
539                            _log.error("User ID " + request.getRemoteUser());
540                            _log.error("Current URL " + PortalUtil.getCurrentURL(request));
541                            _log.error("Referer " + request.getHeader("Referer"));
542                            _log.error("Remote address " + request.getRemoteAddr());
543    
544                            _log.error(msg + " " + path);
545                    }
546    
547                    return actionMapping;
548            }
549    
550            @Override
551            protected HttpServletRequest processMultipart(HttpServletRequest request) {
552    
553                    // Disable Struts from automatically wrapping a multipart request
554    
555                    return request;
556            }
557    
558            @Override
559            protected String processPath(
560                            HttpServletRequest request, HttpServletResponse response)
561                    throws IOException {
562    
563                    String path = GetterUtil.getString(
564                            super.processPath(request, response));
565    
566                    HttpSession session = request.getSession();
567    
568                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
569                            WebKeys.THEME_DISPLAY);
570    
571                    // Current users
572    
573                    UserTracker userTracker = LiveUsers.getUserTracker(
574                            themeDisplay.getCompanyId(), session.getId());
575    
576                    if ((userTracker != null) && !path.equals(_PATH_C) &&
577                            !path.contains(_PATH_J_SECURITY_CHECK) &&
578                            !path.contains(_PATH_PORTAL_PROTECTED) &&
579                            !_trackerIgnorePaths.contains(path)) {
580    
581                            String fullPath = null;
582    
583                            try {
584                                    if (PropsValues.SESSION_TRACKER_FRIENDLY_PATHS_ENABLED) {
585                                            fullPath = getFriendlyTrackerPath(
586                                                    path, themeDisplay, request);
587                                    }
588                            }
589                            catch (Exception e) {
590                                    _log.error(e, e);
591                            }
592    
593                            String fullPathWithoutQueryString = fullPath;
594    
595                            if (Validator.isNull(fullPath)) {
596                                    String queryString = request.getQueryString();
597    
598                                    fullPathWithoutQueryString = path;
599    
600                                    if (Validator.isNotNull(queryString)) {
601                                            fullPath = path.concat(StringPool.QUESTION).concat(
602                                                    queryString);
603                                    }
604                                    else {
605                                            fullPath = path;
606                                    }
607                            }
608    
609                            int pos = fullPathWithoutQueryString.indexOf(StringPool.QUESTION);
610    
611                            if (pos != -1) {
612                                    fullPathWithoutQueryString =
613                                            fullPathWithoutQueryString.substring(0, pos);
614                            }
615    
616                            if (!_trackerIgnorePaths.contains(fullPathWithoutQueryString)) {
617                                    UserTrackerPath userTrackerPath = UserTrackerPathUtil.create(0);
618    
619                                    userTrackerPath.setUserTrackerId(
620                                            userTracker.getUserTrackerId());
621                                    userTrackerPath.setPath(fullPath);
622                                    userTrackerPath.setPathDate(new Date());
623    
624                                    userTracker.addPath(userTrackerPath);
625                            }
626                    }
627    
628                    String remoteUser = request.getRemoteUser();
629    
630                    User user = null;
631    
632                    try {
633                            user = PortalUtil.getUser(request);
634                    }
635                    catch (Exception e) {
636                    }
637    
638                    // Last path
639    
640                    if (_lastPaths.contains(path) && !_trackerIgnorePaths.contains(path)) {
641                            boolean saveLastPath = ParamUtil.getBoolean(
642                                    request, "saveLastPath", true);
643    
644                            if (themeDisplay.isLifecycleResource() ||
645                                    themeDisplay.isStateExclusive() ||
646                                    themeDisplay.isStatePopUp() ||
647                                    !request.getMethod().equalsIgnoreCase(HttpMethods.GET)) {
648    
649                                    saveLastPath = false;
650                            }
651    
652                            // Save last path
653    
654                            if (saveLastPath) {
655    
656                                    // Was a last path set by another servlet that dispatched to the
657                                    // MainServlet? If so, use that last path instead.
658    
659                                    LastPath lastPath = (LastPath)request.getAttribute(
660                                            WebKeys.LAST_PATH);
661    
662                                    if (lastPath == null) {
663                                            lastPath = new LastPath(
664                                                    themeDisplay.getPathMain(), path,
665                                                    request.getParameterMap());
666                                    }
667    
668                                    session.setAttribute(WebKeys.LAST_PATH, lastPath);
669                            }
670                    }
671    
672                    // Setup wizard
673    
674                    if (!SetupWizardUtil.isSetupFinished()) {
675                            if (!path.equals(_PATH_PORTAL_LICENSE) &&
676                                    !path.equals(_PATH_PORTAL_STATUS)) {
677    
678                                    return _PATH_PORTAL_SETUP_WIZARD;
679                            }
680                    }
681                    else if (path.equals(_PATH_PORTAL_SETUP_WIZARD)) {
682                            return _PATH_PORTAL_LAYOUT;
683                    }
684    
685                    // Authenticated users can always log out
686    
687                    if (((remoteUser != null) || (user != null)) &&
688                            path.equals(_PATH_PORTAL_LOGOUT)) {
689    
690                            return path;
691                    }
692    
693                    // Authenticated users can always extend or confirm their session
694    
695                    if (((remoteUser != null) || (user != null)) &&
696                            (path.equals(_PATH_PORTAL_EXPIRE_SESSION) ||
697                             path.equals(_PATH_PORTAL_EXTEND_SESSION))) {
698    
699                            return path;
700                    }
701    
702                    // Authenticated users can always agree to terms of use
703    
704                    if (((remoteUser != null) || (user != null)) &&
705                            path.equals(_PATH_PORTAL_UPDATE_TERMS_OF_USE)) {
706    
707                            return path;
708                    }
709    
710                    // Authenticated users must still exist in the system
711    
712                    if ((remoteUser != null) && (user == null)) {
713                            return _PATH_PORTAL_LOGOUT;
714                    }
715    
716                    // Authenticated users must be active
717    
718                    if ((user != null) && !user.isActive()) {
719                            SessionErrors.add(request, UserActiveException.class.getName());
720    
721                            return _PATH_PORTAL_ERROR;
722                    }
723    
724                    if (!path.equals(_PATH_PORTAL_JSON_SERVICE) &&
725                            !path.equals(_PATH_PORTAL_RENDER_PORTLET) &&
726                            !ParamUtil.getBoolean(request, "wsrp") &&
727                            !themeDisplay.isImpersonated()) {
728    
729                            // Authenticated users should agree to Terms of Use
730    
731                            if ((user != null) && !user.isAgreedToTermsOfUse()) {
732                                    boolean termsOfUseRequired = false;
733    
734                                    try {
735                                            termsOfUseRequired = PrefsPropsUtil.getBoolean(
736                                                    user.getCompanyId(), PropsKeys.TERMS_OF_USE_REQUIRED);
737                                    }
738                                    catch (SystemException se) {
739                                            termsOfUseRequired = PropsValues.TERMS_OF_USE_REQUIRED;
740                                    }
741    
742                                    if (termsOfUseRequired) {
743                                            return _PATH_PORTAL_TERMS_OF_USE;
744                                    }
745                            }
746    
747                            // Authenticated users should have a verified email address
748    
749                            boolean emailAddressVerificationRequired = false;
750    
751                            try {
752                                    Company company = PortalUtil.getCompany(request);
753    
754                                    emailAddressVerificationRequired = company.isStrangersVerify();
755                            }
756                            catch (Exception e) {
757                                    _log.error(e, e);
758                            }
759    
760                            if ((user != null) && !user.isEmailAddressVerified() &&
761                                    emailAddressVerificationRequired &&
762                                    !path.equals(_PATH_PORTAL_UPDATE_EMAIL_ADDRESS)) {
763    
764                                    return _PATH_PORTAL_VERIFY_EMAIL_ADDRESS;
765                            }
766    
767                            // Authenticated users must have a current password
768    
769                            if ((user != null) && user.isPasswordReset()) {
770                                    return _PATH_PORTAL_UPDATE_PASSWORD;
771                            }
772                            else if ((user != null) && !user.isPasswordReset() &&
773                                             path.equals(_PATH_PORTAL_UPDATE_PASSWORD)) {
774    
775                                    return null;
776                            }
777    
778                            // Authenticated users must have an email address
779    
780                            if ((user != null) &&
781                                    (Validator.isNull(user.getEmailAddress()) ||
782                                     (PropsValues.USERS_EMAIL_ADDRESS_REQUIRED &&
783                                      Validator.isNull(user.getDisplayEmailAddress())))) {
784    
785                                    return _PATH_PORTAL_UPDATE_EMAIL_ADDRESS;
786                            }
787    
788                            // Authenticated users should have a reminder query
789    
790                            if ((user != null) && !user.isDefaultUser() &&
791                                    (Validator.isNull(user.getReminderQueryQuestion()) ||
792                                     Validator.isNull(user.getReminderQueryAnswer()))) {
793    
794                                    if (PropsValues.USERS_REMINDER_QUERIES_ENABLED) {
795                                            return _PATH_PORTAL_UPDATE_REMINDER_QUERY;
796                                    }
797                            }
798                    }
799    
800                    // Users must sign in
801    
802                    if (!isPublicPath(path)) {
803                            if (user == null) {
804                                    SessionErrors.add(request, PrincipalException.class.getName());
805    
806                                    return _PATH_PORTAL_LOGIN;
807                            }
808                    }
809    
810                    ActionMapping actionMapping =
811                            (ActionMapping)moduleConfig.findActionConfig(path);
812    
813                    if (actionMapping == null) {
814                            Action strutsAction = StrutsActionRegistryUtil.getAction(path);
815    
816                            if (strutsAction == null) {
817                                    return null;
818                            }
819                    }
820                    else {
821                            path = actionMapping.getPath();
822                    }
823    
824                    // Define the portlet objects
825    
826                    if (isPortletPath(path)) {
827                            try {
828                                    Portlet portlet = null;
829    
830                                    long companyId = PortalUtil.getCompanyId(request);
831                                    String portletId = ParamUtil.getString(request, "p_p_id");
832    
833                                    if (Validator.isNotNull(portletId)) {
834                                            portlet = PortletLocalServiceUtil.getPortletById(
835                                                    companyId, portletId);
836                                    }
837    
838                                    if (portlet == null) {
839                                            String strutsPath = path.substring(
840                                                    1, path.lastIndexOf(CharPool.SLASH));
841    
842                                            portlet = PortletLocalServiceUtil.getPortletByStrutsPath(
843                                                    companyId, strutsPath);
844                                    }
845    
846                                    if ((portlet != null) && portlet.isActive()) {
847                                            defineObjects(request, response, portlet);
848                                    }
849                            }
850                            catch (Exception e) {
851                                    request.setAttribute(PageContext.EXCEPTION, e);
852    
853                                    path = _PATH_COMMON_ERROR;
854                            }
855                    }
856    
857                    // Authenticated users must have access to at least one layout
858    
859                    if (SessionErrors.contains(
860                                    request, LayoutPermissionException.class.getName())) {
861    
862                            return _PATH_PORTAL_ERROR;
863                    }
864    
865                    return path;
866            }
867    
868            @Override
869            protected boolean processRoles(
870                            HttpServletRequest request, HttpServletResponse response,
871                            ActionMapping actionMapping)
872                    throws IOException, ServletException {
873    
874                    String path = actionMapping.getPath();
875    
876                    if (isPublicPath(path)) {
877                            return true;
878                    }
879    
880                    boolean authorized = true;
881    
882                    User user = null;
883    
884                    try {
885                            user = PortalUtil.getUser(request);
886                    }
887                    catch (Exception e) {
888                    }
889    
890                    if ((user != null) && isPortletPath(path)) {
891                            try {
892    
893                                    // Authenticated users can always log out
894    
895                                    if (path.equals(_PATH_PORTAL_LOGOUT)) {
896                                            return true;
897                                    }
898    
899                                    Portlet portlet = null;
900    
901                                    String portletId = ParamUtil.getString(request, "p_p_id");
902    
903                                    if (Validator.isNotNull(portletId)) {
904                                            portlet = PortletLocalServiceUtil.getPortletById(
905                                                    user.getCompanyId(), portletId);
906                                    }
907    
908                                    String strutsPath = path.substring(
909                                            1, path.lastIndexOf(CharPool.SLASH));
910    
911                                    if (portlet != null) {
912                                            if (!strutsPath.equals(portlet.getStrutsPath())) {
913                                                    throw new PrincipalException();
914                                            }
915                                    }
916                                    else {
917                                            portlet = PortletLocalServiceUtil.getPortletByStrutsPath(
918                                                    user.getCompanyId(), strutsPath);
919                                    }
920    
921                                    if ((portlet != null) && portlet.isActive()) {
922                                            ThemeDisplay themeDisplay =
923                                                    (ThemeDisplay)request.getAttribute(
924                                                            WebKeys.THEME_DISPLAY);
925    
926                                            Layout layout = themeDisplay.getLayout();
927                                            PermissionChecker permissionChecker =
928                                                    themeDisplay.getPermissionChecker();
929    
930                                            if (!PortletPermissionUtil.contains(
931                                                            permissionChecker, layout, portlet,
932                                                            ActionKeys.VIEW)) {
933    
934                                                    throw new PrincipalException();
935                                            }
936                                    }
937                                    else if ((portlet != null) && !portlet.isActive()) {
938                                            SessionErrors.add(
939                                                    request, PortletActiveException.class.getName());
940    
941                                            authorized = false;
942                                    }
943                            }
944                            catch (Exception e) {
945                                    SessionErrors.add(request, PrincipalException.class.getName());
946    
947                                    authorized = false;
948                            }
949                    }
950    
951                    if (!authorized) {
952                            ForwardConfig forwardConfig = actionMapping.findForward(
953                                    _PATH_PORTAL_ERROR);
954    
955                            processForwardConfig(request, response, forwardConfig);
956    
957                            return false;
958                    }
959                    else {
960                            return true;
961                    }
962            }
963    
964            private static final String _PATH_C = "/c";
965    
966            private static final String _PATH_COMMON = "/common";
967    
968            private static final String _PATH_COMMON_ERROR = "/common/error";
969    
970            private static final String _PATH_J_SECURITY_CHECK = "/j_security_check";
971    
972            private static final String _PATH_PORTAL = "/portal";
973    
974            private static final String _PATH_PORTAL_API_JSONWS = "/portal/api/jsonws";
975    
976            private static final String _PATH_PORTAL_ERROR = "/portal/error";
977    
978            private static final String _PATH_PORTAL_EXPIRE_SESSION =
979                    "/portal/expire_session";
980    
981            private static final String _PATH_PORTAL_EXTEND_SESSION =
982                    "/portal/extend_session";
983    
984            private static final String _PATH_PORTAL_FLASH = "/portal/flash";
985    
986            private static final String _PATH_PORTAL_J_LOGIN = "/portal/j_login";
987    
988            private static final String _PATH_PORTAL_JSON_SERVICE =
989                    "/portal/json_service";
990    
991            private static final String _PATH_PORTAL_LAYOUT = "/portal/layout";
992    
993            private static final String _PATH_PORTAL_LICENSE = "/portal/license";
994    
995            private static final String _PATH_PORTAL_LOGIN = "/portal/login";
996    
997            private static final String _PATH_PORTAL_LOGOUT = "/portal/logout";
998    
999            private static final String _PATH_PORTAL_PROTECTED = "/portal/protected";
1000    
1001            private static final String _PATH_PORTAL_RENDER_PORTLET =
1002                    "/portal/render_portlet";
1003    
1004            private static final String _PATH_PORTAL_SETUP_WIZARD =
1005                    "/portal/setup_wizard";
1006    
1007            private static final String _PATH_PORTAL_STATUS = "/portal/status";
1008    
1009            private static final String _PATH_PORTAL_TCK = "/portal/tck";
1010    
1011            private static final String _PATH_PORTAL_TERMS_OF_USE =
1012                    "/portal/terms_of_use";
1013    
1014            private static final String _PATH_PORTAL_UPDATE_EMAIL_ADDRESS =
1015                    "/portal/update_email_address";
1016    
1017            private static final String _PATH_PORTAL_UPDATE_PASSWORD =
1018                    "/portal/update_password";
1019    
1020            private static final String _PATH_PORTAL_UPDATE_REMINDER_QUERY =
1021                    "/portal/update_reminder_query";
1022    
1023            private static final String _PATH_PORTAL_UPDATE_TERMS_OF_USE =
1024                    "/portal/update_terms_of_use";
1025    
1026            private static final String _PATH_PORTAL_VERIFY_EMAIL_ADDRESS =
1027                    "/portal/verify_email_address";
1028    
1029            private static Log _log = LogFactoryUtil.getLog(
1030                    PortalRequestProcessor.class);
1031    
1032            private Set<String> _lastPaths;
1033            private Set<String> _publicPaths;
1034            private Set<String> _trackerIgnorePaths;
1035    
1036    }