001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.taglib.ui;
016    
017    import com.liferay.portal.kernel.configuration.Filter;
018    import com.liferay.portal.kernel.util.ArrayUtil;
019    import com.liferay.portal.kernel.util.HttpUtil;
020    import com.liferay.portal.kernel.util.PropsKeys;
021    import com.liferay.portal.kernel.util.PropsUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.taglib.util.IncludeTag;
024    
025    import java.util.HashMap;
026    import java.util.Map;
027    
028    import javax.servlet.http.HttpServletRequest;
029    
030    /**
031     * @author David Truong
032     * @author Jorge Ferrer
033     * @author Brian Wing Shun Chan
034     */
035    public class SocialBookmarkTag extends IncludeTag {
036    
037            public void setTarget(String target) {
038                    _target = target;
039            }
040    
041            public void setTitle(String title) {
042                    _title = title;
043            }
044    
045            public void setType(String type) {
046                    _type = type;
047            }
048    
049            public void setUrl(String url) {
050                    _url = url;
051            }
052    
053            @Override
054            protected void cleanUp() {
055                    _target = null;
056                    _title = null;
057                    _type = null;
058                    _url = null;
059            }
060    
061            @Override
062            protected String getPage() {
063                    String[] socialTypes = PropsUtil.getArray(
064                            PropsKeys.SOCIAL_BOOKMARK_TYPES);
065    
066                    if (ArrayUtil.contains(socialTypes, _type)) {
067                            if (Validator.isNotNull(_jspPath)) {
068                                    return _jspPath;
069                            }
070                            else {
071                                    return _PAGE;
072                            }
073                    }
074                    else {
075                            return null;
076                    }
077            }
078    
079            @Override
080            protected void setAttributes(HttpServletRequest request) {
081                    String jspPath = _jspPaths.get(_type);
082    
083                    if (jspPath == null) {
084                            jspPath = PropsUtil.get(
085                                    PropsKeys.SOCIAL_BOOKMARK_JSP, new Filter(_type));
086    
087                            _jspPaths.put(_type, jspPath);
088                    }
089    
090                    _jspPath = jspPath;
091    
092                    if (Validator.isNull(_jspPath)) {
093                            request.setAttribute(
094                                    "liferay-ui:social-bookmark:postUrl", getPostUrl());
095                    }
096    
097                    request.setAttribute("liferay-ui:social-bookmark:target", _target);
098                    request.setAttribute("liferay-ui:social-bookmark:title", _title);
099                    request.setAttribute("liferay-ui:social-bookmark:type", _type);
100                    request.setAttribute("liferay-ui:social-bookmark:url", _url);
101            }
102    
103            private String getPostUrl() {
104                    Map<String, String> vars = new HashMap<String, String>();
105    
106                    vars.put("liferay:social-bookmark:title", HttpUtil.encodeURL(_title));
107                    vars.put("liferay:social-bookmark:url", _url);
108    
109                    String postUrl = PropsUtil.get(
110                            PropsKeys.SOCIAL_BOOKMARK_POST_URL, new Filter(_type, vars));
111    
112                    return postUrl;
113            }
114    
115            private static final String _PAGE =
116                    "/html/taglib/ui/social_bookmark/page.jsp";
117    
118            private static Map<String, String> _jspPaths =
119                    new HashMap<String, String>();
120    
121            private String _jspPath;
122            private String _target;
123            private String _title;
124            private String _type;
125            private String _url;
126    
127    }