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.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 actionMapping, ActionForm actionForm,
044                            PortletConfig portletConfig, RenderRequest renderRequest,
045                            RenderResponse renderResponse)
046                    throws Exception {
047    
048                    long categoryId = ParamUtil.getLong(renderRequest, "categoryId");
049    
050                    if (categoryId > 0) {
051                            return ViewNodeAction.viewNode(
052                                    actionMapping, renderRequest,
053                                    "portlet.wiki.view_categorized_pages");
054                    }
055    
056                    String tag = ParamUtil.getString(renderRequest, "tag");
057    
058                    if (Validator.isNotNull(tag)) {
059                            return ViewNodeAction.viewNode(
060                                    actionMapping, renderRequest, "portlet.wiki.view_tagged_pages");
061                    }
062    
063                    try {
064                            ActionUtil.getNode(renderRequest);
065                            ActionUtil.getPage(renderRequest);
066                    }
067                    catch (Exception e) {
068                            if (e instanceof NoSuchNodeException ||
069                                    e instanceof NoSuchPageException ||
070                                    e instanceof PrincipalException) {
071    
072                                    SessionErrors.add(renderRequest, e.getClass());
073    
074                                    return actionMapping.findForward("portlet.wiki.error");
075                            }
076                            else {
077                                    throw e;
078                            }
079                    }
080    
081                    return actionMapping.findForward(
082                            getForward(renderRequest, "portlet.wiki.view_page"));
083            }
084    
085            protected void getNode(RenderRequest renderRequest) throws Exception {
086                    ActionUtil.getNode(renderRequest);
087    
088                    WikiNode node = (WikiNode)renderRequest.getAttribute(WebKeys.WIKI_NODE);
089    
090                    if (node != null) {
091                            return;
092                    }
093    
094                    ActionUtil.getFirstVisibleNode(renderRequest);
095            }
096    
097            @Override
098            protected boolean isCheckMethodOnProcessAction() {
099                    return false;
100            }
101    
102    }