001    /**
002     * Copyright (c) 2000-2012 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.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 java.util.Date;
035    
036    import javax.portlet.PortletConfig;
037    import javax.portlet.ResourceRequest;
038    import javax.portlet.ResourceResponse;
039    
040    import javax.servlet.http.HttpServletRequest;
041    import javax.servlet.http.HttpServletResponse;
042    
043    import org.apache.struts.action.ActionForm;
044    import org.apache.struts.action.ActionForward;
045    import org.apache.struts.action.ActionMapping;
046    
047    /**
048     * @author Brian Wing Shun Chan
049     */
050    public class RSSAction extends PortletAction {
051    
052            @Override
053            public void serveResource(
054                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
055                            ResourceRequest resourceRequest, ResourceResponse resourceResponse)
056                    throws Exception {
057    
058                    resourceResponse.setContentType(ContentTypes.TEXT_XML_UTF8);
059    
060                    OutputStream outputStream = resourceResponse.getPortletOutputStream();
061    
062                    try {
063                            byte[] bytes = getRSS(resourceRequest);
064    
065                            outputStream.write(bytes);
066                    }
067                    finally {
068                            outputStream.close();
069                    }
070            }
071    
072            @Override
073            public ActionForward strutsExecute(
074                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
075                            HttpServletResponse response)
076                    throws Exception {
077    
078                    try {
079                            ServletResponseUtil.sendFile(
080                                    request, response, null, getRSS(request),
081                                    ContentTypes.TEXT_XML_UTF8);
082    
083                            return null;
084                    }
085                    catch (Exception e) {
086                            PortalUtil.sendError(e, request, response);
087    
088                            return null;
089                    }
090            }
091    
092            protected byte[] getRSS(HttpServletRequest request) throws Exception {
093                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
094                            WebKeys.THEME_DISPLAY);
095    
096                    Layout layout = themeDisplay.getLayout();
097    
098                    long plid = ParamUtil.getLong(request, "p_l_id");
099                    long companyId = ParamUtil.getLong(request, "companyId");
100                    long groupId = ParamUtil.getLong(request, "groupId");
101                    long organizationId = ParamUtil.getLong(request, "organizationId");
102                    int status = WorkflowConstants.STATUS_APPROVED;
103                    int max = ParamUtil.getInteger(
104                            request, "max", SearchContainer.DEFAULT_DELTA);
105                    String type = ParamUtil.getString(
106                            request, "type", RSSUtil.TYPE_DEFAULT);
107                    double version = ParamUtil.getDouble(
108                            request, "version", RSSUtil.VERSION_DEFAULT);
109                    String displayStyle = ParamUtil.getString(
110                            request, "displayStyle", RSSUtil.DISPLAY_STYLE_FULL_CONTENT);
111    
112                    String feedURL =
113                            themeDisplay.getPortalURL() + themeDisplay.getPathMain() +
114                                    "/blogs/find_entry?";
115    
116                    String entryURL = feedURL;
117    
118                    String rss = StringPool.BLANK;
119    
120                    if (companyId > 0) {
121                            feedURL = StringPool.BLANK;
122    
123                            rss = BlogsEntryServiceUtil.getCompanyEntriesRSS(
124                                    companyId, new Date(), status, max, type, version, displayStyle,
125                                    feedURL, entryURL, themeDisplay);
126                    }
127                    else if (groupId > 0) {
128                            feedURL += "p_l_id=" + plid;
129    
130                            entryURL = feedURL;
131    
132                            rss = BlogsEntryServiceUtil.getGroupEntriesRSS(
133                                    groupId, new Date(), status, max, type, version, displayStyle,
134                                    feedURL, entryURL, themeDisplay);
135                    }
136                    else if (organizationId > 0) {
137                            feedURL = StringPool.BLANK;
138    
139                            rss = BlogsEntryServiceUtil.getOrganizationEntriesRSS(
140                                    organizationId, new Date(), status, max, type, version,
141                                    displayStyle, feedURL, entryURL, themeDisplay);
142                    }
143                    else if (layout != null) {
144                            groupId = themeDisplay.getScopeGroupId();
145    
146                            feedURL =
147                                    PortalUtil.getLayoutFullURL(themeDisplay) +
148                                            Portal.FRIENDLY_URL_SEPARATOR + "blogs/rss";
149    
150                            entryURL = feedURL;
151    
152                            rss = BlogsEntryServiceUtil.getGroupEntriesRSS(
153                                    groupId, new Date(), status, max, type, version, displayStyle,
154                                    feedURL, entryURL, themeDisplay);
155                    }
156    
157                    return rss.getBytes(StringPool.UTF8);
158            }
159    
160            protected byte[] getRSS(ResourceRequest resourceRequest) throws Exception {
161                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
162                            resourceRequest);
163    
164                    return getRSS(request);
165            }
166    
167            @Override
168            protected boolean isCheckMethodOnProcessAction() {
169                    return _CHECK_METHOD_ON_PROCESS_ACTION;
170            }
171    
172            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
173    
174    }