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