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.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            protected String getPostUrl() {
080                    Map<String, String> vars = new HashMap<String, String>();
081    
082                    vars.put("liferay:social-bookmark:title", HttpUtil.encodeURL(_title));
083                    vars.put("liferay:social-bookmark:url", _url);
084    
085                    String postUrl = PropsUtil.get(
086                            PropsKeys.SOCIAL_BOOKMARK_POST_URL, new Filter(_type, vars));
087    
088                    return postUrl;
089            }
090    
091            @Override
092            protected void setAttributes(HttpServletRequest request) {
093                    String jspPath = _jspPaths.get(_type);
094    
095                    if (jspPath == null) {
096                            jspPath = PropsUtil.get(
097                                    PropsKeys.SOCIAL_BOOKMARK_JSP, new Filter(_type));
098    
099                            _jspPaths.put(_type, jspPath);
100                    }
101    
102                    _jspPath = jspPath;
103    
104                    if (Validator.isNull(_jspPath)) {
105                            request.setAttribute(
106                                    "liferay-ui:social-bookmark:postUrl", getPostUrl());
107                    }
108    
109                    request.setAttribute("liferay-ui:social-bookmark:target", _target);
110                    request.setAttribute("liferay-ui:social-bookmark:title", _title);
111                    request.setAttribute("liferay-ui:social-bookmark:type", _type);
112                    request.setAttribute("liferay-ui:social-bookmark:url", _url);
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    }