22 #include "qgsconfig.h" 42 #include <QDomDocument> 43 #include <QNetworkDiskCache> 48 #include <fcgi_stdio.h> 57 QString *QgsServer::sConfigFilePath =
nullptr;
61 bool QgsServer::sInitialized =
false;
69 if ( qobject_cast<QgsApplication *>( qApp ) ==
nullptr )
71 qFatal(
"A QgsApplication must exist before a QgsServer instance can be created." );
78 QString &QgsServer::serverName()
80 static QString *name =
new QString( QStringLiteral(
"qgis_server" ) );
85 QFileInfo QgsServer::defaultAdminSLD()
87 return QFileInfo( QStringLiteral(
"admin.sld" ) );
90 void QgsServer::setupNetworkAccessManager()
94 QNetworkDiskCache *cache =
new QNetworkDiskCache(
nullptr );
97 cache->setCacheDirectory( cacheDirectory );
98 cache->setMaximumCacheSize( cacheSize );
101 nam->setCache( cache );
104 QFileInfo QgsServer::defaultProjectFile()
107 fprintf( FCGI_stderr,
"current directory: %s\n", currentDir.absolutePath().toUtf8().constData() );
108 QStringList nameFilterList;
109 nameFilterList << QStringLiteral(
"*.qgs" )
110 << QStringLiteral(
"*.qgz" );
111 QFileInfoList projectFiles = currentDir.entryInfoList( nameFilterList, QDir::Files, QDir::Name );
112 for (
int x = 0; x < projectFiles.size(); x++ )
116 if ( projectFiles.isEmpty() )
120 return projectFiles.at( 0 );
123 void QgsServer::printRequestParameters(
const QMap< QString, QString> ¶meterMap,
Qgis::MessageLevel logLevel )
130 QMap< QString, QString>::const_iterator pIt = parameterMap.constBegin();
131 for ( ; pIt != parameterMap.constEnd(); ++pIt )
137 QString QgsServer::configPath(
const QString &defaultConfigPath,
const QString &configPath )
139 QString cfPath( defaultConfigPath );
141 if ( !projectFile.isEmpty() )
143 cfPath = projectFile;
144 QgsDebugMsg( QStringLiteral(
"QGIS_PROJECT_FILE:%1" ).arg( cfPath ) );
148 if ( configPath.isEmpty() )
151 if ( getenv(
"QGIS_PROJECT_FILE" ) )
153 cfPath = getenv(
"QGIS_PROJECT_FILE" );
156 else if ( ! defaultConfigPath.isEmpty() )
164 QgsDebugMsg( QStringLiteral(
"MAP:%1" ).arg( cfPath ) );
170 void QgsServer::initLocale()
178 QLocale currentLocale;
181 currentLocale.setNumberOptions( currentLocale.numberOptions() &= ~QLocale::NumberOption::OmitGroupSeparator );
185 currentLocale.setNumberOptions( currentLocale.numberOptions() |= QLocale::NumberOption::OmitGroupSeparator );
187 QLocale::setDefault( currentLocale );
190 bool QgsServer::init()
203 #if defined(SERVER_SKIP_ECW) 216 if ( ! sSettings.
logFile().isEmpty() )
231 setupNetworkAccessManager();
232 QDomImplementation::setInvalidDataPolicy( QDomImplementation::DropInvalidChars );
251 QString defaultConfigFilePath;
252 QFileInfo projectFileInfo = defaultProjectFile();
253 if ( projectFileInfo.exists() )
255 defaultConfigFilePath = projectFileInfo.absoluteFilePath();
260 QFileInfo adminSLDFileInfo = defaultAdminSLD();
261 if ( adminSLDFileInfo.exists() )
263 defaultConfigFilePath = adminSLDFileInfo.absoluteFilePath();
267 sConfigFilePath =
new QString( defaultConfigFilePath );
280 qDebug() <<
"Initializing server modules from " << modulePath << endl;
281 sServiceRegistry->
init( modulePath, sServerInterface );
293 _putenv_s( var.toStdString().c_str(), val.toStdString().c_str() );
295 setenv( var.toStdString().c_str(), val.toStdString().c_str(), 1 );
297 sSettings.
load( var );
305 qApp->processEvents();
322 requestHandler.parseInput();
327 requestHandler.setServiceException( e );
351 responseDecorator.start();
356 response.
sendError( 500, QStringLiteral(
"Internal Server Error" ) );
361 if ( !requestHandler.exceptionRaised() )
366 printRequestParameters( params.
toMap(), logLevel );
371 QString configFilePath = configPath( *sConfigFilePath, params.
map() );
374 project = mConfigCache->
project( configFilePath );
399 if ( ! params.
fileName().isEmpty() )
401 const QString value = QString(
"attachment; filename=\"%1\"" ).arg( params.
fileName() );
402 requestHandler.setResponseHeader( QStringLiteral(
"Content-Disposition" ), value );
414 QStringLiteral(
"Service unknown or unsupported" ) );
420 responseDecorator.write( ex );
427 response.
sendError( 500, QStringLiteral(
"Internal Server Error" ) );
436 responseDecorator.finish();
441 response.
sendError( 500, QStringLiteral(
"Internal Server Error" ) );
457 #ifdef HAVE_SERVER_PYTHON_PLUGINS 458 void QgsServer::initPython()
Server generic API endpoint abstract base class.
QMap< QString, QString > toMap() const
Returns all parameters in a map.
QString cacheDirectory() const
Returns the cache directory.
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.
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.
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
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.
The QgsServerApiContext class encapsulates the resources for a particular client request: the request...
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 ...
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.
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...
QgsServerApi * apiForRequest(const QgsServerRequest &request) const
Searches the API register for an API matching the request and returns a (possibly NULL) pointer to it...
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.