Author: jalkanen
Date: Mon Jun  9 13:01:48 2008
New Revision: 665864

URL: http://svn.apache.org/viewvc?rev=665864&view=rev
Log:
JSPWIKI-14: checkPermissions is now functional, though untested.

Modified:
    incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/xmlrpc/MetaWeblogHandler.java

Modified: 
incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/xmlrpc/MetaWeblogHandler.java
URL: 
http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/xmlrpc/MetaWeblogHandler.java?rev=665864&r1=665863&r2=665864&view=diff
==============================================================================
--- incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/xmlrpc/MetaWeblogHandler.java 
(original)
+++ incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/xmlrpc/MetaWeblogHandler.java 
Mon Jun  9 13:01:48 2008
@@ -20,17 +20,25 @@
  */
 package com.ecyrd.jspwiki.xmlrpc;
 
-import java.io.*;
-import com.ecyrd.jspwiki.*;
+import java.io.ByteArrayInputStream;
+import java.util.*;
+
+import org.apache.log4j.Logger;
+import org.apache.xmlrpc.XmlRpcException;
+
+import com.ecyrd.jspwiki.PageTimeComparator;
+import com.ecyrd.jspwiki.WikiContext;
+import com.ecyrd.jspwiki.WikiEngine;
+import com.ecyrd.jspwiki.WikiPage;
 import com.ecyrd.jspwiki.attachment.Attachment;
 import com.ecyrd.jspwiki.attachment.AttachmentManager;
+import com.ecyrd.jspwiki.auth.AuthenticationManager;
+import com.ecyrd.jspwiki.auth.AuthorizationManager;
+import com.ecyrd.jspwiki.auth.WikiSecurityException;
+import com.ecyrd.jspwiki.auth.permissions.PermissionFactory;
 import com.ecyrd.jspwiki.plugin.WeblogEntryPlugin;
 import com.ecyrd.jspwiki.plugin.WeblogPlugin;
 import com.ecyrd.jspwiki.providers.ProviderException;
-import java.util.*;
-
-import org.apache.log4j.Logger;
-import org.apache.xmlrpc.XmlRpcException;
 
 /**
  *  Provides handlers for all RPC routines of the MetaWeblog API.
@@ -47,13 +55,16 @@
 public class MetaWeblogHandler
     implements WikiRPCHandler
 {
-    Logger log = Logger.getLogger( MetaWeblogHandler.class ); 
+    private static Logger log = Logger.getLogger( MetaWeblogHandler.class ); 
 
-    private WikiEngine m_engine;
+    private WikiContext m_context;
     
+    /**
+     *  [EMAIL PROTECTED]
+     */
     public void initialize( WikiContext context )
     {
-        m_engine = context.getEngine();
+        m_context = context;
     }
 
     /**
@@ -65,58 +76,52 @@
      *
      *  @throw XmlRpcException with the correct error message, if auth fails.
      */
