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                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
093                            actionRequest);
094    
095                    HttpServletRequest originalRequest =
096                            PortalUtil.getOriginalServletRequest(request);
097    
098                    String title = ParamUtil.getString(originalRequest, "title");
099                    String excerpt = ParamUtil.getString(originalRequest, "excerpt");
100                    String url = ParamUtil.getString(originalRequest, "url");
101                    String blogName = ParamUtil.getString(originalRequest, "blog_name");
102    
103                    if (!isCommentsEnabled(actionRequest)) {
104                            sendError(
105                                    actionRequest, actionResponse,
106                                    "Comments have been disabled for this blog entry.");
107    
108                            return;
109                    }
110    
111                    if (Validator.isNull(url)) {
112                            sendError(
113                                    actionRequest, actionResponse,
114                                    "Trackback requires a valid permanent URL.");
115    
116                            return;
117                    }
118    
119                    String remoteIp = request.getRemoteAddr();
120    
121                    String trackbackIp = HttpUtil.getIpAddress(url);
122    
123                    if (!remoteIp.equals(trackbackIp)) {
124                            sendError(
125                                    actionRequest, actionResponse,
126                                    "Remote IP " + remoteIp +
127                                            " does not match trackback URL's IP " + trackbackIp + ".");
128    
129                            return;
130                    }
131    
132                    try {
133                            ActionUtil.getEntry(actionRequest);
134                    }
135                    catch (PrincipalException pe) {
136                            sendError(
137                                    actionRequest, actionResponse,
138                                    "Blog entry must have guest view permissions to enable " +
139                                            "trackbacks.");
140    
141                            return;
142                    }
143    
144                    BlogsEntry entry = (BlogsEntry)actionRequest.getAttribute(
145                            WebKeys.BLOGS_ENTRY);
146    
147                    if (!entry.isAllowTrackbacks()) {
148                            sendError(
149                                    actionRequest, actionResponse,
150                                    "Trackbacks are not enabled on this blog entry.");
151    
152                            return;
153                    }
154    
155                    long userId = UserLocalServiceUtil.getDefaultUserId(
156                            themeDisplay.getCompanyId());
157                    long groupId = entry.getGroupId();
158                    String className = BlogsEntry.class.getName();
159                    long classPK = entry.getEntryId();
160    
161                    MBMessageDisplay messageDisplay =
162                            MBMessageLocalServiceUtil.getDiscussionMessageDisplay(
163                                    userId, groupId, className, classPK,
164                                    WorkflowConstants.STATUS_APPROVED);
165    
166                    MBThread thread = messageDisplay.getThread();
167    
168                    long threadId = thread.getThreadId();
169                    long parentMessageId = thread.getRootMessageId();
170                    String body =
171                            "[...] " + excerpt + " [...] [url=" + url + "]" +
172                                    themeDisplay.translate("read-more") + "[/url]";
173    
174                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
175                            MBMessage.class.getName(), actionRequest);
176    
177                    MBMessage message = MBMessageLocalServiceUtil.addDiscussionMessage(
178                            userId, blogName, groupId, className, classPK, threadId,
179                            parentMessageId, title, body, serviceContext);
180    
181                    String entryURL =
182                            PortalUtil.getLayoutFullURL(themeDisplay) +
183                                    Portal.FRIENDLY_URL_SEPARATOR + "blogs/" + entry.getUrlTitle();
184    
185                    LinkbackConsumerUtil.addNewTrackback(
186                            message.getMessageId(), url, entryURL);
187    
188                    sendSuccess(actionRequest, actionResponse);
189            }
190    
191            @Override
192            protected boolean isCheckMethodOnProcessAction() {
193                    return _CHECK_METHOD_ON_PROCESS_ACTION;
194            }
195    
196            protected boolean isCommentsEnabled(ActionRequest actionRequest)
197                    throws Exception {
198    
199                    PortletPreferences portletPreferences = actionRequest.getPreferences();
200    
201                    String portletResource = ParamUtil.getString(
202                            actionRequest, "portletResource");
203    
204                    if (Validator.isNotNull(portletResource)) {
205                            portletPreferences = PortletPreferencesFactoryUtil.getPortletSetup(
206                                    actionRequest, portletResource);
207                    }
208    
209                    return GetterUtil.getBoolean(
210                            portletPreferences.getValue("enableComments", null), true);
211            }
212    
213            protected void sendError(
214                            ActionRequest actionRequest, ActionResponse actionResponse,
215                            String msg)
216                    throws Exception {
217    
218                    sendResponse(actionRequest, actionResponse, msg, false);
219            }
220    
221            protected void sendResponse(
222                            ActionRequest actionRequest, ActionResponse actionResponse,
223                            String msg, boolean success)
224                    throws Exception {
225    
226                    StringBundler sb = new StringBundler(7);
227    
228                    sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
229                    sb.append("<response>");
230    
231                    if (success) {
232                            sb.append("<error>0</error>");
233                    }
234                    else {
235                            sb.append("<error>1</error>");
236                            sb.append("<message>");
237                            sb.append(msg);
238                            sb.append("</message>");
239                    }
240    
241                    sb.append("</response>");
242    
243                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
244                            actionRequest);
245                    HttpServletResponse response = PortalUtil.getHttpServletResponse(
246                            actionResponse);
247    
248                    ServletResponseUtil.sendFile(
249                            request, response, null, sb.toString().getBytes(StringPool.UTF8),
250                            ContentTypes.TEXT_XML_UTF8);
251            }
252    
253            protected void sendSuccess(
254                            ActionRequest actionRequest, ActionResponse actionResponse)
255                    throws Exception {
256    
257                    sendResponse(actionRequest, actionResponse, null, true);
258            }
259    
260            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
261    
262            private static Log _log = LogFactoryUtil.getLog(TrackbackAction.class);
263    
264    }