1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.taglib.ui;
16  
17  import com.liferay.portal.kernel.configuration.Filter;
18  import com.liferay.portal.kernel.servlet.StringServletResponse;
19  import com.liferay.portal.kernel.util.ArrayUtil;
20  import com.liferay.portal.kernel.util.HttpUtil;
21  import com.liferay.portal.kernel.util.PropsKeys;
22  import com.liferay.portal.kernel.util.PropsUtil;
23  import com.liferay.taglib.util.IncludeTag;
24  
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  import javax.servlet.RequestDispatcher;
29  import javax.servlet.ServletContext;
30  import javax.servlet.http.HttpServletRequest;
31  import javax.servlet.http.HttpServletResponse;
32  import javax.servlet.jsp.JspException;
33  
34  /**
35   * <a href="SocialBookmarkTag.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author David Truong
38   * @author Jorge Ferrer
39   * @author Brian Wing Shun Chan
40   */
41  public class SocialBookmarkTag extends IncludeTag {
42  
43      public static void doTag(
44              String type, String url, String title, String target,
45              ServletContext servletContext, HttpServletRequest request,
46              HttpServletResponse response)
47          throws Exception {
48  
49          doTag(
50              _PAGE, type, url, title, target, servletContext, request, response);
51      }
52  
53      public static void doTag(
54              String page, String type, String url, String title, String target,
55              ServletContext servletContext, HttpServletRequest request,
56              HttpServletResponse response)
57          throws Exception {
58  
59          request.setAttribute("liferay-ui:social-bookmark:type", type);
60          request.setAttribute("liferay-ui:social-bookmark:url", url);
61          request.setAttribute("liferay-ui:social-bookmark:title", title);
62          request.setAttribute("liferay-ui:social-bookmark:target", target);
63  
64          String[] socialTypes = PropsUtil.getArray(
65              PropsKeys.SOCIAL_BOOKMARK_TYPES);
66  
67          if (!ArrayUtil.contains(socialTypes, type)) {
68              return;
69          }
70  
71          String postUrl = _getPostUrl(type, url, title);
72  
73          request.setAttribute("liferay-ui:social-bookmark:postUrl", postUrl);
74  
75          RequestDispatcher requestDispatcher =
76              servletContext.getRequestDispatcher(page);
77  
78          requestDispatcher.include(request, response);
79      }
80  
81      public int doEndTag() throws JspException {
82          try {
83              ServletContext servletContext = getServletContext();
84              HttpServletRequest request = getServletRequest();
85              StringServletResponse stringResponse = getServletResponse();
86  
87              doTag(
88                  getPage(), _type, _url, _title, _target, servletContext,
89                  request, stringResponse);
90  
91              pageContext.getOut().print(stringResponse.getString());
92  
93              return EVAL_PAGE;
94          }
95          catch (Exception e) {
96              throw new JspException(e);
97          }
98      }
99  
100     public void setType(String type) {
101         _type = type;
102     }
103 
104     public void setUrl(String url) {
105         _url = url;
106     }
107 
108     public void setTitle(String title) {
109         _title = title;
110     }
111 
112     public void setTarget(String target) {
113         _target = target;
114     }
115 
116     protected String getDefaultPage() {
117         return _PAGE;
118     }
119 
120     private static String _getPostUrl(String type, String url, String title) {
121         Map<String, String> vars = new HashMap<String, String>();
122 
123         vars.put("liferay:social-bookmark:url", url);
124         vars.put("liferay:social-bookmark:title", HttpUtil.encodeURL(title));
125 
126         String postUrl = PropsUtil.get(
127             PropsKeys.SOCIAL_BOOKMARK_POST_URL, new Filter(type, vars));
128 
129         return postUrl;
130     }
131 
132     private static final String _PAGE =
133         "/html/taglib/ui/social_bookmark/page.jsp";
134 
135     private String _type;
136     private String _url;
137     private String _title;
138     private String _target;
139 
140 }