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.UserLocalServiceUtil;
28 import com.liferay.portal.struts.ActionConstants;
29 import com.liferay.portal.struts.PortletAction;
30 import com.liferay.portal.theme.ThemeDisplay;
31 import com.liferay.portal.util.Portal;
32 import com.liferay.portal.util.PortalUtil;
33 import com.liferay.portal.util.WebKeys;
34 import com.liferay.portlet.PortletPreferencesFactoryUtil;
35 import com.liferay.portlet.blogs.NoSuchEntryException;
36 import com.liferay.portlet.blogs.model.BlogsEntry;
37 import com.liferay.portlet.blogs.util.TrackbackVerifierUtil;
38 import com.liferay.portlet.messageboards.model.MBMessage;
39 import com.liferay.portlet.messageboards.model.MBMessageDisplay;
40 import com.liferay.portlet.messageboards.model.MBThread;
41 import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
42 import com.liferay.util.servlet.ServletResponseUtil;
43
44 import javax.portlet.ActionRequest;
45 import javax.portlet.ActionResponse;
46 import javax.portlet.PortletConfig;
47 import javax.portlet.PortletPreferences;
48
49 import javax.servlet.http.HttpServletRequest;
50 import javax.servlet.http.HttpServletResponse;
51
52 import org.apache.struts.action.ActionForm;
53 import org.apache.struts.action.ActionMapping;
54
55
60 public class TrackbackAction extends PortletAction {
61
62 public void processAction(
63 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
64 ActionRequest actionRequest, ActionResponse actionResponse)
65 throws Exception {
66
67 try {
68 addTrackback(actionRequest, actionResponse);
69 }
70 catch (NoSuchEntryException nsee) {
71 if (_log.isWarnEnabled()) {
72 _log.warn(nsee, nsee);
73 }
74 }
75 catch (Exception e) {
76 _log.error(e, e);
77 }
78
79 setForward(actionRequest, ActionConstants.COMMON_NULL);
80 }
81
82 protected void addTrackback(
83 ActionRequest actionRequest, ActionResponse actionResponse)
84 throws Exception {
85
86 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
87 WebKeys.THEME_DISPLAY);
88
89 String title = ParamUtil.getString(actionRequest, "title");
90 String excerpt = ParamUtil.getString(actionRequest, "excerpt");
91 String url = ParamUtil.getString(actionRequest, "url");
92 String blogName = ParamUtil.getString(actionRequest, "blog_name");
93
94 if (!isCommentsEnabled(actionRequest)) {
95 sendError(
96 actionResponse,
97 "Comments have been disabled for this blog entry.");
98
99 return;
100 }
101
102 if (Validator.isNull(url)) {
103 sendError(
104 actionResponse, "Trackback requires a valid permanent URL.");
105
106 return;
107 }
108
109 HttpServletRequest request = PortalUtil.getHttpServletRequest(
110 actionRequest);
111
112 String remoteIp = request.getRemoteAddr();
113
114 String trackbackIp = HttpUtil.getIpAddress(url);
115
116 if (!remoteIp.equals(trackbackIp)) {
117 sendError(
118 actionResponse,
119 "Remote IP " + remoteIp + " does not match trackback URL's IP "
120 + trackbackIp);
121
122 return;
123 }
124
125 try {
126 ActionUtil.getEntry(actionRequest);
127 }
128 catch (PrincipalException pe) {
129 sendError(
130 actionResponse,
131 "Entry must have guest view permissions to enable trackbacks");
132
133 return;
134 }
135
136 BlogsEntry entry = (BlogsEntry)actionRequest.getAttribute(
137 WebKeys.BLOGS_ENTRY);
138
139 if (!entry.isAllowTrackbacks()) {
140 sendError(
141 actionResponse,
142 "Trackbacks are not enabled on this blog entry.");
143
144 return;
145 }
146
147 long userId = UserLocalServiceUtil.getDefaultUserId(
148 themeDisplay.getCompanyId());
149 long groupId = themeDisplay.getScopeGroupId();
150 String className = BlogsEntry.class.getName();
151 long classPK = entry.getEntryId();
152
153 MBMessageDisplay messageDisplay =
154 MBMessageLocalServiceUtil.getDiscussionMessageDisplay(
155 userId, className, classPK);
156
157 MBThread thread = messageDisplay.getThread();
158
159 long threadId = thread.getThreadId();
160 long parentMessageId = thread.getRootMessageId();
161 String body =
162 "[...] " + excerpt + " [...] [url=" + url + "]" +
163 themeDisplay.translate("read-more") + "[/url]";
164
165 MBMessage message = MBMessageLocalServiceUtil.addDiscussionMessage(
166 userId, blogName, groupId, className, classPK, threadId,
167 parentMessageId, title, body, themeDisplay);
168
169 String entryURL =
170 PortalUtil.getLayoutFullURL(themeDisplay) +
171 Portal.FRIENDLY_URL_SEPARATOR + "blogs/" +
172 entry.getUrlTitle();
173
174 TrackbackVerifierUtil.addNewPost(
175 message.getMessageId(), url, entryURL);
176
177 sendSuccess(actionResponse);
178 }
179
180 protected boolean isCheckMethodOnProcessAction() {
181 return _CHECK_METHOD_ON_PROCESS_ACTION;
182 }
183
184 protected boolean isCommentsEnabled(ActionRequest actionRequest)
185 throws Exception {
186
187 PortletPreferences prefs = actionRequest.getPreferences();
188
189 String portletResource = ParamUtil.getString(
190 actionRequest, "portletResource");
191
192 if (Validator.isNotNull(portletResource)) {
193 prefs = PortletPreferencesFactoryUtil.getPortletSetup(
194 actionRequest, portletResource);
195 }
196
197 return GetterUtil.getBoolean(
198 prefs.getValue("enable-comments", null), true);
199 }
200
201 protected void sendError(ActionResponse actionResponse, String msg)
202 throws Exception {
203
204 sendResponse(actionResponse, msg, false);
205 }
206
207 protected void sendResponse(
208 ActionResponse actionResponse, String msg, boolean success)
209 throws Exception {
210
211 StringBundler sb = new StringBundler(7);
212
213 sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
214 sb.append("<response>");
215
216 if (success) {
217 sb.append("<error>0</error>");
218 }
219 else {
220 sb.append("<error>1</error>");
221 sb.append("<message>");
222 sb.append(msg);
223 sb.append("</message>");
224 }
225
226 sb.append("</response>");
227
228 HttpServletResponse response = PortalUtil.getHttpServletResponse(
229 actionResponse);
230
231 ServletResponseUtil.sendFile(
232 response, null, sb.toString().getBytes(StringPool.UTF8),
233 ContentTypes.TEXT_XML_UTF8);
234 }
235
236 protected void sendSuccess(ActionResponse actionResponse) throws Exception {
237 sendResponse(actionResponse, null, true);
238 }
239
240 private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
241
242 private static Log _log = LogFactoryUtil.getLog(TrackbackAction.class);
243
244 }