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                  actionResponse,
100                 "Comments have been disabled for this blog entry.");
101 
102             return;
103         }
104 
105         if (Validator.isNull(url)) {
106             sendError(
107                 actionResponse, "Trackback requires a valid permanent URL.");
108 
109             return;
110         }
111 
112         HttpServletRequest request = PortalUtil.getHttpServletRequest(
113             actionRequest);
114 
115         String remoteIp = request.getRemoteAddr();
116 
117         String trackbackIp = HttpUtil.getIpAddress(url);
118 
119         if (!remoteIp.equals(trackbackIp)) {
120             sendError(
121                 actionResponse,
122                 "Remote IP " + remoteIp + " does not match trackback URL's IP "
123                     + trackbackIp);
124 
125             return;
126         }
127 
128         try {
129             ActionUtil.getEntry(actionRequest);
130         }
131         catch (PrincipalException pe) {
132             sendError(
133                 actionResponse,
134                 "Entry must have guest view permissions to enable trackbacks");
135 
136             return;
137         }
138 
139         BlogsEntry entry = (BlogsEntry)actionRequest.getAttribute(
140             WebKeys.BLOGS_ENTRY);
141 
142         if (!entry.isAllowTrackbacks()) {
143             sendError(
144                 actionResponse,
145                 "Trackbacks are not enabled on this blog entry.");
146 
147             return;
148         }
149 
150         long userId = UserLocalServiceUtil.getDefaultUserId(
151             themeDisplay.getCompanyId());
152         String className = BlogsEntry.class.getName();
153         long classPK = entry.getEntryId();
154 
155         ServiceContext serviceContext = ServiceContextFactory.getInstance(
156             MBMessage.class.getName(), actionRequest);
157 
158         MBMessageDisplay messageDisplay =
159             MBMessageLocalServiceUtil.getDiscussionMessageDisplay(
160                 userId, className, classPK, StatusConstants.APPROVED);
161 
162         MBThread thread = messageDisplay.getThread();
163 
164         long threadId = thread.getThreadId();
165         long parentMessageId = thread.getRootMessageId();
166         String body =
167             "[...] " + excerpt + " [...] [url=" + url + "]" +
168                 themeDisplay.translate("read-more") + "[/url]";
169 
170         MBMessage message = MBMessageLocalServiceUtil.addDiscussionMessage(
171             userId, blogName, className, classPK, threadId, parentMessageId,
172             title, body, serviceContext);
173 
174         String entryURL =
175             PortalUtil.getLayoutFullURL(themeDisplay) +
176                 Portal.FRIENDLY_URL_SEPARATOR + "blogs/" +
177                     entry.getUrlTitle();
178 
179         LinkbackConsumerUtil.addNewTrackback(
180             message.getMessageId(), url, entryURL);
181 
182         sendSuccess(actionResponse);
183     }
184 
185     protected boolean isCheckMethodOnProcessAction() {
186         return _CHECK_METHOD_ON_PROCESS_ACTION;
187     }
188 
189     protected boolean isCommentsEnabled(ActionRequest actionRequest)
190         throws Exception {
191 
192         PortletPreferences preferences = actionRequest.getPreferences();
193 
194         String portletResource = ParamUtil.getString(
195             actionRequest, "portletResource");
196 
197         if (Validator.isNotNull(portletResource)) {
198             preferences = PortletPreferencesFactoryUtil.getPortletSetup(
199                 actionRequest, portletResource);
200         }
201 
202         return GetterUtil.getBoolean(
203             preferences.getValue("enable-comments", null), true);
204     }
205 
206     protected void sendError(ActionResponse actionResponse, String msg)
207         throws Exception {
208 
209         sendResponse(actionResponse, msg, false);
210     }
211 
212     protected void sendResponse(
213             ActionResponse actionResponse, String msg, boolean success)
214         throws Exception {
215 
216         StringBundler sb = new StringBundler(7);
217 
218         sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
219         sb.append("<response>");
220 
221         if (success) {
222             sb.append("<error>0</error>");
223         }
224         else {
225             sb.append("<error>1</error>");
226             sb.append("<message>");
227             sb.append(msg);
228             sb.append("</message>");
229         }
230 
231         sb.append("</response>");
232 
233         HttpServletResponse response = PortalUtil.getHttpServletResponse(
234             actionResponse);
235 
236         ServletResponseUtil.sendFile(
237             response, null, sb.toString().getBytes(StringPool.UTF8),
238             ContentTypes.TEXT_XML_UTF8);
239     }
240 
241     protected void sendSuccess(ActionResponse actionResponse) throws Exception {
242         sendResponse(actionResponse, null, true);
243     }
244 
245     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
246 
247     private static Log _log = LogFactoryUtil.getLog(TrackbackAction.class);
248 
249 }