1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.util;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.HttpUtil;
28  import com.liferay.portal.kernel.util.Tuple;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.service.UserLocalServiceUtil;
31  import com.liferay.portlet.blogs.model.BlogsEntry;
32  import com.liferay.portlet.messageboards.model.MBMessage;
33  import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
34  
35  import java.util.ArrayList;
36  import java.util.Collections;
37  import java.util.List;
38  
39  /**
40   * <a href="TrackbackVerifierUtil.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Alexander Chow
43   *
44   */
45  public class TrackbackVerifierUtil {
46  
47      public static void addNewPost(
48          long messageId, String url, String entryUrl) {
49  
50          _trackbacks.add(new Tuple(messageId, url, entryUrl));
51      }
52  
53      public static void verifyNewPosts() {
54          Tuple tuple = null;
55  
56          while (!_trackbacks.isEmpty()) {
57              synchronized (_trackbacks) {
58                  tuple = _trackbacks.remove(0);
59              }
60  
61              long messageId = (Long)tuple.getObject(0);
62              String url = (String)tuple.getObject(1);
63              String entryUrl = (String)tuple.getObject(2);
64  
65              _verifyPost(messageId, url, entryUrl);
66          }
67      }
68  
69      public static void verifyPost(BlogsEntry entry, MBMessage message)
70          throws Exception {
71  
72          long messageId = message.getMessageId();
73          String entryURL = "/-/blogs/" + entry.getUrlTitle();
74          String body = message.getBody();
75          String url = null;
76  
77          int start = body.indexOf("[url=");
78  
79          if (start > -1) {
80              start += "[url=".length();
81  
82              int end = body.indexOf("]", start);
83  
84              if (end > -1) {
85                  url = body.substring(start, end);
86              }
87          }
88  
89          if (Validator.isNotNull(url)) {
90              long companyId = message.getCompanyId();
91              long userId = message.getUserId();
92              long defaultUserId = UserLocalServiceUtil.getDefaultUserId(
93                  companyId);
94  
95              if (userId == defaultUserId) {
96                  _verifyPost(messageId, url, entryURL);
97              }
98          }
99      }
100 
101     private static void _verifyPost(
102         long messageId, String url, String entryURL) {
103 
104         try {
105             String result = HttpUtil.URLtoString(url);
106 
107             if (result.contains(entryURL)) {
108                 return;
109             }
110         }
111         catch (Exception e) {
112         }
113 
114         try {
115             MBMessageLocalServiceUtil.deleteDiscussionMessage(
116                 messageId);
117         }
118         catch (Exception e) {
119             _log.error(
120                 "Error trying to delete trackback message " + messageId, e);
121         }
122     }
123 
124     private static Log _log =
125          LogFactoryUtil.getLog(TrackbackVerifierUtil.class);
126 
127     private static List<Tuple> _trackbacks =
128         Collections.synchronizedList(new ArrayList<Tuple>());
129 
130 }