1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.blogs.util;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.HttpUtil;
20  import com.liferay.portal.kernel.util.Tuple;
21  import com.liferay.portal.kernel.util.Validator;
22  import com.liferay.portal.service.UserLocalServiceUtil;
23  import com.liferay.portal.util.Portal;
24  import com.liferay.portlet.blogs.model.BlogsEntry;
25  import com.liferay.portlet.messageboards.model.MBMessage;
26  import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
27  
28  import java.util.ArrayList;
29  import java.util.Collections;
30  import java.util.List;
31  
32  /**
33   * <a href="TrackbackVerifierUtil.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Alexander Chow
36   */
37  public class TrackbackVerifierUtil {
38  
39      public static void addNewPost(
40          long messageId, String url, String entryUrl) {
41  
42          _trackbacks.add(new Tuple(messageId, url, entryUrl));
43      }
44  
45      public static void verifyNewPosts() {
46          Tuple tuple = null;
47  
48          while (!_trackbacks.isEmpty()) {
49              synchronized (_trackbacks) {
50                  tuple = _trackbacks.remove(0);
51              }
52  
53              long messageId = (Long)tuple.getObject(0);
54              String url = (String)tuple.getObject(1);
55              String entryUrl = (String)tuple.getObject(2);
56  
57              _verifyPost(messageId, url, entryUrl);
58          }
59      }
60  
61      public static void verifyPost(BlogsEntry entry, MBMessage message)
62          throws Exception {
63  
64          long messageId = message.getMessageId();
65          String entryURL =
66              Portal.FRIENDLY_URL_SEPARATOR + "blogs/" + entry.getUrlTitle();
67          String body = message.getBody();
68          String url = null;
69  
70          int start = body.indexOf("[url=");
71  
72          if (start > -1) {
73              start += "[url=".length();
74  
75              int end = body.indexOf("]", start);
76  
77              if (end > -1) {
78                  url = body.substring(start, end);
79              }
80          }
81  
82          if (Validator.isNotNull(url)) {
83              long companyId = message.getCompanyId();
84              long userId = message.getUserId();
85              long defaultUserId = UserLocalServiceUtil.getDefaultUserId(
86                  companyId);
87  
88              if (userId == defaultUserId) {
89                  _verifyPost(messageId, url, entryURL);
90              }
91          }
92      }
93  
94      private static void _verifyPost(
95          long messageId, String url, String entryURL) {
96  
97          try {
98              String result = HttpUtil.URLtoString(url);
99  
100             if (result.contains(entryURL)) {
101                 return;
102             }
103         }
104         catch (Exception e) {
105         }
106 
107         try {
108             MBMessageLocalServiceUtil.deleteDiscussionMessage(
109                 messageId);
110         }
111         catch (Exception e) {
112             _log.error(
113                 "Error trying to delete trackback message " + messageId, e);
114         }
115     }
116 
117     private static Log _log = LogFactoryUtil.getLog(
118         TrackbackVerifierUtil.class);
119 
120     private static List<Tuple> _trackbacks =
121         Collections.synchronizedList(new ArrayList<Tuple>());
122 
123 }