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 java.net.MalformedURLException;
022import java.net.URL;
023import java.util.Hashtable;
024import java.util.Properties;
025import java.util.Vector;
026
027import org.apache.log4j.Logger;
028import org.apache.wiki.WikiContext;
029import org.apache.wiki.WikiEngine;
030import org.apache.wiki.api.filters.BasicPageFilter;
031import org.apache.xmlrpc.AsyncCallback;
032import org.apache.xmlrpc.XmlRpcClient;
033
034/**
035 *  A very dumb class that pings weblogs.com on each save.  INTERNAL USE ONLY SO FAR!
036 *  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
043    extends BasicPageFilter
044{
045    private static final Logger log = Logger.getLogger( PingWeblogsComFilter.class );
046
047    private String m_pingURL;
048
049    /**
050     *  The property name for the URL to ping.  Value is <tt>{@value}</tt>.
051     */
052    public static final String PROP_PINGURL = "pingurl";
053
054    /**
055     *  {@inheritDoc}
056     */
057    public void initialize( WikiEngine engine, Properties props )
058    {
059        m_pingURL = props.getProperty( PROP_PINGURL, "http://rpc.weblogs.com/RPC2" );
060    }
061
062    /**
063     *  {@inheritDoc}
064     */
065    public void postSave( WikiContext context, String pagecontent )
066    {
067        String     blogName = context.getPage().getName();
068        WikiEngine engine   = context.getEngine();
069
070        int blogentryTxt = blogName.indexOf("_blogentry_");
071        if( blogentryTxt == -1 )
072        {
073            return; // This is not a weblog entry.
074        }
075        
076        blogName = blogName.substring( 0, blogentryTxt );
077
078        if( blogName.equals( engine.getFrontPage() ) )
079        {
080            blogName = null;
081        }
082
083        try
084        {
085            XmlRpcClient xmlrpc = new XmlRpcClient(m_pingURL);
086            Vector<String> params = new Vector<String>();
087            params.addElement( "The Butt Ugly Weblog" ); // FIXME: Must be settable
088            params.addElement( engine.getURL( WikiContext.VIEW, blogName, null, true ) );
089
090            if( log.isDebugEnabled() )
091                log.debug("Pinging weblogs.com with URL: "+engine.getURL( WikiContext.VIEW, blogName, null, true ));
092
093            xmlrpc.executeAsync("weblogUpdates.ping", params, 
094                                new AsyncCallback() 
095                                {
096                                    public void handleError( Exception ex,
097                                                             URL url,
098                                                             String method )
099                                    {
100                                        log.error("Unable to execute weblogs.com ping to URL: "+url.toString(),ex);
101                                    }
102
103                                    public void handleResult( Object result,
104                                                              URL url,
105                                                              String method )
106                                    {
107                                        @SuppressWarnings("unchecked")
108                                        Hashtable< String, Object > res = (Hashtable < String, Object > ) result;
109
110                                        Boolean flerror = (Boolean)res.get("flerror");
111                                        String  msg     = (String)res.get("message");
112
113                                        if( flerror.booleanValue() )
114                                        {
115                                            log.error("Failed to ping: "+msg);
116                                        }
117
118                                        log.info("Weblogs.com has been pinged.");
119                                    }
120                                }
121                                );
122        }
123        catch( MalformedURLException e )
124        {
125            log.error("Malformed URL",e);
126        }
127    }
128}