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