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.ServiceContext;
33  import com.liferay.portal.service.ServiceContextFactory;
34  import com.liferay.portal.service.UserLocalServiceUtil;
35  import com.liferay.portal.struts.ActionConstants;
36  import com.liferay.portal.struts.PortletAction;
37  import com.liferay.portal.theme.ThemeDisplay;
38  import com.liferay.portal.util.Portal;
39  import com.liferay.portal.util.PortalUtil;
40  import com.liferay.portal.util.WebKeys;
41  import com.liferay.portlet.PortletPreferencesFactoryUtil;
42  import com.liferay.portlet.blogs.model.BlogsEntry;
43  import com.liferay.portlet.blogs.util.TrackbackVerifierUtil;
44  import com.liferay.portlet.messageboards.model.MBMessage;
45  import com.liferay.portlet.messageboards.model.MBMessageDisplay;
46  import com.liferay.portlet.messageboards.model.MBThread;
47  import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
48  import com.liferay.util.servlet.ServletResponseUtil;
49  
50  import javax.portlet.ActionRequest;
51  import javax.portlet.ActionResponse;
52  import javax.portlet.PortletConfig;
53  import javax.portlet.PortletPreferences;
54  
55  import javax.servlet.http.HttpServletResponse;
56  
57  import org.apache.struts.action.ActionForm;
58  import org.apache.struts.action.ActionMapping;
59  
60  /**
61   * <a href="TrackbackAction.java.html"><b><i>View Source</i></b></a>
62   *
63   * @author Alexander Chow
64   *
65   */
66  public class TrackbackAction extends PortletAction {
67  
68      public void processAction(
69              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
70              ActionRequest actionRequest, ActionResponse actionResponse)
71          throws Exception {
72  
73          try {
74              addTrackback(actionRequest, actionResponse);
75          }
76          catch (Exception e) {
77              sendError(actionResponse, "An unknown error has occurred.");
78  
79              _log.error(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         ActionUtil.getEntry(actionRequest);
113 
114         BlogsEntry entry = (BlogsEntry)actionRequest.getAttribute(
115             WebKeys.BLOGS_ENTRY);
116 
117         if (!entry.isAllowTrackbacks()) {
118             sendError(
119                 actionResponse,
120                 "Trackbacks are not enabled on this blog entry.");
121 
122             return;
123         }
124 
125         long userId = UserLocalServiceUtil.getDefaultUserId(
126             themeDisplay.getCompanyId());
127         String className = BlogsEntry.class.getName();
128         long classPK = entry.getEntryId();
129 
130         ServiceContext serviceContext = ServiceContextFactory.getInstance(
131             MBMessage.class.getName(), actionRequest);
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, className, classPK, threadId, parentMessageId,
147             title, body, serviceContext);
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 preferences = actionRequest.getPreferences();
169 
170         String portletResource = ParamUtil.getString(
171             actionRequest, "portletResource");
172 
173         if (Validator.isNotNull(portletResource)) {
174             preferences = PortletPreferencesFactoryUtil.getPortletSetup(
175                 actionRequest, portletResource);
176         }
177 
178         return GetterUtil.getBoolean(
179             preferences.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 }