33 QString makeServiceKey(
const QString &name,
const QString &version )
35 return QString(
"%1_%2" ).arg( name, version );
45 bool isVersionGreater(
const QString &v1,
const QString &v2 )
47 QStringList l1 = v1.split(
'.' );
48 QStringList l2 = v2.split(
'.' );
49 QStringList::iterator it1 = l1.begin();
50 QStringList::iterator it2 = l2.begin();
52 while ( it1 != l1.end() && it2 != l2.end() )
57 const int i1 = it1->toInt( &isint );
60 const int i2 = it2->toInt( &isint );
61 if ( isint && i1 != i2 )
95 const VersionTable::const_iterator v = mServiceVersions.constFind( name );
96 if ( v != mServiceVersions.constEnd() )
98 key = version.isEmpty() ? v->second : makeServiceKey( name, version );
99 const ServiceTable::const_iterator it = mServices.constFind( key );
100 if ( it != mServices.constEnd() )
108 service = mServices[v->second].get();
120 const QString name = service->
name();
121 const QString version = service->
version();
124 const QString key = makeServiceKey( name, version );
125 if ( mServices.constFind( key ) != mServices.constEnd() )
132 mServices.insert( key, std::shared_ptr<QgsService>( service ) );
139 const VersionTable::const_iterator v = mServiceVersions.constFind( name );
140 if ( v == mServiceVersions.constEnd() )
143 mServiceVersions.insert( name, VersionTable::mapped_type( version, key ) );
165 const VersionTable::const_iterator v = mApiVersions.constFind( name );
166 if ( v != mApiVersions.constEnd() )
168 if ( version.isEmpty() )
171 ApiTable::iterator it = mApis.begin();
172 while ( it != mApis.end() )
174 if ( ( *it )->name() == name )
177 it = mApis.erase( it );
186 mApiVersions.remove( name );
190 const QString key = makeServiceKey( name, version );
191 const ApiTable::iterator found = mApis.find( key );
192 if ( found != mApis.end() )
195 mApis.erase( found );
202 const std::function<void(
const ApiTable::mapped_type & )>
203 findGreaterVersion = [name, &maxVer](
const ApiTable::mapped_type &api ) {
204 if ( api->name() == name && ( maxVer.isEmpty() || isVersionGreater( api->version(), maxVer ) ) )
205 maxVer = api->version();
208 mApiVersions.remove( name );
210 std::for_each( mApis.constBegin(), mApis.constEnd(), findGreaterVersion );
211 if ( !maxVer.isEmpty() )
214 const QString key = makeServiceKey( name, maxVer );
215 mApiVersions.insert( name, VersionTable::mapped_type( version, key ) );
225 for (
const auto &api : mApis )
228 if ( api->accept( request.
url() ) )
230 Q_ASSERT( !api->name().isEmpty() );
244 const VersionTable::const_iterator v = mApiVersions.constFind( name );
245 if ( v != mApiVersions.constEnd() )
247 key = version.isEmpty() ? v->second : makeServiceKey( name, version );
248 const ApiTable::const_iterator it = mApis.constFind( key );
249 if ( it != mApis.constEnd() )
257 api = mApis[v->second].get();
271 const VersionTable::const_iterator v = mServiceVersions.constFind( name );
272 if ( v != mServiceVersions.constEnd() )
274 if ( version.isEmpty() )
277 ServiceTable::iterator it = mServices.begin();
278 while ( it != mServices.end() )
280 if ( ( *it )->name() == name )
283 it = mServices.erase( it );
292 mServiceVersions.remove( name );
296 const QString key = makeServiceKey( name, version );
297 const ServiceTable::iterator found = mServices.find( key );
298 if ( found != mServices.end() )
301 mServices.erase( found );
308 const std::function<void(
const ServiceTable::mapped_type & )>
309 findGreaterVersion = [name, &maxVer](
const ServiceTable::mapped_type &service ) {
310 if ( service->name() == name && ( maxVer.isEmpty() || isVersionGreater( service->version(), maxVer ) ) )
311 maxVer = service->version();
314 mServiceVersions.remove( name );
316 std::for_each( mServices.constBegin(), mServices.constEnd(), findGreaterVersion );
317 if ( !maxVer.isEmpty() )
320 const QString key = makeServiceKey( name, maxVer );
321 mServiceVersions.insert( name, VersionTable::mapped_type( version, key ) );
331 mNativeLoader.
loadModules( nativeModulePath, *
this, serverIface );
337 mServiceVersions.clear();
345 const QString name = api->
name();
346 const QString version = api->
version();
349 const QString key = makeServiceKey( name, version );
350 if ( mApis.constFind( key ) != mApis.constEnd() )
357 mApis.insert( key, std::shared_ptr<QgsServerApi>( api ) );
364 const VersionTable::const_iterator v = mApiVersions.constFind( name );
365 if ( v == mApiVersions.constEnd() )
368 mApiVersions.insert( name, VersionTable::mapped_type( version, key ) );
@ Warning
Warning message.
@ Critical
Critical/error message.
@ Info
Information message.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
Server generic API endpoint abstract base class.
virtual const QString version() const
Returns the version of the service.
virtual const QString name() const =0
Returns the API name.
QgsServerInterface Class defining interfaces exposed by QGIS Server and made available to plugins.
QgsServerRequest Class defining request interface passed to services QgsService::executeRequest() met...
void unloadModules()
Unload all modules.
void loadModules(const QString &modulePath, QgsServiceRegistry ®istrar, QgsServerInterface *serverIface=nullptr)
Load all modules from path.
void cleanUp()
Clean up registered service and unregister modules.
bool registerApi(QgsServerApi *api)
Registers the QgsServerApi api.
int unregisterService(const QString &name, const QString &version=QString())
Unregister service from its name and version.
int unregisterApi(const QString &name, const QString &version=QString())
Unregisters API from its name and version.
QgsServerApi * apiForRequest(const QgsServerRequest &request) const
Searches the API register for an API matching the request and returns a (possibly NULL) pointer to it...
void init(const QString &nativeModulepath, QgsServerInterface *serverIface=nullptr)
Initialize registry, load modules and auto register services.
QgsService * getService(const QString &name, const QString &version=QString())
Retrieve a service from its name.
void registerService(QgsService *service)
Register a service by its name and version.
QgsServerApi * getApi(const QString &name, const QString &version=QString())
Retrieves an API from its name.
QgsService Class defining interfaces for QGIS server services.
virtual QString name() const =0
virtual QString version() const =0