UniSet  2.6.0
UHttpRequestHandler.h
00001 #ifndef DISABLE_REST_API
00002 /*
00003  * Copyright (c) 2015 Pavel Vainerman.
00004  *
00005  * This program is free software: you can redistribute it and/or modify
00006  * it under the terms of the GNU Lesser General Public License as
00007  * published by the Free Software Foundation, version 2.1.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00012  * Lesser General Lesser Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public License
00015  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 // -------------------------------------------------------------------------
00018 #ifndef UHttpRequesrHandler_H_
00019 #define UHttpRequesrHandler_H_
00020 // -------------------------------------------------------------------------
00021 #include <memory>
00022 #include <Poco/Net/HTTPRequestHandler.h>
00023 #include <Poco/Net/HTTPRequestHandlerFactory.h>
00024 #include <Poco/Net/HTTPServerRequest.h>
00025 #include <Poco/Net/HTTPServerResponse.h>
00026 #include <Poco/URI.h>
00027 #include <Poco/JSON/Object.h>
00028 #include "ujson.h"
00029 #include "DebugStream.h"
00030 // -------------------------------------------------------------------------
00072 // -------------------------------------------------------------------------
00073 namespace uniset
00074 {
00075 namespace UHttp
00076 {
00077 // текущая версия API
00078 const std::string UHTTP_API_VERSION = "v0";
00079 
00081 class IHttpRequest
00082 {
00083     public:
00084         IHttpRequest() {}
00085         virtual ~IHttpRequest() {}
00086 
00087         // throw SystemError
00088         virtual Poco::JSON::Object::Ptr httpGet( const Poco::URI::QueryParameters& p ) = 0;
00089         virtual Poco::JSON::Object::Ptr httpHelp( const Poco::URI::QueryParameters& p ) = 0;
00090 
00091         // не обязательная функция.
00092         virtual Poco::JSON::Object::Ptr httpRequest( const std::string& req, const Poco::URI::QueryParameters& p );
00093 };
00094 // -------------------------------------------------------------------------
00096 class IHttpRequestRegistry
00097 {
00098     public:
00099         IHttpRequestRegistry() {}
00100         virtual ~IHttpRequestRegistry() {}
00101 
00102         // throw SystemError, NameNotFound
00103         virtual Poco::JSON::Object::Ptr httpGetByName( const std::string& name, const Poco::URI::QueryParameters& p ) = 0;
00104 
00105         // throw SystemError
00106         virtual Poco::JSON::Array::Ptr httpGetObjectsList( const Poco::URI::QueryParameters& p ) = 0;
00107         virtual Poco::JSON::Object::Ptr httpHelpByName( const std::string& name, const Poco::URI::QueryParameters& p ) = 0;
00108         virtual Poco::JSON::Object::Ptr httpRequestByName( const std::string& name, const std::string& req, const Poco::URI::QueryParameters& p ) = 0;
00109 };
00110 
00111 // -------------------------------------------------------------------------
00112 class UHttpRequestHandler:
00113     public Poco::Net::HTTPRequestHandler
00114 {
00115     public:
00116         UHttpRequestHandler( std::shared_ptr<IHttpRequestRegistry> _registry );
00117 
00118         virtual void handleRequest( Poco::Net::HTTPServerRequest& req, Poco::Net::HTTPServerResponse& resp ) override;
00119 
00120     private:
00121 
00122         std::shared_ptr<IHttpRequestRegistry> registry;
00123         std::shared_ptr<DebugStream> log;
00124 };
00125 // -------------------------------------------------------------------------
00126 class UHttpRequestHandlerFactory:
00127     public Poco::Net::HTTPRequestHandlerFactory
00128 {
00129     public:
00130 
00131         UHttpRequestHandlerFactory( std::shared_ptr<IHttpRequestRegistry>& _registry );
00132 
00133         virtual Poco::Net::HTTPRequestHandler* createRequestHandler( const Poco::Net::HTTPServerRequest& ) override;
00134 
00135     private:
00136         std::shared_ptr<IHttpRequestRegistry> registry;
00137 };
00138 }
00139 // -------------------------------------------------------------------------
00140 } // end of uniset namespace
00141 // -------------------------------------------------------------------------
00142 #endif // UHttpRequesrHandler_H_
00143 // -------------------------------------------------------------------------
00144 #endif