Java tutorial
/** * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved. * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. */ package com.liferay.portlet.blogs.action; import com.liferay.portal.kernel.log.Log; import com.liferay.portal.kernel.log.LogFactoryUtil; import com.liferay.portal.kernel.servlet.ServletResponseUtil; import com.liferay.portal.kernel.util.ContentTypes; import com.liferay.portal.kernel.util.GetterUtil; import com.liferay.portal.kernel.util.HttpUtil; import com.liferay.portal.kernel.util.ParamUtil; import com.liferay.portal.kernel.util.StringBundler; import com.liferay.portal.kernel.util.StringPool; import com.liferay.portal.kernel.util.Validator; import com.liferay.portal.kernel.workflow.WorkflowConstants; import com.liferay.portal.security.auth.PrincipalException; import com.liferay.portal.service.ServiceContext; import com.liferay.portal.service.ServiceContextFactory; import com.liferay.portal.service.UserLocalServiceUtil; import com.liferay.portal.struts.ActionConstants; import com.liferay.portal.struts.PortletAction; import com.liferay.portal.theme.ThemeDisplay; import com.liferay.portal.util.Portal; import com.liferay.portal.util.PortalUtil; import com.liferay.portal.util.WebKeys; import com.liferay.portlet.PortletPreferencesFactoryUtil; import com.liferay.portlet.blogs.NoSuchEntryException; import com.liferay.portlet.blogs.model.BlogsEntry; import com.liferay.portlet.blogs.util.LinkbackConsumerUtil; import com.liferay.portlet.messageboards.model.MBMessage; import com.liferay.portlet.messageboards.model.MBMessageDisplay; import com.liferay.portlet.messageboards.model.MBThread; import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil; import javax.portlet.ActionRequest; import javax.portlet.ActionResponse; import javax.portlet.PortletConfig; import javax.portlet.PortletPreferences; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; /** * @author Alexander Chow */ public class TrackbackAction extends PortletAction { @Override public void processAction(ActionMapping mapping, ActionForm form, PortletConfig portletConfig, ActionRequest actionRequest, ActionResponse actionResponse) throws Exception { try { addTrackback(actionRequest, actionResponse); } catch (NoSuchEntryException nsee) { if (_log.isWarnEnabled()) { _log.warn(nsee, nsee); } } catch (Exception e) { _log.error(e, e); } setForward(actionRequest, ActionConstants.COMMON_NULL); } protected void addTrackback(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception { ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY); String title = ParamUtil.getString(actionRequest, "title"); String excerpt = ParamUtil.getString(actionRequest, "excerpt"); String url = ParamUtil.getString(actionRequest, "url"); String blogName = ParamUtil.getString(actionRequest, "blog_name"); if (!isCommentsEnabled(actionRequest)) { sendError(actionRequest, actionResponse, "Comments have been disabled for this blog entry."); return; } if (Validator.isNull(url)) { sendError(actionRequest, actionResponse, "Trackback requires a valid permanent URL."); return; } HttpServletRequest request = PortalUtil.getHttpServletRequest(actionRequest); String remoteIp = request.getRemoteAddr(); String trackbackIp = HttpUtil.getIpAddress(url); if (!remoteIp.equals(trackbackIp)) { sendError(actionRequest, actionResponse, "Remote IP " + remoteIp + " does not match trackback URL's IP " + trackbackIp + "."); return; } try { ActionUtil.getEntry(actionRequest); } catch (PrincipalException pe) { sendError(actionRequest, actionResponse, "Blog entry must have guest view permissions to enable " + "trackbacks."); return; } BlogsEntry entry = (BlogsEntry) actionRequest.getAttribute(WebKeys.BLOGS_ENTRY); if (!entry.isAllowTrackbacks()) { sendError(actionRequest, actionResponse, "Trackbacks are not enabled on this blog entry."); return; } long userId = UserLocalServiceUtil.getDefaultUserId(themeDisplay.getCompanyId()); long groupId = entry.getGroupId(); String className = BlogsEntry.class.getName(); long classPK = entry.getEntryId(); MBMessageDisplay messageDisplay = MBMessageLocalServiceUtil.getDiscussionMessageDisplay(userId, groupId, className, classPK, WorkflowConstants.STATUS_APPROVED); MBThread thread = messageDisplay.getThread(); long threadId = thread.getThreadId(); long parentMessageId = thread.getRootMessageId(); String body = "[...] " + excerpt + " [...] [url=" + url + "]" + themeDisplay.translate("read-more") + "[/url]"; ServiceContext serviceContext = ServiceContextFactory.getInstance(MBMessage.class.getName(), actionRequest); MBMessage message = MBMessageLocalServiceUtil.addDiscussionMessage(userId, blogName, groupId, className, classPK, threadId, parentMessageId, title, body, serviceContext); String entryURL = PortalUtil.getLayoutFullURL(themeDisplay) + Portal.FRIENDLY_URL_SEPARATOR + "blogs/" + entry.getUrlTitle(); LinkbackConsumerUtil.addNewTrackback(message.getMessageId(), url, entryURL); sendSuccess(actionRequest, actionResponse); } @Override protected boolean isCheckMethodOnProcessAction() { return _CHECK_METHOD_ON_PROCESS_ACTION; } protected boolean isCommentsEnabled(ActionRequest actionRequest) throws Exception { PortletPreferences preferences = actionRequest.getPreferences(); String portletResource = ParamUtil.getString(actionRequest, "portletResource"); if (Validator.isNotNull(portletResource)) { preferences = PortletPreferencesFactoryUtil.getPortletSetup(actionRequest, portletResource); } return GetterUtil.getBoolean(preferences.getValue("enableComments", null), true); } protected void sendError(ActionRequest actionRequest, ActionResponse actionResponse, String msg) throws Exception { sendResponse(actionRequest, actionResponse, msg, false); } protected void sendResponse(ActionRequest actionRequest, ActionResponse actionResponse, String msg, boolean success) throws Exception { StringBundler sb = new StringBundler(7); sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); sb.append("<response>"); if (success) { sb.append("<error>0</error>"); } else { sb.append("<error>1</error>"); sb.append("<message>"); sb.append(msg); sb.append("</message>"); } sb.append("</response>"); HttpServletRequest request = PortalUtil.getHttpServletRequest(actionRequest); HttpServletResponse response = PortalUtil.getHttpServletResponse(actionResponse); ServletResponseUtil.sendFile(request, response, null, sb.toString().getBytes(StringPool.UTF8), ContentTypes.TEXT_XML_UTF8); } protected void sendSuccess(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception { sendResponse(actionRequest, actionResponse, null, true); } private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false; private static Log _log = LogFactoryUtil.getLog(TrackbackAction.class); }