QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
qgsservercachemanager.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsservercachemanager.cpp
3 -------------------------
4
5 begin : 2018-07-05
6 copyright : (C) 2018 by René-Luc D'Hont
7 email : rldhont at 3liz dot com
8 ***************************************************************************/
9
10/***************************************************************************
11 * *
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
16 * *
17 ***************************************************************************/
18
20
21#include <memory>
22
23#include "qgis.h"
24#include "qgsmessagelog.h"
26
28 : mSettings( settings )
29{
30 mPluginsServerCaches = std::make_unique<QgsServerCacheFilterMap>();
31}
32
34 : mSettings( copy.mSettings )
35{
36 if ( copy.mPluginsServerCaches )
37 {
38 mPluginsServerCaches = std::make_unique<QgsServerCacheFilterMap>( *copy.mPluginsServerCaches );
39 }
40 else
41 {
42 mPluginsServerCaches.reset( nullptr );
43 }
44}
45
47{
48 if ( copy.mPluginsServerCaches )
49 {
50 mPluginsServerCaches = std::make_unique<QgsServerCacheFilterMap>( *copy.mPluginsServerCaches );
51 }
52 else
53 {
54 mPluginsServerCaches.reset( nullptr );
55 }
56 return *this;
57}
58
60{
61 mPluginsServerCaches.reset();
62}
63
64bool QgsServerCacheManager::getCachedDocument( QDomDocument *doc, const QgsProject *project, const QgsServerRequest &request, QgsAccessControl *accessControl ) const
65{
66 bool cache = true;
67 const QString key = getCacheKey( cache, accessControl, request );
68
69 if ( !cache )
70 {
71 return false;
72 }
73
74 QByteArray content;
75 QgsServerCacheFilterMap::const_iterator scIterator;
76 for ( scIterator = mPluginsServerCaches->constBegin(); scIterator != mPluginsServerCaches->constEnd(); ++scIterator )
77 {
78 content = scIterator.value()->getCachedDocument( project, request, key );
79 if ( !content.isEmpty() )
80 {
81 break;
82 }
83 }
84 if ( content.isEmpty() )
85 {
86 return false;
87 }
88
89 if ( !doc->setContent( content ) )
90 {
91 return false;
92 }
93
94 return true;
95}
96
97bool QgsServerCacheManager::setCachedDocument( const QDomDocument *doc, const QgsProject *project, const QgsServerRequest &request, QgsAccessControl *accessControl ) const
98{
99 bool cache = true;
100 const QString key = getCacheKey( cache, accessControl, request );
101
102 if ( !cache )
103 {
104 return false;
105 }
106
107 QgsServerCacheFilterMap::const_iterator scIterator;
108 for ( scIterator = mPluginsServerCaches->constBegin(); scIterator != mPluginsServerCaches->constEnd(); ++scIterator )
109 {
110 if ( scIterator.value()->setCachedDocument( doc, project, request, key ) )
111 {
112 return true;
113 }
114 }
115 return false;
116}
117
118bool QgsServerCacheManager::deleteCachedDocument( const QgsProject *project, const QgsServerRequest &request, QgsAccessControl *accessControl ) const
119{
120 bool cache = true;
121 const QString key = getCacheKey( cache, accessControl, request );
122
123 QgsServerCacheFilterMap::const_iterator scIterator;
124 for ( scIterator = mPluginsServerCaches->constBegin(); scIterator != mPluginsServerCaches->constEnd(); ++scIterator )
125 {
126 if ( scIterator.value()->deleteCachedDocument( project, request, key ) )
127 {
128 return true;
129 }
130 }
131 return false;
132}
133
135{
136 QgsServerCacheFilterMap::const_iterator scIterator;
137 for ( scIterator = mPluginsServerCaches->constBegin(); scIterator != mPluginsServerCaches->constEnd(); ++scIterator )
138 {
139 if ( scIterator.value()->deleteCachedDocuments( project ) )
140 {
141 return true;
142 }
143 }
144 return false;
145}
146
147QByteArray QgsServerCacheManager::getCachedImage( const QgsProject *project, const QgsServerRequest &request, QgsAccessControl *accessControl ) const
148{
149 bool cache = true;
150 const QString key = getCacheKey( cache, accessControl, request );
151
152 QgsServerCacheFilterMap::const_iterator scIterator;
153 for ( scIterator = mPluginsServerCaches->constBegin(); scIterator != mPluginsServerCaches->constEnd(); ++scIterator )
154 {
155 QByteArray content = scIterator.value()->getCachedImage( project, request, key );
156 if ( !content.isEmpty() )
157 {
158 return content;
159 }
160 }
161 return QByteArray();
162}
163
164bool QgsServerCacheManager::setCachedImage( const QByteArray *img, const QgsProject *project, const QgsServerRequest &request, QgsAccessControl *accessControl ) const
165{
166 bool cache = true;
167 const QString key = getCacheKey( cache, accessControl, request );
168
169 QgsServerCacheFilterMap::const_iterator scIterator;
170 for ( scIterator = mPluginsServerCaches->constBegin(); scIterator != mPluginsServerCaches->constEnd(); ++scIterator )
171 {
172 if ( scIterator.value()->setCachedImage( img, project, request, key ) )
173 {
174 return true;
175 }
176 }
177 return false;
178}
179
180bool QgsServerCacheManager::deleteCachedImage( const QgsProject *project, const QgsServerRequest &request, QgsAccessControl *accessControl ) const
181{
182 bool cache = true;
183 const QString key = getCacheKey( cache, accessControl, request );
184
185 QgsServerCacheFilterMap::const_iterator scIterator;
186 for ( scIterator = mPluginsServerCaches->constBegin(); scIterator != mPluginsServerCaches->constEnd(); ++scIterator )
187 {
188 if ( scIterator.value()->deleteCachedImage( project, request, key ) )
189 {
190 return true;
191 }
192 }
193 return false;
194}
195
197{
198 QgsServerCacheFilterMap::const_iterator scIterator;
199 for ( scIterator = mPluginsServerCaches->constBegin(); scIterator != mPluginsServerCaches->constEnd(); ++scIterator )
200 {
201 if ( scIterator.value()->deleteCachedImages( project ) )
202 {
203 return true;
204 }
205 }
206 return false;
207}
208
210{
211 mPluginsServerCaches->insert( priority, serverCache );
212}
213
214QString QgsServerCacheManager::getCacheKey( bool &cache, QgsAccessControl *accessControl, const QgsServerRequest &request ) const
215{
216 QStringList cacheKeyList;
217 cacheKeyList << QgsServerProjectUtils::serviceUrl( request.serverParameters().service(), request, mSettings );
218 if ( accessControl )
219 {
220 cache = accessControl->fillCacheKey( cacheKeyList );
221 }
222 else
223 {
224 cache = true;
225 }
226 return cacheKeyList.join( '-' );
227}
A helper class that centralizes restrictions given by all the access control filter plugins.
bool fillCacheKey(QStringList &cacheKey) const
Fill the capabilities caching key.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:109
Defines cache interface for QGIS Server plugins.
void registerServerCache(QgsServerCacheFilter *serverCache, int priority=0)
Register a server cache filter.
bool deleteCachedDocuments(const QgsProject *project) const
Deletes all cached documents for a QGIS project.
QgsServerCacheManager & operator=(const QgsServerCacheManager &copy)
bool deleteCachedImages(const QgsProject *project) const
Deletes all cached images for a QGIS project.
bool deleteCachedDocument(const QgsProject *project, const QgsServerRequest &request, QgsAccessControl *accessControl) const
Deletes the cached document.
bool setCachedDocument(const QDomDocument *doc, const QgsProject *project, const QgsServerRequest &request, QgsAccessControl *accessControl) const
Updates or inserts the document in cache like capabilities.
bool getCachedDocument(QDomDocument *doc, const QgsProject *project, const QgsServerRequest &request, QgsAccessControl *accessControl) const
Returns cached document (or 0 if document not in cache) like capabilities.
bool setCachedImage(const QByteArray *img, const QgsProject *project, const QgsServerRequest &request, QgsAccessControl *accessControl) const
Updates or inserts the image in cache like tiles.
bool deleteCachedImage(const QgsProject *project, const QgsServerRequest &request, QgsAccessControl *accessControl) const
Deletes the cached image.
QgsServerCacheManager(const QgsServerSettings &settings=QgsServerSettings())
Constructor.
QByteArray getCachedImage(const QgsProject *project, const QgsServerRequest &request, QgsAccessControl *accessControl) const
Returns cached image (or 0 if image not in cache) like tiles.
QString service() const
Returns SERVICE parameter as a string or an empty string if not defined.
static QString serviceUrl(const QString &service, const QgsServerRequest &request, const QgsServerSettings &settings)
Returns the service url defined in the environment variable or with HTTP header.
Defines requests passed to QgsService classes.
QgsServerParameters serverParameters() const
Returns parameters.
Provides a way to retrieve settings by prioritizing according to environment variables,...