Skip to content

Commit

Permalink
[Server][Feature][needs-docs] Server Cache can be manage by plugins
Browse files Browse the repository at this point in the history
First commit to add a way to manage the QGIS Server cache with plugins.

In this commit only GetCapabilities document can be cached by plugins.
  • Loading branch information
rldhont committed Aug 20, 2018
1 parent b39ee5a commit 9bd23b5
Show file tree
Hide file tree
Showing 16 changed files with 685 additions and 13 deletions.
13 changes: 13 additions & 0 deletions python/server/auto_generated/qgsserverinterface.sip.in
Expand Up @@ -83,6 +83,19 @@ Register an access control filter
virtual QgsAccessControl *accessControls() const = 0;
%Docstring
Gets the registered access control filters
%End

virtual void registerServerCache( QgsServerCacheFilter *serverCache /Transfer/, int priority = 0 ) = 0;
%Docstring
Register a server cache filter

:param serverCache: the server cache to register
:param priority: the priority used to order them
%End

virtual QgsServerCacheManager *cacheManager() const = 0;
%Docstring
Gets the registered server cache filters
%End

virtual QString getEnv( const QString &name ) const = 0;
Expand Down
6 changes: 6 additions & 0 deletions python/server/server_auto.sip
Expand Up @@ -29,3 +29,9 @@
%If ( HAVE_SERVER_PYTHON_PLUGINS )
%Include auto_generated/qgsaccesscontrol.sip
%End
%If ( HAVE_SERVER_PYTHON_PLUGINS )
%Include auto_generated/qgsservercachefilter.sip
%End
%If ( HAVE_SERVER_PYTHON_PLUGINS )
%Include auto_generated/qgsservercachemanager.sip
%End
2 changes: 2 additions & 0 deletions src/server/CMakeLists.txt
Expand Up @@ -73,6 +73,8 @@ IF (WITH_SERVER_PLUGINS)
qgsserverfilter.cpp
qgsaccesscontrolfilter.cpp
qgsaccesscontrol.cpp
qgsservercachefilter.cpp
qgsservercachemanager.cpp
)
ENDIF (WITH_SERVER_PLUGINS)

