1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.wiki.action;
16  
17  import com.liferay.portal.kernel.dao.search.SearchContainer;
18  import com.liferay.portal.kernel.util.ContentTypes;
19  import com.liferay.portal.kernel.util.ParamUtil;
20  import com.liferay.portal.kernel.util.StringPool;
21  import com.liferay.portal.kernel.util.Validator;
22  import com.liferay.portal.model.Layout;
23  import com.liferay.portal.struts.ActionConstants;
24  import com.liferay.portal.struts.PortletAction;
25  import com.liferay.portal.theme.ThemeDisplay;
26  import com.liferay.portal.util.PortalUtil;
27  import com.liferay.portal.util.PortletKeys;
28  import com.liferay.portal.util.WebKeys;
29  import com.liferay.portlet.PortletURLImpl;
30  import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
31  import com.liferay.util.RSSUtil;
32  import com.liferay.util.servlet.ServletResponseUtil;
33  
34  import java.util.Locale;
35  
36  import javax.portlet.ActionRequest;
37  import javax.portlet.ActionResponse;
38  import javax.portlet.PortletConfig;
39  import javax.portlet.PortletRequest;
40  import javax.portlet.PortletURL;
41  
42  import javax.servlet.http.HttpServletRequest;
43  import javax.servlet.http.HttpServletResponse;
44  
45  import org.apache.struts.action.ActionForm;
46  import org.apache.struts.action.ActionForward;
47  import org.apache.struts.action.ActionMapping;
48  
49  /**
50   * <a href="RSSAction.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Jorge Ferrer
53   */
54  public class RSSAction extends PortletAction {
55  
56      public ActionForward strutsExecute(
57              ActionMapping mapping, ActionForm form, HttpServletRequest request,
58              HttpServletResponse response)
59          throws Exception {
60  
61          try {
62              ServletResponseUtil.sendFile(
63                  request, response, null, getRSS(request),
64                  ContentTypes.TEXT_XML_UTF8);
65  
66              return null;
67          }
68          catch (Exception e) {
69              PortalUtil.sendError(e, request, response);
70  
71              return null;
72          }
73      }
74  
75      public void processAction(
76              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
77              ActionRequest actionRequest, ActionResponse actionResponse)
78          throws Exception {
79  
80          try {
81              HttpServletRequest request = PortalUtil.getHttpServletRequest(
82                  actionRequest);
83              HttpServletResponse response = PortalUtil.getHttpServletResponse(
84                  actionResponse);
85  
86              ServletResponseUtil.sendFile(
87                  request, response, null, getRSS(request),
88                  ContentTypes.TEXT_XML_UTF8);
89  
90              setForward(actionRequest, ActionConstants.COMMON_NULL);
91          }
92          catch (Exception e) {
93              PortalUtil.sendError(e, actionRequest, actionResponse);
94          }
95      }
96  
97      protected byte[] getRSS(HttpServletRequest request) throws Exception {
98          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
99              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.DEFAULT_TYPE);
110         double version = ParamUtil.getDouble(
111             request, "version", RSSUtil.DEFAULT_VERSION);
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     protected boolean isCheckMethodOnProcessAction() {
147         return _CHECK_METHOD_ON_PROCESS_ACTION;
148     }
149 
150     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
151 
152 }