001    /**
002     * Copyright (c) 2000-2010 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.blogs.action;
016    
017    import com.liferay.portal.kernel.dao.search.SearchContainer;
018    import com.liferay.portal.kernel.util.ContentTypes;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.workflow.WorkflowConstants;
022    import com.liferay.portal.model.Layout;
023    import com.liferay.portal.struts.ActionConstants;
024    import com.liferay.portal.struts.PortletAction;
025    import com.liferay.portal.theme.ThemeDisplay;
026    import com.liferay.portal.util.Portal;
027    import com.liferay.portal.util.PortalUtil;
028    import com.liferay.portal.util.WebKeys;
029    import com.liferay.portlet.blogs.service.BlogsEntryServiceUtil;
030    import com.liferay.util.RSSUtil;
031    import com.liferay.util.servlet.ServletResponseUtil;
032    
033    import javax.portlet.ActionRequest;
034    import javax.portlet.ActionResponse;
035    import javax.portlet.PortletConfig;
036    
037    import javax.servlet.http.HttpServletRequest;
038    import javax.servlet.http.HttpServletResponse;
039    
040    import org.apache.struts.action.ActionForm;
041    import org.apache.struts.action.ActionForward;
042    import org.apache.struts.action.ActionMapping;
043    
044    /**
045     * @author Brian Wing Shun Chan
046     */
047    public class RSSAction extends PortletAction {
048    
049            public ActionForward strutsExecute(
050                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
051                            HttpServletResponse response)
052                    throws Exception {
053    
054                    try {
055                            ServletResponseUtil.sendFile(
056                                    request, response, null, getRSS(request),
057                                    ContentTypes.TEXT_XML_UTF8);
058    
059                            return null;
060                    }
061                    catch (Exception e) {
062                            PortalUtil.sendError(e, request, response);
063    
064                            return null;
065                    }
066            }
067    
068            public void processAction(
069                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
070                            ActionRequest actionRequest, ActionResponse actionResponse)
071                    throws Exception {
072    
073                    try {
074                            HttpServletRequest request = PortalUtil.getHttpServletRequest(
075                                    actionRequest);
076                            HttpServletResponse response = PortalUtil.getHttpServletResponse(
077                                    actionResponse);
078    
079                            ServletResponseUtil.sendFile(
080                                    request, response, null, getRSS(request),
081                                    ContentTypes.TEXT_XML_UTF8);
082    
083                            setForward(actionRequest, ActionConstants.COMMON_NULL);
084                    }
085                    catch (Exception e) {
086                            PortalUtil.sendError(e, actionRequest, actionResponse);
087                    }
088            }
089    
090            protected byte[] getRSS(HttpServletRequest request) throws Exception {
091                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
092                            WebKeys.THEME_DISPLAY);
093    
094                    Layout layout = themeDisplay.getLayout();
095    
096                    long plid = ParamUtil.getLong(request, "p_l_id");
097                    long companyId = ParamUtil.getLong(request, "companyId");
098                    long groupId = ParamUtil.getLong(request, "groupId");
099                    long organizationId = ParamUtil.getLong(request, "organizationId");
100                    int status = WorkflowConstants.STATUS_APPROVED;
101                    int max = ParamUtil.getInteger(
102                            request, "max", SearchContainer.DEFAULT_DELTA);
103                    String type = ParamUtil.getString(
104                            request, "type", RSSUtil.DEFAULT_TYPE);
105                    double version = ParamUtil.getDouble(
106                            request, "version", RSSUtil.DEFAULT_VERSION);
107                    String displayStyle = ParamUtil.getString(
108                            request, "displayStyle", RSSUtil.DISPLAY_STYLE_FULL_CONTENT);
109    
110                    String feedURL =
111                            themeDisplay.getPortalURL() + themeDisplay.getPathMain() +
112                                    "/blogs/find_entry?";
113    
114                    String entryURL = feedURL;
115    
116                    String rss = StringPool.BLANK;
117    
118                    if (companyId > 0) {
119                            feedURL = StringPool.BLANK;
120    
121                            rss = BlogsEntryServiceUtil.getCompanyEntriesRSS(
122                                    companyId, status, max, type, version, displayStyle, feedURL,
123                                    entryURL, themeDisplay);
124                    }
125                    else if (groupId > 0) {
126                            feedURL += "p_l_id=" + plid;
127    
128                            entryURL = feedURL;
129    
130                            rss = BlogsEntryServiceUtil.getGroupEntriesRSS(
131                                    groupId, status, max, type, version, displayStyle, feedURL,
132                                    entryURL, themeDisplay);
133                    }
134                    else if (organizationId > 0) {
135                            feedURL = StringPool.BLANK;
136    
137                            rss = BlogsEntryServiceUtil.getOrganizationEntriesRSS(
138                                    organizationId, status, max, type, version, displayStyle,
139                                    feedURL, entryURL, themeDisplay);
140                    }
141                    else if (layout != null) {
142                            if (layout.hasScopeGroup()) {
143                                    groupId = layout.getScopeGroup().getGroupId();
144                            }
145                            else {
146                                    groupId = layout.getGroupId();
147                            }
148    
149                            feedURL =
150                                    PortalUtil.getLayoutFullURL(themeDisplay) +
151                                            Portal.FRIENDLY_URL_SEPARATOR + "blogs/rss";
152    
153                            entryURL = feedURL;
154    
155                            rss = BlogsEntryServiceUtil.getGroupEntriesRSS(
156                                    groupId, status, max, type, version, displayStyle, feedURL,
157                                    entryURL, themeDisplay);
158                    }
159    
160                    return rss.getBytes(StringPool.UTF8);
161            }
162    
163            protected boolean isCheckMethodOnProcessAction() {
164                    return _CHECK_METHOD_ON_PROCESS_ACTION;
165            }
166    
167            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
168    
169    }