001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.blogs.action;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.ContentTypes;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.HttpUtil;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portal.kernel.util.StringBundler;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.kernel.workflow.WorkflowConstants;
027    import com.liferay.portal.security.auth.PrincipalException;
028    import com.liferay.portal.service.ServiceContext;
029    import com.liferay.portal.service.ServiceContextFactory;
030    import com.liferay.portal.service.UserLocalServiceUtil;
031    import com.liferay.portal.struts.ActionConstants;
032    import com.liferay.portal.struts.PortletAction;
033    import com.liferay.portal.theme.ThemeDisplay;
034    import com.liferay.portal.util.Portal;
035    import com.liferay.portal.util.PortalUtil;
036    import com.liferay.portal.util.WebKeys;
037    import com.liferay.portlet.PortletPreferencesFactoryUtil;
038    import com.liferay.portlet.blogs.NoSuchEntryException;
039    import com.liferay.portlet.blogs.model.BlogsEntry;
040    import com.liferay.portlet.blogs.util.LinkbackConsumerUtil;
041    import com.liferay.portlet.messageboards.model.MBMessage;
042    import com.liferay.portlet.messageboards.model.MBMessageDisplay;
043    import com.liferay.portlet.messageboards.model.MBThread;
044    import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
045    import com.liferay.util.servlet.ServletResponseUtil;
046    
047    import javax.portlet.ActionRequest;
048    import javax.portlet.ActionResponse;
049    import javax.portlet.PortletConfig;
050    import javax.portlet.PortletPreferences;
051    
052    import javax.servlet.http.HttpServletRequest;
053    import javax.servlet.http.HttpServletResponse;
054    
055    import org.apache.struts.action.ActionForm;
056    import org.apache.struts.action.ActionMapping;
057    
058    /**
059     * @author Alexander Chow
060     */
061    public class TrackbackAction extends PortletAction {
062    
063            public void processAction(
064                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
065                            ActionRequest actionRequest, ActionResponse actionResponse)
066                    throws Exception {
067    
068                    try {
069                            addTrackback(actionRequest, actionResponse);
070                    }
071                    catch (NoSuchEntryException nsee) {
072                            if (_log.isWarnEnabled()) {
073                                    _log.warn(nsee, nsee);
074                            }
075                    }
076                    catch (Exception e) {
077                            _log.error(e, e);
078                    }
079    
080                    setForward(actionRequest, ActionConstants.COMMON_NULL);
081            }
082    
083            protected void addTrackback(
084                            ActionRequest actionRequest, ActionResponse actionResponse)
085                    throws Exception {
086    
087                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
088                            WebKeys.THEME_DISPLAY);
089    
090                    String title = ParamUtil.getString(actionRequest, "title");
091                    String excerpt = ParamUtil.getString(actionRequest, "excerpt");
092                    String url = ParamUtil.getString(actionRequest, "url");
093                    String blogName = ParamUtil.getString(actionRequest, "blog_name");
094    
095                    if (!isCommentsEnabled(actionRequest)) {
096                            sendError(
097                                    actionRequest, actionResponse,
098                                    "Comments have been disabled for this blog entry.");
099    
100                            return;
101                    }
102    
103                    if (Validator.isNull(url)) {
104                            sendError(
105                                    actionRequest, actionResponse,
106                                    "Trackback requires a valid permanent URL.");
107    
108                            return;
109                    }
110    
111                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
112                            actionRequest);
113    
114                    String remoteIp = request.getRemoteAddr();
115    
116                    String trackbackIp = HttpUtil.getIpAddress(url);
117    
118                    if (!remoteIp.equals(trackbackIp)) {
119                            sendError(
120                                    actionRequest, actionResponse,
121                                    "Remote IP " + remoteIp +
122                                            " does not match trackback URL's IP " + trackbackIp + ".");
123    
124                            return;
125                    }
126    
127                    try {
128                            ActionUtil.getEntry(actionRequest);
129                    }
130                    catch (PrincipalException pe) {
131                            sendError(
132                                    actionRequest, actionResponse,
133                                    "Blog entry must have guest view permissions to enable " +
134                                            "trackbacks.");
135    
136                            return;
137                    }
138    
139                    BlogsEntry entry = (BlogsEntry)actionRequest.getAttribute(
140                            WebKeys.BLOGS_ENTRY);
141    
142                    if (!entry.isAllowTrackbacks()) {
143                            sendError(
144                                    actionRequest, actionResponse,
145                                    "Trackbacks are not enabled on this blog entry.");
146    
147                            return;
148                    }
149    
150                    long userId = UserLocalServiceUtil.getDefaultUserId(
151                            themeDisplay.getCompanyId());
152                    long groupId = entry.getGroupId();
153                    String className = BlogsEntry.class.getName();
154                    long classPK = entry.getEntryId();
155    
156                    MBMessageDisplay messageDisplay =
157                            MBMessageLocalServiceUtil.getDiscussionMessageDisplay(
158                                    userId, groupId, className, classPK,
159                                    WorkflowConstants.STATUS_APPROVED);
160    
161                    MBThread thread = messageDisplay.getThread();
162    
163                    long threadId = thread.getThreadId();
164                    long parentMessageId = thread.getRootMessageId();
165                    String body =
166                            "[...] " + excerpt + " [...] [url=" + url + "]" +
167                                    themeDisplay.translate("read-more") + "[/url]";
168    
169                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
170                            MBMessage.class.getName(), actionRequest);
171    
172                    MBMessage message = MBMessageLocalServiceUtil.addDiscussionMessage(
173                            userId, blogName, groupId, className, classPK, threadId,
174                            parentMessageId, title, body, serviceContext);
175    
176                    String entryURL =
177                            PortalUtil.getLayoutFullURL(themeDisplay) +
178                                    Portal.FRIENDLY_URL_SEPARATOR + "blogs/" +
179                                            entry.getUrlTitle();
180    
181                    LinkbackConsumerUtil.addNewTrackback(
182                            message.getMessageId(), url, entryURL);
183    
184                    sendSuccess(actionRequest, actionResponse);
185            }
186    
187            protected boolean isCheckMethodOnProcessAction() {
188                    return _CHECK_METHOD_ON_PROCESS_ACTION;
189            }
190    
191            protected boolean isCommentsEnabled(ActionRequest actionRequest)
192                    throws Exception {
193    
194                    PortletPreferences preferences = actionRequest.getPreferences();
195    
196                    String portletResource = ParamUtil.getString(
197                            actionRequest, "portletResource");
198    
199                    if (Validator.isNotNull(portletResource)) {
200                            preferences = PortletPreferencesFactoryUtil.getPortletSetup(
201                                    actionRequest, portletResource);
202                    }
203    
204                    return GetterUtil.getBoolean(
205                            preferences.getValue("enable-comments", null), true);
206            }
207    
208            protected void sendError(
209                            ActionRequest actionRequest, ActionResponse actionResponse,
210                            String msg)
211                    throws Exception {
212    
213                    sendResponse(actionRequest, actionResponse, msg, false);
214            }
215    
216            protected void sendResponse(
217                            ActionRequest actionRequest, ActionResponse actionResponse,
218                            String msg, boolean success)
219                    throws Exception {
220    
221                    StringBundler sb = new StringBundler(7);
222    
223                    sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
224                    sb.append("<response>");
225    
226                    if (success) {
227                            sb.append("<error>0</error>");
228                    }
229                    else {
230                            sb.append("<error>1</error>");
231                            sb.append("<message>");
232                            sb.append(msg);
233                            sb.append("</message>");
234                    }
235    
236                    sb.append("</response>");
237    
238                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
239                            actionRequest);
240                    HttpServletResponse response = PortalUtil.getHttpServletResponse(
241                            actionResponse);
242    
243                    ServletResponseUtil.sendFile(
244                            request, response, null, sb.toString().getBytes(StringPool.UTF8),
245                            ContentTypes.TEXT_XML_UTF8);
246            }
247    
248            protected void sendSuccess(
249                            ActionRequest actionRequest, ActionResponse actionResponse)
250                    throws Exception {
251    
252                    sendResponse(actionRequest, actionResponse, null, true);
253            }
254    
255            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
256    
257            private static Log _log = LogFactoryUtil.getLog(TrackbackAction.class);
258    
259    }