llhttpnode.h

Go to the documentation of this file.
00001 
00032 #ifndef LL_LLHTTPNODE_H
00033 #define LL_LLHTTPNODE_H
00034 
00035 #include "llmemory.h"
00036 #include "llsd.h"
00037 
00038 class LLChainIOFactory;
00039 
00040 
00066 class LLHTTPNode
00067 {
00068 public:
00069         LLHTTPNode();
00070         virtual ~LLHTTPNode();
00071 
00084 public: 
00085                 virtual LLSD get() const;
00086                 virtual LLSD put(const LLSD& input) const;
00087                 virtual LLSD post(const LLSD& input) const;
00088 
00089                 virtual LLSD del(const LLSD& context) const;
00090 
00091                 class Response : public LLRefCount
00092                 {
00093                 protected:
00094                         virtual ~Response();
00095 
00096                 public:
00097                         virtual void result(const LLSD&) = 0;
00098                         virtual void status(S32 code, const std::string& message) = 0;
00099 
00100                         void status(S32 code);
00101                         void notFound(const std::string& message);
00102                         void notFound();
00103                         void methodNotAllowed();
00104                 };
00105 
00106                 typedef LLPointer<Response> ResponsePtr;
00107 
00108                 virtual void get(ResponsePtr, const LLSD& context) const;
00109                 virtual void put(ResponsePtr, const LLSD& context, const LLSD& input) const;
00110                 virtual void post(ResponsePtr, const LLSD& context, const LLSD& input) const;
00111                 virtual void del(ResponsePtr, const LLSD& context) const;
00113         
00114 
00125 public:
00126         virtual LLHTTPNode* getChild(const std::string& name, LLSD& context) const;
00131         virtual bool handles(const LLSD& remainder, LLSD& context) const;
00136         virtual bool validate(const std::string& name, LLSD& context) const;
00148         const LLHTTPNode* traverse(const std::string& path, LLSD& context) const;
00153  
00165         
00166         virtual void addNode(const std::string& path, LLHTTPNode* nodeToAdd);
00167 
00168         LLSD allNodePaths() const;
00170 
00171         const LLHTTPNode* rootNode() const;
00172         const LLHTTPNode* findNode(const std::string& name) const;
00173 
00175 
00176         /* @name Description system
00177                 The Description object contains information about a service.
00178                 All subclasses of LLHTTPNode should override describe() and use
00179                 the methods of the Description class to set the various properties.
00180          */
00182                 class Description
00183                 {
00184                 public:
00185                         void shortInfo(const std::string& s){ mInfo["description"] = s; }
00186                         void longInfo(const std::string& s)     { mInfo["details"] = s; }
00187 
00188                         // Call this method when the service supports the specified verb.
00189                         void getAPI() { mInfo["api"].append("GET"); }
00190                         void putAPI() { mInfo["api"].append("PUT");  }
00191                         void postAPI() { mInfo["api"].append("POST"); }
00192                         void delAPI() { mInfo["api"].append("DELETE"); }
00193 
00194                         void input(const std::string& s)        { mInfo["input"] = s; }
00195                         void output(const std::string& s)       { mInfo["output"] = s; }
00196                         void source(const char* f, int l)       { mInfo["__file__"] = f;
00197                                                                                                   mInfo["__line__"] = l; }
00198                         
00199                         LLSD getInfo() const { return mInfo; }
00200                         
00201                 private:
00202                         LLSD mInfo;
00203                 };
00204         
00205         virtual void describe(Description&) const;
00206         
00208         
00209         
00210         virtual const LLChainIOFactory* getProtocolHandler() const;
00220 private:
00221         class Impl;
00222         Impl& impl;
00223 };
00224 
00225 
00226 
00227 class LLSimpleResponse : public LLHTTPNode::Response
00228 {
00229 public:
00230         static LLPointer<LLSimpleResponse> create();
00231         
00232         void result(const LLSD& result);
00233         void status(S32 code, const std::string& message);
00234 
00235         void print(std::ostream& out) const;
00236 
00237         S32 mCode;
00238         std::string mMessage;
00239 
00240 protected:
00241         ~LLSimpleResponse();
00242 
00243 private:
00244         LLSimpleResponse() {;} // Must be accessed through LLPointer.
00245 };
00246 
00247 std::ostream& operator<<(std::ostream& out, const LLSimpleResponse& resp);
00248 
00249 
00250 
00270 
00271 class LLHTTPRegistrar
00272 {
00273 public:
00274         class NodeFactory
00275         {
00276         public:
00277                 virtual ~NodeFactory();
00278                 virtual LLHTTPNode* build() const = 0;
00279         };
00280 
00281         static void buildAllServices(LLHTTPNode& root);
00282 
00283         static void registerFactory(const std::string& path, NodeFactory& factory);
00285 };
00286 
00287 template < class NodeType >
00288 class LLHTTPRegistration
00289 {
00290 public:
00291         LLHTTPRegistration(const std::string& path)
00292         {
00293                 LLHTTPRegistrar::registerFactory(path, mFactory);
00294         }
00295 
00296 private:
00297         class ThisNodeFactory : public LLHTTPRegistrar::NodeFactory
00298         {
00299         public:
00300                 virtual LLHTTPNode* build() const { return new NodeType; }
00301         };
00302         
00303         ThisNodeFactory mFactory;       
00304 };
00305 
00306 template < class NodeType>
00307 class LLHTTPParamRegistration
00308 {
00309 public:
00310         LLHTTPParamRegistration(const std::string& path, LLSD params) :
00311                 mFactory(params)
00312         {
00313                 LLHTTPRegistrar::registerFactory(path, mFactory);
00314         }
00315 
00316 private:
00317         class ThisNodeFactory : public LLHTTPRegistrar::NodeFactory
00318         {
00319         public:
00320                 ThisNodeFactory(LLSD params) : mParams(params) {}
00321                 virtual LLHTTPNode* build() const { return new NodeType(mParams); }
00322         private:
00323                 LLSD mParams;
00324         };
00325         
00326         ThisNodeFactory mFactory;       
00327 };
00328         
00330 
00331 #endif // LL_LLHTTPNODE_H

Generated on Fri May 16 08:32:26 2008 for SecondLife by  doxygen 1.5.5