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 mapping, ActionForm form, PortletConfig portletConfig,
044                            ActionRequest actionRequest, ActionResponse actionResponse)
045                    throws Exception {
046    
047                    if (!PortalUtil.isRSSFeedsEnabled()) {
048                            PortalUtil.sendRSSFeedsDisabledError(actionRequest, actionResponse);
049    
050                            return;
051                    }
052    
053                    try {
054                            HttpServletRequest request = PortalUtil.getHttpServletRequest(
055                                    actionRequest);
056                            HttpServletResponse response = PortalUtil.getHttpServletResponse(
057                                    actionResponse);
058    
059                            ServletResponseUtil.sendFile(
060                                    request, response, null, getRSS(request),
061                                    ContentTypes.TEXT_XML_UTF8);
062    
063                            setForward(actionRequest, ActionConstants.COMMON_NULL);
064                    }
065                    catch (Exception e) {
066                            PortalUtil.sendError(e, actionRequest, actionResponse);
067                    }
068            }
069    
070            @Override
071            public void serveResource(
072                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
073                            ResourceRequest resourceRequest, ResourceResponse resourceResponse)
074                    throws Exception {
075    
076                    if (!PortalUtil.isRSSFeedsEnabled()) {
077                            PortalUtil.sendRSSFeedsDisabledError(
078                                    resourceRequest, resourceResponse);
079    
080                            return;
081                    }
082    
083                    resourceResponse.setContentType(ContentTypes.TEXT_XML_UTF8);
084    
085                    OutputStream outputStream = resourceResponse.getPortletOutputStream();
086    
087                    try {
088                            byte[] bytes = getRSS(resourceRequest, resourceResponse);
089    
090                            outputStream.write(bytes);
091                    }
092                    finally {
093                            outputStream.close();
094                    }
095            }
096    
097            @Override
098            public ActionForward strutsExecute(
099                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
100                            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            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
143    
144    }