1 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 ; 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 77 public class BlogPingListener extends JahiaEventListener { 78 79 static Logger log = Logger.getLogger(BlogPingListener.class); 81 82 private BlogDefinitionNames containerNames; 83 84 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 String [] res = preparePingData(je); 98 String 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 e) { 105 log.error(e.getMessage()); 106 e.printStackTrace(); 107 } 108 } 109 110 113 public void containerUpdated(JahiaEvent je) { 114 containerAdded(je); 115 } 116 117 120 protected String getPostURL(JahiaEvent je, JahiaContainer container, 121 ContentPage page) throws JahiaException { 122 StringBuffer buffer = new StringBuffer (); 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 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 144 protected String [] 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 pingURL = field.getValue(); 156 157 log.debug("About to send ping to: "+pingURL); 158 159 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 fieldName = containerNames.getValue(containerNames.POST_TITLE); 177 field = postContainer.getField(fieldName); 178 final String postTitle = field.getValue(); 180 181 fieldName = containerNames.getValue(containerNames.POST_EXCERPT); 182 field = postContainer.getField(fieldName); 183 final String postExcerpt = field.getValue(); 185 186 final String blogName = blogContentPage.getTitle(je.getParams()); 188 189 final String postURL = getPostURL(je, postContainer, blogContentPage); 191 192 String [] result = {pingURL, postURL, postTitle, postExcerpt, blogName}; 193 return result; 194 } 195 196 197 200 protected static String sendPing(String pingURL, String postURL, 201 String postTitle, String postExcerpt, String blogName) 202 throws IOException { 203 204 log.debug("sendPing: " +pingURL+ ", " +postURL+ ", " +postTitle+ ", " 205 +postExcerpt+ ", " +blogName); 206 207 if (pingURL == null || pingURL.length() < 1) { 208 throw new IllegalArgumentException ("Argument 'pingURL' cannot be null or an empty String"); 209 } 210 211 if (postURL == null || postURL.length() < 1) { 212 throw new IllegalArgumentException ("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 body = post.getResponseBodyAsString(); 237 238 StringBuffer buff = new StringBuffer (); 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 |