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.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.StringBundler;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.Validator;
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.Portal;
028    import com.liferay.portal.util.PortalUtil;
029    import com.liferay.portal.util.PortletKeys;
030    import com.liferay.portal.util.WebKeys;
031    import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
032    import com.liferay.portlet.wiki.util.WikiUtil;
033    import com.liferay.util.RSSUtil;
034    
035    import java.util.Locale;
036    
037    import javax.portlet.ActionRequest;
038    import javax.portlet.ActionResponse;
039    import javax.portlet.PortletConfig;
040    
041    import javax.servlet.http.HttpServletRequest;
042    import javax.servlet.http.HttpServletResponse;
043    
044    import org.apache.struts.action.ActionForm;
045    import org.apache.struts.action.ActionForward;
046    import org.apache.struts.action.ActionMapping;
047    
048    /**
049     * @author Jorge Ferrer
050     */
051    public class RSSAction extends PortletAction {
052    
053            @Override
054            public void processAction(
055                            ActionMapping actionMapping, ActionForm actionForm,
056                            PortletConfig portletConfig, ActionRequest actionRequest,
057                            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 actionMapping, ActionForm actionForm,
080                            HttpServletRequest request, 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                    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.TYPE_DEFAULT);
108                    double version = ParamUtil.getDouble(
109                            request, "version", RSSUtil.VERSION_DEFAULT);
110                    String displayStyle = ParamUtil.getString(
111                            request, "displayStyle", RSSUtil.DISPLAY_STYLE_FULL_CONTENT);
112    
113                    String layoutFullURL = PortalUtil.getLayoutFullURL(
114                            themeDisplay.getScopeGroupId(), PortletKeys.WIKI);
115    
116                    StringBundler sb = new StringBundler(4);
117    
118                    sb.append(layoutFullURL);
119                    sb.append(Portal.FRIENDLY_URL_SEPARATOR);
120                    sb.append("wiki/");
121                    sb.append(String.valueOf(nodeId));
122    
123                    String feedURL = sb.toString();
124    
125                    String entryURL = feedURL + StringPool.SLASH + title;
126    
127                    Locale locale = themeDisplay.getLocale();
128    
129                    String rss = StringPool.BLANK;
130    
131                    if (nodeId > 0) {
132                            String attachmentURLPrefix = WikiUtil.getAttachmentURLPrefix(
133                                    themeDisplay.getPathMain(), themeDisplay.getPlid(), nodeId,
134                                    title);
135    
136                            if (Validator.isNotNull(title)) {
137                                    rss = WikiPageServiceUtil.getPagesRSS(
138                                            companyId, nodeId, title, max, type, version, displayStyle,
139                                            feedURL, entryURL, attachmentURLPrefix, locale);
140                            }
141                            else {
142                                    rss = WikiPageServiceUtil.getNodePagesRSS(
143                                            nodeId, max, type, version, displayStyle, feedURL, entryURL,
144                                            attachmentURLPrefix);
145                            }
146                    }
147    
148                    return rss.getBytes(StringPool.UTF8);
149            }
150    
151            @Override
152            protected boolean isCheckMethodOnProcessAction() {
153                    return _CHECK_METHOD_ON_PROCESS_ACTION;
154            }
155    
156            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
157    
158    }