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.portlet.blogs.action;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.ContentTypes;
20  import com.liferay.portal.kernel.util.GetterUtil;
21  import com.liferay.portal.kernel.util.HttpUtil;
22  import com.liferay.portal.kernel.util.ParamUtil;
23  import com.liferay.portal.kernel.util.StringBundler;
24  import com.liferay.portal.kernel.util.StringPool;
25  import com.liferay.portal.kernel.util.Validator;
26  import com.liferay.portal.kernel.workflow.StatusConstants;
27  import com.liferay.portal.security.auth.PrincipalException;
28  import com.liferay.portal.service.ServiceContext;
29  import com.liferay.portal.service.ServiceContextFactory;
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.Portal;
35  import com.liferay.portal.util.PortalUtil;
36  import com.liferay.portal.util.WebKeys;
37  import com.liferay.portlet.PortletPreferencesFactoryUtil;
38  import com.liferay.portlet.blogs.NoSuchEntryException;
39  import com.liferay.portlet.blogs.model.BlogsEntry;
40  import com.liferay.portlet.blogs.util.LinkbackConsumerUtil;
41  import com.liferay.portlet.messageboards.model.MBMessage;
42  import com.liferay.portlet.messageboards.model.MBMessageDisplay;
43  import com.liferay.portlet.messageboards.model.MBThread;
44  import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
45  import com.liferay.util.servlet.ServletResponseUtil;
46  
47  import javax.portlet.ActionRequest;
48  import javax.portlet.ActionResponse;
49  import javax.portlet.PortletConfig;
50  import javax.portlet.PortletPreferences;
51  
52  import javax.servlet.http.HttpServletRequest;
53  import javax.servlet.http.HttpServletResponse;
54  
55  import org.apache.struts.action.ActionForm;
56  import org.apache.struts.action.ActionMapping;
57  
58  /**
59   * <a href="TrackbackAction.java.html"><b><i>View Source</i></b></a>
60   *
61   * @author Alexander Chow
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 (NoSuchEntryException nsee) {
74              if (_log.isWarnEnabled()) {
75                  _log.warn(nsee, nsee);
76              }
77          }
78          catch (Exception e) {
79              _log.error(e, e);
80          }
81  
82          setForward(actionRequest, ActionConstants.COMMON_NULL);
83      }
84  
85      protected void addTrackback(
86              ActionRequest actionRequest, ActionResponse actionResponse)
87          throws Exception {
88  
89          ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
90              WebKeys.THEME_DISPLAY);
91  
92          String title = ParamUtil.getString(actionRequest, "title");
93          String excerpt = ParamUtil.getString(actionRequest, "excerpt");
94          String url = ParamUtil.getString(actionRequest, "url");
95          String blogName = ParamUtil.getString(actionRequest, "blog_name");
96  
97          if (!isCommentsEnabled(actionRequest)) {
98              sendError(
99                  actionRequest, actionResponse,
100                 "Comments have been disabled for this blog entry.");
101 
102             return;
103         }
104 
105         if (Validator.isNull(url)) {
106             sendError(
107                 actionRequest, actionResponse,
108                 "Trackback requires a valid permanent URL.");
109 
110             return;
111         }
112 
113         HttpServletRequest request = PortalUtil.getHttpServletRequest(
114             actionRequest);
115 
116         String remoteIp = request.getRemoteAddr();
117 
118         String trackbackIp = HttpUtil.getIpAddress(url);
119 
120         if (!remoteIp.equals(trackbackIp)) {
121             sendError(
122                 actionRequest, actionResponse,
123                 "Remote IP " + remoteIp +
124                     " does not match trackback URL's IP " + trackbackIp + ".");
125 
126             return;
127         }
128 
129         try {
130             ActionUtil.getEntry(actionRequest);
131         }
132         catch (PrincipalException pe) {
133             sendError(
134                 actionRequest, actionResponse,
135                 "Blog entry must have guest view permissions to enable " +
136                     "trackbacks.");
137 
138             return;
139         }
140 
141         BlogsEntry entry = (BlogsEntry)actionRequest.getAttribute(
142             WebKeys.BLOGS_ENTRY);
143 
144         if (!entry.isAllowTrackbacks()) {
145             sendError(
146                 actionRequest, actionResponse,
147                 "Trackbacks are not enabled on this blog entry.");
148 
149             return;
150         }
151 
152         long userId = UserLocalServiceUtil.getDefaultUserId(
153             themeDisplay.getCompanyId());
154         String className = BlogsEntry.class.getName();
155         long classPK = entry.getEntryId();
156 
157         ServiceContext serviceContext = ServiceContextFactory.getInstance(
158             MBMessage.class.getName(), actionRequest);
159 
160         MBMessageDisplay messageDisplay =
161             MBMessageLocalServiceUtil.getDiscussionMessageDisplay(
162                 userId, className, classPK, StatusConstants.APPROVED);
163 
164         MBThread thread = messageDisplay.getThread();
165 
166         long threadId = thread.getThreadId();
167         long parentMessageId = thread.getRootMessageId();
168         String body =
169             "[...] " + excerpt + " [...] [url=" + url + "]" +
170                 themeDisplay.translate("read-more") + "[/url]";
171 
172         MBMessage message = MBMessageLocalServiceUtil.addDiscussionMessage(
173             userId, blogName, className, classPK, threadId, parentMessageId,
174             title, body, serviceContext);
175 
176         String entryURL =
177             PortalUtil.getLayoutFullURL(themeDisplay) +
178                 Portal.FRIENDLY_URL_SEPARATOR + "blogs/" +
179                     entry.getUrlTitle();
180 
181         LinkbackConsumerUtil.addNewTrackback(
182             message.getMessageId(), url, entryURL);
183 
184         sendSuccess(actionRequest, actionResponse);
185     }
186 
187     protected boolean isCheckMethodOnProcessAction() {
188         return _CHECK_METHOD_ON_PROCESS_ACTION;
189     }
190 
191     protected boolean isCommentsEnabled(ActionRequest actionRequest)
192         throws Exception {
193 
194         PortletPreferences preferences = actionRequest.getPreferences();
195 
196         String portletResource = ParamUtil.getString(
197             actionRequest, "portletResource");
198 
199         if (Validator.isNotNull(portletResource)) {
200             preferences = PortletPreferencesFactoryUtil.getPortletSetup(
201                 actionRequest, portletResource);
202         }
203 
204         return GetterUtil.getBoolean(
205             preferences.getValue("enable-comments", null), true);
206     }
207 
208     protected void sendError(
209             ActionRequest actionRequest, ActionResponse actionResponse,
210             String msg)
211         throws Exception {
212 
213         sendResponse(actionRequest, actionResponse, msg, false);
214     }
215 
216     protected void sendResponse(
217             ActionRequest actionRequest, ActionResponse actionResponse,
218             String msg, boolean success)
219         throws Exception {
220 
221         StringBundler sb = new StringBundler(7);
222 
223         sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
224         sb.append("<response>");
225 
226         if (success) {
227             sb.append("<error>0</error>");
228         }
229         else {
230             sb.append("<error>1</error>");
231             sb.append("<message>");
232             sb.append(msg);
233             sb.append("</message>");
234         }
235 
236         sb.append("</response>");
237 
238         HttpServletRequest request = PortalUtil.getHttpServletRequest(
239             actionRequest);
240         HttpServletResponse response = PortalUtil.getHttpServletResponse(
241             actionResponse);
242 
243         ServletResponseUtil.sendFile(
244             request, response, null, sb.toString().getBytes(StringPool.UTF8),
245             ContentTypes.TEXT_XML_UTF8);
246     }
247 
248     protected void sendSuccess(
249             ActionRequest actionRequest, ActionResponse actionResponse)
250         throws Exception {
251 
252         sendResponse(actionRequest, actionResponse, null, true);
253     }
254 
255     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
256 
257     private static Log _log = LogFactoryUtil.getLog(TrackbackAction.class);
258 
259 }