KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > blogs > BlogPingListener


1 /*
2  * ____.
3  * __/\ ______| |__/\. _______
4  * __ .____| | \ | +----+ \
5  * _______| /--| | | - \ _ | : - \_________
6  * \\______: :---| : : | : | \________>
7  * |__\---\_____________:______: :____|____:_____\
8  * /_____|
9  *
10  * . . . i n j a h i a w e t r u s t . . .
11  *
12  *
13  *
14  * ----- BEGIN LICENSE BLOCK -----
15  * Version: JCSL 1.0
16  *
17  * The contents of this file are subject to the Jahia Community Source License
18  * 1.0 or later (the "License"); you may not use this file except in
19  * compliance with the License. You may obtain a copy of the License at
20  * http://www.jahia.org/license
21  *
22  * Software distributed under the License is distributed on an "AS IS" basis,
23  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
24  * for the rights, obligations and limitations governing use of the contents
25  * of the file. The Original and Upgraded Code is the Jahia CMS and Portal
26  * Server. The developer of the Original and Upgraded Code is JAHIA Ltd. JAHIA
27  * Ltd. owns the copyrights in the portions it created. All Rights Reserved.
28  *
29  * The Shared Modifications are Jahia View Helper.
30  *
31  * The Developer of the Shared Modifications is Jahia Solution Sàrl.
32  * Portions created by the Initial Developer are Copyright (C) 2002 by the
33  * Initial Developer. All Rights Reserved.
34  *
35  * ----- END LICENSE BLOCK -----
36  */

37
38 package org.jahia.blogs;
39
40 import org.jahia.blogs.actions.BlogDefinitionNames;
41
42 import org.jahia.data.events.JahiaEventListener;
43 import org.jahia.data.events.JahiaEvent;
44
45 import org.jahia.data.fields.JahiaField;
46 import org.jahia.data.fields.LoadFlags;
47
48 import org.jahia.data.containers.JahiaContainer;
49 import org.jahia.data.containers.JahiaContainerList;
50
51 import org.jahia.services.pages.ContentPage;
52
53 import org.jahia.services.version.EntryLoadRequest;
54
55 import org.jahia.services.containers.JahiaContainersService;
56 import org.jahia.registries.ServicesRegistry;
57
58 import org.jahia.params.ParamBean;
59
60 import org.jahia.exceptions.JahiaException;
61
62 import java.io.IOException JavaDoc;
63
64 import org.apache.commons.httpclient.HttpClient;
65 import org.apache.commons.httpclient.methods.PostMethod;
66
67 import org.apache.log4j.Logger;
68
69 /**
70  * Jahia Listener reacting to any added or updated Container matching
71  * the BLOG_TB_PING_LIST definition. Once a Container containing a ping url is
72  * added or updated, a trackback ping request is sent to the url present in the
73  * container.
74  *
75  * @author Xavier Lawrence
76  */

