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     */
019    package org.apache.wiki.xmlrpc;
020    
021    import java.security.Permission;
022    import java.util.*;
023    
024    import org.apache.xmlrpc.AuthenticationFailed;
025    
026    import org.apache.wiki.WikiContext;
027    import org.apache.wiki.WikiEngine;
028    import org.apache.wiki.WikiPage;
029    import org.apache.wiki.auth.AuthorizationManager;
030    import org.apache.wiki.auth.permissions.PagePermission;
031    import org.apache.wiki.auth.permissions.WikiPermission;
032    
033    /**
034     *  Provides definitions for RPC handler routines.
035     *
036     *  @since 1.6.13
037     */
038    
039    public abstract class AbstractRPCHandler
040        implements WikiRPCHandler
041    {
042        /** Error code: no such page. */
043        public static final int ERR_NOPAGE       = 1;
044        public static final int ERR_NOPERMISSION = 2;
045    
046        /**
047         *  Link to a local wiki page.
048         */
049        public static final String LINK_LOCAL    = "local";
050    
051        /**
052         *  Link to an external resource.
053         */
054        public static final String LINK_EXTERNAL = "external";
055    
056        /**
057         *  This is an inlined image.
058         */
059        public static final String LINK_INLINE   = "inline";
060    
061        protected WikiEngine m_engine;
062        protected WikiContext m_context;
063        
064        
065        /**
066         *  This is the currently implemented JSPWiki XML-RPC code revision.
067         */
068        public static final int RPC_VERSION = 1;
069    
070        public void initialize( WikiContext context )
071        {
072            m_context = context;
073            m_engine  = context.getEngine();
074        }
075    
076        protected abstract Hashtable encodeWikiPage( WikiPage p );
077    
078        public Vector getRecentChanges( Date since )
079        {
080            checkPermission( PagePermission.VIEW );
081            Collection pages = m_engine.getRecentChanges();
082            Vector<Hashtable<?, ?>> result    = new Vector<Hashtable<?, ?>>();
083    
084            // Transform UTC into local time.
085            Calendar cal = Calendar.getInstance();
086            cal.setTime( since );
087            cal.add( Calendar.MILLISECOND, cal.get( Calendar.ZONE_OFFSET ) + 
088                      (cal.getTimeZone().inDaylightTime( since ) ? cal.get( Calendar.DST_OFFSET ) : 0 ) );
089    
090            for( Iterator i = pages.iterator(); i.hasNext(); )
091            {
092                WikiPage page = (WikiPage)i.next();
093    
094                if( page.getLastModified().after( cal.getTime() ) )
095                {
096                    result.add( encodeWikiPage( page ) );
097                }
098            }
099    
100            return result;
101        }
102    
103        /**
104         *  Checks whether the current user has permission to perform the RPC action; 
105         *  throws an exception if not allowed by {@link org.apache.wiki.auth.AuthorizationManager}.
106         *  
107         *  @param perm the Permission to check
108         */
109        protected void checkPermission( Permission perm )
110        {
111            AuthorizationManager mgr = m_engine.getAuthorizationManager();
112            
113            if( mgr.checkPermission( m_context.getWikiSession(), perm ) )
114                return;
115            
116            throw new AuthenticationFailed( "You have no access to this resource, o master" );
117        }
118        
119        /**
120         *  Returns the current supported JSPWiki XML-RPC API.
121         */
122        public int getRPCVersionSupported()
123        {
124            checkPermission( WikiPermission.LOGIN );
125            
126            return RPC_VERSION;
127        }
128    }