22 #include "qgsconfig.h" 43 #include <QDomDocument> 44 #include <QNetworkDiskCache> 50 #include <fcgi_stdio.h> 59 QString *QgsServer::sConfigFilePath =
nullptr;
63 bool QgsServer::sInitialized =
false;
71 if ( qobject_cast<QgsApplication *>( qApp ) ==
nullptr )
73 qFatal(
"A QgsApplication must exist before a QgsServer instance can be created." );
80 QString &QgsServer::serverName()
82 static QString *name =
new QString( QStringLiteral(
"qgis_server" ) );
87 QFileInfo QgsServer::defaultAdminSLD()
89 return QFileInfo( QStringLiteral(
"admin.sld" ) );
92 void QgsServer::setupNetworkAccessManager()
96 QNetworkDiskCache *cache =
new QNetworkDiskCache(
nullptr );
99 cache->setCacheDirectory( cacheDirectory );
100 cache->setMaximumCacheSize( cacheSize );
103 nam->setCache( cache );
106 QFileInfo QgsServer::defaultProjectFile()
109 fprintf( FCGI_stderr,
"current directory: %s\n", currentDir.absolutePath().toUtf8().constData() );
110 QStringList nameFilterList;
111 nameFilterList << QStringLiteral(
"*.qgs" )
112 << QStringLiteral(
"*.qgz" );
113 QFileInfoList projectFiles = currentDir.entryInfoList( nameFilterList, QDir::Files, QDir::Name );
114 for (
int x = 0; x < projectFiles.size(); x++ )
118 if ( projectFiles.isEmpty() )
122 return projectFiles.at( 0 );
125 void QgsServer::printRequestParameters(
const QMap< QString, QString> ¶meterMap,
Qgis::MessageLevel logLevel )
132 QMap< QString, QString>::const_iterator pIt = parameterMap.constBegin();
133 for ( ; pIt != parameterMap.constEnd(); ++pIt )
139 QString QgsServer::configPath(
const QString &defaultConfigPath,
const QString &configPath )
141 QString cfPath( defaultConfigPath );
143 if ( !projectFile.isEmpty() )
145 cfPath = projectFile;
146 QgsDebugMsg( QStringLiteral(
"QGIS_PROJECT_FILE:%1" ).arg( cfPath ) );
150 if ( configPath.isEmpty() )
157 QgsDebugMsg( QStringLiteral(
"MAP:%1" ).arg( cfPath ) );
163 void QgsServer::initLocale()
171 QLocale currentLocale;
174 currentLocale.setNumberOptions( currentLocale.numberOptions() &= ~QLocale::NumberOption::OmitGroupSeparator );
178 currentLocale.setNumberOptions( currentLocale.numberOptions() |= QLocale::NumberOption::OmitGroupSeparator );
180 QLocale::setDefault( currentLocale );
183 bool QgsServer::init()
196 #if defined(SERVER_SKIP_ECW) 209 if ( ! sSettings.
logFile().isEmpty() )
224 setupNetworkAccessManager();
225 QDomImplementation::setInvalidDataPolicy( QDomImplementation::DropInvalidChars );
248 QString defaultConfigFilePath;
249 QFileInfo projectFileInfo = defaultProjectFile();
250 if ( projectFileInfo.exists() )
252 defaultConfigFilePath = projectFileInfo.absoluteFilePath();
257 QFileInfo adminSLDFileInfo = defaultAdminSLD();
258 if ( adminSLDFileInfo.exists() )
260 defaultConfigFilePath = adminSLDFileInfo.absoluteFilePath();
264 sConfigFilePath =
new QString( defaultConfigFilePath );
277 qDebug() <<
"Initializing server modules from " << modulePath << endl;
278 sServiceRegistry->
init( modulePath, sServerInterface );
290 _putenv_s( var.toStdString().c_str(), val.toStdString().c_str() );
292 setenv( var.toStdString().c_str(), val.toStdString().c_str(), 1 );
294 sSettings.
load( var );
302 qApp->processEvents();
319 requestHandler.parseInput();
324 requestHandler.setServiceException( e );
345 responseDecorator.start();
348 if ( !requestHandler.exceptionRaised() )
353 printRequestParameters( params.
toMap(), logLevel );
366 if ( ! params.
fileName().isEmpty() )
368 const QString value = QString(
"attachment; filename=\"%1\"" ).arg( params.
fileName() );
369 requestHandler.setResponseHeader( QStringLiteral(
"Content-Disposition" ), value );
381 QStringLiteral(
"Service unknown or unsupported" ) );
386 responseDecorator.write( ex );
395 responseDecorator.finish();
408 #ifdef HAVE_SERVER_PYTHON_PLUGINS 409 void QgsServer::initPython()
QMap< QString, QString > toMap() const
Returns all parameters in a map.
QString logFile() const
Returns the log file.
QString cacheDirectory() const
Returns the cache directory.
static QString qgisUserDatabaseFilePath()
Returns the path to the user qgis.db file.
virtual void sendError(int code, const QString &message)=0
Send error This method delegates error handling at the server level.
void setConfigFilePath(const QString &configFilePath) override
Set the configuration file path.
QString configFilePath() override
Returns the configuration file path.
Exception base class for service exceptions.
static bool initPlugins(QgsServerInterface *interface)
Initializes the Python plugins.
Provides a way to retrieve settings by prioritizing according to environment variables, ini file and default values.
Class defining decorator for calling filter's hooks.
MessageLevel
Level for messages This will be used both for message log and message bar in application.
void putenv(const QString &var, const QString &val)
Set environment variable.
QString fileName() const
Returns FILE_NAME parameter as a string or an empty string if not defined.
bool showGroupSeparator() const
Show group (thousand) separator.
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
void setLogLevel(Qgis::MessageLevel level)
Set the current log level.
Interfaces exposed by QGIS Server and made available to plugins.
static QString pluginPath()
Returns the path to the application plugin directory.
QString projectFile() const
Returns the QGS project file to use.
QString version() const
Returns VERSION parameter as a string or an empty string if not defined.
static bool createDatabase(QString *errorMessage=nullptr)
initialize qgis.db
virtual void executeRequest(const QgsServerRequest &request, QgsServerResponse &response, const QgsProject *project)=0
Execute the requests and set result in QgsServerRequest.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
QgsServerParameters serverParameters() const
Returns parameters.
qint64 cacheSize() const
Returns the cache size.
void init(const QString &nativeModulepath, QgsServerInterface *serverIface=nullptr)
Initialize registry, load modules and auto register services.
Reads and writes project states.
QgsServerFiltersMap filters() override
Returns the list of current QgsServerFilter.
void logSummary() const
Log a summary of settings currently loaded.
static bool loadStandardTestFonts(const QStringList &loadstyles)
Loads standard test fonts from filesystem or qrc resource.
void setLogStderr()
Activates logging to stderr.
This class is an interface hiding the details of reading input and writing output from/to a wms reque...
static const char * QGIS_ORGANIZATION_NAME
const QgsProject * project(const QString &path)
If the project is not cached yet, then the project is read thanks to the path.
Qgis::MessageLevel logLevel() const
Gets the current log level.
QString map() const
Returns MAP parameter as a string or an empty string if not defined.
Exception base class for server exceptions.
static QString pkgDataPath()
Returns the common root path of all application data directories.
static void skipGdalDriver(const QString &driver)
Sets the GDAL_SKIP environment variable to include the specified driver and then calls GDALDriverMana...
QgsService Class defining interfaces for QGIS server services.
A cache for capabilities xml documents (by configuration file path)
QgsServerRequest Class defining request interface passed to services QgsService::executeRequest() met...
bool logStderr() const
Returns whether logging to stderr is activated.
static QgsAuthManager * authManager()
Returns the application's authentication manager instance.
static QgsNetworkAccessManager * instance(Qt::ConnectionType connectionType=Qt::BlockingQueuedConnection)
Returns a pointer to the active QgsNetworkAccessManager for the current thread.
QgsServiceRegistry Class defining the registry manager for QGIS server services.
static const char * QGIS_ORGANIZATION_DOMAIN
static void init(QString profileFolder=QString())
This method initializes paths etc for QGIS.
QgsService * getService(const QString &name, const QString &version=QString())
Retrieve a service from its name.
QgsServerParameters provides an interface to retrieve and manipulate global parameters received from ...
void load()
Load settings according to current environment variables.
static QgsServerLogger * instance()
Gets the singleton instance.
bool init(const QString &pluginPath=QString(), const QString &authDatabasePath=QString())
init initialize QCA, prioritize qca-ossl plugin and optionally set up the authentication database ...
static QStringList svgPaths()
Returns the paths to svg directories.
void clearRequestHandler() override
Clear the request handler.
void setLogFile(const QString &filename=QString())
Set the current log file.
QgsServerResponse Class defining response interface passed to services QgsService::executeRequest() m...
static QString qgisAuthDatabaseFilePath()
Returns the path to the user authentication database file: qgis-auth.db.
static QgsConfigCache * instance()
Returns the current instance.
QString message() const
Returns the exception message.
QString overrideSystemLocale() const
Overrides system locale.
static QString libexecPath()
Returns the path with utility executables (help viewer, crssync, ...)
static QString prefixPath()
Returns the path to the application prefix directory.
static const char * QGIS_APPLICATION_NAME
Exception class for WMS service exceptions (for compatibility only).
Defines a QGIS exception class.
network access manager for QGISThis class implements the QGIS network access manager.
QString service() const
Returns SERVICE parameter as a string or an empty string if not defined.
Qgis::MessageLevel logLevel() const
Returns the log level.
QgsServer()
Creates the server instance.
void setRequestHandler(QgsRequestHandler *requestHandler) override
Set the request handler.
void handleRequest(QgsServerRequest &request, QgsServerResponse &response, const QgsProject *project=nullptr)
Handles the request.