AbstractAPIService.hpp   AbstractAPIService.hpp 
skipping to change at line 23 skipping to change at line 23
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA. * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
*/ */
#ifndef ABSTRACTAPISERVICE_HPP_ #ifndef ABSTRACTAPISERVICE_HPP_
#define ABSTRACTAPISERVICE_HPP_ #define ABSTRACTAPISERVICE_HPP_
#include <QByteArray> #include "RemoteControlServiceInterface.hpp"
#include <QMap>
#include <QObject>
//Uncomment to force each service to run in the HTTP handling threads thems
elves, like the initial versions
//#define FORCE_THREADED_SERVICES
class HttpResponse;
class APIController;
//! \addtogroup remoteControl //! \addtogroup remoteControl
//! @{ //! @{
//! Thread-safe version of HttpResponse that can be passed around through Q //! Abstract base class for all RemoteControlServiceInterface implementatio
MetaObject::invokeMethod. ns which are provided by the \ref remoteControl plugin directly.
//! It contains the data that will be sent back to the client in the HTTP t class AbstractAPIService : public QObject, public RemoteControlServiceInter
hread, when control returns to the APIController. face
struct APIServiceResponse
{
public:
//! Constructs an invalid response
APIServiceResponse() : status(-1)
{
}
//! Sets a specific HTTP header to the specified value
void setHeader(const QByteArray& name, const QByteArray& val);
//! Shortcut for int header values
void setHeader(const QByteArray& name, const int val);
//! Sets the HTTP status type and status text
void setStatus(int status, const QByteArray& text);
//! Replaces the current return data
void setData(const QByteArray& data);
//! Appends to the current return data
void appendData(const QByteArray& data);
//! Sets the HTTP status to 400, and sets the response data to the m
essage
void writeRequestError(const QByteArray& msg);
//! Sets the Content-Type to "application/json" and serializes the g
iven document
//! into JSON text format
void writeJSON(const QJsonDocument& doc);
private:
int status;
QByteArray statusText;
QMap<QByteArray,QByteArray> headers;
QByteArray responseData;
//! Applies the data in this APIServiceResponse onto the HttpRespons
e
//! Must be called in the HTTP thread, done by APIController
void applyResponse(HttpResponse* response) const;
static int metaTypeId;
static int parametersMetaTypeId;
friend class APIController;
};
//! Defines the HTTP request parameters for the service
typedef QMultiMap<QByteArray, QByteArray> APIParameters;
Q_DECLARE_METATYPE(APIServiceResponse)
Q_DECLARE_METATYPE(APIParameters)
//! Abstract base class for all \ref remoteControl service implementations.
Different implementations of this class are mapped to
//! different HTTP request paths. For each API request, the APIController f
inds out which service to use, and calls its get() or post() method.
class AbstractAPIService : public QObject
{ {
Q_OBJECT Q_OBJECT
//Probably not really necessary to do this here but it probably won'
t hurt either
Q_INTERFACES(RemoteControlServiceInterface)
public: public:
//! Abstract constructor. The service name is used by the APIControl //! Only calls QObject constructor
ler for request path mapping. AbstractAPIService(QObject* parent = Q_NULLPTR) : QObject(parent)
AbstractAPIService(const QByteArray& serviceName, QObject* parent =
0) : QObject(parent), m_serviceName(serviceName)
{ {
} }
virtual ~AbstractAPIService() { } // Provides a default implementation which returns false.
virtual bool isThreadSafe() const Q_DECL_OVERRIDE;
//! Returns the service name, used for request path mapping by the A
PIController
QByteArray serviceName() { return m_serviceName; }
//! Return true if the service can safely be run in the HTTP handler
thread,
//! instead of having to queue it into the Stellarium main thread.
//! Default implementation returns false, i.e. no special threading
precautions have to be used
//! in the get() and post() implementation.
//! @warning If the macro \c FORCE_THREADED_SERVICES is set, all ser
vices will be run
//! in the HTTP threads for testing, and this method will be ignored
.
virtual bool supportsThreadedOperation() const;
//! Called in the main thread each frame. Default implementation doe s nothing. //! Called in the main thread each frame. Default implementation doe s nothing.
//! Can be used for ongoing actions, for example movement control. //! Can be used for ongoing actions, for example movement control.
virtual void update(double deltaTime); virtual void update(double deltaTime) Q_DECL_OVERRIDE;
//! Provides a default implementation which returns an error message
//! Wrapper around getImpl(), constructs an APIServiceResponse objec .
t for the response and passes it on virtual void get(const QByteArray &operation, const APIParameters &p
//! @note The thread this is called in depends on the supportThreade arameters, APIServiceResponse& response) Q_DECL_OVERRIDE;
dOperation() return value //! Provides a default implementation which returns an error message
Q_INVOKABLE APIServiceResponse get(const QByteArray &operation, cons .
t APIParameters &parameters); virtual void post(const QByteArray &operation, const APIParameters &
//! Wrapper around postImpl(), constructs an APIServiceResponse obje parameters, const QByteArray& data, APIServiceResponse& response) Q_DECL_OV
ct for the response and passes it on ERRIDE;
//! @note The thread this is called in depends on the supportThreade
dOperation() return value
Q_INVOKABLE APIServiceResponse post(const QByteArray &operation, con
st APIParameters &parameters, const QByteArray& data);
protected: protected:
//! Subclasses should implement this to define reactions to HTTP GET
requests.
//! GET requests generally should only query data or program state,
and not change it.
//! If there is an error with the request, use APIServiceResponse::w
riteRequestError to notify the client.
//! @param operation The operation string of the request (i.e. the p
art of the request URL after the service name, without parameters)
//! @param parameters The extracted service parameters (extracted fr
om the URL)
//! @param response The response object, write your response into th
is
//! @note The thread this is called in depends on the supportThreade
dOperation() return value
virtual void getImpl(const QByteArray& operation, const APIParameter
s& parameters, APIServiceResponse& response);
//! Subclasses should implement this to define reactions to HTTP POS
T requests.
//! POST requests generally should change data or perform some actio
n.
//! If there is an error with the request, use APIServiceResponse::w
riteRequestError to notify the client.
//! @param operation The operation string of the request (i.e. the p
art of the request URL after the service name, without parameters)
//! @param parameters The extracted service parameters (extracted fr
om the URL, and form data, if applicable)
//! @param data The unmodified data as sent from the client
//! @param response The response object, write your response into th
is
//! @note The thread this is called in depends on the supportThreade
dOperation() return value
virtual void postImpl(const QByteArray& operation, const APIParamete
rs& parameters, const QByteArray& data, APIServiceResponse& response);
//! This defines the connection type QMetaObject::invokeMethod has t o use inside a service: either Qt::DirectConnection for main thread handlin g, or //! This defines the connection type QMetaObject::invokeMethod has t o use inside a service: either Qt::DirectConnection for main thread handlin g, or
//! Qt::BlockingQueuedConnection for HTTP thread handling //! Qt::BlockingQueuedConnection for HTTP thread handling
static const Qt::ConnectionType SERVICE_DEFAULT_INVOKETYPE; static const Qt::ConnectionType SERVICE_DEFAULT_INVOKETYPE;
//! Because the HTML descriptions in Stellarium are often not compat
ible
//! with "clean" HTML5 which is used for the main interface,
//! this method can be used to explicitely set the doctype
//! to 4.01 transitional for better results, and include the stylesh
eet
//! \c iframestyle.css
//! @param text The text to wrap with HTML document tags
//! @param title The title of the page
QString wrapHtml(QString& text, const QString& title) const;
private:
QByteArray m_serviceName;
}; };
//! @} //! @}
#endif #endif
 End of changes. 9 change blocks. 
155 lines changed or deleted 22 lines changed or added

This html diff was produced by rfcdiff 1.41. The latest version is available from http://tools.ietf.org/tools/rfcdiff/