22 #include "qgsconfig.h" 40 #include <QDomDocument> 41 #include <QNetworkDiskCache> 46 #include <fcgi_stdio.h> 55 QString *QgsServer::sConfigFilePath =
nullptr;
59 bool QgsServer::sInitialized =
false;
67 if ( qobject_cast<QgsApplication *>( qApp ) ==
nullptr )
69 qFatal(
"A QgsApplication must exist before a QgsServer instance can be created." );
76 QString &QgsServer::serverName()
78 static QString *name =
new QString( QStringLiteral(
"qgis_server" ) );
83 QFileInfo QgsServer::defaultAdminSLD()
85 return QFileInfo( QStringLiteral(
"admin.sld" ) );
88 void QgsServer::setupNetworkAccessManager()
92 QNetworkDiskCache *cache =
new QNetworkDiskCache(
nullptr );
95 cache->setCacheDirectory( cacheDirectory );
96 cache->setMaximumCacheSize( cacheSize );
99 nam->setCache( cache );
102 QFileInfo QgsServer::defaultProjectFile()
105 fprintf( FCGI_stderr,
"current directory: %s\n", currentDir.absolutePath().toUtf8().constData() );
106 QStringList nameFilterList;
107 nameFilterList << QStringLiteral(
"*.qgs" )
108 << QStringLiteral(
"*.qgz" );
109 QFileInfoList projectFiles = currentDir.entryInfoList( nameFilterList, QDir::Files, QDir::Name );
110 for (
int x = 0; x < projectFiles.size(); x++ )
114 if ( projectFiles.isEmpty() )
118 return projectFiles.at( 0 );
121 void QgsServer::printRequestParameters(
const QMap< QString, QString> ¶meterMap,
Qgis::MessageLevel logLevel )
128 QMap< QString, QString>::const_iterator pIt = parameterMap.constBegin();
129 for ( ; pIt != parameterMap.constEnd(); ++pIt )
135 QString QgsServer::configPath(
const QString &defaultConfigPath,
const QString &configPath )
137 QString cfPath( defaultConfigPath );
139 if ( !projectFile.isEmpty() )
141 cfPath = projectFile;
142 QgsDebugMsg( QStringLiteral(
"QGIS_PROJECT_FILE:%1" ).arg( cfPath ) );
146 if ( configPath.isEmpty() )
153 QgsDebugMsg( QStringLiteral(
"MAP:%1" ).arg( cfPath ) );
159 void QgsServer::initLocale()
167 QLocale currentLocale;
170 currentLocale.setNumberOptions( currentLocale.numberOptions() &= ~QLocale::NumberOption::OmitGroupSeparator );
174 currentLocale.setNumberOptions( currentLocale.numberOptions() |= QLocale::NumberOption::OmitGroupSeparator );
176 QLocale::setDefault( currentLocale );
179 bool QgsServer::init()
192 #if defined(SERVER_SKIP_ECW) 205 if ( ! sSettings.
logFile().isEmpty() )
220 setupNetworkAccessManager();
221 QDomImplementation::setInvalidDataPolicy( QDomImplementation::DropInvalidChars );
240 QString defaultConfigFilePath;
241 QFileInfo projectFileInfo = defaultProjectFile();
242 if ( projectFileInfo.exists() )
244 defaultConfigFilePath = projectFileInfo.absoluteFilePath();
249 QFileInfo adminSLDFileInfo = defaultAdminSLD();
250 if ( adminSLDFileInfo.exists() )
252 defaultConfigFilePath = adminSLDFileInfo.absoluteFilePath();
256 sConfigFilePath =
new QString( defaultConfigFilePath );
269 qDebug() <<
"Initializing server modules from " << modulePath << endl;
270 sServiceRegistry->
init( modulePath, sServerInterface );
282 _putenv_s( var.toStdString().c_str(), val.toStdString().c_str() );
284 setenv( var.toStdString().c_str(), val.toStdString().c_str(), 1 );
286 sSettings.
load( var );
294 qApp->processEvents();
311 requestHandler.parseInput();
316 requestHandler.setServiceException( e );
337 responseDecorator.start();
340 if ( !requestHandler.exceptionRaised() )
345 printRequestParameters( params.
toMap(), logLevel );
358 if ( ! params.
fileName().isEmpty() )
360 const QString value = QString(
"attachment; filename=\"%1\"" ).arg( params.
fileName() );
361 requestHandler.setResponseHeader( QStringLiteral(
"Content-Disposition" ), value );
373 QStringLiteral(
"Service unknown or unsupported" ) );
378 responseDecorator.write( ex );
385 response.
sendError( 500, QStringLiteral(
"Internal Server Error" ) );
390 responseDecorator.finish();
403 #ifdef HAVE_SERVER_PYTHON_PLUGINS 404 void QgsServer::initPython()
QMap< QString, QString > toMap() const
Returns all parameters in a map.
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.
Qgis::MessageLevel logLevel() const
Gets the current log level.
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.
QString projectFile() const
Returns the QGS project file to use.
MessageLevel
Level for messages This will be used both for message log and message bar in application.
QString message() const
Returns the exception message.
void putenv(const QString &var, const QString &val)
Set environment variable.
qint64 cacheSize() const
Returns the cache size.
QString map() const
Returns MAP parameter as a string or an empty string if not defined.
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.
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.
bool showGroupSeparator() const
Show group (thousand) separator.
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).
QString version() const
Returns VERSION parameter as a string or an empty string if not defined.
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.
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.
QString logFile() const
Returns the log file.
Exception base class for server exceptions.
static QString pkgDataPath()
Returns the common root path of all application data directories.
QString overrideSystemLocale() const
Overrides system locale.
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...
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.
QString service() const
Returns SERVICE parameter as a string or an empty string if not defined.
virtual QByteArray formatResponse(QString &responseFormat) const
Formats the exception for sending to client.
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 ...
QgsServerParameters serverParameters() const
Returns parameters.
void load()
Load settings according to current environment variables.
static QgsServerLogger * instance()
Gets the singleton instance.
void logSummary() const
Log a summary of settings currently loaded.
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...
Qgis::MessageLevel logLevel() const
Returns the log level.
static QString qgisAuthDatabaseFilePath()
Returns the path to the user authentication database file: qgis-auth.db.
static QgsConfigCache * instance()
Returns the current instance.
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).
bool logStderr() const
Returns whether logging to stderr is activated.
Defines a QGIS exception class.
network access manager for QGISThis class implements the QGIS network access manager.
QgsServer()
Creates the server instance.
void setRequestHandler(QgsRequestHandler *requestHandler) override
Set the request handler.
QString fileName() const
Returns FILE_NAME parameter as a string or an empty string if not defined.
void handleRequest(QgsServerRequest &request, QgsServerResponse &response, const QgsProject *project=nullptr)
Handles the request.