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.blogs.NoSuchEntryException;
039 import com.liferay.portlet.blogs.model.BlogsEntry;
040 import com.liferay.portlet.blogs.util.LinkbackConsumerUtil;
041 import com.liferay.portlet.messageboards.model.MBMessage;
042 import com.liferay.portlet.messageboards.model.MBMessageDisplay;
043 import com.liferay.portlet.messageboards.model.MBThread;
044 import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
045
046 import javax.portlet.ActionRequest;
047 import javax.portlet.ActionResponse;
048 import javax.portlet.PortletConfig;
049 import javax.portlet.PortletPreferences;
050
051 import javax.servlet.http.HttpServletRequest;
052 import javax.servlet.http.HttpServletResponse;
053
054 import org.apache.struts.action.ActionForm;
055 import org.apache.struts.action.ActionMapping;
056
057
060 public class TrackbackAction extends PortletAction {
061
062 @Override
063 public void processAction(
064 ActionMapping actionMapping, ActionForm actionForm,
065 PortletConfig portletConfig, ActionRequest actionRequest,
066 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 HttpServletRequest request = PortalUtil.getHttpServletRequest(
092 actionRequest);
093
094 HttpServletRequest originalRequest =
095 PortalUtil.getOriginalServletRequest(request);
096
097 String title = ParamUtil.getString(originalRequest, "title");
098 String excerpt = ParamUtil.getString(originalRequest, "excerpt");
099 String url = ParamUtil.getString(originalRequest, "url");
100 String blogName = ParamUtil.getString(originalRequest, "blog_name");
101
102 if (!isCommentsEnabled(actionRequest)) {
103 sendError(
104 actionRequest, actionResponse,
105 "Comments have been disabled for this blog entry.");
106
107 return;
108 }
109
110 if (Validator.isNull(url)) {
111 sendError(
112 actionRequest, actionResponse,
113 "Trackback requires a valid permanent URL.");
114
115 return;
116 }
117
118 String remoteIp = request.getRemoteAddr();
119
120 String trackbackIp = HttpUtil.getIpAddress(url);
121
122 if (!remoteIp.equals(trackbackIp)) {
123 sendError(
124 actionRequest, actionResponse,
125 "Remote IP " + remoteIp +
126 " does not match trackback URL's IP " + trackbackIp + ".");
127
128 return;
129 }
130
131 try {
132 ActionUtil.getEntry(actionRequest);
133 }
134 catch (PrincipalException pe) {
135 sendError(
136 actionRequest, actionResponse,
137 "Blog entry must have guest view permissions to enable " +
138 "trackbacks.");
139
140 return;
141 }
142
143 BlogsEntry entry = (BlogsEntry)actionRequest.getAttribute(
144 WebKeys.BLOGS_ENTRY);
145
146 if (!entry.isAllowTrackbacks()) {
147 sendError(
148 actionRequest, actionResponse,
149 "Trackbacks are not enabled on this blog entry.");
150
151 return;
152 }
153
154 long userId = UserLocalServiceUtil.getDefaultUserId(
155 themeDisplay.getCompanyId());
156 long groupId = entry.getGroupId();
157 String className = BlogsEntry.class.getName();
158 long classPK = entry.getEntryId();
159
160 MBMessageDisplay messageDisplay =
161 MBMessageLocalServiceUtil.getDiscussionMessageDisplay(
162 userId, groupId, className, classPK,
163 WorkflowConstants.STATUS_APPROVED);
164
165 MBThread thread = messageDisplay.getThread();
166
167 long threadId = thread.getThreadId();
168 long parentMessageId = thread.getRootMessageId();
169 String body =
170 "[...] " + excerpt + " [...] [url=" + url + "]" +
171 themeDisplay.translate("read-more") + "[/url]";
172
173 ServiceContext serviceContext = ServiceContextFactory.getInstance(
174 MBMessage.class.getName(), actionRequest);
175
176 MBMessage message = MBMessageLocalServiceUtil.addDiscussionMessage(
177 userId, blogName, groupId, className, classPK, threadId,
178 parentMessageId, title, body, serviceContext);
179
180 String entryURL =
181 PortalUtil.getLayoutFullURL(themeDisplay) +
182 Portal.FRIENDLY_URL_SEPARATOR + "blogs/" + entry.getUrlTitle();
183
184 LinkbackConsumerUtil.addNewTrackback(
185 message.getMessageId(), url, entryURL);
186
187 sendSuccess(actionRequest, actionResponse);
188 }
189
190 @Override
191 protected boolean isCheckMethodOnProcessAction() {
192 return _CHECK_METHOD_ON_PROCESS_ACTION;
193 }
194
195 protected boolean isCommentsEnabled(ActionRequest actionRequest)
196 throws Exception {
197
198 PortletPreferences portletPreferences = getStrictPortletSetup(
199 actionRequest);
200
201 if (portletPreferences == null) {
202 portletPreferences = actionRequest.getPreferences();
203 }
204
205 return GetterUtil.getBoolean(
206 portletPreferences.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 }