1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.blogs.action;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.util.ContentTypes;
25  import com.liferay.portal.kernel.util.GetterUtil;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.service.UserLocalServiceUtil;
30  import com.liferay.portal.struts.ActionConstants;
31  import com.liferay.portal.struts.PortletAction;
32  import com.liferay.portal.theme.ThemeDisplay;
33  import com.liferay.portal.util.PortalUtil;
34  import com.liferay.portal.util.WebKeys;
35  import com.liferay.portlet.PortletPreferencesFactoryUtil;
36  import com.liferay.portlet.blogs.model.BlogsEntry;
37  import com.liferay.portlet.blogs.util.TrackbackVerifierUtil;
38  import com.liferay.portlet.messageboards.model.MBMessage;
39  import com.liferay.portlet.messageboards.model.MBMessageDisplay;
40  import com.liferay.portlet.messageboards.model.MBThread;
41  import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
42  import com.liferay.util.servlet.ServletResponseUtil;
43  
44  import javax.portlet.ActionRequest;
45  import javax.portlet.ActionResponse;
46  import javax.portlet.PortletConfig;
47  import javax.portlet.PortletPreferences;
48  
49  import javax.servlet.http.HttpServletResponse;
50  
51  import org.apache.struts.action.ActionForm;
52  import org.apache.struts.action.ActionMapping;
53  
54  /**
55   * <a href="TrackbackAction.java.html"><b><i>View Source</i></b></a>
56   *
57   * @author Alexander Chow
58   *
59   */
60  public class TrackbackAction extends PortletAction {
61  
62      public void processAction(
63              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
64              ActionRequest actionRequest, ActionResponse actionResponse)
65          throws Exception {
66  
67          try {
68              addTrackback(actionRequest, actionResponse);
69          }
70          catch (Exception e) {
71              sendError(actionResponse, "An unknown error has occurred.");
72  
73              _log.error(e);
74          }
75  
76          setForward(actionRequest, ActionConstants.COMMON_NULL);
77      }
78  
79      protected void addTrackback(
80              ActionRequest actionRequest, ActionResponse actionResponse)
81          throws Exception {
82  
83          ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
84              WebKeys.THEME_DISPLAY);
85  
86          String title = ParamUtil.getString(actionRequest, "title");
87          String excerpt = ParamUtil.getString(actionRequest, "excerpt");
88          String url = ParamUtil.getString(actionRequest, "url");
89          String blogName = ParamUtil.getString(actionRequest, "blog_name");
90  
91          if (!isCommentsEnabled(actionRequest)) {
92              sendError(
93                  actionResponse,
94                  "Comments have been disabled for this blog entry.");
95  
96              return;
97          }
98  
99          if (Validator.isNull(url)) {
100             sendError(
101                 actionResponse, "Trackback requires a valid permanent URL.");
102 
103             return;
104         }
105 
106         ActionUtil.getEntry(actionRequest);
107 
108         BlogsEntry entry = (BlogsEntry)actionRequest.getAttribute(
109             WebKeys.BLOGS_ENTRY);
110 
111         if (!entry.isAllowTrackbacks()) {
112             sendError(
113                 actionResponse,
114                 "Trackbacks are not enabled on this blog entry.");
115 
116             return;
117         }
118 
119         long userId = UserLocalServiceUtil.getDefaultUserId(
120             themeDisplay.getCompanyId());
121         long groupId = themeDisplay.getScopeGroupId();
122         String className = BlogsEntry.class.getName();
123         long classPK = entry.getEntryId();
124 
125         MBMessageDisplay messageDisplay =
126             MBMessageLocalServiceUtil.getDiscussionMessageDisplay(
127                 userId, className, classPK);
128 
129         MBThread thread = messageDisplay.getThread();
130 
131         long threadId = thread.getThreadId();
132         long parentMessageId = thread.getRootMessageId();
133         String body =
134             "[...] " + excerpt + " [...] [url=" + url + "]" +
135                 themeDisplay.translate("read-more") + "[/url]";
136 
137         MBMessage message = MBMessageLocalServiceUtil.addDiscussionMessage(
138             userId, blogName, groupId, className, classPK, threadId,
139             parentMessageId, title, body, themeDisplay);
140 
141         String entryURL =
142             themeDisplay.getPortalURL() +
143                 PortalUtil.getLayoutURL(themeDisplay) + "/-/blogs/" +
144                     entry.getUrlTitle();
145 
146         TrackbackVerifierUtil.addNewPost(
147             message.getMessageId(), url, entryURL);
148 
149         sendSuccess(actionResponse);
150     }
151 
152     protected boolean isCommentsEnabled(ActionRequest actionRequest)
153         throws Exception {
154 
155         PortletPreferences prefs = actionRequest.getPreferences();
156 
157         String portletResource = ParamUtil.getString(
158             actionRequest, "portletResource");
159 
160         if (Validator.isNotNull(portletResource)) {
161             prefs = PortletPreferencesFactoryUtil.getPortletSetup(
162                 actionRequest, portletResource);
163         }
164 
165         return GetterUtil.getBoolean(
166             prefs.getValue("enable-comments", null), true);
167     }
168 
169     protected void sendError(ActionResponse actionResponse, String msg)
170         throws Exception {
171 
172         sendResponse(actionResponse, msg, false);
173     }
174 
175     protected void sendResponse(
176             ActionResponse actionResponse, String msg, boolean success)
177         throws Exception {
178 
179         StringBuilder sb = new StringBuilder();
180 
181         sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
182         sb.append("<response>");
183 
184         if (success) {
185             sb.append("<error>0</error>");
186         }
187         else {
188             sb.append("<error>1</error>");
189             sb.append("<message>" + msg + "</message>");
190         }
191 
192         sb.append("</response>");
193 
194         HttpServletResponse response = PortalUtil.getHttpServletResponse(
195             actionResponse);
196 
197         ServletResponseUtil.sendFile(
198             response, null, sb.toString().getBytes(StringPool.UTF8),
199             ContentTypes.TEXT_XML_UTF8);
200     }
201 
202     protected void sendSuccess(ActionResponse actionResponse) throws Exception {
203         sendResponse(actionResponse, null, true);
204     }
205 
206     private static Log _log = LogFactoryUtil.getLog(TrackbackAction.class);
207 
208 }