1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.wiki.action;
24  
25  import com.liferay.portal.kernel.dao.search.SearchContainer;
26  import com.liferay.portal.kernel.util.ContentTypes;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.model.Layout;
31  import com.liferay.portal.struts.ActionConstants;
32  import com.liferay.portal.struts.PortletAction;
33  import com.liferay.portal.theme.ThemeDisplay;
34  import com.liferay.portal.util.PortalUtil;
35  import com.liferay.portal.util.PortletKeys;
36  import com.liferay.portal.util.WebKeys;
37  import com.liferay.portlet.PortletURLImpl;
38  import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
39  import com.liferay.util.RSSUtil;
40  import com.liferay.util.servlet.ServletResponseUtil;
41  
42  import java.util.Locale;
43  
44  import javax.portlet.ActionRequest;
45  import javax.portlet.ActionResponse;
46  import javax.portlet.PortletConfig;
47  import javax.portlet.PortletRequest;
48  import javax.portlet.PortletURL;
49  
50  import javax.servlet.http.HttpServletRequest;
51  import javax.servlet.http.HttpServletResponse;
52  
53  import org.apache.struts.action.ActionForm;
54  import org.apache.struts.action.ActionForward;
55  import org.apache.struts.action.ActionMapping;
56  
57  /**
58   * <a href="RSSAction.java.html"><b><i>View Source</i></b></a>
59   *
60   * @author Jorge Ferrer
61   *
62   */
63  public class RSSAction extends PortletAction {
64  
65      public ActionForward strutsExecute(
66              ActionMapping mapping, ActionForm form, HttpServletRequest request,
67              HttpServletResponse response)
68          throws Exception {
69  
70          try {
71              ServletResponseUtil.sendFile(
72                  response, null, getRSS(request), ContentTypes.TEXT_XML_UTF8);
73  
74              return null;
75          }
76          catch (Exception e) {
77              PortalUtil.sendError(e, request, response);
78  
79              return null;
80          }
81      }
82  
83      public void processAction(
84              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
85              ActionRequest actionRequest, ActionResponse actionResponse)
86          throws Exception {
87  
88          try {
89              HttpServletRequest request = PortalUtil.getHttpServletRequest(
90                  actionRequest);
91              HttpServletResponse response = PortalUtil.getHttpServletResponse(
92                  actionResponse);
93  
94              ServletResponseUtil.sendFile(
95                  response, null, getRSS(request), ContentTypes.TEXT_XML_UTF8);
96  
97              setForward(actionRequest, ActionConstants.COMMON_NULL);
98          }
99          catch (Exception e) {
100             PortalUtil.sendError(e, actionRequest, actionResponse);
101         }
102     }
103 
104     protected byte[] getRSS(HttpServletRequest request) throws Exception {
105         ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
106             WebKeys.THEME_DISPLAY);
107 
108         Layout layout = themeDisplay.getLayout();
109 
110         long companyId = ParamUtil.getLong(request, "companyId");
111         long nodeId = ParamUtil.getLong(request, "nodeId");
112         String title = ParamUtil.getString(request, "title");
113         int max = ParamUtil.getInteger(
114             request, "max", SearchContainer.DEFAULT_DELTA);
115         String type = ParamUtil.getString(
116             request, "type", RSSUtil.DEFAULT_TYPE);
117         double version = ParamUtil.getDouble(
118             request, "version", RSSUtil.DEFAULT_VERSION);
119         String displayStyle = ParamUtil.getString(
120             request, "displayStyle", RSSUtil.DISPLAY_STYLE_FULL_CONTENT);
121 
122         PortletURL feedURL = new PortletURLImpl(
123             request, PortletKeys.WIKI, layout.getPlid(),
124             PortletRequest.RENDER_PHASE);
125 
126         feedURL.setParameter("nodeId", String.valueOf(nodeId));
127 
128         PortletURL entryURL = new PortletURLImpl(
129             request, PortletKeys.WIKI, layout.getPlid(),
130             PortletRequest.RENDER_PHASE);
131 
132         entryURL.setParameter("nodeId", String.valueOf(nodeId));
133         entryURL.setParameter("title", title);
134 
135         Locale locale = themeDisplay.getLocale();
136 
137         String rss = StringPool.BLANK;
138 
139         if ((nodeId > 0) && (Validator.isNotNull(title))) {
140             rss = WikiPageServiceUtil.getPagesRSS(
141                 companyId, nodeId, title, max, type, version, displayStyle,
142                 feedURL.toString(), entryURL.toString(), locale);
143         }
144         else if (nodeId > 0) {
145             rss = WikiPageServiceUtil.getNodePagesRSS(
146                 nodeId, max, type, version, displayStyle, feedURL.toString(),
147                 entryURL.toString());
148         }
149 
150         return rss.getBytes(StringPool.UTF8);
151     }
152 
153     protected boolean isCheckMethodOnProcessAction() {
154         return _CHECK_METHOD_ON_PROCESS_ACTION;
155     }
156 
157     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
158 
159 }