QGIS API Documentation  3.6.0-Noosa (5873452)
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 
22 {
23  mPluginsServerCaches.reset( new QgsServerCacheFilterMap() );
24 }
25 
27 {
28  if ( copy.mPluginsServerCaches )
29  {
30  mPluginsServerCaches.reset( new QgsServerCacheFilterMap( *copy.mPluginsServerCaches ) );
31  }
32  else
33  {
34  mPluginsServerCaches.reset( nullptr );
35  }
36 }
37 
39 {
40  if ( copy.mPluginsServerCaches )
41  {
42  mPluginsServerCaches.reset( new QgsServerCacheFilterMap( *copy.mPluginsServerCaches ) );
43  }
44  else
45  {
46  mPluginsServerCaches.reset( nullptr );
47  }
48  return *this;
49 }
50 
52 {
53  mPluginsServerCaches.reset();
54 }
55 
56 bool QgsServerCacheManager::getCachedDocument( QDomDocument *doc, const QgsProject *project, const QgsServerRequest &request, QgsAccessControl *accessControl ) const
57 {
58  bool cache = true;
59  QString key = getCacheKey( cache, accessControl );
60 
61  if ( !cache )
62  {
63  return false;
64  }
65 
66  QByteArray content;
67  QgsServerCacheFilterMap::const_iterator scIterator;
68  for ( scIterator = mPluginsServerCaches->constBegin(); scIterator != mPluginsServerCaches->constEnd(); ++scIterator )
69  {
70  content = scIterator.value()->getCachedDocument( project, request, key );
71  if ( !content.isEmpty() )
72  {
73  break;
74  }
75  }
76  if ( content.isEmpty() )
77  {
78  return false;
79  }
80 
81  if ( !doc->setContent( content ) )
82  {
83  return false;
84  }
85 
86  return true;
87 }
88 
89 bool QgsServerCacheManager::setCachedDocument( const QDomDocument *doc, const QgsProject *project, const QgsServerRequest &request, QgsAccessControl *accessControl ) const
90 {
91  bool cache = true;
92  QString key = getCacheKey( cache, accessControl );
93 
94  if ( !cache )
95  {
96  return false;
97  }
98 
99  QgsServerCacheFilterMap::const_iterator scIterator;
100  for ( scIterator = mPluginsServerCaches->constBegin(); scIterator != mPluginsServerCaches->constEnd(); ++scIterator )
101  {
102  if ( scIterator.value()->setCachedDocument( doc, project, request, key ) )
103  {
104  return true;
105  }
106  }
107  return false;
108 }
109 
110 bool QgsServerCacheManager::deleteCachedDocument( const QgsProject *project, const QgsServerRequest &request, QgsAccessControl *accessControl ) const
111 {
112  bool cache = true;
113  QString key = getCacheKey( cache, accessControl );
114 
115  QgsServerCacheFilterMap::const_iterator scIterator;
116  for ( scIterator = mPluginsServerCaches->constBegin(); scIterator != mPluginsServerCaches->constEnd(); ++scIterator )
117  {
118  if ( scIterator.value()->deleteCachedDocument( project, request, key ) )
119  {
120  return true;
121  }
122  }
123  return false;
124 }
125 
127 {
128  QgsServerCacheFilterMap::const_iterator scIterator;
129  for ( scIterator = mPluginsServerCaches->constBegin(); scIterator != mPluginsServerCaches->constEnd(); ++scIterator )
130  {
131  if ( scIterator.value()->deleteCachedDocuments( project ) )
132  {
133  return true;
134  }
135  }
136  return false;
137 }
138 
139 QByteArray QgsServerCacheManager::getCachedImage( const QgsProject *project, const QgsServerRequest &request, QgsAccessControl *accessControl ) const
140 {
141  bool cache = true;
142  QString key = getCacheKey( cache, accessControl );
143 
144  QgsServerCacheFilterMap::const_iterator scIterator;
145  for ( scIterator = mPluginsServerCaches->constBegin(); scIterator != mPluginsServerCaches->constEnd(); ++scIterator )
146  {
147  QByteArray content = scIterator.value()->getCachedImage( project, request, key );
148  if ( !content.isEmpty() )
149  {
150  return content;
151  }
152  }
153  return QByteArray();
154 }
155 
156 bool QgsServerCacheManager::setCachedImage( const QByteArray *img, const QgsProject *project, const QgsServerRequest &request, QgsAccessControl *accessControl ) const
157 {
158  bool cache = true;
159  QString key = getCacheKey( cache, accessControl );
160 
161  QgsServerCacheFilterMap::const_iterator scIterator;
162  for ( scIterator = mPluginsServerCaches->constBegin(); scIterator != mPluginsServerCaches->constEnd(); ++scIterator )
163  {
164  if ( scIterator.value()->setCachedImage( img, project, request, key ) )
165  {
166  return true;
167  }
168  }
169  return false;
170 }
171 
172 bool QgsServerCacheManager::deleteCachedImage( const QgsProject *project, const QgsServerRequest &request, QgsAccessControl *accessControl ) const
173 {
174  bool cache = true;
175  QString key = getCacheKey( cache, accessControl );
176 
177  QgsServerCacheFilterMap::const_iterator scIterator;
178  for ( scIterator = mPluginsServerCaches->constBegin(); scIterator != mPluginsServerCaches->constEnd(); ++scIterator )
179  {
180  if ( scIterator.value()->deleteCachedImage( project, request, key ) )
181  {
182  return true;
183  }
184  }
185  return false;
186 }
187 
189 {
190  QgsServerCacheFilterMap::const_iterator scIterator;
191  for ( scIterator = mPluginsServerCaches->constBegin(); scIterator != mPluginsServerCaches->constEnd(); ++scIterator )
192  {
193  if ( scIterator.value()->deleteCachedImages( project ) )
194  {
195  return true;
196  }
197  }
198  return false;
199 }
200 
202 {
203  mPluginsServerCaches->insert( priority, serverCache );
204 }
205 
206 QString QgsServerCacheManager::getCacheKey( bool &cache, QgsAccessControl *accessControl ) const
207 {
208  QStringList cacheKeyList;
209  if ( accessControl )
210  {
211  cache = accessControl->fillCacheKey( cacheKeyList );
212  }
213  else
214  {
215  cache = true;
216  }
217  return cacheKeyList.join( '-' );
218 }
void registerServerCache(QgsServerCacheFilter *serverCache, int priority=0)
Register a server cache filter.
bool fillCacheKey(QStringList &cacheKey) const
Fill the capabilities caching key.
bool setCachedImage(const QByteArray *img, const QgsProject *project, const QgsServerRequest &request, QgsAccessControl *accessControl) const
Updates or inserts the image in cache like tiles.
bool deleteCachedDocument(const QgsProject *project, const QgsServerRequest &request, QgsAccessControl *accessControl) const
Deletes the cached document.
bool deleteCachedDocuments(const QgsProject *project) const
Deletes all cached documents for a QGIS project.
A helper class that centralizes caches accesses given by all the server cache filter plugins...
Reads and writes project states.
Definition: qgsproject.h:89
QgsServerCacheManager & operator=(const QgsServerCacheManager &copy)
Assignment operator.
Class defining cache interface for QGIS Server plugins.
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.
QgsServerRequest Class defining request interface passed to services QgsService::executeRequest() met...
QMultiMap< int, QgsServerCacheFilter * > QgsServerCacheFilterMap
The registry definition.
bool setCachedDocument(const QDomDocument *doc, const QgsProject *project, const QgsServerRequest &request, QgsAccessControl *accessControl) const
Updates or inserts the document in cache like capabilities.
A helper class that centralizes restrictions given by all the access control filter plugins...
QgsServerCacheManager()
Constructor.
bool deleteCachedImages(const QgsProject *project) const
Deletes all cached images for a QGIS project.
bool deleteCachedImage(const QgsProject *project, const QgsServerRequest &request, QgsAccessControl *accessControl) const
Deletes the cached image.
QByteArray getCachedImage(const QgsProject *project, const QgsServerRequest &request, QgsAccessControl *accessControl) const
Returns cached image (or 0 if image not in cache) like tiles.