001    /**
002     * Copyright (c) 2000-2012 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.portlet.wiki.action;
016    
017    import com.liferay.portal.kernel.servlet.SessionErrors;
018    import com.liferay.portal.kernel.util.ParamUtil;
019    import com.liferay.portal.kernel.util.Validator;
020    import com.liferay.portal.security.auth.PrincipalException;
021    import com.liferay.portal.struts.PortletAction;
022    import com.liferay.portal.util.WebKeys;
023    import com.liferay.portlet.wiki.NoSuchNodeException;
024    import com.liferay.portlet.wiki.NoSuchPageException;
025    import com.liferay.portlet.wiki.model.WikiNode;
026    
027    import javax.portlet.PortletConfig;
028    import javax.portlet.RenderRequest;
029    import javax.portlet.RenderResponse;
030    
031    import org.apache.struts.action.ActionForm;
032    import org.apache.struts.action.ActionForward;
033    import org.apache.struts.action.ActionMapping;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     * @author Jorge Ferrer
038     */
039    public class ViewPageAction extends PortletAction {
040    
041            @Override
042            public ActionForward render(
043                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
044                            RenderRequest renderRequest, RenderResponse renderResponse)
045                    throws Exception {
046    
047                    long categoryId = ParamUtil.getLong(renderRequest, "categoryId");
048    
049                    if (categoryId > 0) {
050                            return ViewNodeAction.viewNode(
051                                    mapping, renderRequest, "portlet.wiki.view_categorized_pages");
052                    }
053    
054                    String tag = ParamUtil.getString(renderRequest, "tag");
055    
056                    if (Validator.isNotNull(tag)) {
057                            return ViewNodeAction.viewNode(
058                                    mapping, renderRequest, "portlet.wiki.view_tagged_pages");
059                    }
060    
061                    try {
062                            ActionUtil.getNode(renderRequest);
063                            ActionUtil.getPage(renderRequest);
064                    }
065                    catch (Exception e) {
066                            if (e instanceof NoSuchNodeException ||
067                                    e instanceof NoSuchPageException ||
068                                    e instanceof PrincipalException) {
069    
070                                    SessionErrors.add(renderRequest, e.getClass().getName());
071    
072                                    return mapping.findForward("portlet.wiki.error");
073                            }
074                            else {
075                                    throw e;
076                            }
077                    }
078    
079                    return mapping.findForward(
080                            getForward(renderRequest, "portlet.wiki.view_page"));
081            }
082    
083            protected void getNode(RenderRequest renderRequest) throws Exception {
084                    ActionUtil.getNode(renderRequest);
085    
086                    WikiNode node = (WikiNode)renderRequest.getAttribute(WebKeys.WIKI_NODE);
087    
088                    if (node != null) {
089                            return;
090                    }
091    
092                    ActionUtil.getFirstVisibleNode(renderRequest);
093            }
094    
095            @Override
096            protected boolean isCheckMethodOnProcessAction() {
097                    return false;
098            }
099    
100    }