77 public class BlogPingListener extends JahiaEventListener {
78     
79     // log4j logger
80
static Logger log = Logger.getLogger(BlogPingListener.class);
81     
82     private BlogDefinitionNames containerNames;
83     
84     /**
85      * Sends a ping
86      */

87     public void containerAdded(JahiaEvent je) {
88         JahiaContainer source = (JahiaContainer)je.getObject();
89         
90         containerNames = new BlogDefinitionNames(je.getParams());
91         
92         try {
93             if (source.getDefinition().getName().equals(containerNames.getValue(
94                     containerNames.BLOG_TB_PING_LIST))) {
95       
96                 // send a ping
97
String JavaDoc[] res = preparePingData(je);
98                 String JavaDoc result = sendPing(res[0], res[1], res[2], res[3], res[4]);
99
100                 log.debug(result);
101                 log.debug("End containerAdded...");
102             }
103             
104         } catch (Exception JavaDoc e) {
105             log.error(e.getMessage());
106             e.printStackTrace();
107         }
108     }
109      
110     /**
111      * Does the same as containerAdded (calls method containerAdded)
112      */

113     public void containerUpdated(JahiaEvent je) {
114         containerAdded(je);
115     }
116     
117     /**
118      * Get the URL of a given post
119      */

120     protected String JavaDoc getPostURL(JahiaEvent je, JahiaContainer container,
121             ContentPage page) throws JahiaException {
122         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
123         buffer.append(je.getParams().getRequest().getScheme());
124         buffer.append("://");
125         buffer.append(je.getParams().getRequest().getServerName());
126         buffer.append(":");
127         buffer.append(je.getParams().getRequest().getServerPort());
128         
129         String JavaDoc savedCacheStatus = je.getParams().getCacheStatus();
130         je.getParams().setCacheStatus(ParamBean.CACHE_BYPASS);
131         buffer.append(page.getURL(je.getParams()));
132         je.getParams().setCacheStatus(savedCacheStatus);
133         
134         buffer.append("?entryId=");
135         buffer.append(container.getID());
136         return buffer.toString();
137     }
138     
139     /**
140      * Fetches the data required by the ping request
141      * @return An Array of String containing in respective order:
142      * pingURL, postURL, postTitle, postExcerpt & blogName
143      */

144     protected String JavaDoc[] preparePingData(JahiaEvent je)
145             throws JahiaException {
146         
147         JahiaContainer source = (JahiaContainer)je.getObject();
148         
149         ContentPage blogContentPage = ContentPage.getPage(
150                 source.getPageID());
151         je.getParams().changePage(blogContentPage);
152         
153         JahiaField field = source.getField(containerNames.getValue(
154                 BlogDefinitionNames.TB_PING_URL));
155         final String JavaDoc pingURL = field.getValue();
156         
157         log.debug("About to send ping to: "+pingURL);
158         
159         // Get the post information
160
int ctnListID = source.getListID();
161         
162         JahiaContainersService cntService = ServicesRegistry.getInstance().
163                 getJahiaContainersService();
164         
165         JahiaContainerList tbPingList = cntService.loadContainerList(
166                 ctnListID, LoadFlags.ALL, je.getParams());
167         
168         int postContainerID = tbPingList.getParentEntryID();
169         
170         EntryLoadRequest elr = new EntryLoadRequest(
171                 EntryLoadRequest.STAGING_WORKFLOW_STATE, 0,
172                 je.getParams().getLocales());
173         JahiaContainer postContainer = cntService.loadContainer(
174                 postContainerID, LoadFlags.ALL, je.getParams(), elr);
175         
176         String JavaDoc fieldName = containerNames.getValue(containerNames.POST_TITLE);
177         field = postContainer.getField(fieldName);
178         // set the postTitle
179
final String JavaDoc postTitle = field.getValue();
180         
181         fieldName = containerNames.getValue(containerNames.POST_EXCERPT);
182         field = postContainer.getField(fieldName);
183         // set the excerpt
184
final String JavaDoc postExcerpt = field.getValue();
185              
186         // set the blogName
187
final String JavaDoc blogName = blogContentPage.getTitle(je.getParams());
188         
189         // set the postURL
190
final String JavaDoc postURL = getPostURL(je, postContainer, blogContentPage);
191
192         String JavaDoc[] result = {pingURL, postURL, postTitle, postExcerpt, blogName};
193         return result;
194     }
195     
196     
197     /**
198      * Sends a ping request by sending an HTTP POST
199      */

200     protected static String JavaDoc sendPing(String JavaDoc pingURL, String JavaDoc postURL,
201             String JavaDoc postTitle, String JavaDoc postExcerpt, String JavaDoc blogName)
202             throws IOException JavaDoc {
203         
204         log.debug("sendPing: " +pingURL+ ", " +postURL+ ", " +postTitle+ ", "
205                 +postExcerpt+ ", " +blogName);
206         
207         if (pingURL == null || pingURL.length() < 1) {
208             throw new IllegalArgumentException JavaDoc("Argument 'pingURL' cannot be null or an empty String");
209         }
210         
211         if (postURL == null || postURL.length() < 1) {
212             throw new IllegalArgumentException JavaDoc("Argument 'postURL' cannot be null or an empty String");
213         }
214         
215         HttpClient client = new HttpClient();
216         PostMethod post = new PostMethod(pingURL);
217         
218         post.setRequestHeader("Content-type",
219                 "application/x-www-form-urlencoded; charset=utf-8");
220         
221         post.addParameter("url", postURL);
222         
223         if (postTitle != null && postTitle.length() > 0) {
224             post.addParameter("title", postTitle);
225         }
226         
227         if (postExcerpt != null && postExcerpt.length() > 0) {
228             post.addParameter("excerpt", postExcerpt);
229         }
230         
231         if (blogName != null && blogName.length() > 0) {
232             post.addParameter("blog_name", blogName);
233         }
234         
235         int status = client.executeMethod(post);
236         String JavaDoc body = post.getResponseBodyAsString();
237         
238         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
239         buff.append("StatusCode: ");
240         buff.append(status);
241         buff.append("; ");
242         buff.append(body);
243         
244         return buff.toString();
245     }
246 }
247
Popular Tags