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.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.StringPool;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.taglib.util.IncludeTag;
022    import com.liferay.util.RSSUtil;
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                            _resourceURL.setCacheability(ResourceURL.FULL);
091    
092                            if ((_delta > 0) && (_delta != SearchContainer.DEFAULT_DELTA)) {
093                                    _resourceURL.setParameter("max", String.valueOf(_delta));
094                            }
095    
096                            if (Validator.isNotNull(_displayStyle) &&
097                                    !_displayStyle.equals(RSSUtil.DISPLAY_STYLE_DEFAULT)) {
098    
099                                    _resourceURL.setParameter("displayStyle", _displayStyle);
100                            }
101    
102                            if (Validator.isNotNull(_feedType) &&
103                                    !_feedType.equals(RSSUtil.FEED_TYPE_DEFAULT)) {
104    
105                                    _resourceURL.setParameter(
106                                            "type", RSSUtil.getFeedTypeFormat(_feedType));
107                                    _resourceURL.setParameter(
108                                            "version",
109                                            String.valueOf(RSSUtil.getFeedTypeVersion(_feedType)));
110                            }
111    
112                            if (Validator.isNotNull(_name)) {
113                                    _resourceURL.setParameter("feedTitle", _name);
114                            }
115    
116                            return _resourceURL.toString();
117                    }
118                    else if (Validator.isNotNull(_url)) {
119                            if ((_delta > 0) && (_delta != SearchContainer.DEFAULT_DELTA)) {
120                                    _url = HttpUtil.addParameter(_url, "max", _delta);
121                            }
122    
123                            if (Validator.isNotNull(_displayStyle) &&
124                                    !_displayStyle.equals(RSSUtil.DISPLAY_STYLE_DEFAULT)) {
125    
126                                    _url = HttpUtil.addParameter(
127                                            _url, "displayStyle", _displayStyle);
128                            }
129    
130                            if (Validator.isNotNull(_feedType) &&
131                                    !_feedType.equals(RSSUtil.FEED_TYPE_DEFAULT)) {
132    
133                                    _url = HttpUtil.addParameter(
134                                            _url, "type", RSSUtil.getFeedTypeFormat(_feedType));
135                                    _url = HttpUtil.addParameter(
136                                            _url, "version",
137                                            String.valueOf(RSSUtil.getFeedTypeVersion(_feedType)));
138                            }
139    
140                            if (Validator.isNotNull(_name)) {
141                                    _url = HttpUtil.addParameter(_url, "feedTitle", _name);
142                            }
143    
144                            return _url;
145                    }
146    
147                    return StringPool.BLANK;
148            }
149    
150            private static final boolean _CLEAN_UP_SET_ATTRIBUTES = true;
151    
152            private static final String _PAGE = "/html/taglib/ui/rss/page.jsp";
153    
154            private int _delta = SearchContainer.DEFAULT_DELTA;
155            private String _displayStyle = RSSUtil.DISPLAY_STYLE_DEFAULT;
156            private String _feedType = RSSUtil.FEED_TYPE_DEFAULT;
157            private String _message = "rss";
158            private String _name;
159            private ResourceURL _resourceURL;
160            private String _url;
161    
162    }