1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.blogs.action;
24  
25  import com.liferay.portal.kernel.util.ContentTypes;
26  import com.liferay.portal.kernel.util.GetterUtil;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.service.UserLocalServiceUtil;
31  import com.liferay.portal.struts.ActionConstants;
32  import com.liferay.portal.struts.PortletAction;
33  import com.liferay.portal.theme.ThemeDisplay;
34  import com.liferay.portal.util.PortalUtil;
35  import com.liferay.portal.util.WebKeys;
36  import com.liferay.portlet.PortletPreferencesFactoryUtil;
37  import com.liferay.portlet.blogs.model.BlogsEntry;
38  import com.liferay.portlet.blogs.util.TrackbackVerifierUtil;
39  import com.liferay.portlet.messageboards.model.MBMessage;
40  import com.liferay.portlet.messageboards.model.MBMessageDisplay;
41  import com.liferay.portlet.messageboards.model.MBThread;
42  import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
43  import com.liferay.util.servlet.ServletResponseUtil;
44  
45  import javax.portlet.ActionRequest;
46  import javax.portlet.ActionResponse;
47  import javax.portlet.PortletConfig;
48  import javax.portlet.PortletPreferences;
49  
50  import javax.servlet.http.HttpServletResponse;
51  
52  import org.apache.commons.logging.Log;
53  import org.apache.commons.logging.LogFactory;
54  import org.apache.struts.action.ActionForm;
55  import org.apache.struts.action.ActionMapping;
56  
57  /**
58   * <a href="TrackbackAction.java.html"><b><i>View Source</i></b></a>
59   *
60   * @author Alexander Chow
61   *
62   */
63  public class TrackbackAction extends PortletAction {
64  
65      public void processAction(
66              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
67              ActionRequest actionRequest, ActionResponse actionResponse)
68          throws Exception {
69  
70          try {
71              addTrackback(actionRequest, actionResponse);
72          }
73          catch (Exception e) {
74              sendError(actionResponse, "An unknown error has occurred.");
75  
76              _log.error(e);
77          }
78  
79          setForward(actionRequest, ActionConstants.COMMON_NULL);
80      }
81  
82      protected void addTrackback(
83              ActionRequest actionRequest, ActionResponse actionResponse)
84          throws Exception {
85  
86          ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
87              WebKeys.THEME_DISPLAY);
88  
89          String title = ParamUtil.getString(actionRequest, "title");
90          String excerpt = ParamUtil.getString(actionRequest, "excerpt");
91          String url = ParamUtil.getString(actionRequest, "url");
92          String blogName = ParamUtil.getString(actionRequest, "blog_name");
93  
94          if (!isCommentsEnabled(actionRequest)) {
95              sendError(
96                  actionResponse,
97                  "Comments have been disabled for this blog entry.");
98  
99              return;
100         }
101 
102         if (Validator.isNull(url)) {
103             sendError(
104                 actionResponse, "Trackback requires a valid permanent URL.");
105 
106             return;
107         }
108 
109         ActionUtil.getEntry(actionRequest);
110 
111         BlogsEntry entry = (BlogsEntry)actionRequest.getAttribute(
112             WebKeys.BLOGS_ENTRY);
113 
114         if (!entry.isAllowTrackbacks()) {
115             sendError(
116                 actionResponse,
117                 "Trackbacks are not enabled on this blog entry.");
118 
119             return;
120         }
121 
122         long userId = UserLocalServiceUtil.getDefaultUserId(
123             themeDisplay.getCompanyId());
124         long groupId = themeDisplay.getPortletGroupId();
125         String className = BlogsEntry.class.getName();
126         long classPK = entry.getEntryId();
127 
128         MBMessageDisplay messageDisplay =
129             MBMessageLocalServiceUtil.getDiscussionMessageDisplay(
130                 userId, className, classPK);
131 
132         MBThread thread = messageDisplay.getThread();
133 
134         long threadId = thread.getThreadId();
135         long parentMessageId = thread.getRootMessageId();
136         String body =
137             "[...] " + excerpt + " [...] [url=" + url + "]" +
138                 themeDisplay.translate("read-more") + "[/url]";
139 
140         MBMessage message = MBMessageLocalServiceUtil.addDiscussionMessage(
141             userId, blogName, groupId, className, classPK, threadId,
142             parentMessageId, title, body, themeDisplay);
143 
144         String trackbackUrl =
145             themeDisplay.getPortalURL() +
146                 PortalUtil.getLayoutURL(themeDisplay) + "/-/blogs/trackback/" +
147                     entry.getUrlTitle();
148 
149         TrackbackVerifierUtil.addNewPost(
150             message.getMessageId(), url, trackbackUrl);
151 
152         sendSuccess(actionResponse);
153     }
154 
155     protected boolean isCommentsEnabled(ActionRequest actionRequest)
156         throws Exception {
157 
158         PortletPreferences prefs = actionRequest.getPreferences();
159 
160         String portletResource = ParamUtil.getString(
161             actionRequest, "portletResource");
162 
163         if (Validator.isNotNull(portletResource)) {
164             prefs = PortletPreferencesFactoryUtil.getPortletSetup(
165                 actionRequest, portletResource);
166         }
167 
168         return GetterUtil.getBoolean(
169             prefs.getValue("enable-comments", null), true);
170     }
171 
172     protected void sendError(ActionResponse actionResponse, String msg)
173         throws Exception {
174 
175         sendResponse(actionResponse, msg, false);
176     }
177 
178     protected void sendResponse(
179             ActionResponse actionResponse, String msg, boolean success)
180         throws Exception {
181 
182         StringBuilder sb = new StringBuilder();
183 
184         sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
185         sb.append("<response>");
186 
187         if (success) {
188             sb.append("<error>0</error>");
189         }
190         else {
191             sb.append("<error>1</error>");
192             sb.append("<message>" + msg + "</message>");
193         }
194 
195         sb.append("</response>");
196 
197         HttpServletResponse response = PortalUtil.getHttpServletResponse(
198             actionResponse);
199 
200         ServletResponseUtil.sendFile(
201             response, null, sb.toString().getBytes(StringPool.UTF8),
202             ContentTypes.TEXT_XML_UTF8);
203     }
204 
205     protected void sendSuccess(ActionResponse actionResponse) throws Exception {
206         sendResponse(actionResponse, null, true);
207     }
208 
209     private static Log _log = LogFactory.getLog(TrackbackAction.class);
210 
211 }