001    /**
002     * Copyright (c) 2000-2010 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.dao.search.SearchContainer;
018    import com.liferay.portal.kernel.util.ContentTypes;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.model.Layout;
023    import com.liferay.portal.struts.ActionConstants;
024    import com.liferay.portal.struts.PortletAction;
025    import com.liferay.portal.theme.ThemeDisplay;
026    import com.liferay.portal.util.PortalUtil;
027    import com.liferay.portal.util.PortletKeys;
028    import com.liferay.portal.util.WebKeys;
029    import com.liferay.portlet.PortletURLImpl;
030    import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
031    import com.liferay.util.RSSUtil;
032    import com.liferay.util.servlet.ServletResponseUtil;
033    
034    import java.util.Locale;
035    
036    import javax.portlet.ActionRequest;
037    import javax.portlet.ActionResponse;
038    import javax.portlet.PortletConfig;
039    import javax.portlet.PortletRequest;
040    import javax.portlet.PortletURL;
041    
042    import javax.servlet.http.HttpServletRequest;
043    import javax.servlet.http.HttpServletResponse;
044    
045    import org.apache.struts.action.ActionForm;
046    import org.apache.struts.action.ActionForward;
047    import org.apache.struts.action.ActionMapping;
048    
049    /**
050     * @author Jorge Ferrer
051     */
052    public class RSSAction extends PortletAction {
053    
054            public ActionForward strutsExecute(
055                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
056                            HttpServletResponse response)
057                    throws Exception {
058    
059                    try {
060                            ServletResponseUtil.sendFile(
061                                    request, response, null, getRSS(request),
062                                    ContentTypes.TEXT_XML_UTF8);
063    
064                            return null;
065                    }
066                    catch (Exception e) {
067                            PortalUtil.sendError(e, request, response);
068    
069                            return null;
070                    }
071            }
072    
073            public void processAction(
074                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
075                            ActionRequest actionRequest, ActionResponse actionResponse)
076                    throws Exception {
077    
078                    try {
079                            HttpServletRequest request = PortalUtil.getHttpServletRequest(
080                                    actionRequest);
081                            HttpServletResponse response = PortalUtil.getHttpServletResponse(
082                                    actionResponse);
083    
084                            ServletResponseUtil.sendFile(
085                                    request, response, null, getRSS(request),
086                                    ContentTypes.TEXT_XML_UTF8);
087    
088                            setForward(actionRequest, ActionConstants.COMMON_NULL);
089                    }
090                    catch (Exception e) {
091                            PortalUtil.sendError(e, actionRequest, actionResponse);
092                    }
093            }
094    
095            protected byte[] getRSS(HttpServletRequest request) throws Exception {
096                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
097                            WebKeys.THEME_DISPLAY);
098    
099                    Layout layout = themeDisplay.getLayout();
100    
101                    long companyId = ParamUtil.getLong(request, "companyId");
102                    long nodeId = ParamUtil.getLong(request, "nodeId");
103                    String title = ParamUtil.getString(request, "title");
104                    int max = ParamUtil.getInteger(
105                            request, "max", SearchContainer.DEFAULT_DELTA);
106                    String type = ParamUtil.getString(
107                            request, "type", RSSUtil.DEFAULT_TYPE);
108                    double version = ParamUtil.getDouble(
109                            request, "version", RSSUtil.DEFAULT_VERSION);
110                    String displayStyle = ParamUtil.getString(
111                            request, "displayStyle", RSSUtil.DISPLAY_STYLE_FULL_CONTENT);
112    
113                    PortletURL feedURL = new PortletURLImpl(
114                            request, PortletKeys.WIKI, layout.getPlid(),
115                            PortletRequest.RENDER_PHASE);
116    
117                    feedURL.setParameter("nodeId", String.valueOf(nodeId));
118    
119                    PortletURL entryURL = new PortletURLImpl(
120                            request, PortletKeys.WIKI, layout.getPlid(),
121                            PortletRequest.RENDER_PHASE);
122    
123                    entryURL.setParameter("nodeId", String.valueOf(nodeId));
124                    entryURL.setParameter("title", title);
125    
126                    Locale locale = themeDisplay.getLocale();
127    
128                    String rss = StringPool.BLANK;
129    
130                    if ((nodeId > 0) && (Validator.isNotNull(title))) {
131                            rss = WikiPageServiceUtil.getPagesRSS(
132                                    companyId, nodeId, title, max, type, version, displayStyle,
133                                    feedURL.toString(), entryURL.toString(), locale);
134                    }
135                    else if (nodeId > 0) {
136                            rss = WikiPageServiceUtil.getNodePagesRSS(
137                                    nodeId, max, type, version, displayStyle, feedURL.toString(),
138                                    entryURL.toString());
139                    }
140    
141                    return rss.getBytes(StringPool.UTF8);
142            }
143    
144            protected boolean isCheckMethodOnProcessAction() {
145                    return _CHECK_METHOD_ON_PROCESS_ACTION;
146            }
147    
148            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
149    
150    }