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