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