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