com.liferay.portlet.wiki.action.RSSAction.java Source code

Java tutorial

Introduction

Here is the source code for com.liferay.portlet.wiki.action.RSSAction.java

Source

/**
 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.liferay.portlet.wiki.action;

import com.liferay.portal.kernel.dao.search.SearchContainer;
import com.liferay.portal.kernel.servlet.ServletResponseUtil;
import com.liferay.portal.kernel.util.ContentTypes;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.model.Layout;
import com.liferay.portal.struts.ActionConstants;
import com.liferay.portal.struts.PortletAction;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.PortalUtil;
import com.liferay.portal.util.PortletKeys;
import com.liferay.portal.util.WebKeys;
import com.liferay.portlet.PortletURLImpl;
import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
import com.liferay.util.RSSUtil;

import java.util.Locale;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.PortletRequest;
import javax.portlet.PortletURL;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 * @author Jorge Ferrer
 */
public class RSSAction extends PortletAction {

    @Override
    public ActionForward strutsExecute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        try {
            ServletResponseUtil.sendFile(request, response, null, getRSS(request), ContentTypes.TEXT_XML_UTF8);

            return null;
        } catch (Exception e) {
            PortalUtil.sendError(e, request, response);

            return null;
        }
    }

    @Override
    public void processAction(ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
            ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {

        try {
            HttpServletRequest request = PortalUtil.getHttpServletRequest(actionRequest);
            HttpServletResponse response = PortalUtil.getHttpServletResponse(actionResponse);

            ServletResponseUtil.sendFile(request, response, null, getRSS(request), ContentTypes.TEXT_XML_UTF8);

            setForward(actionRequest, ActionConstants.COMMON_NULL);
        } catch (Exception e) {
            PortalUtil.sendError(e, actionRequest, actionResponse);
        }
    }

    protected byte[] getRSS(HttpServletRequest request) throws Exception {
        ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);

        Layout layout = themeDisplay.getLayout();

        long companyId = ParamUtil.getLong(request, "companyId");
        long nodeId = ParamUtil.getLong(request, "nodeId");
        String title = ParamUtil.getString(request, "title");
        int max = ParamUtil.getInteger(request, "max", SearchContainer.DEFAULT_DELTA);
        String type = ParamUtil.getString(request, "type", RSSUtil.TYPE_DEFAULT);
        double version = ParamUtil.getDouble(request, "version", RSSUtil.VERSION_DEFAULT);
        String displayStyle = ParamUtil.getString(request, "displayStyle", RSSUtil.DISPLAY_STYLE_FULL_CONTENT);

        PortletURL feedURL = new PortletURLImpl(request, PortletKeys.WIKI, layout.getPlid(),
                PortletRequest.RENDER_PHASE);

        feedURL.setParameter("nodeId", String.valueOf(nodeId));

        PortletURL entryURL = new PortletURLImpl(request, PortletKeys.WIKI, layout.getPlid(),
                PortletRequest.RENDER_PHASE);

        entryURL.setParameter("nodeId", String.valueOf(nodeId));
        entryURL.setParameter("title", title);

        Locale locale = themeDisplay.getLocale();

        String rss = StringPool.BLANK;

        if ((nodeId > 0) && (Validator.isNotNull(title))) {
            rss = WikiPageServiceUtil.getPagesRSS(companyId, nodeId, title, max, type, version, displayStyle,
                    feedURL.toString(), entryURL.toString(), locale);
        } else if (nodeId > 0) {
            rss = WikiPageServiceUtil.getNodePagesRSS(nodeId, max, type, version, displayStyle, feedURL.toString(),
                    entryURL.toString());
        }

        return rss.getBytes(StringPool.UTF8);
    }

    @Override
    protected boolean isCheckMethodOnProcessAction() {
        return _CHECK_METHOD_ON_PROCESS_ACTION;
    }

    private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;

}