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