-    private void checkPermissions( WikiPage page, 
+    private void checkPermissions( WikiPage page,
                                    String username,
                                    String password,
                                    String permission )
         throws XmlRpcException
     {
-        return;
-        /*
-        AuthorizationManager mgr = m_engine.getAuthorizationManager();
-        UserProfile currentUser  = m_engine.getUserManager().getUserProfile( 
username );
-        currentUser.setPassword( password );
-
-        WikiAuthenticator auth = m_engine.getUserManager().getAuthenticator();
-
-        if( auth != null )
+        try
         {
-            boolean isValid = auth.authenticate( currentUser );
+            AuthenticationManager amm = 
m_context.getEngine().getAuthenticationManager();
+            AuthorizationManager mgr = 
m_context.getEngine().getAuthorizationManager();
         
-            if( isValid )
+            if( amm.login( m_context.getWikiSession(), username, password ) )
             {
-                if( !mgr.checkPermission( page,
-                                          currentUser,
-                                          permission ) )
+                if( !mgr.checkPermission( m_context.getWikiSession(), 
PermissionFactory.getPagePermission( page, permission ) ))
                 {
-                    return;
-                }
-                else
-                {
-                    String msg = "Insufficient permissions to do 
"+permission+" on "+page.getName();
-                    log.error( msg );
-                    throw new XmlRpcException(0, msg );
-                }
+                    throw new XmlRpcException( 1, "No permission" );
+                }   
             }
-            else 
+            else
             {
-                log.error( "Username '"+username+"' or password not valid." );
-                throw new XmlRpcException(0, "Password or username not 
valid.");
+                throw new XmlRpcException( 1, "Unknown login" );
             }
         }
-        */
+        catch( WikiSecurityException e )
+        {
+            throw new XmlRpcException( 1, e.getMessage(), e );
+        }
+        return;
     }
 
     /**
      *  JSPWiki does not support categories, therefore JSPWiki
      *  always returns an empty list for categories.
+     *  
+     *  @param blogid The id of the blog.
+     *  @param username The username to use
+     *  @param password The password
+     *  @throws XmlRpcException If something goes wrong
+     *  @return An empty hashtable.
      */
     public Hashtable getCategories( String blogid,
                                     String username,
                                     String password )
         throws XmlRpcException
     {
-        WikiPage page = m_engine.getPage( blogid );
+        WikiPage page = m_context.getEngine().getPage( blogid );
 
         checkPermissions( page, username, password, "view" );
 
@@ -127,10 +132,10 @@
 
     private String getURL( String page )
     {
-        return m_engine.getURL( WikiContext.VIEW,
-                                page,
-                                null,
-                                true ); // Force absolute urls
+        return m_context.getEngine().getURL( WikiContext.VIEW,
+                                             page,
+                                             null,
+                                             true ); // Force absolute urls
     }
 
     /**
@@ -139,11 +144,11 @@
      *  @param page The actual entry page
      *  @return A metaWeblog entry struct.
      */
-    private Hashtable makeEntry( WikiPage page )
+    private Hashtable<String,Object> makeEntry( WikiPage page )
     {
-        Hashtable ht = new Hashtable();
+        Hashtable<String, Object> ht = new Hashtable<String, Object>();
 
-        WikiPage firstVersion = m_engine.getPage( page.getName(), 1 );
+        WikiPage firstVersion = m_context.getEngine().getPage( page.getName(), 
1 );
 
         ht.put("dateCreated", firstVersion.getLastModified());
         ht.put("link", getURL(page.getName()));
@@ -151,7 +156,7 @@
         ht.put("postid", page.getName());
         ht.put("userid", page.getAuthor());
 
-        String pageText = m_engine.getText(page.getName());
+        String pageText = m_context.getEngine().getText(page.getName());
         String title = "";
         int firstLine = pageText.indexOf('\n');
 
@@ -173,22 +178,30 @@
 
     /**
      *  Returns a list of the recent posts to this weblog.
+     *  
+     *  @param blogid The id of the blog.
+     *  @param username The username to use
+     *  @param password The password
+     *  @param numberOfPosts How many posts to find
+     *  @throws XmlRpcException If something goes wrong
+     *  @return As per MetaweblogAPI specification
      */
 
     // FIXME: The implementation is suboptimal, as it
     //        goes through all of the blog entries.
 
+    @SuppressWarnings("unchecked")
     public Hashtable getRecentPosts( String blogid,
                                      String username,
                                      String password,
                                      int numberOfPosts)
         throws XmlRpcException
     {
-        Hashtable result = new Hashtable();
+        Hashtable<String, Hashtable<String, Object>> result = new 
Hashtable<String, Hashtable<String, Object>>();
 
         log.info( "metaWeblog.getRecentPosts() called");
 
-        WikiPage page = m_engine.getPage( blogid );
+        WikiPage page = m_context.getEngine().getPage( blogid );
 
         checkPermissions( page, username, password, "view" );
 
@@ -196,10 +209,10 @@
         {
             WeblogPlugin plugin = new WeblogPlugin();
 
-            List changed = plugin.findBlogEntries(m_engine.getPageManager(), 
-                                                  blogid,
-                                                  new Date(0L),
-                                                  new Date());
+            List<WikiPage> changed = 
plugin.findBlogEntries(m_context.getEngine().getPageManager(), 
+                                                            blogid,
+                                                            new Date(0L),
+                                                            new Date());
 
             Collections.sort( changed, new PageTimeComparator() );
 
@@ -224,7 +237,14 @@
 
     /**
      *  Adds a new post to the blog.
+     *  
+     *  @param blogid The id of the blog.
+     *  @param username The username to use
+     *  @param password The password
+     *  @param content As per Metaweblogapi contract
      *  @param publish This parameter is ignored for JSPWiki.
+     *  @return Returns an empty string 
+     *  @throws XmlRpcException If something goes wrong
      */
     public String newPost( String blogid,
                            String username,
@@ -234,20 +254,21 @@
         throws XmlRpcException
     {
         log.info("metaWeblog.newPost() called");
+        WikiEngine engine = m_context.getEngine();
         
-        WikiPage page = m_engine.getPage( blogid );
-        checkPermissions( page, username, password, "create" );
+        WikiPage page = engine.getPage( blogid );
+        checkPermissions( page, username, password, "createPages" );
 
         try
         {
             WeblogEntryPlugin plugin = new WeblogEntryPlugin();
 
-            String pageName = plugin.getNewEntryPage( m_engine, blogid );
+            String pageName = plugin.getNewEntryPage( engine, blogid );
 
-            WikiPage entryPage = new WikiPage( m_engine, pageName );
+            WikiPage entryPage = new WikiPage( engine, pageName );
             entryPage.setAuthor( username );
 
-            WikiContext context = new WikiContext( m_engine, entryPage );
+            WikiContext context = new WikiContext( engine, entryPage );
 
             StringBuffer text = new StringBuffer();
             text.append( "!"+content.get("title") );
@@ -256,7 +277,7 @@
 
             log.debug("Writing entry: "+text);
 
-            m_engine.saveText( context, text.toString() );
+            engine.saveText( context, text.toString() );
         }
         catch( Exception e )
         {
@@ -271,6 +292,14 @@
      *  Creates an attachment and adds it to the blog.  The attachment
      *  is created into the main blog page, not the actual post page,
      *  because we do not know it at this point.
+     *  
+     *  @param blogid The id of the blog.
+     *  @param username The username to use
+     *  @param password The password
+     *  @param content As per the MetaweblogAPI contract
+     *  @return As per the MetaweblogAPI contract 
+     *  @throws XmlRpcException If something goes wrong
+     *  
      */
     public Hashtable newMediaObject( String blogid, 
                                      String username,
@@ -278,25 +307,26 @@
                                      Hashtable content )
         throws XmlRpcException
     {
+        WikiEngine engine = m_context.getEngine();
         String url = "";
 
         log.info("metaWeblog.newMediaObject() called");
 
-        WikiPage page = m_engine.getPage( blogid );
+        WikiPage page = engine.getPage( blogid );
         checkPermissions( page, username, password, "upload" );
 
         String name = (String) content.get( "name" );
         byte[] data = (byte[]) content.get( "bits" );
 
-        AttachmentManager attmgr = m_engine.getAttachmentManager();
+        AttachmentManager attmgr = engine.getAttachmentManager();
 
         try
         {
-            Attachment att = new Attachment( m_engine, blogid, name );
+            Attachment att = new Attachment( engine, blogid, name );
             att.setAuthor( username );
             attmgr.storeAttachment( att, new ByteArrayInputStream( data ) );
 
-            url = m_engine.getURL( WikiContext.ATTACH, att.getName(), null, 
true );
+            url = engine.getURL( WikiContext.ATTACH, att.getName(), null, true 
);
         }
         catch( Exception e )
         {
@@ -304,7 +334,7 @@
             throw new XmlRpcException( 0, "Failed to upload media object: 
"+e.getMessage() );
         }
 
-        Hashtable result = new Hashtable();
+        Hashtable<String, Object> result = new Hashtable<String, Object>();
         result.put("url", url);
 
         return result;
@@ -323,10 +353,11 @@
                       boolean publish )
         throws XmlRpcException
     {
+        WikiEngine engine = m_context.getEngine();
         log.info("metaWeblog.editPost("+postid+") called");
 
         // FIXME: Is postid correct?  Should we determine it from the page 
name?
-        WikiPage page = m_engine.getPage( postid );
+        WikiPage page = engine.getPage( postid );
         checkPermissions( page, username, password, "edit" );
 
         try
@@ -334,7 +365,7 @@
             WikiPage entryPage = (WikiPage)page.clone();
             entryPage.setAuthor( username );
 
-            WikiContext context = new WikiContext( m_engine, entryPage );
+            WikiContext context = new WikiContext( engine, entryPage );
 
             StringBuffer text = new StringBuffer();
             text.append( "!"+content.get("title") );
@@ -343,7 +374,7 @@
 
             log.debug("Updating entry: "+text);
 
-            m_engine.saveText( context, text.toString() );
+            engine.saveText( context, text.toString() );
         }
         catch( Exception e )
         {
@@ -365,7 +396,7 @@
     {
         String wikiname = "FIXME";
 
-        WikiPage page = m_engine.getPage( wikiname );
+        WikiPage page = m_context.getEngine().getPage( wikiname );
 
         checkPermissions( page, username, password, "view" );
 


Reply via email to