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.PortalUtil;
39  import com.liferay.portal.util.WebKeys;
40  import com.liferay.portlet.PortletPreferencesFactoryUtil;
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 (Exception e) {
76              sendError(actionResponse, "An unknown error has occurred.");
77  
78              _log.error(e);
79          }
80  
81          setForward(actionRequest, ActionConstants.COMMON_NULL);
82      }
83  
84      protected void addTrackback(
85              ActionRequest actionRequest, ActionResponse actionResponse)
86          throws Exception {
87  
88          ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
89              WebKeys.THEME_DISPLAY);
90  
91          String title = ParamUtil.getString(actionRequest, "title");
92          String excerpt = ParamUtil.getString(actionRequest, "excerpt");
93          String url = ParamUtil.getString(actionRequest, "url");
94          String blogName = ParamUtil.getString(actionRequest, "blog_name");
95  
96          if (!isCommentsEnabled(actionRequest)) {
97              sendError(
98                  actionResponse,
99                  "Comments have been disabled for this blog entry.");
100 
101             return;
102         }
103 
104         if (Validator.isNull(url)) {
105             sendError(
106                 actionResponse, "Trackback requires a valid permanent URL.");
107 
108             return;
109         }
110 
111         ActionUtil.getEntry(actionRequest);
112 
113         BlogsEntry entry = (BlogsEntry)actionRequest.getAttribute(
114             WebKeys.BLOGS_ENTRY);
115 
116         if (!entry.isAllowTrackbacks()) {
117             sendError(
118                 actionResponse,
119                 "Trackbacks are not enabled on this blog entry.");
120 
121             return;
122         }
123 
124         long userId = UserLocalServiceUtil.getDefaultUserId(
125             themeDisplay.getCompanyId());
126         String className = BlogsEntry.class.getName();
127         long classPK = entry.getEntryId();
128 
129         ServiceContext serviceContext = ServiceContextFactory.getInstance(
130             MBMessage.class.getName(), actionRequest);
131 
132         MBMessageDisplay messageDisplay =
133             MBMessageLocalServiceUtil.getDiscussionMessageDisplay(
134                 userId, className, classPK);
135 
136         MBThread thread = messageDisplay.getThread();
137 
138         long threadId = thread.getThreadId();
139         long parentMessageId = thread.getRootMessageId();
140         String body =
141             "[...] " + excerpt + " [...] [url=" + url + "]" +
142                 themeDisplay.translate("read-more") + "[/url]";
143 
144         MBMessage message = MBMessageLocalServiceUtil.addDiscussionMessage(
145             userId, blogName, className, classPK, threadId, parentMessageId,
146             title, body, serviceContext);
147 
148         String entryURL =
149             themeDisplay.getPortalURL() +
150                 PortalUtil.getLayoutURL(themeDisplay) + "/-/blogs/" +
151                     entry.getUrlTitle();
152 
153         TrackbackVerifierUtil.addNewPost(
154             message.getMessageId(), url, entryURL);
155 
156         sendSuccess(actionResponse);
157     }
158 
159     protected boolean isCommentsEnabled(ActionRequest actionRequest)
160         throws Exception {
161 
162         PortletPreferences preferences = actionRequest.getPreferences();
163 
164         String portletResource = ParamUtil.getString(
165             actionRequest, "portletResource");
166 
167         if (Validator.isNotNull(portletResource)) {
168             preferences = PortletPreferencesFactoryUtil.getPortletSetup(
169                 actionRequest, portletResource);
170         }
171 
172         return GetterUtil.getBoolean(
173             preferences.getValue("enable-comments", null), true);
174     }
175 
176     protected void sendError(ActionResponse actionResponse, String msg)
177         throws Exception {
178 
179         sendResponse(actionResponse, msg, false);
180     }
181 
182     protected void sendResponse(
183             ActionResponse actionResponse, String msg, boolean success)
184         throws Exception {
185 
186         StringBuilder sb = new StringBuilder();
187 
188         sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
189         sb.append("<response>");
190 
191         if (success) {
192             sb.append("<error>0</error>");
193         }
194         else {
195             sb.append("<error>1</error>");
196             sb.append("<message>" + msg + "</message>");
197         }
198 
199         sb.append("</response>");
200 
201         HttpServletResponse response = PortalUtil.getHttpServletResponse(
202             actionResponse);
203 
204         ServletResponseUtil.sendFile(
205             response, null, sb.toString().getBytes(StringPool.UTF8),
206             ContentTypes.TEXT_XML_UTF8);
207     }
208 
209     protected void sendSuccess(ActionResponse actionResponse) throws Exception {
210         sendResponse(actionResponse, null, true);
211     }
212 
213     private static Log _log = LogFactoryUtil.getLog(TrackbackAction.class);
214 
215 }