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.taglib.ui;
016    
017    import com.liferay.portal.kernel.dao.search.SearchContainer;
018    import com.liferay.portal.kernel.util.HttpUtil;
019    import com.liferay.portal.kernel.util.RSSUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.taglib.util.IncludeTag;
023    
024    import javax.portlet.ResourceURL;
025    
026    import javax.servlet.http.HttpServletRequest;
027    
028    /**
029     * @author Eduardo Garcia
030     */
031    public class RSSTag extends IncludeTag {
032    
033            public void setDelta(int delta) {
034                    _delta = delta;
035            }
036    
037            public void setDisplayStyle(String displayStyle) {
038                    _displayStyle = displayStyle;
039            }
040    
041            public void setFeedType(String feedType) {
042                    _feedType = feedType;
043            }
044    
045            public void setMessage(String message) {
046                    _message = message;
047            }
048    
049            public void setName(String name) {
050                    _name = name;
051            }
052    
053            public void setResourceURL(ResourceURL resourceURL) {
054                    _resourceURL = resourceURL;
055            }
056    
057            public void setUrl(String url) {
058                    _url = url;
059            }
060    
061            @Override
062            protected void cleanUp() {
063                    _delta = SearchContainer.DEFAULT_DELTA;
064                    _displayStyle = RSSUtil.DISPLAY_STYLE_DEFAULT;
065                    _feedType = RSSUtil.FEED_TYPE_DEFAULT;
066                    _message = "RSS";
067                    _name = null;
068                    _resourceURL = null;
069                    _url = null;
070            }
071    
072            @Override
073            protected String getPage() {
074                    return _PAGE;
075            }
076    
077            @Override
078            protected boolean isCleanUpSetAttributes() {
079                    return _CLEAN_UP_SET_ATTRIBUTES;
080            }
081    
082            @Override
083            protected void setAttributes(HttpServletRequest request) {
084                    request.setAttribute("liferay-ui:rss:message", _message);
085                    request.setAttribute("liferay-ui:rss:url", getURL());
086            }
087    
088            private String getURL() {
089                    if (_resourceURL != null) {
090                            if ((_delta > 0) && (_delta != SearchContainer.DEFAULT_DELTA)) {
091                                    _resourceURL.setParameter("max", String.valueOf(_delta));
092                            }
093    
094                            if (Validator.isNotNull(_displayStyle) &&
095                                    !_displayStyle.equals(RSSUtil.DISPLAY_STYLE_DEFAULT)) {
096    
097                                    _resourceURL.setParameter("displayStyle", _displayStyle);
098                            }
099    
100                            if (Validator.isNotNull(_feedType) &&
101                                    !_feedType.equals(RSSUtil.FEED_TYPE_DEFAULT)) {
102    
103                                    _resourceURL.setParameter(
104                                            "type", RSSUtil.getFeedTypeFormat(_feedType));
105                                    _resourceURL.setParameter(
106                                            "version",
107                                            String.valueOf(RSSUtil.getFeedTypeVersion(_feedType)));
108                            }
109    
110                            if (Validator.isNotNull(_name)) {
111                                    _resourceURL.setParameter("feedTitle", _name);
112                            }
113    
114                            return _resourceURL.toString();
115                    }
116                    else if (Validator.isNotNull(_url)) {
117                            if ((_delta > 0) && (_delta != SearchContainer.DEFAULT_DELTA)) {
118                                    _url = HttpUtil.addParameter(_url, "max", _delta);
119                            }
120    
121                            if (Validator.isNotNull(_displayStyle) &&
122                                    !_displayStyle.equals(RSSUtil.DISPLAY_STYLE_DEFAULT)) {
123    
124                                    _url = HttpUtil.addParameter(
125                                            _url, "displayStyle", _displayStyle);
126                            }
127    
128                            if (Validator.isNotNull(_feedType) &&
129                                    !_feedType.equals(RSSUtil.FEED_TYPE_DEFAULT)) {
130    
131                                    _url = HttpUtil.addParameter(
132                                            _url, "type", RSSUtil.getFeedTypeFormat(_feedType));
133                                    _url = HttpUtil.addParameter(
134                                            _url, "version",
135                                            String.valueOf(RSSUtil.getFeedTypeVersion(_feedType)));
136                            }
137    
138                            if (Validator.isNotNull(_name)) {
139                                    _url = HttpUtil.addParameter(_url, "feedTitle", _name);
140                            }
141    
142                            return _url;
143                    }
144    
145                    return StringPool.BLANK;
146            }
147    
148            private static final boolean _CLEAN_UP_SET_ATTRIBUTES = true;
149    
150            private static final String _PAGE = "/html/taglib/ui/rss/page.jsp";
151    
152            private int _delta = SearchContainer.DEFAULT_DELTA;
153            private String _displayStyle = RSSUtil.DISPLAY_STYLE_DEFAULT;
154            private String _feedType = RSSUtil.FEED_TYPE_DEFAULT;
155            private String _message = "rss";
156            private String _name;
157            private ResourceURL _resourceURL;
158            private String _url;
159    
160    }