Skip to content
Permalink
Browse files

Add QgsServerParameters

  • Loading branch information
pblottiere committed Jun 27, 2018
1 parent b58f194 commit 6a88a75ceef991cf72351d81bdcd32d454803437
@@ -30,6 +30,7 @@ SET(QGIS_SERVER_SRCS
qgsfilterrestorer.cpp
qgsrequesthandler.cpp
qgsserver.cpp
qgsserverparameters.cpp
qgsserverexception.cpp
qgsserverinterface.cpp
qgsserverinterfaceimpl.cpp
@@ -56,6 +57,7 @@ SET (QGIS_SERVER_MOC_HDRS
qgsconfigcache.h
qgsserverlogger.h
qgsserversettings.h
qgsserverparameters.h
)


@@ -38,6 +38,7 @@
#include "qgsfilterresponsedecorator.h"
#include "qgsservice.h"
#include "qgsserverprojectutils.h"
#include "qgsserverparameters.h"

#include <QDomDocument>
#include <QNetworkDiskCache>
@@ -135,7 +136,7 @@ void QgsServer::printRequestParameters( const QMap< QString, QString> &parameter
}
}

QString QgsServer::configPath( const QString &defaultConfigPath, const QMap<QString, QString> &parameters )
QString QgsServer::configPath( const QString &defaultConfigPath, const QString &configPath )
{
QString cfPath( defaultConfigPath );
QString projectFile = sSettings.projectFile();
@@ -146,14 +147,13 @@ QString QgsServer::configPath( const QString &defaultConfigPath, const QMap<QStr
}
else
{
QMap<QString, QString>::const_iterator paramIt = parameters.find( QStringLiteral( "MAP" ) );
if ( paramIt == parameters.constEnd() )
if ( configPath.isEmpty() )
{
QgsMessageLog::logMessage( QStringLiteral( "Using default configuration file path: %1" ).arg( defaultConfigPath ), QStringLiteral( "Server" ), Qgis::Info );
}
else
{
cfPath = paramIt.value();
cfPath = configPath;
QgsDebugMsg( QString( "MAP:%1" ).arg( cfPath ) );
}
}
@@ -303,13 +303,13 @@ void QgsServer::handleRequest( QgsServerRequest &request, QgsServerResponse &res
{
try
{
QMap<QString, QString> parameterMap = request.parameters();
printRequestParameters( parameterMap, logLevel );
const QgsServerParameters params( request.parameters() );
printRequestParameters( request.parameters(), logLevel );

//Config file path
if ( ! project )
{
QString configFilePath = configPath( *sConfigFilePath, parameterMap );
QString configFilePath = configPath( *sConfigFilePath, params.map() );

// load the project if needed and not empty
project = mConfigCache->project( configFilePath );
@@ -325,30 +325,14 @@ void QgsServer::handleRequest( QgsServerRequest &request, QgsServerResponse &res
sServerInterface->setConfigFilePath( project->fileName() );
}

//Service parameter
QString serviceString = parameterMap.value( QStringLiteral( "SERVICE" ) );

if ( serviceString.isEmpty() )
{
// SERVICE not mandatory for WMS 1.3.0 GetMap & GetFeatureInfo
QString requestString = parameterMap.value( QStringLiteral( "REQUEST" ) );
if ( requestString == QLatin1String( "GetMap" ) || requestString == QLatin1String( "GetFeatureInfo" ) )
{
serviceString = QStringLiteral( "WMS" );
}
}

QString versionString = parameterMap.value( QStringLiteral( "VERSION" ) );

//possibility for client to suggest a download filename
QString outputFileName = parameterMap.value( QStringLiteral( "FILE_NAME" ) );
if ( !outputFileName.isEmpty() )
if ( ! params.fileName().isEmpty() )
{
requestHandler.setResponseHeader( QStringLiteral( "Content-Disposition" ), "attachment; filename=\"" + outputFileName + "\"" );
const QString value = QString( "attachment; filename=\"%1\"" ).arg( params.fileName() );
requestHandler.setResponseHeader( QStringLiteral( "Content-Disposition" ), value );
}

// Lookup for service
QgsService *service = sServiceRegistry->getService( serviceString, versionString );
QgsService *service = sServiceRegistry->getService( params.service(), params.version() );
if ( service )
{
service->executeRequest( request, responseDecorator, project );
@@ -106,7 +106,7 @@ class SERVER_EXPORT QgsServer
* Returns the configuration file path.
*/
static QString configPath( const QString &defaultConfigPath,
const QMap<QString, QString> &parameters );
const QString &configPath );

/**
* \brief QgsServer::printRequestParameters prints the request parameters
@@ -105,5 +105,20 @@ class SERVER_EXPORT QgsOgcServiceException
QString mVersion;
};

/**
* \ingroup server
* \class QgsBadRequestException
* \brief Exception thrown in case of malformed request
*/
#ifndef SIP_RUN
class SERVER_EXPORT QgsBadRequestException: public QgsOgcServiceException
{
public:
QgsBadRequestException( const QString &code, const QString &message, const QString &locator = QString() )
: QgsOgcServiceException( code, message, locator, 400 )
{}
};
#endif

#endif

@@ -0,0 +1,154 @@
/***************************************************************************
qgsserverparameters.cpp
--------------------
begin : Jun 27, 2018
copyright : (C) 2018 by Paul Blottiere
email : paul dot blottiere at oslandia dot com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsserverparameters.h"
#include "qgsserverexception.h"

QgsServerParameters::QgsServerParameters()
{
const Parameter pService = { ParameterName::SERVICE,
QVariant::String,
QVariant( "" ),
QVariant()
};
save( pService );

const Parameter pRequest = { ParameterName::REQUEST,
QVariant::String,
QVariant( "" ),
QVariant()
};
save( pRequest );

const Parameter pVersion = { ParameterName::VERSION_SERVICE,
QVariant::String,
QVariant( "" ),
QVariant()
};
save( pService );

const Parameter pMap = { ParameterName::MAP,
QVariant::String,
QVariant( "" ),
QVariant()
};
save( pMap );

const Parameter pFile = { ParameterName::FILE_NAME,
QVariant::String,
QVariant( "" ),
QVariant()
};
save( pFile );
}

QgsServerParameters::QgsServerParameters( const QgsServerRequest::Parameters &parameters )
: QgsServerParameters()
{
load( parameters );
}

void QgsServerParameters::save( const Parameter &parameter )
{
mParameters[ parameter.mName ] = parameter;
}

QString QgsServerParameters::map() const
{
return value( ParameterName::MAP ).toString();
}

QString QgsServerParameters::version() const
{
return value( ParameterName::VERSION_SERVICE ).toString();
}

QString QgsServerParameters::fileName() const
{
return value( ParameterName::FILE_NAME ).toString();
}

QString QgsServerParameters::service() const
{
QString serviceValue = value( ParameterName::SERVICE ).toString();

if ( serviceValue.isEmpty() )
{
// SERVICE not mandatory for WMS 1.3.0 GetMap & GetFeatureInfo
if ( request() == QLatin1String( "GetMap" ) \
|| request() == QLatin1String( "GetFeatureInfo" ) )
{
serviceValue = "WMS";
}
}

return serviceValue;
}

QString QgsServerParameters::request() const
{
return value( ParameterName::REQUEST ).toString();
}

QVariant QgsServerParameters::value( ParameterName name ) const
{
return mParameters[name].mValue;
}

void QgsServerParameters::load( const QgsServerRequest::Parameters &parameters )
{
const QMetaEnum metaEnum( QMetaEnum::fromType<ParameterName>() );

for ( const QString &key : parameters.keys() )
{
const ParameterName name = ( ParameterName ) metaEnum.keyToValue( key.toStdString().c_str() );
if ( name >= 0 )
{
const QVariant value( parameters[key] );
mParameters[name].mValue = value;

if ( !value.canConvert( mParameters[name].mType ) )
{
raiseError( name );
}
}
else
{
mUnmanagedParameters[key] = parameters[key];
}
}
}

QString QgsServerParameters::name( ParameterName name ) const
{
const QMetaEnum metaEnum( QMetaEnum::fromType<ParameterName>() );
return metaEnum.valueToKey( name );
}

void QgsServerParameters::raiseError( ParameterName paramName ) const
{
const QString value = mParameters[paramName].mValue.toString();
const QString param = name( paramName );
const QString type = QVariant::typeToName( mParameters[paramName].mType );
const QString msg = QString( "%1 ('%2') cannot be converted into %3" ).arg( param, value, type );
raiseError( msg );
}

void QgsServerParameters::raiseError( const QString &msg ) const
{
throw QgsBadRequestException( QStringLiteral( "Invalid WMS Parameter" ), msg );
}

0 comments on commit 6a88a75

Please sign in to comment.
You can’t perform that action at this time.