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