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