1   /**
2    * Copyright (c) 2000-2008 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.WebKeys;
36  import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
37  import com.liferay.util.RSSUtil;
38  import com.liferay.util.servlet.ServletResponseUtil;
39  
40  import java.util.Locale;
41  
42  import javax.portlet.ActionRequest;
43  import javax.portlet.ActionResponse;
44  import javax.portlet.PortletConfig;
45  
46  import javax.servlet.http.HttpServletRequest;
47  import javax.servlet.http.HttpServletResponse;
48  
49  import org.apache.struts.action.ActionForm;
50  import org.apache.struts.action.ActionForward;
51  import org.apache.struts.action.ActionMapping;
52  
53  /**
54   * <a href="RSSAction.java.html"><b><i>View Source</i></b></a>
55   *
56   * @author Jorge Ferrer
57   *
58   */
59  public class RSSAction extends PortletAction {
60  
61      public ActionForward strutsExecute(
62              ActionMapping mapping, ActionForm form, HttpServletRequest request,
63              HttpServletResponse response)
64          throws Exception {
65  
66          try {
67              ServletResponseUtil.sendFile(
68                  response, null, getRSS(request), ContentTypes.TEXT_XML_UTF8);
69  
70              return null;
71          }
72          catch (Exception e) {
73              PortalUtil.sendError(e, request, response);
74  
75              return null;
76          }
77      }
78  
79      public void processAction(
80              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
81              ActionRequest actionRequest, ActionResponse actionResponse)
82          throws Exception {
83  
84          try {
85              HttpServletRequest request = PortalUtil.getHttpServletRequest(
86                  actionRequest);
87              HttpServletResponse response = PortalUtil.getHttpServletResponse(
88                  actionResponse);
89  
90              ServletResponseUtil.sendFile(
91                  response, null, getRSS(request), ContentTypes.TEXT_XML_UTF8);
92  
93              setForward(actionRequest, ActionConstants.COMMON_NULL);
94          }
95          catch (Exception e) {
96              PortalUtil.sendError(e, actionRequest, actionResponse);
97          }
98      }
99  
100     protected byte[] getRSS(HttpServletRequest request) throws Exception {
101         ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
102             WebKeys.THEME_DISPLAY);
103 
104         Layout layout = themeDisplay.getLayout();
105 
106         long companyId = ParamUtil.getLong(request, "companyId");
107         long nodeId = ParamUtil.getLong(request, "nodeId");
108         String title = ParamUtil.getString(request, "title");
109         int max = ParamUtil.getInteger(
110             request, "max", SearchContainer.DEFAULT_DELTA);
111         String type = ParamUtil.getString(
112             request, "type", RSSUtil.DEFAULT_TYPE);
113         double version = ParamUtil.getDouble(
114             request, "version", RSSUtil.DEFAULT_VERSION);
115         String displayStyle = ParamUtil.getString(
116             request, "displayStyle", RSSUtil.DISPLAY_STYLE_FULL_CONTENT);
117 
118         String feedURL =
119             themeDisplay.getURLPortal() +
120                 PortalUtil.getLayoutURL(layout, themeDisplay) + "/wiki/" +
121                     nodeId;
122 
123         String entryURL = feedURL + StringPool.SLASH + title;
124 
125         Locale locale = themeDisplay.getLocale();
126 
127         String rss = StringPool.BLANK;
128 
129         if ((nodeId > 0) && (Validator.isNotNull(title))) {
130             rss = WikiPageServiceUtil.getPagesRSS(
131                 companyId, nodeId, title, max, type, version, displayStyle,
132                 feedURL, entryURL, locale);
133         }
134         else if (nodeId > 0) {
135             rss = WikiPageServiceUtil.getNodePagesRSS(
136                 nodeId, max, type, version, displayStyle, feedURL, entryURL);
137         }
138 
139         return rss.getBytes(StringPool.UTF8);
140     }
141 
142     protected boolean isCheckMethodOnProcessAction() {
143         return _CHECK_METHOD_ON_PROCESS_ACTION;
144     }
145 
146     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
147 
148 }