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.xmlrpc;
020
021import java.security.Permission;
022import java.util.*;
023
024import org.apache.xmlrpc.AuthenticationFailed;
025
026import org.apache.wiki.WikiContext;
027import org.apache.wiki.WikiEngine;
028import org.apache.wiki.WikiPage;
029import org.apache.wiki.auth.AuthorizationManager;
030import org.apache.wiki.auth.permissions.PagePermission;
031import org.apache.wiki.auth.permissions.WikiPermission;
032
033/**
034 *  Provides definitions for RPC handler routines.
035 *
036 *  @since 1.6.13
037 */
038
039public 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< WikiPage > 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( WikiPage page : pages )
091        {
092            if( page.getLastModified().after( cal.getTime() ) )
093            {
094                result.add( encodeWikiPage( page ) );
095            }
096        }
097
098        return result;
099    }
100
101    /**
102     *  Checks whether the current user has permission to perform the RPC action;
103     *  throws an exception if not allowed by {@link org.apache.wiki.auth.AuthorizationManager}.
104     *
105     *  @param perm the Permission to check
106     */
107    protected void checkPermission( Permission perm )
108    {
109        AuthorizationManager mgr = m_engine.getAuthorizationManager();
110
111        if( mgr.checkPermission( m_context.getWikiSession(), perm ) )
112            return;
113
114        throw new AuthenticationFailed( "You have no access to this resource, o master" );
115    }
116
117    /**
118     *  Returns the current supported JSPWiki XML-RPC API.
119     */
120    public int getRPCVersionSupported()
121    {
122        checkPermission( WikiPermission.LOGIN );
123
124        return RPC_VERSION;
125    }
126}