001    /**
002     * Copyright (c) 2000-2013 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.servlet.ServletResponseUtil;
020    import com.liferay.portal.kernel.util.ContentTypes;
021    import com.liferay.portal.kernel.util.GetterUtil;
022    import com.liferay.portal.kernel.util.HttpUtil;
023    import com.liferay.portal.kernel.util.ParamUtil;
024    import com.liferay.portal.kernel.util.StringBundler;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.util.Validator;
027    import com.liferay.portal.kernel.workflow.WorkflowConstants;
028    import com.liferay.portal.security.auth.PrincipalException;
029    import com.liferay.portal.service.ServiceContext;
030    import com.liferay.portal.service.ServiceContextFactory;
031    import com.liferay.portal.service.UserLocalServiceUtil;
032    import com.liferay.portal.struts.ActionConstants;
033    import com.liferay.portal.struts.PortletAction;
034    import com.liferay.portal.theme.ThemeDisplay;
035    import com.liferay.portal.util.Portal;
036    import com.liferay.portal.util.PortalUtil;
037    import com.liferay.portal.util.WebKeys;
038    import com.liferay.portlet.PortletPreferencesFactoryUtil;
039    import com.liferay.portlet.blogs.NoSuchEntryException;
040    import com.liferay.portlet.blogs.model.BlogsEntry;
041    import com.liferay.portlet.blogs.util.LinkbackConsumerUtil;
042    import com.liferay.portlet.messageboards.model.MBMessage;
043    import com.liferay.portlet.messageboards.model.MBMessageDisplay;
044    import com.liferay.portlet.messageboards.model.MBThread;
045    import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
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            @Override
064            public void processAction(
065                            ActionMapping actionMapping, ActionForm actionForm,
066                            PortletConfig portletConfig, ActionRequest actionRequest,
067                            ActionResponse actionResponse)
068                    throws Exception {
069    
070                    try {
071                            addTrackback(actionRequest, actionResponse);
072                    }
073                    catch (NoSuchEntryException nsee) {
074                            if (_log.isWarnEnabled()) {
075                                    _log.warn(nsee, nsee);
076                            }
077                    }
078                    catch (Exception e) {
079                            _log.error(e, e);
080                    }
081    
082                    setForward(actionRequest, ActionConstants.COMMON_NULL);
083            }
084    
085            protected void addTrackback(
086                            ActionRequest actionRequest, ActionResponse actionResponse)
087                    throws Exception {
088    
089                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
090                            WebKeys.THEME_DISPLAY);
091    
092                    String title = ParamUtil.getString(actionRequest, "title");
093                    String excerpt = ParamUtil.getString(actionRequest, "excerpt");
094                    String url = ParamUtil.getString(actionRequest, "url");
095                    String blogName = ParamUtil.getString(actionRequest, "blog_name");
096    
097                    if (!isCommentsEnabled(actionRequest)) {
098                            sendError(
099                                    actionRequest, actionResponse,
100                                    "Comments have been disabled for this blog entry.");
101    
102                            return;
103                    }
104    
105                    if (Validator.isNull(url)) {
106                            sendError(
107                                    actionRequest, actionResponse,
108                                    "Trackback requires a valid permanent URL.");
109    
110                            return;
111                    }
112    
113                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
114                            actionRequest);
115    
116                    String remoteIp = request.getRemoteAddr();
117    
118                    String trackbackIp = HttpUtil.getIpAddress(url);
119    
120                    if (!remoteIp.equals(trackbackIp)) {
121                            sendError(
122                                    actionRequest, actionResponse,
123                                    "Remote IP " + remoteIp +
124                                            " does not match trackback URL's IP " + trackbackIp + ".");
125    
126                            return;
127                    }
128    
129                    try {
130                            ActionUtil.getEntry(actionRequest);
131                    }
132                    catch (PrincipalException pe) {
133                            sendError(
134                                    actionRequest, actionResponse,
135                                    "Blog entry must have guest view permissions to enable " +
136                                            "trackbacks.");
137    
138                            return;
139                    }
140    
141                    BlogsEntry entry = (BlogsEntry)actionRequest.getAttribute(
142                            WebKeys.BLOGS_ENTRY);
143    
144                    if (!entry.isAllowTrackbacks()) {
145                            sendError(
146                                    actionRequest, actionResponse,
147                                    "Trackbacks are not enabled on this blog entry.");
148    
149                            return;
150                    }
151    
152                    long userId = UserLocalServiceUtil.getDefaultUserId(
153                            themeDisplay.getCompanyId());
154                    long groupId = entry.getGroupId();
155                    String className = BlogsEntry.class.getName();
156                    long classPK = entry.getEntryId();
157    
158                    MBMessageDisplay messageDisplay =
159                            MBMessageLocalServiceUtil.getDiscussionMessageDisplay(
160                                    userId, groupId, className, classPK,
161                                    WorkflowConstants.STATUS_APPROVED);
162    
163                    MBThread thread = messageDisplay.getThread();
164    
165                    long threadId = thread.getThreadId();
166                    long parentMessageId = thread.getRootMessageId();
167                    String body =
168                            "[...] " + excerpt + " [...] [url=" + url + "]" +
169                                    themeDisplay.translate("read-more") + "[/url]";
170    
171                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
172                            MBMessage.class.getName(), actionRequest);
173    
174                    MBMessage message = MBMessageLocalServiceUtil.addDiscussionMessage(
175                            userId, blogName, groupId, className, classPK, threadId,
176                            parentMessageId, title, body, serviceContext);
177    
178                    String entryURL =
179                            PortalUtil.getLayoutFullURL(themeDisplay) +
180                                    Portal.FRIENDLY_URL_SEPARATOR + "blogs/" + entry.getUrlTitle();
181    
182                    LinkbackConsumerUtil.addNewTrackback(
183                            message.getMessageId(), url, entryURL);
184    
185                    sendSuccess(actionRequest, actionResponse);
186            }
187    
188            @Override
189            protected boolean isCheckMethodOnProcessAction() {
190                    return _CHECK_METHOD_ON_PROCESS_ACTION;
191            }
192    
193            protected boolean isCommentsEnabled(ActionRequest actionRequest)
194                    throws Exception {
195    
196                    PortletPreferences preferences = actionRequest.getPreferences();
197    
198                    String portletResource = ParamUtil.getString(
199                            actionRequest, "portletResource");
200    
201                    if (Validator.isNotNull(portletResource)) {
202                            preferences = PortletPreferencesFactoryUtil.getPortletSetup(
203                                    actionRequest, portletResource);
204                    }
205    
206                    return GetterUtil.getBoolean(
207                            preferences.getValue("enableComments", null), true);
208            }
209    
210            protected void sendError(
211                            ActionRequest actionRequest, ActionResponse actionResponse,
212                            String msg)
213                    throws Exception {
214    
215                    sendResponse(actionRequest, actionResponse, msg, false);
216            }
217    
218            protected void sendResponse(
219                            ActionRequest actionRequest, ActionResponse actionResponse,
220                            String msg, boolean success)
221                    throws Exception {
222    
223                    StringBundler sb = new StringBundler(7);
224    
225                    sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
226                    sb.append("<response>");
227    
228                    if (success) {
229                            sb.append("<error>0</error>");
230                    }
231                    else {
232                            sb.append("<error>1</error>");
233                            sb.append("<message>");
234                            sb.append(msg);
235                            sb.append("</message>");
236                    }
237    
238                    sb.append("</response>");
239    
240                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
241                            actionRequest);
242                    HttpServletResponse response = PortalUtil.getHttpServletResponse(
243                            actionResponse);
244    
245                    ServletResponseUtil.sendFile(
246                            request, response, null, sb.toString().getBytes(StringPool.UTF8),
247                            ContentTypes.TEXT_XML_UTF8);
248            }
249    
250            protected void sendSuccess(
251                            ActionRequest actionRequest, ActionResponse actionResponse)
252                    throws Exception {
253    
254                    sendResponse(actionRequest, actionResponse, null, true);
255            }
256    
257            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
258    
259            private static Log _log = LogFactoryUtil.getLog(TrackbackAction.class);
260    
261    }