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.kernel.portlet.bridges.mvc;
016    
017    import com.liferay.portal.kernel.portlet.PortletResponseUtil;
018    import com.liferay.portal.kernel.util.ContentTypes;
019    import com.liferay.portal.util.PortalUtil;
020    
021    import javax.portlet.PortletException;
022    import javax.portlet.ResourceRequest;
023    import javax.portlet.ResourceResponse;
024    
025    /**
026     * @author Eduardo Garcia
027     */
028    public abstract class BaseRSSMVCResourceCommand implements MVCResourceCommand {
029    
030            @Override
031            public boolean serveResource(
032                            ResourceRequest resourceRequest, ResourceResponse resourceResponse)
033                    throws PortletException {
034    
035                    if (!isRSSFeedsEnabled(resourceRequest)) {
036                            try {
037                                    PortalUtil.sendRSSFeedsDisabledError(
038                                            resourceRequest, resourceResponse);
039                            }
040                            catch (Exception e) {
041                            }
042    
043                            return false;
044                    }
045    
046                    try {
047                            PortletResponseUtil.sendFile(
048                                    resourceRequest, resourceResponse, null,
049                                    getRSS(resourceRequest, resourceResponse),
050                                    ContentTypes.TEXT_XML_UTF8);
051                    }
052                    catch (Exception e) {
053                            throw new PortletException(e);
054                    }
055    
056                    return true;
057            }
058    
059            protected abstract byte[] getRSS(
060                            ResourceRequest resourceRequest, ResourceResponse resourceResponse)
061                    throws Exception;
062    
063            protected boolean isRSSFeedsEnabled(ResourceRequest resourceRequest) {
064                    return PortalUtil.isRSSFeedsEnabled();
065            }
066    
067    }