Expand Down
56 changes: 56 additions & 0 deletions src/server/qgsservercachefilter.cpp
@@ -0,0 +1,56 @@
/***************************************************************************
qgsservercachefilter.cpp
------------------------
Cache interface for Qgis Server plugins
begin : 2018-07-05
copyright : (C) 2018 by René-Luc D'Hont
email : rldhont at 3liz dot com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsservercachefilter.h"

#include <QDomDocument>

//! Constructor
QgsServerCacheFilter::QgsServerCacheFilter( const QgsServerInterface *serverInterface ):
mServerInterface( serverInterface )
{
}

//! Returns cached document
QByteArray QgsServerCacheFilter::getCachedDocument( const QgsProject *project, const QgsServerRequest &request, const QString &key ) const
{
Q_UNUSED( project );
Q_UNUSED( request );
Q_UNUSED( key );
return QByteArray();
}

//! Updates or inserts the document in cache
bool QgsServerCacheFilter::setCachedDocument( const QDomDocument *doc, const QgsProject *project, const QgsServerRequest &request, const QString &key ) const
{
Q_UNUSED( doc );
Q_UNUSED( project );
Q_UNUSED( request );
Q_UNUSED( key );
return false;
}

//! Deletes the cached document
bool QgsServerCacheFilter::deleteCachedDocument( const QgsProject *project, const QgsServerRequest &request, const QString &key ) const
{
Q_UNUSED( project );
Q_UNUSED( request );
Q_UNUSED( key );
return false;
}
93 changes: 93 additions & 0 deletions src/server/qgsservercachefilter.h
@@ -0,0 +1,93 @@
/***************************************************************************
qgsservercachefilter.h
------------------------
Cache interface for Qgis Server plugins
begin : 2018-07-05
copyright : (C) 2018 by René-Luc D'Hont
email : rldhont at 3liz dot com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSSERVERCACHEPLUGIN_H
#define QGSSERVERCACHEPLUGIN_H

#include <QMultiMap>
#include <QDomDocument>
#include <QObject>
#include "qgsproject.h"
#include "qgsserverrequest.h"
#include "qgis_server.h"
#include "qgis_sip.h"

SIP_IF_MODULE( HAVE_SERVER_PYTHON_PLUGINS )

class QgsServerInterface;


/**
* \ingroup server
* \class QgsServerCacheFilter
* \brief Class defining cache interface for QGIS Server plugins.
*/
class SERVER_EXPORT QgsServerCacheFilter
{

public:

/**
* Constructor
* QgsServerInterface passed to plugins constructors
* and must be passed to QgsServerCacheFilter instances.
*/
QgsServerCacheFilter( const QgsServerInterface *serverInterface );

virtual ~QgsServerCacheFilter() = default;

/**
* Returns cached document (or 0 if document not in cache) like capabilities
* \param project the project used to generate the document to provide path
* \param request the request used to generate the document to provider parameters or data
* \param key the key provided by the access control to identify differents documents for the same request
* \returns QByteArray of the cached document or an empty one if no corresponding document found
*/
virtual QByteArray getCachedDocument( const QgsProject *project, const QgsServerRequest &request, const QString &key ) const;

/**
* Updates or inserts the document in cache like capabilities
* \param doc the document to cache
* \param project the project used to generate the document to provide path
* \param request the request used to generate the document to provider parameters or data
* \param key the key provided by the access control to identify differents documents for the same request
* \returns true if the document has been cached
*/
virtual bool setCachedDocument( const QDomDocument *doc, const QgsProject *project, const QgsServerRequest &request, const QString &key ) const;

/**
* Deletes the cached document
* \param project the project used to generate the document to provide path
* \param request the request used to generate the document to provider parameters or data
* \param key the key provided by the access control to identify differents documents for the same request
* \returns true if the document has been deleted
*/
virtual bool deleteCachedDocument( const QgsProject *project, const QgsServerRequest &request, const QString &key ) const;

private:

//! The server interface
const QgsServerInterface *mServerInterface = nullptr;

};

//! The registry definition
typedef QMultiMap<int, QgsServerCacheFilter *> QgsServerCacheFilterMap;

