QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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 
19 #include "qgsservercachemanager.h"
20 #include "qgsserverprojectutils.h"
21 #include "qgsmessagelog.h"
22 #include "qgis.h"
23 
25  mSettings( settings )
26 {
27  mPluginsServerCaches.reset( new QgsServerCacheFilterMap() );
28 }
29 
31  mSettings( copy.mSettings )
32 {
33  if ( copy.mPluginsServerCaches )
34  {
35  mPluginsServerCaches.reset( new QgsServerCacheFilterMap( *copy.mPluginsServerCaches ) );
36  }
37  else
38  {
39  mPluginsServerCaches.reset( nullptr );
40  }
41 }
42 
44 {
45  if ( copy.mPluginsServerCaches )
46  {
47  mPluginsServerCaches.reset( new QgsServerCacheFilterMap( *copy.mPluginsServerCaches ) );
48  }
49  else
50  {
51  mPluginsServerCaches.reset( nullptr );
52  }
53  return *this;
54 }
55 
57 {
58  mPluginsServerCaches.reset();
59 }
60 
61 bool QgsServerCacheManager::getCachedDocument( QDomDocument *doc, const QgsProject *project, const QgsServerRequest &request, QgsAccessControl *accessControl ) const
62 {
63  bool cache = true;
64  const QString key = getCacheKey( cache, accessControl, request );
65 
66  if ( !cache )
67  {
68  return false;
69  }
70 
71  QByteArray content;
72  QgsServerCacheFilterMap::const_iterator scIterator;
73  for ( scIterator = mPluginsServerCaches->constBegin(); scIterator != mPluginsServerCaches->constEnd(); ++scIterator )
74  {
75  content = scIterator.value()->getCachedDocument( project, request, key );
76  if ( !content.isEmpty() )
77  {
78  break;
79  }
80  }
81  if ( content.isEmpty() )
82  {
83  return false;
84  }
85 
86  if ( !doc->setContent( content ) )
87  {
88  return false;
89  }
90 
91  return true;
92 }
93 
94 bool QgsServerCacheManager::setCachedDocument( const QDomDocument *doc, const QgsProject *project, const QgsServerRequest &request, QgsAccessControl *accessControl ) const
95 {
96  bool cache = true;
97  const QString key = getCacheKey( cache, accessControl, request );
98 
99  if ( !cache )
100  {
101  return false;
102  }
103 
104  QgsServerCacheFilterMap::const_iterator scIterator;
105  for ( scIterator = mPluginsServerCaches->constBegin(); scIterator != mPluginsServerCaches->constEnd(); ++scIterator )
106  {
107  if ( scIterator.value()->setCachedDocument( doc, project, request, key ) )
108  {
109  return true;
110  }
111  }
112  return false;
113 }
114 
115 bool QgsServerCacheManager::deleteCachedDocument( const QgsProject *project, const QgsServerRequest &request, QgsAccessControl *accessControl ) const
116 {
117  bool cache = true;
118  const QString key = getCacheKey( cache, accessControl, request );
119 
120  QgsServerCacheFilterMap::const_iterator scIterator;
121  for ( scIterator = mPluginsServerCaches->constBegin(); scIterator != mPluginsServerCaches->constEnd(); ++scIterator )
122  {
123  if ( scIterator.value()->deleteCachedDocument( project, request, key ) )
124  {
125  return true;
126  }
127  }
128  return false;
129 }
130 
132 {
133  QgsServerCacheFilterMap::const_iterator scIterator;
134  for ( scIterator = mPluginsServerCaches->constBegin(); scIterator != mPluginsServerCaches->constEnd(); ++scIterator )
135  {
136  if ( scIterator.value()->deleteCachedDocuments( project ) )
137  {
138  return true;
139  }
140  }
141  return false;
142 }
143 
144 QByteArray QgsServerCacheManager::getCachedImage( const QgsProject *project, const QgsServerRequest &request, QgsAccessControl *accessControl ) const
145 {
146  bool cache = true;
147  const QString key = getCacheKey( cache, accessControl, request );
148 
149  QgsServerCacheFilterMap::const_iterator scIterator;
150  for ( scIterator = mPluginsServerCaches->constBegin(); scIterator != mPluginsServerCaches->constEnd(); ++scIterator )
151  {
152  QByteArray content = scIterator.value()->getCachedImage( project, request, key );
153  if ( !content.isEmpty() )
154  {
155  return content;
156  }
157  }
158  return QByteArray();
159 }
160 
161 bool QgsServerCacheManager::setCachedImage( const QByteArray *img, const QgsProject *project, const QgsServerRequest &request, QgsAccessControl *accessControl ) const
162 {
163  bool cache = true;
164  const QString key = getCacheKey( cache, accessControl, request );
165 
166  QgsServerCacheFilterMap::const_iterator scIterator;
167  for ( scIterator = mPluginsServerCaches->constBegin(); scIterator != mPluginsServerCaches->constEnd(); ++scIterator )
168  {
169  if ( scIterator.value()->setCachedImage( img, project, request, key ) )
170  {
171  return true;
172  }
173  }
174  return false;
175 }
176 
177 bool QgsServerCacheManager::deleteCachedImage( const QgsProject *project, const QgsServerRequest &request, QgsAccessControl *accessControl ) const
178 {
179  bool cache = true;
180  const QString key = getCacheKey( cache, accessControl, request );
181 
182  QgsServerCacheFilterMap::const_iterator scIterator;
183  for ( scIterator = mPluginsServerCaches->constBegin(); scIterator != mPluginsServerCaches->constEnd(); ++scIterator )
184  {
185  if ( scIterator.value()->deleteCachedImage( project, request, key ) )
186  {
187  return true;
188  }
189  }
190  return false;
191 }
192 
194 {
195  QgsServerCacheFilterMap::const_iterator scIterator;
196  for ( scIterator = mPluginsServerCaches->constBegin(); scIterator != mPluginsServerCaches->constEnd(); ++scIterator )
197  {
198  if ( scIterator.value()->deleteCachedImages( project ) )
199  {
200  return true;
201  }
202  }
203  return false;
204 }
205 
207 {
208  mPluginsServerCaches->insert( priority, serverCache );
209 }
210 
211 QString QgsServerCacheManager::getCacheKey( bool &cache, QgsAccessControl *accessControl, const QgsServerRequest &request ) const
212 {
213  QStringList cacheKeyList;
214  cacheKeyList << QgsServerProjectUtils::serviceUrl( request.serverParameters().service(), request, mSettings );
215  if ( accessControl )
216  {
217  cache = accessControl->fillCacheKey( cacheKeyList );
218  }
219  else
220  {
221  cache = true;
222  }
223  return cacheKeyList.join( '-' );
224 }
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:101
Class defining cache interface for QGIS Server plugins.
A helper class that centralizes caches accesses given by all the server cache filter 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)
Assignment operator.
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.
QgsServerRequest Class defining request interface passed to services QgsService::executeRequest() met...
QgsServerParameters serverParameters() const
Returns parameters.
Provides a way to retrieve settings by prioritizing according to environment variables,...
SERVER_EXPORT QString serviceUrl(const QString &service, const QgsServerRequest &request, const QgsServerSettings &settings)
Returns the service url defined in the environment variable or with HTTP header.
QMultiMap< int, QgsServerCacheFilter * > QgsServerCacheFilterMap
The registry definition.