1   /**
2    * Copyright (c) 2000-2009 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.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.ContentTypes;
28  import com.liferay.portal.kernel.util.GetterUtil;
29  import com.liferay.portal.kernel.util.ParamUtil;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.service.UserLocalServiceUtil;
33  import com.liferay.portal.struts.ActionConstants;
34  import com.liferay.portal.struts.PortletAction;
35  import com.liferay.portal.theme.ThemeDisplay;
36  import com.liferay.portal.util.Portal;
37  import com.liferay.portal.util.PortalUtil;
38  import com.liferay.portal.util.WebKeys;
39  import com.liferay.portlet.PortletPreferencesFactoryUtil;
40  import com.liferay.portlet.blogs.NoSuchEntryException;
41  import com.liferay.portlet.blogs.model.BlogsEntry;
42  import com.liferay.portlet.blogs.util.TrackbackVerifierUtil;
43  import com.liferay.portlet.messageboards.model.MBMessage;
44  import com.liferay.portlet.messageboards.model.MBMessageDisplay;
45  import com.liferay.portlet.messageboards.model.MBThread;
46  import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
47  import com.liferay.util.servlet.ServletResponseUtil;
48  
49  import javax.portlet.ActionRequest;
50  import javax.portlet.ActionResponse;
51  import javax.portlet.PortletConfig;
52  import javax.portlet.PortletPreferences;
53  
54  import javax.servlet.http.HttpServletResponse;
55  
56  import org.apache.struts.action.ActionForm;
57  import org.apache.struts.action.ActionMapping;
58  
59  /**
60   * <a href="TrackbackAction.java.html"><b><i>View Source</i></b></a>
61   *
62   * @author Alexander Chow
63   *
64   */
65  public class TrackbackAction extends PortletAction {
66  
67      public void processAction(
68              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
69              ActionRequest actionRequest, ActionResponse actionResponse)
70          throws Exception {
71  
72          try {
73              addTrackback(actionRequest, actionResponse);
74          }
75          catch (NoSuchEntryException nsee) {
76              if (_log.isWarnEnabled()) {
77                  _log.warn(nsee, nsee);
78              }
79          }
80          catch (Exception e) {
81              _log.error(e, e);
82          }
83  
84          setForward(actionRequest, ActionConstants.COMMON_NULL);
85      }
86  
87      protected void addTrackback(
88              ActionRequest actionRequest, ActionResponse actionResponse)
89          throws Exception {
90  
91          ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
92              WebKeys.THEME_DISPLAY);
93  
94          String title = ParamUtil.getString(actionRequest, "title");
95          String excerpt = ParamUtil.getString(actionRequest, "excerpt");
96          String url = ParamUtil.getString(actionRequest, "url");
97          String blogName = ParamUtil.getString(actionRequest, "blog_name");
98  
99          if (!isCommentsEnabled(actionRequest)) {
100             sendError(
101                 actionResponse,
102                 "Comments have been disabled for this blog entry.");
103 
104             return;
105         }
106 
107         if (Validator.isNull(url)) {
108             sendError(
109                 actionResponse, "Trackback requires a valid permanent URL.");
110 
111             return;
112         }
113 
114         ActionUtil.getEntry(actionRequest);
115 
116         BlogsEntry entry = (BlogsEntry)actionRequest.getAttribute(
117             WebKeys.BLOGS_ENTRY);
118 
119         if (!entry.isAllowTrackbacks()) {
120             sendError(
121                 actionResponse,
122                 "Trackbacks are not enabled on this blog entry.");
123 
124             return;
125         }
126 
127         long userId = UserLocalServiceUtil.getDefaultUserId(
128             themeDisplay.getCompanyId());
129         long groupId = themeDisplay.getScopeGroupId();
130         String className = BlogsEntry.class.getName();
131         long classPK = entry.getEntryId();
132 
133         MBMessageDisplay messageDisplay =
134             MBMessageLocalServiceUtil.getDiscussionMessageDisplay(
135                 userId, className, classPK);
136 
137         MBThread thread = messageDisplay.getThread();
138 
139         long threadId = thread.getThreadId();
140         long parentMessageId = thread.getRootMessageId();
141         String body =
142             "[...] " + excerpt + " [...] [url=" + url + "]" +
143                 themeDisplay.translate("read-more") + "[/url]";
144 
145         MBMessage message = MBMessageLocalServiceUtil.addDiscussionMessage(
146             userId, blogName, groupId, className, classPK, threadId,
147             parentMessageId, title, body, themeDisplay);
148 
149         String entryURL =
150             themeDisplay.getPortalURL() +
151                 PortalUtil.getLayoutURL(themeDisplay) +
152                     Portal.FRIENDLY_URL_SEPARATOR + "blogs/" +
153                         entry.getUrlTitle();
154 
155         TrackbackVerifierUtil.addNewPost(
156             message.getMessageId(), url, entryURL);
157 
158         sendSuccess(actionResponse);
159     }
160 
161     protected boolean isCheckMethodOnProcessAction() {
162         return _CHECK_METHOD_ON_PROCESS_ACTION;
163     }
164 
165     protected boolean isCommentsEnabled(ActionRequest actionRequest)
166         throws Exception {
167 
168         PortletPreferences prefs = actionRequest.getPreferences();
169 
170         String portletResource = ParamUtil.getString(
171             actionRequest, "portletResource");
172 
173         if (Validator.isNotNull(portletResource)) {
174             prefs = PortletPreferencesFactoryUtil.getPortletSetup(
175                 actionRequest, portletResource);
176         }
177 
178         return GetterUtil.getBoolean(
179             prefs.getValue("enable-comments", null), true);
180     }
181 
182     protected void sendError(ActionResponse actionResponse, String msg)
183         throws Exception {
184 
185         sendResponse(actionResponse, msg, false);
186     }
187 
188     protected void sendResponse(
189             ActionResponse actionResponse, String msg, boolean success)
190         throws Exception {
191 
192         StringBuilder sb = new StringBuilder();
193 
194         sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
195         sb.append("<response>");
196 
197         if (success) {
198             sb.append("<error>0</error>");
199         }
200         else {
201             sb.append("<error>1</error>");
202             sb.append("<message>" + msg + "</message>");
203         }
204 
205         sb.append("</response>");
206 
207         HttpServletResponse response = PortalUtil.getHttpServletResponse(
208             actionResponse);
209 
210         ServletResponseUtil.sendFile(
211             response, null, sb.toString().getBytes(StringPool.UTF8),
212             ContentTypes.TEXT_XML_UTF8);
213     }
214 
215     protected void sendSuccess(ActionResponse actionResponse) throws Exception {
216         sendResponse(actionResponse, null, true);
217     }
218 
219     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
220 
221     private static Log _log = LogFactoryUtil.getLog(TrackbackAction.class);
222 
223 }