#endif // QGSSERVERSECURITY_H
72 changes: 72 additions & 0 deletions src/server/qgsservercachemanager.cpp
@@ -0,0 +1,72 @@
/***************************************************************************
qgsservercachemanager.cpp
-------------------------
begin : 2018-07-05
copyright : (C) 2018 by René-Luc D'Hont
email : rldhont at 3liz dot com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsservercachemanager.h"

//! Returns cached document (or 0 if document not in cache) like capabilities
const QDomDocument *QgsServerCacheManager::getCachedDocument( const QgsProject *project, const QgsServerRequest &request, const QString &key ) const
{
QgsServerCacheFilterMap::const_iterator scIterator;
for ( scIterator = mPluginsServerCaches->constBegin(); scIterator != mPluginsServerCaches->constEnd(); ++scIterator )
{
QByteArray content = scIterator.value()->getCachedDocument( project, request, key );
if ( !content.isEmpty() )
{
QDomDocument doc;
if ( doc.setContent( content ) )
{
return &doc;
}
}
}
return nullptr;
}

//! Updates or inserts the document in cache like capabilities
bool QgsServerCacheManager::setCachedDocument( const QDomDocument *doc, const QgsProject *project, const QgsServerRequest &request, const QString &key ) const
{
QgsServerCacheFilterMap::const_iterator scIterator;
for ( scIterator = mPluginsServerCaches->constBegin(); scIterator != mPluginsServerCaches->constEnd(); ++scIterator )
{
if ( scIterator.value()->setCachedDocument( doc, project, request, key ) )
{
return true;
}
}
return false;
}

//! Deletes the cached document
bool QgsServerCacheManager::deleteCachedDocument( const QgsProject *project, const QgsServerRequest &request, const QString &key ) const
{
QgsServerCacheFilterMap::const_iterator scIterator;
for ( scIterator = mPluginsServerCaches->constBegin(); scIterator != mPluginsServerCaches->constEnd(); ++scIterator )
{
if ( scIterator.value()->deleteCachedDocument( project, request, key ) )
{
return true;
}
}
return false;
}

//! Register a new access control filter
void QgsServerCacheManager::registerServerCache( QgsServerCacheFilter *serverCache, int priority )
{
mPluginsServerCaches->insert( priority, serverCache );
}
111 changes: 111 additions & 0 deletions src/server/qgsservercachemanager.h
@@ -0,0 +1,111 @@
/***************************************************************************
qgsservercachemanager.h
-----------------------
begin : 2018-07-05
copyright : (C) 2018 by René-Luc D'Hont
email : rldhont at 3liz dot com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSSERVERCACHEMANAGER_H
#define QGSSERVERCACHEMANAGER_H

#include "qgsservercachefilter.h"
#include "qgsserverrequest.h"

#include <QMultiMap>
#include <QDomDocument>
#include "qgsproject.h"
#include "qgis_server.h"
#include "qgis_sip.h"

SIP_IF_MODULE( HAVE_SERVER_PYTHON_PLUGINS )

class QgsServerCachePlugin;


/**
* \ingroup server
* \class QgsServerCacheManager
* \brief A helper class that centralizes caches accesses given by all the server cache filter plugins.
* \since QGIS 3.4
*/
class SERVER_EXPORT QgsServerCacheManager
{
#ifdef SIP_RUN
#include "qgsservercachefilter.h"
#endif

public:
//! Constructor
QgsServerCacheManager()
{
mPluginsServerCaches = new QgsServerCacheFilterMap();
mResolved = false;
}

//! Constructor
QgsServerCacheManager( const QgsServerCacheManager &copy )
{
mPluginsServerCaches = new QgsServerCacheFilterMap( *copy.mPluginsServerCaches );
mResolved = copy.mResolved;
}


~QgsServerCacheManager()
{
delete mPluginsServerCaches;
}

/**
* Returns cached document (or 0 if document not in cache) like capabilities
* \param project the project used to generate the document to provide path
* \param request the request used to generate the document to provider parameters or data
* \param key the key provided by the access control to identify differents documents for the same request
* \returns the cached document or 0 if no corresponding document found
*/
const QDomDocument *getCachedDocument( const QgsProject *project, const QgsServerRequest &request, const QString &key ) const;

/**
* Updates or inserts the document in cache like capabilities
* \param doc the document to cache
* \param project the project used to generate the document to provide path
* \param request the request used to generate the document to provider parameters or data
* \param key the key provided by the access control to identify differents documents for the same request
* \returns true if the document has been cached
*/
bool setCachedDocument( const QDomDocument *doc, const QgsProject *project, const QgsServerRequest &request, const QString &key ) const;

/**
* Deletes the cached document
* \param project the project used to generate the document to provide path
* \param request the request used to generate the document to provider parameters or data
* \param key the key provided by the access control to identify differents documents for the same request
* \returns true if the document has been deleted
*/
bool deleteCachedDocument( const QgsProject *project, const QgsServerRequest &request, const QString &key ) const;

/**
* Register a server cache filter
* \param serverCache the server cache to add
* \param priority the priority used to define the order
*/
void registerServerCache( QgsServerCacheFilter *serverCache, int priority = 0 );

private:
//! The ServerCache plugins registry
QgsServerCacheFilterMap *mPluginsServerCaches = nullptr;

bool mResolved;
};

#endif

0 comments on commit 9bd23b5

Please sign in to comment.