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.portal.struts;
016    
017    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
018    import com.liferay.portal.kernel.util.ContentTypes;
019    import com.liferay.portal.util.PortalUtil;
020    
021    import java.io.OutputStream;
022    
023    import javax.portlet.ActionRequest;
024    import javax.portlet.ActionResponse;
025    import javax.portlet.PortletConfig;
026    import javax.portlet.PortletRequest;
027    import javax.portlet.ResourceRequest;
028    import javax.portlet.ResourceResponse;
029    
030    import javax.servlet.http.HttpServletRequest;
031    import javax.servlet.http.HttpServletResponse;
032    
033    import org.apache.struts.action.ActionForm;
034    import org.apache.struts.action.ActionForward;
035    import org.apache.struts.action.ActionMapping;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     */
040    public class RSSAction extends PortletAction {
041    
042            @Override
043            public void processAction(
044                            ActionMapping actionMapping, ActionForm actionForm,
045                            PortletConfig portletConfig, ActionRequest actionRequest,
046                            ActionResponse actionResponse)
047                    throws Exception {
048    
049                    if (!isRSSFeedsEnabled(actionRequest)) {
050                            PortalUtil.sendRSSFeedsDisabledError(actionRequest, actionResponse);
051    
052                            return;
053                    }
054    
055                    try {
056                            HttpServletRequest request = PortalUtil.getHttpServletRequest(
057                                    actionRequest);
058                            HttpServletResponse response = PortalUtil.getHttpServletResponse(
059                                    actionResponse);
060    
061                            ServletResponseUtil.sendFile(
062                                    request, response, null, getRSS(request),
063                                    ContentTypes.TEXT_XML_UTF8);
064    
065                            setForward(actionRequest, ActionConstants.COMMON_NULL);
066                    }
067                    catch (Exception e) {
068                            PortalUtil.sendError(e, actionRequest, actionResponse);
069                    }
070            }
071    
072            @Override
073            public void serveResource(
074                            ActionMapping actionMapping, ActionForm actionForm,
075                            PortletConfig portletConfig, ResourceRequest resourceRequest,
076                            ResourceResponse resourceResponse)
077                    throws Exception {
078    
079                    if (!isRSSFeedsEnabled(resourceRequest)) {
080                            PortalUtil.sendRSSFeedsDisabledError(
081                                    resourceRequest, resourceResponse);
082    
083                            return;
084                    }
085    
086                    resourceResponse.setContentType(ContentTypes.TEXT_XML_UTF8);
087    
088                    OutputStream outputStream = resourceResponse.getPortletOutputStream();
089    
090                    try {
091                            byte[] bytes = getRSS(resourceRequest, resourceResponse);
092    
093                            outputStream.write(bytes);
094                    }
095                    finally {
096                            outputStream.close();
097                    }
098            }
099    
100            @Override
101            public ActionForward strutsExecute(
102                            ActionMapping actionMapping, ActionForm actionForm,
103                            HttpServletRequest request, HttpServletResponse response)
104                    throws Exception {
105    
106                    if (!isRSSFeedsEnabled(request)) {
107                            PortalUtil.sendRSSFeedsDisabledError(request, response);
108    
109                            return null;
110                    }
111    
112                    try {
113                            ServletResponseUtil.sendFile(
114                                    request, response, null, getRSS(request),
115                                    ContentTypes.TEXT_XML_UTF8);
116    
117                            return null;
118                    }
119                    catch (Exception e) {
120                            PortalUtil.sendError(e, request, response);
121    
122                            return null;
123                    }
124            }
125    
126            protected byte[] getRSS(HttpServletRequest request) throws Exception {
127                    throw new UnsupportedOperationException();
128            }
129    
130            protected byte[] getRSS(
131                            ResourceRequest resourceRequest, ResourceResponse resourceResponse)
132                    throws Exception {
133    
134                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
135                            resourceRequest);
136    
137                    return getRSS(request);
138            }
139    
140            @Override
141            protected boolean isCheckMethodOnProcessAction() {
142                    return _CHECK_METHOD_ON_PROCESS_ACTION;
143            }
144    
145            protected boolean isRSSFeedsEnabled(HttpServletRequest request)
146                    throws Exception {
147    
148                    return PortalUtil.isRSSFeedsEnabled();
149            }
150    
151            protected boolean isRSSFeedsEnabled(PortletRequest portletRequest)
152                    throws Exception {
153    
154                    return PortalUtil.isRSSFeedsEnabled();
155            }
156    
157            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
158    
159    }