001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.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.StringPool;
022    import com.liferay.portal.kernel.workflow.WorkflowConstants;
023    import com.liferay.portal.model.Layout;
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    
032    import java.io.OutputStream;
033    
034    import javax.portlet.PortletConfig;
035    import javax.portlet.ResourceRequest;
036    import javax.portlet.ResourceResponse;
037    
038    import javax.servlet.http.HttpServletRequest;
039    import javax.servlet.http.HttpServletResponse;
040    
041    import org.apache.struts.action.ActionForm;
042    import org.apache.struts.action.ActionForward;
043    import org.apache.struts.action.ActionMapping;
044    
045    /**
046     * @author Brian Wing Shun Chan
047     */
048    public class RSSAction extends PortletAction {
049    
050            @Override
051            public void serveResource(
052                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
053                            ResourceRequest resourceRequest, ResourceResponse resourceResponse)
054                    throws Exception {
055    
056                    resourceResponse.setContentType(ContentTypes.TEXT_XML_UTF8);
057    
058                    OutputStream outputStream = resourceResponse.getPortletOutputStream();
059    
060                    try {
061                            byte[] bytes = getRSS(resourceRequest);
062    
063                            outputStream.write(bytes);
064                    }
065                    finally {
066                            outputStream.close();
067                    }
068            }
069    
070            @Override
071            public ActionForward strutsExecute(
072                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
073                            HttpServletResponse response)
074                    throws Exception {
075    
076                    try {
077                            ServletResponseUtil.sendFile(
078                                    request, response, null, getRSS(request),
079                                    ContentTypes.TEXT_XML_UTF8);
080    
081                            return null;
082                    }
083                    catch (Exception e) {
084                            PortalUtil.sendError(e, request, response);
085    
086                            return null;
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.TYPE_DEFAULT);
105                    double version = ParamUtil.getDouble(
106                            request, "version", RSSUtil.VERSION_DEFAULT);
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                            groupId = themeDisplay.getScopeGroupId();
143    
144                            feedURL =
145                                    PortalUtil.getLayoutFullURL(themeDisplay) +
146                                            Portal.FRIENDLY_URL_SEPARATOR + "blogs/rss";
147    
148                            entryURL = feedURL;
149    
150                            rss = BlogsEntryServiceUtil.getGroupEntriesRSS(
151                                    groupId, status, max, type, version, displayStyle, feedURL,
152                                    entryURL, themeDisplay);
153                    }
154    
155                    return rss.getBytes(StringPool.UTF8);
156            }
157    
158            protected byte[] getRSS(ResourceRequest resourceRequest) throws Exception {
159                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
160                            resourceRequest);
161    
162                    return getRSS(request);
163            }
164    
165            @Override
166            protected boolean isCheckMethodOnProcessAction() {
167                    return _CHECK_METHOD_ON_PROCESS_ACTION;
168            }
169    
170            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
171    
172    }