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

Generated on Thu Jul 1 06:08:42 2010 for Second Life Viewer by  doxygen 1.4.7