1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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.Portal;
39  import com.liferay.portal.util.PortalUtil;
40  import com.liferay.portal.util.WebKeys;
41  import com.liferay.portlet.PortletPreferencesFactoryUtil;
42  import com.liferay.portlet.blogs.NoSuchEntryException;
43  import com.liferay.portlet.blogs.model.BlogsEntry;
44  import com.liferay.portlet.blogs.util.TrackbackVerifierUtil;
45  import com.liferay.portlet.messageboards.model.MBMessage;
46  import com.liferay.portlet.messageboards.model.MBMessageDisplay;
47  import com.liferay.portlet.messageboards.model.MBThread;
48  import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
49  import com.liferay.util.servlet.ServletResponseUtil;
50  
51  import javax.portlet.ActionRequest;
52  import javax.portlet.ActionResponse;
53  import javax.portlet.PortletConfig;
54  import javax.portlet.PortletPreferences;
55  
56  import javax.servlet.http.HttpServletResponse;
57  
58  import org.apache.struts.action.ActionForm;
59  import org.apache.struts.action.ActionMapping;
60  
61  /**
62   * <a href="TrackbackAction.java.html"><b><i>View Source</i></b></a>
63   *
64   * @author Alexander Chow
65   *
66   */
67  public class TrackbackAction extends PortletAction {
68  
69      public void processAction(
70              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
71              ActionRequest actionRequest, ActionResponse actionResponse)
72          throws Exception {
73  
74          try {
75              addTrackback(actionRequest, actionResponse);
76          }
77          catch (NoSuchEntryException nsee) {
78              if (_log.isWarnEnabled()) {
79                  _log.warn(nsee, nsee);
80              }
81          }
82          catch (Exception e) {
83              _log.error(e, e);
84          }
85  
86          setForward(actionRequest, ActionConstants.COMMON_NULL);
87      }
88  
89      protected void addTrackback(
90              ActionRequest actionRequest, ActionResponse actionResponse)
91          throws Exception {
92  
93          ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
94              WebKeys.THEME_DISPLAY);
95  
96          String title = ParamUtil.getString(actionRequest, "title");
97          String excerpt = ParamUtil.getString(actionRequest, "excerpt");
98          String url = ParamUtil.getString(actionRequest, "url");
99          String blogName = ParamUtil.getString(actionRequest, "blog_name");
100 
101         if (!isCommentsEnabled(actionRequest)) {
102             sendError(
103                 actionResponse,
104                 "Comments have been disabled for this blog entry.");
105 
106             return;
107         }
108 
109         if (Validator.isNull(url)) {
110             sendError(
111                 actionResponse, "Trackback requires a valid permanent URL.");
112 
113             return;
114         }
115 
116         ActionUtil.getEntry(actionRequest);
117 
118         BlogsEntry entry = (BlogsEntry)actionRequest.getAttribute(
119             WebKeys.BLOGS_ENTRY);
120 
121         if (!entry.isAllowTrackbacks()) {
122             sendError(
123                 actionResponse,
124                 "Trackbacks are not enabled on this blog entry.");
125 
126             return;
127         }
128 
129         long userId = UserLocalServiceUtil.getDefaultUserId(
130             themeDisplay.getCompanyId());
131         String className = BlogsEntry.class.getName();
132         long classPK = entry.getEntryId();
133 
134         ServiceContext serviceContext = ServiceContextFactory.getInstance(
135             MBMessage.class.getName(), actionRequest);
136 
137         MBMessageDisplay messageDisplay =
138             MBMessageLocalServiceUtil.getDiscussionMessageDisplay(
139                 userId, className, classPK);
140 
141         MBThread thread = messageDisplay.getThread();
142 
143         long threadId = thread.getThreadId();
144         long parentMessageId = thread.getRootMessageId();
145         String body =
146             "[...] " + excerpt + " [...] [url=" + url + "]" +
147                 themeDisplay.translate("read-more") + "[/url]";
148 
149         MBMessage message = MBMessageLocalServiceUtil.addDiscussionMessage(
150             userId, blogName, className, classPK, threadId, parentMessageId,
151             title, body, serviceContext);
152 
153         String entryURL =
154             themeDisplay.getPortalURL() +
155                 PortalUtil.getLayoutURL(themeDisplay) +
156                     Portal.FRIENDLY_URL_SEPARATOR + "blogs/" +
157                         entry.getUrlTitle();
158 
159         TrackbackVerifierUtil.addNewPost(
160             message.getMessageId(), url, entryURL);
161 
162         sendSuccess(actionResponse);
163     }
164 
165     protected boolean isCheckMethodOnProcessAction() {
166         return _CHECK_METHOD_ON_PROCESS_ACTION;
167     }
168 
169     protected boolean isCommentsEnabled(ActionRequest actionRequest)
170         throws Exception {
171 
172         PortletPreferences preferences = actionRequest.getPreferences();
173 
174         String portletResource = ParamUtil.getString(
175             actionRequest, "portletResource");
176 
177         if (Validator.isNotNull(portletResource)) {
178             preferences = PortletPreferencesFactoryUtil.getPortletSetup(
179                 actionRequest, portletResource);
180         }
181 
182         return GetterUtil.getBoolean(
183             preferences.getValue("enable-comments", null), true);
184     }
185 
186     protected void sendError(ActionResponse actionResponse, String msg)
187         throws Exception {
188 
189         sendResponse(actionResponse, msg, false);
190     }
191 
192     protected void sendResponse(
193             ActionResponse actionResponse, String msg, boolean success)
194         throws Exception {
195 
196         StringBuilder sb = new StringBuilder();
197 
198         sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
199         sb.append("<response>");
200 
201         if (success) {
202             sb.append("<error>0</error>");
203         }
204         else {
205             sb.append("<error>1</error>");
206             sb.append("<message>" + msg + "</message>");
207         }
208 
209         sb.append("</response>");
210 
211         HttpServletResponse response = PortalUtil.getHttpServletResponse(
212             actionResponse);
213 
214         ServletResponseUtil.sendFile(
215             response, null, sb.toString().getBytes(StringPool.UTF8),
216             ContentTypes.TEXT_XML_UTF8);
217     }
218 
219     protected void sendSuccess(ActionResponse actionResponse) throws Exception {
220         sendResponse(actionResponse, null, true);
221     }
222 
223     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
224 
225     private static Log _log = LogFactoryUtil.getLog(TrackbackAction.class);
226 
227 }