diff Core/RestApi/RestApiHierarchy.h @ 978:ce3106e5843f

refactoring
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 30 Jun 2014 16:04:58 +0200
parents c550e99c452b
children 6e7e5ed91c2d
line wrap: on
line diff
--- a/Core/RestApi/RestApiHierarchy.h	Mon Jun 30 14:53:18 2014 +0200
+++ b/Core/RestApi/RestApiHierarchy.h	Mon Jun 30 16:04:58 2014 +0200
@@ -37,12 +37,14 @@
 #include "RestApiPutCall.h"
 #include "RestApiDeleteCall.h"
 
+#include <set>
+
 namespace Orthanc
 {
-  class RestApiHierarchy
+  class RestApiHierarchy : public boost::noncopyable
   {
-  private:
-    class Handlers
+  public:
+    class Resource : public boost::noncopyable
     {
     private:
       RestApiGetCall::Handler     getHandler_;
@@ -51,18 +53,10 @@
       RestApiDeleteCall::Handler  deleteHandler_;
 
     public:
-      Handlers();
+      Resource();
 
       bool HasHandler(HttpMethod method) const;
 
-      RestApiGetCall::Handler GetGetHandler() const;
-
-      RestApiPutCall::Handler GetPutHandler() const;
-
-      RestApiPostCall::Handler GetPostHandler() const;
-
-      RestApiDeleteCall::Handler GetDeleteHandler() const;
-
       void Register(RestApiGetCall::Handler handler)
       {
         getHandler_ = handler;
@@ -84,21 +78,38 @@
       }
 
       bool IsEmpty() const;
+
+      bool Handle(RestApiGetCall& call) const;
+
+      bool Handle(RestApiPutCall& call) const;
+
+      bool Handle(RestApiPostCall& call) const;
+
+      bool Handle(RestApiDeleteCall& call) const;
     };
 
 
+    class IVisitor : public boost::noncopyable
+    {
+    public:
+      virtual ~IVisitor()
+      {
+      }
+
+      virtual bool Visit(const Resource& resource,
+                         const UriComponents& uri,
+                         const HttpHandler::Arguments& components,
+                         const UriComponents& trailing) = 0;
+    };
+
+
+  private:
     typedef std::map<std::string, RestApiHierarchy*>  Children;
-    typedef bool (*ResourceCallback) (Handlers& handlers,
-                                      const UriComponents& uri,
-                                      const HttpHandler::Arguments& components,
-                                      const UriComponents& trailing,
-                                      void* call);
 
-    Handlers  handlers_;
+    Resource  handlers_;
     Children  children_;
     Children  wildcardChildren_;
-    Handlers  universalHandlers_;
-
+    Resource  universalHandlers_;
 
     static RestApiHierarchy& AddChild(Children& children,
                                       const std::string& name);
@@ -110,40 +121,16 @@
                           Handler handler,
                           size_t level);
 
-    bool LookupHandler(HttpHandler::Arguments& components,
-                       const UriComponents& uri,
-                       ResourceCallback callback,
-                       size_t level,
-                       void* call);
+    bool CanGenerateDirectory() const;
+
+    bool LookupResource(HttpHandler::Arguments& components,
+                        const UriComponents& uri,
+                        IVisitor& visitor,
+                        size_t level);
 
     bool GetDirectory(Json::Value& result,
                       const UriComponents& uri,
                       size_t level);
-                     
-    static bool GetCallback(Handlers& handlers,
-                            const UriComponents& uri,
-                            const HttpHandler::Arguments& components,
-                            const UriComponents& trailing,
-                            void* call);
-
-    static bool PostCallback(Handlers& handlers,
-                             const UriComponents& uri,
-                             const HttpHandler::Arguments& components,
-                             const UriComponents& trailing,
-                             void* call);
-
-    static bool PutCallback(Handlers& handlers,
-                            const UriComponents& uri,
-                            const HttpHandler::Arguments& components,
-                            const UriComponents& trailing,
-                            void* call);
-
-    static bool DeleteCallback(Handlers& handlers,
-                               const UriComponents& uri,
-                               const HttpHandler::Arguments& components,
-                               const UriComponents& trailing,
-                               void* call);
-                       
 
   public:
     ~RestApiHierarchy();
@@ -168,16 +155,10 @@
       return GetDirectory(result, uri, 0);
     }
 
-    bool Handle(RestApiGetCall& call,
-                const UriComponents& uri);
-
-    bool Handle(RestApiPutCall& call,
-                const UriComponents& uri);
+    bool LookupResource(const UriComponents& uri,
+                        IVisitor& visitor);
 
-    bool Handle(RestApiPostCall& call,
-                const UriComponents& uri);
-
-    bool Handle(RestApiDeleteCall& call,
-                const UriComponents& uri);
+    void GetAcceptedMethods(std::set<HttpMethod>& methods,
+                            const UriComponents& uri);
   };
 }