001/* 002 Licensed to the Apache Software Foundation (ASF) under one 003 or more contributor license agreements. See the NOTICE file 004 distributed with this work for additional information 005 regarding copyright ownership. The ASF licenses this file 006 to you under the Apache License, Version 2.0 (the 007 "License"); you may not use this file except in compliance 008 with the License. You may obtain a copy of the License at 009 010 http://www.apache.org/licenses/LICENSE-2.0 011 012 Unless required by applicable law or agreed to in writing, 013 software distributed under the License is distributed on an 014 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 KIND, either express or implied. See the License for the 016 specific language governing permissions and limitations 017 under the License. 018 */ 019package org.apache.wiki.filters; 020 021import org.apache.log4j.Logger; 022import org.apache.wiki.api.core.Context; 023import org.apache.wiki.api.core.ContextEnum; 024import org.apache.wiki.api.core.Engine; 025import org.apache.wiki.api.filters.BasePageFilter; 026import org.apache.xmlrpc.AsyncCallback; 027import org.apache.xmlrpc.XmlRpcClient; 028 029import java.net.MalformedURLException; 030import java.net.URL; 031import java.util.Hashtable; 032import java.util.Properties; 033import java.util.Vector; 034 035/** 036 * A very dumb class that pings weblogs.com on each save. INTERNAL USE ONLY SO FAR! Look, but don't use as-is. 037 */ 038// FIXME: Needs to figure out when only weblogs have been saved. 039// FIXME: rpc endpoint must be configurable 040// FIXME: Should really be settable per-page. 041// FIXME: Weblog name has been set to stone 042public class PingWeblogsComFilter extends BasePageFilter { 043 044 private static final Logger log = Logger.getLogger( PingWeblogsComFilter.class ); 045 046 private String m_pingURL; 047 048 /** 049 * The property name for the URL to ping. Value is <tt>{@value}</tt>. 050 */ 051 public static final String PROP_PINGURL = "pingurl"; 052 053 /** 054 * {@inheritDoc} 055 */ 056 @Override 057 public void initialize( final Engine engine, final Properties props ) { 058 m_pingURL = props.getProperty( PROP_PINGURL, "http://rpc.weblogs.com/RPC2" ); 059 } 060 061 /** 062 * {@inheritDoc} 063 */ 064 @Override 065 public void postSave( final Context context, final String pagecontent ) { 066 String blogName = context.getPage().getName(); 067 final Engine engine = context.getEngine(); 068 069 final int blogentryTxt = blogName.indexOf("_blogentry_"); 070 if( blogentryTxt == -1 ) { 071 return; // This is not a weblog entry. 072 } 073 074 blogName = blogName.substring( 0, blogentryTxt ); 075 076 if( blogName.equals( engine.getFrontPage() ) ) { 077 blogName = null; 078 } 079 080 try { 081 final XmlRpcClient xmlrpc = new XmlRpcClient(m_pingURL); 082 final Vector< String > params = new Vector<>(); 083 params.addElement( "The Butt Ugly Weblog" ); // FIXME: Must be settable 084 params.addElement( engine.getURL( ContextEnum.PAGE_VIEW.getRequestContext(), blogName, null ) ); 085 086 if( log.isDebugEnabled() ) { 087 log.debug( "Pinging weblogs.com with URL: " + engine.getURL( ContextEnum.PAGE_VIEW.getRequestContext(), blogName, null ) ); 088 } 089 090 xmlrpc.executeAsync("weblogUpdates.ping", params, 091 new AsyncCallback() { 092 @Override 093 public void handleError( final Exception ex, final URL url, final String method ) { 094 log.error( "Unable to execute weblogs.com ping to URL: " + url.toString(), ex ); 095 } 096 097 @Override 098 public void handleResult( final Object result, final URL url, final String method ) { 099 @SuppressWarnings("unchecked") 100 final Hashtable< String, Object > res = (Hashtable < String, Object > ) result; 101 102 final Boolean flerror = ( Boolean )res.get( "flerror" ); 103 final String msg = ( String )res.get( "message" ); 104 105 if( flerror ) { 106 log.error( "Failed to ping: " + msg ); 107 } 108 109 log.info( "Weblogs.com has been pinged." ); 110 } 111 } 112 ); 113 } catch( final MalformedURLException e ) { 114 log.error("Malformed URL",e); 115 } 116 } 117 118}