    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.logging.log4j.LogManager;
    022import org.apache.logging.log4j.Logger;
    023import org.apache.wiki.api.core.Context;
    024import org.apache.wiki.api.core.ContextEnum;
    025import org.apache.wiki.api.core.Engine;
    026import org.apache.wiki.api.filters.BasePageFilter;
    027import org.apache.xmlrpc.AsyncCallback;
    028import org.apache.xmlrpc.XmlRpcClient;
    029
    030import java.net.MalformedURLException;
    031import java.net.URL;
    032import java.util.Hashtable;
    033import java.util.Properties;
    034import java.util.Vector;
    035
    036/**
    037 *  A very dumb class that pings weblogs.com on each save.  INTERNAL USE ONLY SO FAR! Look, but don't use as-is.
    038 */
    039// FIXME: Needs to figure out when only weblogs have been saved.
    040// FIXME: rpc endpoint must be configurable
    041// FIXME: Should really be settable per-page.
    042// FIXME: Weblog name has been set to stone
    043public class PingWeblogsComFilter extends BasePageFilter {
    044
    045    private static final Logger LOG = LogManager.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    @Override
    058    public void initialize( final Engine engine, final Properties props ) {
    059        m_pingURL = props.getProperty( PROP_PINGURL, "http://rpc.weblogs.com/RPC2" );
    060    }
    061
    062    /**
    063     *  {@inheritDoc}
    064     */
    065    @Override
    066    public void postSave( final Context context, final String pagecontent ) {
    067        String blogName = context.getPage().getName();
    068        final Engine engine   = context.getEngine();
    069
    070        final int blogentryTxt = blogName.indexOf("_blogentry_");
    071        if( blogentryTxt == -1 ) {
    072            return; // This is not a weblog entry.
    073        }
    074
    075        blogName = blogName.substring( 0, blogentryTxt );
    076
    077        if( blogName.equals( engine.getFrontPage() ) ) {
    078            blogName = null;
    079        }
    080
    081        try {
    082            final XmlRpcClient xmlrpc = new XmlRpcClient(m_pingURL);
    083            final Vector< String > params = new Vector<>();
    084            params.addElement( "The Butt Ugly Weblog" ); // FIXME: Must be settable
    085            params.addElement( engine.getURL( ContextEnum.PAGE_VIEW.getRequestContext(), blogName, null ) );
    086
    087            LOG.debug( "Pinging weblogs.com with URL: {}", engine.getURL( ContextEnum.PAGE_VIEW.getRequestContext(), blogName, null ) );
    088
    089            xmlrpc.executeAsync("weblogUpdates.ping", params,
    090                                new AsyncCallback() {
    091                                    @Override
    092                                    public void handleError( final Exception ex, final URL url, final String method ) {
    093                                        LOG.error( "Unable to execute weblogs.com ping to URL: " + url.toString(), ex );
    094                                    }
    095
    096                                    @Override
    097                                    public void handleResult( final Object result, final URL url, final String method ) {
    098                                        @SuppressWarnings("unchecked")
    099                                        final Hashtable< String, Object > res = (Hashtable < String, Object > ) result;
    100
    101                                        final Boolean flerror = ( Boolean )res.get( "flerror" );
    102                                        final String  msg     = ( String )res.get( "message" );
    103
    104                                        if( flerror ) {
    105                                            LOG.error( "Failed to ping: " + msg );
    106                                        }
    107
    108                                        LOG.info( "Weblogs.com has been pinged." );
    109                                    }
    110                                }
    111                                );
    112        } catch( final MalformedURLException e ) {
    113            LOG.error("Malformed URL",e);
    114        }
    115    }
    116
    117}