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.ServiceContext;
33 import com.liferay.portal.service.ServiceContextFactory;
34 import com.liferay.portal.service.UserLocalServiceUtil;
35 import com.liferay.portal.struts.ActionConstants;
36 import com.liferay.portal.struts.PortletAction;
37 import com.liferay.portal.theme.ThemeDisplay;
38 import com.liferay.portal.util.PortalUtil;
39 import com.liferay.portal.util.WebKeys;
40 import com.liferay.portlet.PortletPreferencesFactoryUtil;
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 (Exception e) {
76 sendError(actionResponse, "An unknown error has occurred.");
77
78 _log.error(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 actionResponse,
99 "Comments have been disabled for this blog entry.");
100
101 return;
102 }
103
104 if (Validator.isNull(url)) {
105 sendError(
106 actionResponse, "Trackback requires a valid permanent URL.");
107
108 return;
109 }
110
111 ActionUtil.getEntry(actionRequest);
112
113 BlogsEntry entry = (BlogsEntry)actionRequest.getAttribute(
114 WebKeys.BLOGS_ENTRY);
115
116 if (!entry.isAllowTrackbacks()) {
117 sendError(
118 actionResponse,
119 "Trackbacks are not enabled on this blog entry.");
120
121 return;
122 }
123
124 long userId = UserLocalServiceUtil.getDefaultUserId(
125 themeDisplay.getCompanyId());
126 String className = BlogsEntry.class.getName();
127 long classPK = entry.getEntryId();
128
129 ServiceContext serviceContext = ServiceContextFactory.getInstance(
130 MBMessage.class.getName(), actionRequest);
131
132 MBMessageDisplay messageDisplay =
133 MBMessageLocalServiceUtil.getDiscussionMessageDisplay(
134 userId, className, classPK);
135
136 MBThread thread = messageDisplay.getThread();
137
138 long threadId = thread.getThreadId();
139 long parentMessageId = thread.getRootMessageId();
140 String body =
141 "[...] " + excerpt + " [...] [url=" + url + "]" +
142 themeDisplay.translate("read-more") + "[/url]";
143
144 MBMessage message = MBMessageLocalServiceUtil.addDiscussionMessage(
145 userId, blogName, className, classPK, threadId, parentMessageId,
146 title, body, serviceContext);
147
148 String entryURL =
149 themeDisplay.getPortalURL() +
150 PortalUtil.getLayoutURL(themeDisplay) + "/-/blogs/" +
151 entry.getUrlTitle();
152
153 TrackbackVerifierUtil.addNewPost(
154 message.getMessageId(), url, entryURL);
155
156 sendSuccess(actionResponse);
157 }
158
159 protected boolean isCommentsEnabled(ActionRequest actionRequest)
160 throws Exception {
161
162 PortletPreferences preferences = actionRequest.getPreferences();
163
164 String portletResource = ParamUtil.getString(
165 actionRequest, "portletResource");
166
167 if (Validator.isNotNull(portletResource)) {
168 preferences = PortletPreferencesFactoryUtil.getPortletSetup(
169 actionRequest, portletResource);
170 }
171
172 return GetterUtil.getBoolean(
173 preferences.getValue("enable-comments", null), true);
174 }
175
176 protected void sendError(ActionResponse actionResponse, String msg)
177 throws Exception {
178
179 sendResponse(actionResponse, msg, false);
180 }
181
182 protected void sendResponse(
183 ActionResponse actionResponse, String msg, boolean success)
184 throws Exception {
185
186 StringBuilder sb = new StringBuilder();
187
188 sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
189 sb.append("<response>");
190
191 if (success) {
192 sb.append("<error>0</error>");
193 }
194 else {
195 sb.append("<error>1</error>");
196 sb.append("<message>" + msg + "</message>");
197 }
198
199 sb.append("</response>");
200
201 HttpServletResponse response = PortalUtil.getHttpServletResponse(
202 actionResponse);
203
204 ServletResponseUtil.sendFile(
205 response, null, sb.toString().getBytes(StringPool.UTF8),
206 ContentTypes.TEXT_XML_UTF8);
207 }
208
209 protected void sendSuccess(ActionResponse actionResponse) throws Exception {
210 sendResponse(actionResponse, null, true);
211 }
212
213 private static Log _log = LogFactoryUtil.getLog(TrackbackAction.class);
214
215 }