1
14
15 package com.liferay.portlet.blogs.action;
16
17 import com.liferay.portal.kernel.log.Log;
18 import com.liferay.portal.kernel.log.LogFactoryUtil;
19 import com.liferay.portal.kernel.util.ContentTypes;
20 import com.liferay.portal.kernel.util.GetterUtil;
21 import com.liferay.portal.kernel.util.HttpUtil;
22 import com.liferay.portal.kernel.util.ParamUtil;
23 import com.liferay.portal.kernel.util.StringBundler;
24 import com.liferay.portal.kernel.util.StringPool;
25 import com.liferay.portal.kernel.util.Validator;
26 import com.liferay.portal.security.auth.PrincipalException;
27 import com.liferay.portal.service.ServiceContext;
28 import com.liferay.portal.service.ServiceContextFactory;
29 import com.liferay.portal.service.UserLocalServiceUtil;
30 import com.liferay.portal.struts.ActionConstants;
31 import com.liferay.portal.struts.PortletAction;
32 import com.liferay.portal.theme.ThemeDisplay;
33 import com.liferay.portal.util.Portal;
34 import com.liferay.portal.util.PortalUtil;
35 import com.liferay.portal.util.WebKeys;
36 import com.liferay.portlet.PortletPreferencesFactoryUtil;
37 import com.liferay.portlet.blogs.NoSuchEntryException;
38 import com.liferay.portlet.blogs.model.BlogsEntry;
39 import com.liferay.portlet.blogs.util.TrackbackVerifierUtil;
40 import com.liferay.portlet.messageboards.model.MBMessage;
41 import com.liferay.portlet.messageboards.model.MBMessageDisplay;
42 import com.liferay.portlet.messageboards.model.MBThread;
43 import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
44 import com.liferay.util.servlet.ServletResponseUtil;
45
46 import javax.portlet.ActionRequest;
47 import javax.portlet.ActionResponse;
48 import javax.portlet.PortletConfig;
49 import javax.portlet.PortletPreferences;
50
51 import javax.servlet.http.HttpServletRequest;
52 import javax.servlet.http.HttpServletResponse;
53
54 import org.apache.struts.action.ActionForm;
55 import org.apache.struts.action.ActionMapping;
56
57
62 public class TrackbackAction extends PortletAction {
63
64 public void processAction(
65 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
66 ActionRequest actionRequest, ActionResponse actionResponse)
67 throws Exception {
68
69 try {
70 addTrackback(actionRequest, actionResponse);
71 }
72 catch (NoSuchEntryException nsee) {
73 if (_log.isWarnEnabled()) {
74 _log.warn(nsee, nsee);
75 }
76 }
77 catch (Exception e) {
78 _log.error(e, e);
79 }
80
81 setForward(actionRequest, ActionConstants.COMMON_NULL);
82 }
83
84 protected void addTrackback(
85 ActionRequest actionRequest, ActionResponse actionResponse)
86 throws Exception {
87
88 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
89 WebKeys.THEME_DISPLAY);
90
91 String title = ParamUtil.getString(actionRequest, "title");
92 String excerpt = ParamUtil.getString(actionRequest, "excerpt");
93 String url = ParamUtil.getString(actionRequest, "url");
94 String blogName = ParamUtil.getString(actionRequest, "blog_name");
95
96 if (!isCommentsEnabled(actionRequest)) {
97 sendError(
98 actionRequest, actionResponse,
99 "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 String className = BlogsEntry.class.getName();
154 long classPK = entry.getEntryId();
155
156 ServiceContext serviceContext = ServiceContextFactory.getInstance(
157 MBMessage.class.getName(), actionRequest);
158
159 MBMessageDisplay messageDisplay =
160 MBMessageLocalServiceUtil.getDiscussionMessageDisplay(
161 userId, className, classPK);
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 MBMessage message = MBMessageLocalServiceUtil.addDiscussionMessage(
172 userId, blogName, className, classPK, threadId, parentMessageId,
173 title, body, serviceContext);
174
175 String entryURL =
176 PortalUtil.getLayoutFullURL(themeDisplay) +
177 Portal.FRIENDLY_URL_SEPARATOR + "blogs/" +
178 entry.getUrlTitle();
179
180 TrackbackVerifierUtil.addNewPost(
181 message.getMessageId(), url, entryURL);
182
183 sendSuccess(actionRequest, actionResponse);
184 }
185
186 protected boolean isCheckMethodOnProcessAction() {
187 return _CHECK_METHOD_ON_PROCESS_ACTION;
188 }
189
190 protected boolean isCommentsEnabled(ActionRequest actionRequest)
191 throws Exception {
192
193 PortletPreferences preferences = actionRequest.getPreferences();
194
195 String portletResource = ParamUtil.getString(
196 actionRequest, "portletResource");
197
198 if (Validator.isNotNull(portletResource)) {
199 preferences = PortletPreferencesFactoryUtil.getPortletSetup(
200 actionRequest, portletResource);
201 }
202
203 return GetterUtil.getBoolean(
204 preferences.getValue("enable-comments", null), true);
205 }
206
207 protected void sendError(
208 ActionRequest actionRequest, ActionResponse actionResponse,
209 String msg)
210 throws Exception {
211
212 sendResponse(actionRequest, actionResponse, msg, false);
213 }
214
215 protected void sendResponse(
216 ActionRequest actionRequest, ActionResponse actionResponse,
217 String msg, boolean success)
218 throws Exception {
219
220 StringBundler sb = new StringBundler(7);
221
222 sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
223 sb.append("<response>");
224
225 if (success) {
226 sb.append("<error>0</error>");
227 }
228 else {
229 sb.append("<error>1</error>");
230 sb.append("<message>");
231 sb.append(msg);
232 sb.append("</message>");
233 }
234
235 sb.append("</response>");
236
237 HttpServletRequest request = PortalUtil.getHttpServletRequest(
238 actionRequest);
239 HttpServletResponse response = PortalUtil.getHttpServletResponse(
240 actionResponse);
241
242 ServletResponseUtil.sendFile(
243 request, response, null, sb.toString().getBytes(StringPool.UTF8),
244 ContentTypes.TEXT_XML_UTF8);
245 }
246
247 protected void sendSuccess(
248 ActionRequest actionRequest, ActionResponse actionResponse)
249 throws Exception {
250
251 sendResponse(actionRequest, actionResponse, null, true);
252 }
253
254 private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
255
256 private static Log _log = LogFactoryUtil.getLog(TrackbackAction.class);
257
258 }