Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #7859 from rldhont/fix-server-sld-param-support
[Bugfix][Server] Add WMS SLD parameter support
  • Loading branch information
rldhont committed Sep 29, 2018
2 parents 8ec5fec + 907c1a9 commit ad7d03c
Show file tree
Hide file tree
Showing 9 changed files with 307 additions and 26 deletions.
22 changes: 22 additions & 0 deletions python/server/auto_generated/qgsserverparameters.sip.in
Expand Up @@ -138,6 +138,28 @@ Converts the parameter into a color.
:param ok: True if there's no error during the conversion, false otherwise

:return: A color
%End

QUrl toUrl( bool &ok ) const;
%Docstring
Converts the parameter into an url.

:param ok: True if there's no error during the conversion, false otherwise

:return: An url

.. versionadded:: 3.4
%End

QString loadUrl( bool &ok ) const;
%Docstring
Loads the data associated to the parameter converted into an url.

:param ok: True if there's no error during the load, false otherwise

:return: The content loaded

.. versionadded:: 3.4
%End

static void raiseError( const QString &msg );
Expand Down
14 changes: 0 additions & 14 deletions src/server/qgsrequesthandler.cpp
Expand Up @@ -154,20 +154,6 @@ void QgsRequestHandler::setupParameters()
{
const QgsServerRequest::Parameters parameters = mRequest.parameters();

// SLD
QString value = parameters.value( QStringLiteral( "SLD" ) );
if ( !value.isEmpty() )
{
QgsMessageLog::logMessage( QStringLiteral( "http and ftp methods not supported with Qt5." ) );
}

// SLD_BODY
value = parameters.value( QStringLiteral( "SLD_BODY" ) );
if ( ! value.isEmpty() )
{
mRequest.setParameter( QStringLiteral( "SLD" ), value );
}

//feature info format?
QString infoFormat = parameters.value( QStringLiteral( "INFO_FORMAT" ) );
if ( !infoFormat.isEmpty() )
Expand Down
92 changes: 92 additions & 0 deletions src/server/qgsserverparameters.cpp
Expand Up @@ -17,6 +17,12 @@

#include "qgsserverparameters.h"
#include "qgsserverexception.h"
#include "qgsnetworkcontentfetcher.h"
#include "qgsmessagelog.h"
#include <QObject>
#include <QUrl>
#include <QNetworkReply>
#include <QNetworkRequest>

//
// QgsServerParameterDefinition
Expand Down Expand Up @@ -200,6 +206,92 @@ QgsRectangle QgsServerParameterDefinition::toRectangle( bool &ok ) const
return extent;
}

QString QgsServerParameterDefinition::loadUrl( bool &ok ) const
{
ok = true;

// Get URL
QUrl url = toUrl( ok );
if ( !ok )
{
return QString();
}

// fetching content
QgsNetworkContentFetcher fetcher;
QEventLoop loop;
QObject::connect( &fetcher, &QgsNetworkContentFetcher::finished, &loop, &QEventLoop::quit );

QgsMessageLog::logMessage(
QObject::tr( "Request started [url: %1]" ).arg( url.toString() ),
QStringLiteral( "Server" ) );
QNetworkRequest request( url );
request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache );
request.setAttribute( QNetworkRequest::CacheSaveControlAttribute, true );
fetcher.fetchContent( request );

//wait until content fetched
loop.exec( QEventLoop::ExcludeUserInputEvents );

QNetworkReply *reply = fetcher.reply();
if ( !reply )
{
ok = false;
QgsMessageLog::logMessage(
QObject::tr( "Request failed [error: no reply - url: %1]" ).arg( url.toString() ),
QStringLiteral( "Server" ) );
return QString();
}

QVariant status = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute );
if ( !status.isNull() && status.toInt() >= 400 )
{
ok = false;
if ( reply->error() != QNetworkReply::NoError )
{
QgsMessageLog::logMessage(
QObject::tr( "Request failed [error: %1 - url: %2]" ).arg( reply->errorString(), reply->url().toString() ),
QStringLiteral( "Server" ) );
}
QVariant phrase = reply->attribute( QNetworkRequest::HttpReasonPhraseAttribute );
QgsMessageLog::logMessage(
QObject::tr( "Request error [status: %1 - reason phrase: %2] for %3" ).arg( status.toInt() ).arg( phrase.toString(), reply->url().toString() ),
QStringLiteral( "Server" ) );
return QString();
}

