001    /**
002     * Copyright (c) 2000-present 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                    try (OutputStream outputStream =
089                                    resourceResponse.getPortletOutputStream()) {
090    
091                            byte[] bytes = getRSS(resourceRequest, resourceResponse);
092    
093                            outputStream.write(bytes);
094                    }
095            }
096    
097            @Override
098            public ActionForward strutsExecute(
099                            ActionMapping actionMapping, ActionForm actionForm,
100                            HttpServletRequest request, HttpServletResponse response)
101                    throws Exception {
102    
103                    if (!PortalUtil.isRSSFeedsEnabled()) {
104                            PortalUtil.sendRSSFeedsDisabledError(request, response);
105    
106                            return null;
107                    }
108    
109                    try {
110                            ServletResponseUtil.sendFile(
111                                    request, response, null, getRSS(request),
112                                    ContentTypes.TEXT_XML_UTF8);
113    
114                            return null;
115                    }
116                    catch (Exception e) {
117                            PortalUtil.sendError(e, request, response);
118    
119                            return null;
120                    }
121            }
122    
123            protected byte[] getRSS(HttpServletRequest request) throws Exception {
124                    throw new UnsupportedOperationException();
125            }
126    
127            protected byte[] getRSS(
128                            ResourceRequest resourceRequest, ResourceResponse resourceResponse)
129                    throws Exception {
130    
131                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
132                            resourceRequest);
133    
134                    return getRSS(request);
135            }
136    
137            @Override
138            protected boolean isCheckMethodOnProcessAction() {
139                    return _CHECK_METHOD_ON_PROCESS_ACTION;
140            }
141    
142            protected boolean isRSSFeedsEnabled(PortletRequest portletRequest)
143                    throws Exception {
144    
145                    return PortalUtil.isRSSFeedsEnabled();
146            }
147    
148            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
149    
150    }