22 #include "qgsconfig.h" 42 #include <QDomDocument> 43 #include <QNetworkDiskCache> 45 #include <QElapsedTimer> 48 #include <fcgi_stdio.h> 57 QString *QgsServer::sConfigFilePath =
nullptr;
61 bool QgsServer::sInitialized =
false;
70 if ( qobject_cast<QgsApplication *>( qApp ) ==
nullptr )
72 qFatal(
"A QgsApplication must exist before a QgsServer instance can be created." );
79 QString &QgsServer::serverName()
81 static QString *name =
new QString( QStringLiteral(
"qgis_server" ) );
86 QFileInfo QgsServer::defaultAdminSLD()
88 return QFileInfo( QStringLiteral(
"admin.sld" ) );
91 void QgsServer::setupNetworkAccessManager()
95 QNetworkDiskCache *cache =
new QNetworkDiskCache(
nullptr );
96 qint64 cacheSize = sSettings()->cacheSize();
97 QString cacheDirectory = sSettings()->cacheDirectory();
98 cache->setCacheDirectory( cacheDirectory );
99 cache->setMaximumCacheSize( cacheSize );
102 nam->setCache( cache );
105 QFileInfo QgsServer::defaultProjectFile()
108 fprintf( FCGI_stderr,
"current directory: %s\n", currentDir.absolutePath().toUtf8().constData() );
109 QStringList nameFilterList;
110 nameFilterList << QStringLiteral(
"*.qgs" )
111 << QStringLiteral(
"*.qgz" );
112 QFileInfoList projectFiles = currentDir.entryInfoList( nameFilterList, QDir::Files, QDir::Name );
113 for (
int x = 0; x < projectFiles.size(); x++ )
117 if ( projectFiles.isEmpty() )
121 return projectFiles.at( 0 );
124 void QgsServer::printRequestParameters(
const QMap< QString, QString> ¶meterMap,
Qgis::MessageLevel logLevel )
131 QMap< QString, QString>::const_iterator pIt = parameterMap.constBegin();
132 for ( ; pIt != parameterMap.constEnd(); ++pIt )
138 QString QgsServer::configPath(
const QString &defaultConfigPath,
const QString &configPath )
140 QString cfPath( defaultConfigPath );
141 QString projectFile = sSettings()->projectFile();
142 if ( !projectFile.isEmpty() )
144 cfPath = projectFile;
145 QgsDebugMsg( QStringLiteral(
"QGIS_PROJECT_FILE:%1" ).arg( cfPath ) );
149 if ( configPath.isEmpty() )
152 if ( getenv(
"QGIS_PROJECT_FILE" ) )
154 cfPath = getenv(
"QGIS_PROJECT_FILE" );
157 else if ( ! defaultConfigPath.isEmpty() )
165 QgsDebugMsg( QStringLiteral(
"MAP:%1" ).arg( cfPath ) );
171 void QgsServer::initLocale()
174 if ( ! sSettings()->overrideSystemLocale().isEmpty() )
176 QLocale::setDefault( QLocale( sSettings()->overrideSystemLocale() ) );
179 QLocale currentLocale;
180 if ( sSettings()->showGroupSeparator() )
182 currentLocale.setNumberOptions( currentLocale.numberOptions() &= ~QLocale::NumberOption::OmitGroupSeparator );
186 currentLocale.setNumberOptions( currentLocale.numberOptions() |= QLocale::NumberOption::OmitGroupSeparator );
188 QLocale::setDefault( currentLocale );
191 bool QgsServer::init()
204 #if defined(SERVER_SKIP_ECW) 217 if ( ! sSettings()->logFile().isEmpty() )
221 else if ( sSettings()->logStderr() )
230 sSettings()->logSummary();
232 setupNetworkAccessManager();
233 QDomImplementation::setInvalidDataPolicy( QDomImplementation::DropInvalidChars );
252 QString defaultConfigFilePath;
253 QFileInfo projectFileInfo = defaultProjectFile();
254 if ( projectFileInfo.exists() )
256 defaultConfigFilePath = projectFileInfo.absoluteFilePath();
261 QFileInfo adminSLDFileInfo = defaultAdminSLD();
262 if ( adminSLDFileInfo.exists() )
264 defaultConfigFilePath = adminSLDFileInfo.absoluteFilePath();
268 sConfigFilePath =
new QString( defaultConfigFilePath );
282 sServiceRegistry->
init( modulePath, sServerInterface );
295 qunsetenv( var.toUtf8().data() );
299 qputenv( var.toUtf8().data(), val.toUtf8() );
301 sSettings()->load( var );
309 qApp->processEvents();
326 requestHandler.parseInput();
331 requestHandler.setServiceException( e );
355 responseDecorator.start();
360 response.
sendError( 500, QStringLiteral(
"Internal Server Error" ) );
365 if ( !requestHandler.exceptionRaised() )
370 printRequestParameters( params.
toMap(), logLevel );
375 QString configFilePath = configPath( *sConfigFilePath, params.
map() );
378 project = mConfigCache->
project( configFilePath );
403 if ( ! params.
fileName().isEmpty() )
405 const QString value = QString(
"attachment; filename=\"%1\"" ).arg( params.
fileName() );
406 requestHandler.setResponseHeader( QStringLiteral(
"Content-Disposition" ), value );
418 QStringLiteral(
"Service unknown or unsupported" ) );
424 responseDecorator.write( ex );
431 response.
sendError( 500, QStringLiteral(
"Internal Server Error" ) );
440 responseDecorator.finish();
445 response.
sendError( 500, QStringLiteral(
"Internal Server Error" ) );
461 #ifdef HAVE_SERVER_PYTHON_PLUGINS 462 void QgsServer::initPython()
Server generic API endpoint abstract base class.
QMap< QString, QString > toMap() const
Returns all parameters in a map.
virtual const QString rootPath() const =0
Returns the root path for the API.
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.
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.
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.
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.
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.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts, annotations, canvases, etc.
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
Q_GLOBAL_STATIC(QgsServerSettings, sSettings)
const QgsProject * project(const QString &path)
If the project is not cached yet, then the project is read thanks to the path.
Exception base class for server exceptions.
static QString pkgDataPath()
Returns the common root path of all application data directories.
The QgsServerApiContext class encapsulates the resources for a particular client request: the request...
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 ...
virtual void executeRequest(const QgsServerApiContext &context) const =0
Executes a request by passing the given context to the API handlers.
QgsServerParameters serverParameters() const
Returns parameters.
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...
QgsServerApi * apiForRequest(const QgsServerRequest &request) const
Searches the API register for an API matching the request and returns a (possibly NULL) pointer to it...
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).
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.