if ( reply->error() != QNetworkReply::NoError )
{
ok = false;
QgsMessageLog::logMessage(
QObject::tr( "Request failed [error: %1 - url: %2]" ).arg( reply->errorString(), reply->url().toString() ),
QStringLiteral( "Server" ) );
return QString();
}

QgsMessageLog::logMessage(
QObject::tr( "Request finished [url: %1]" ).arg( url.toString() ),
QStringLiteral( "Server" ) );

QString content = fetcher.contentAsString();
ok = ( !content.isEmpty() );
return content;
}

QUrl QgsServerParameterDefinition::toUrl( bool &ok ) const
{
ok = true;
QUrl val;

if ( !mValue.toString().isEmpty() )
{
val = mValue.toUrl();
}

ok = ( !val.isEmpty() && val.isValid() );
return val;
}

int QgsServerParameterDefinition::toInt( bool &ok ) const
{
ok = true;
Expand Down
16 changes: 16 additions & 0 deletions src/server/qgsserverparameters.h
Expand Up @@ -137,6 +137,22 @@ class SERVER_EXPORT QgsServerParameterDefinition
*/
QColor toColor( bool &ok ) const;

/**
* Converts the parameter into an url.
* \param ok True if there's no error during the conversion, false otherwise
* \returns An url
* \since QGIS 3.4
*/
QUrl toUrl( bool &ok ) const;

/**
* Loads the data associated to the parameter converted into an url.
* \param ok True if there's no error during the load, false otherwise
* \returns The content loaded
* \since QGIS 3.4
*/
QString loadUrl( bool &ok ) const;

/**
* Raises an exception in case of an invalid parameters.
* \param msg The message describing the exception
Expand Down
48 changes: 46 additions & 2 deletions src/server/services/wms/qgswmsparameters.cpp
Expand Up @@ -19,6 +19,7 @@
#include "qgsdatasourceuri.h"
#include "qgsmessagelog.h"
#include <iostream>
#include <QUrl>

namespace QgsWms
{
Expand Down Expand Up @@ -85,6 +86,36 @@ namespace QgsWms
return val;
}

QString QgsWmsParameter::loadUrl() const
{
// Check URL -- it will be used in error messages
const QUrl url = toUrl();

bool ok = false;
const QString content = QgsServerParameterDefinition::loadUrl( ok );

if ( !ok )
{
const QString msg = QString( "%1 request error for %2" ).arg( name( mName ), url.toString() );
QgsServerParameterDefinition::raiseError( msg );
}

return content;
}

QUrl QgsWmsParameter::toUrl() const
{
bool ok = false;
const QUrl url = QgsServerParameterDefinition::toUrl( ok );

if ( !ok )
{
raiseError();
}

return url;
}

QColor QgsWmsParameter::toColor() const
{
bool ok = false;
Expand Down Expand Up @@ -336,6 +367,9 @@ namespace QgsWms
const QgsWmsParameter pSld( QgsWmsParameter::SLD );
save( pSld );

const QgsWmsParameter pSldBody( QgsWmsParameter::SLD_BODY );
save( pSldBody );

const QgsWmsParameter pLayer( QgsWmsParameter::LAYER );
save( pLayer );

Expand Down Expand Up @@ -470,6 +504,16 @@ namespace QgsWms
: QgsWmsParameters()
{
load( parameters.urlQuery() );

const QString sld = mWmsParameters[ QgsWmsParameter::SLD ].toString();
if ( !sld.isEmpty() )
{
const QString sldBody = mWmsParameters[ QgsWmsParameter::SLD ].loadUrl();
if ( !sldBody.isEmpty() )
{
loadParameter( QgsWmsParameter::name( QgsWmsParameter::SLD_BODY ), sldBody );
}
}
}

bool QgsWmsParameters::loadParameter( const QString &key, const QString &value )
Expand Down Expand Up @@ -1159,9 +1203,9 @@ namespace QgsWms
return mWmsParameters[ QgsWmsParameter::WMS_PRECISION ].toInt();
}

QString QgsWmsParameters::sld() const
QString QgsWmsParameters::sldBody() const
{
return mWmsParameters[ QgsWmsParameter::SLD ].toString();
return mWmsParameters[ QgsWmsParameter::SLD_BODY ].toString();
}

QStringList QgsWmsParameters::filters() const
Expand Down
23 changes: 20 additions & 3 deletions src/server/services/wms/qgswmsparameters.h
Expand Up @@ -132,6 +132,7 @@ namespace QgsWms
SYMBOLWIDTH,
OPACITIES,
SLD,
SLD_BODY,
FI_POLYGON_TOLERANCE,
FI_LINE_TOLERANCE,
FI_POINT_TOLERANCE,
Expand Down Expand Up @@ -251,6 +252,22 @@ namespace QgsWms
*/
QColor toColor() const;

/**
* Converts the parameter into an url.
* \returns An url
* \throws QgsBadRequestException Invalid parameter exception
* \since QGIS 3.4
*/
QUrl toUrl() const;

/**
* Loads the data associated to the parameter converted into an url.
* \returns The content loaded
* \throws QgsBadRequestException Invalid parameter exception
* \since QGIS 3.4
*/
QString loadUrl() const;

/**
* Raises an error in case of an invalid conversion.
* \throws QgsBadRequestException Invalid parameter exception
Expand Down Expand Up @@ -377,10 +394,10 @@ namespace QgsWms
QgsRectangle bboxAsRectangle() const;

/**
* Returns SLD if defined or an empty string.
* \returns sld
* Returns SLD_body if defined or an empty string.
* \returns sld body
*/
QString sld() const;
QString sldBody() const;

/**
* Returns the list of feature selection found in SELECTION parameter.
Expand Down
10 changes: 5 additions & 5 deletions src/server/services/wms/qgswmsrenderer.cpp
Expand Up @@ -170,7 +170,7 @@ namespace QgsWms
QList<QgsMapLayer *> layers;
QList<QgsWmsParametersLayer> params = mWmsParameters.layersParameters();

QString sld = mWmsParameters.sld();
QString sld = mWmsParameters.sldBody();
if ( !sld.isEmpty() )
layers = sldStylizedLayers( sld );
else
Expand Down Expand Up @@ -312,7 +312,7 @@ namespace QgsWms
restorer.reset( new QgsLayerRestorer( mNicknameLayers.values() ) );

// init stylized layers according to LAYERS/STYLES or SLD
QString sld = mWmsParameters.sld();
QString sld = mWmsParameters.sldBody();
if ( !sld.isEmpty() )
{
layers = sldStylizedLayers( sld );
Expand Down Expand Up @@ -661,7 +661,7 @@ namespace QgsWms
restorer.reset( new QgsLayerRestorer( mNicknameLayers.values() ) );

// init stylized layers according to LAYERS/STYLES or SLD
QString sld = mWmsParameters.sld();
QString sld = mWmsParameters.sldBody();
if ( !sld.isEmpty() )
{
layers = sldStylizedLayers( sld );
Expand Down Expand Up @@ -749,7 +749,7 @@ namespace QgsWms
restorer.reset( new QgsLayerRestorer( mNicknameLayers.values() ) );

// init stylized layers according to LAYERS/STYLES or SLD
QString sld = mWmsParameters.sld();
QString sld = mWmsParameters.sldBody();
if ( !sld.isEmpty() )
{
layers = sldStylizedLayers( sld );
Expand Down Expand Up @@ -903,7 +903,7 @@ namespace QgsWms
restorer.reset( new QgsLayerRestorer( mNicknameLayers.values() ) );

// init stylized layers according to LAYERS/STYLES or SLD
QString sld = mWmsParameters.sld();
QString sld = mWmsParameters.sldBody();
if ( !sld.isEmpty() )
layers = sldStylizedLayers( sld );
else
Expand Down

0 comments on commit ad7d03c

Please sign in to comment.