1 23 24 package org.infoglue.cms.util; 25 26 import java.io.BufferedReader ; 27 import java.io.InputStream ; 28 import java.io.InputStreamReader ; 29 import java.io.PrintWriter ; 30 import java.net.URL ; 31 import java.net.URLConnection ; 32 import java.net.URLEncoder ; 33 import java.util.ArrayList ; 34 import java.util.Enumeration ; 35 import java.util.Hashtable ; 36 import java.util.Iterator ; 37 import java.util.List ; 38 import java.util.Map ; 39 import java.util.StringTokenizer ; 40 41 import org.apache.log4j.Logger; 42 43 44 50 51 public class RemoteCacheUpdater implements NotificationListener 52 { 53 private final static Logger logger = Logger.getLogger(RemoteCacheUpdater.class.getName()); 54 55 private static List waitingSystemNotificationMessages = new ArrayList (); 57 58 61 62 public RemoteCacheUpdater() 63 { 64 } 65 66 70 71 public void setContextParameters(Map map) 72 { 73 } 74 75 public static List getSystemNotificationMessages() 76 { 77 return waitingSystemNotificationMessages; 78 } 79 80 public static void clearSystemNotificationMessages() 81 { 82 synchronized(waitingSystemNotificationMessages) 83 { 84 waitingSystemNotificationMessages.clear(); 85 } 86 } 87 88 92 93 public void notify(NotificationMessage notificationMessage) 94 { 95 try 96 { 97 logger.info("Update Remote caches...."); 98 updateRemoteCaches(notificationMessage); 99 logger.info("Done updating remote caches...."); 100 } 101 catch(Exception e) 102 { 103 e.printStackTrace(); 104 } 105 } 106 107 108 113 114 public void updateRemoteCaches(NotificationMessage notificationMessage) throws Exception 115 { 116 throw new Exception ("Wrong - this message handler should not be called..."); 117 153 } 154 155 156 161 public void updateRemoteCaches(Hashtable internalMessage, Hashtable publicMessage) throws Exception 162 { 163 List internalUrls = CmsPropertyHandler.getInternalDeliveryUrls(); 164 165 if(internalMessage != null) 166 { 167 Iterator urlsIterator = internalUrls.iterator(); 168 while(urlsIterator.hasNext()) 169 { 170 String deliverUrl = (String )urlsIterator.next(); 171 String address = deliverUrl + "/" + CmsPropertyHandler.getCacheUpdateAction(); 172 logger.info("Updating cache at " + address); 173 try 174 { 175 String response = postToUrl(address, internalMessage); 176 } 177 catch(Exception e) 178 { 179 logger.warn("Error updating cache at " + address + ":" + e.getMessage(), e); 180 } 181 } 182 } 183 184 List publicUrls = CmsPropertyHandler.getPublicDeliveryUrls(); 185 186 if(publicMessage != null) 187 { 188 Iterator urlsIterator = publicUrls.iterator(); 189 while(urlsIterator.hasNext()) 190 { 191 String deliverUrl = (String )urlsIterator.next(); 192 String address = deliverUrl + "/" + CmsPropertyHandler.getCacheUpdateAction(); 193 logger.info("Updating cache at " + address); 194 try 195 { 196 String response = postToUrl(address, publicMessage); 197 } 198 catch(Exception e) 199 { 200 logger.warn("Error updating cache at " + address + ":" + e.getMessage(), e); 201 } 202 } 203 } 204 205 } 206 207 private Hashtable notificationMessageToHashtable(NotificationMessage notificationMessage) 208 { 209 Hashtable hash = new Hashtable (); 210 211 logger.info("Serializing:" + notificationMessage); 212 213 hash.put("className", notificationMessage.getClassName()); 214 hash.put("objectId", notificationMessage.getObjectId()); 215 hash.put("objectName", notificationMessage.getObjectName()); 216 hash.put("typeId", "" + notificationMessage.getType()); 217 218 return hash; 219 } 220 221 222 232 233 private String postToUrl(String urlAddress, Hashtable inHash) throws Exception 234 { 235 URL url = new URL (urlAddress); 236 URLConnection urlConn = url.openConnection(); 237 urlConn.setAllowUserInteraction(false); 238 urlConn.setDoOutput (true); 239 urlConn.setDoInput (true); 240 urlConn.setUseCaches (false); 241 urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 242 PrintWriter printout = new PrintWriter (urlConn.getOutputStream(), true); 243 String argString = ""; 244 if(inHash != null) 245 { 246 argString = toEncodedString(inHash); 247 } 248 printout.print(argString); 249 printout.flush(); 250 printout.close (); 251 InputStream inStream = null; 252 inStream = urlConn.getInputStream(); 253 InputStreamReader inStreamReader = new InputStreamReader (inStream); 254 BufferedReader buffer = new BufferedReader (inStreamReader); 255 StringBuffer strbuf = new StringBuffer (); 256 String line; 257 while((line = buffer.readLine()) != null) 258 { 259 strbuf.append(line); 260 } 261 String readData = strbuf.toString(); 262 buffer.close(); 263 return readData; 264 } 265 266 267 273 274 private String toEncodedString(Hashtable inHash) throws Exception 275 { 276 StringBuffer buffer = new StringBuffer (); 277 Enumeration names = inHash.keys(); 278 while(names.hasMoreElements()) 279 { 280 String name = names.nextElement().toString(); 281 String value = inHash.get(name).toString(); 282 buffer.append(URLEncoder.encode(name, "UTF-8") + "=" + URLEncoder.encode(value, "UTF-8")); 283 if(names.hasMoreElements()) 284 { 285 buffer.append("&"); 286 } 287 } 288 return buffer.toString(); 289 } 290 291 292 300 301 public String decodeHTTPEncodedString(String encoded) 302 { 303 StringBuffer decoded = new StringBuffer (encoded.length()); 304 int i = 0; 305 int j = 0; 306 307 while (i < encoded.length()) { 308 char tecken = encoded.charAt(i); 309 i++; 310 311 if (tecken == '+') 312 tecken = ' '; 313 else if (tecken == '%') { 314 tecken = (char)Integer.parseInt(encoded.substring(i,i+2), 16); 315 i+=2; 316 } 317 decoded.append(tecken); 318 j++; 319 } 320 return new String (decoded); 321 } 322 323 324 333 334 public Hashtable httpEncodedStringToHashtable(String encodedstrang) 335 { 336 Hashtable anropin = new Hashtable (); 337 StringTokenizer andsplitter = new StringTokenizer (encodedstrang,"&"); 338 while (andsplitter.hasMoreTokens()) 339 { 340 String namevaluepair = andsplitter.nextToken(); 341 StringTokenizer equalsplitter = new StringTokenizer (namevaluepair,"="); 342 if (equalsplitter.countTokens() == 2) 343 { 344 String name = equalsplitter.nextToken(); 345 String value = equalsplitter.nextToken(); 346 anropin.put(decodeHTTPEncodedString(name),decodeHTTPEncodedString(value)); 347 } 348 } 349 return anropin; 350 } 351 352 353 354 } 355 | Popular Tags |