24 #include <QNetworkReply> 25 #include <QNetworkRequest> 32 const QVariant defaultValue )
34 , mDefaultValue( defaultValue )
40 return QVariant::typeToName(
mType );
47 QString cStr =
mValue.toString();
49 if ( !cStr.isEmpty() )
52 if ( cStr.startsWith( QLatin1String(
"0x" ), Qt::CaseInsensitive ) )
54 cStr.replace( 0, 2, QStringLiteral(
"#" ) );
57 color = QColor( cStr );
72 return toString().split( delimiter, QString::SkipEmptyParts );
78 QList<QgsGeometry> geoms;
91 return QList<QgsGeometry>();
101 QList<QColor> colors;
105 QString cStr( part );
106 if ( !cStr.isEmpty() )
109 if ( cStr.startsWith( QLatin1String(
"0x" ), Qt::CaseInsensitive ) )
111 cStr.replace( 0, 2, QStringLiteral(
"#" ) );
114 const QColor color = QColor( cStr );
115 ok = color.isValid();
119 return QList<QColor>();
122 colors.append( color );
136 const int val = part.toInt( &ok );
156 const double val = part.toDouble( &ok );
160 return QList<double>();
174 if ( !
mValue.toString().isEmpty() )
176 QStringList corners =
mValue.toString().split(
',' );
178 if ( corners.size() == 4 )
182 for (
int i = 0; i < 4; i++ )
184 corners[i].replace(
' ',
'+' );
185 d[i] = corners[i].toDouble( &ok );
192 if ( d[0] > d[2] || d[1] > d[3] )
215 QUrl url =
toUrl( ok );
227 QObject::tr(
"Request started [url: %1]" ).arg( url.toString() ),
228 QStringLiteral(
"Server" ) );
229 QNetworkRequest request( url );
230 request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache );
231 request.setAttribute( QNetworkRequest::CacheSaveControlAttribute,
true );
235 loop.exec( QEventLoop::ExcludeUserInputEvents );
237 QNetworkReply *reply = fetcher.
reply();
242 QObject::tr(
"Request failed [error: no reply - url: %1]" ).arg( url.toString() ),
243 QStringLiteral(
"Server" ) );
247 QVariant status = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute );
248 if ( !status.isNull() && status.toInt() >= 400 )
251 if ( reply->error() != QNetworkReply::NoError )
254 QObject::tr(
"Request failed [error: %1 - url: %2]" ).arg( reply->errorString(), reply->url().toString() ),
255 QStringLiteral(
"Server" ) );
257 QVariant phrase = reply->attribute( QNetworkRequest::HttpReasonPhraseAttribute );
259 QObject::tr(
"Request error [status: %1 - reason phrase: %2] for %3" ).arg( status.toInt() ).arg( phrase.toString(), reply->url().toString() ),
260 QStringLiteral(
"Server" ) );
264 if ( reply->error() != QNetworkReply::NoError )
268 QObject::tr(
"Request failed [error: %1 - url: %2]" ).arg( reply->errorString(), reply->url().toString() ),
269 QStringLiteral(
"Server" ) );
274 QObject::tr(
"Request finished [url: %1]" ).arg( url.toString() ),
275 QStringLiteral(
"Server" ) );
278 ok = ( !content.isEmpty() );
287 if ( !
mValue.toString().isEmpty() )
292 ok = ( !val.isEmpty() && val.isValid() );
301 if ( !
mValue.toString().isEmpty() )
303 val =
mValue.toInt( &ok );
313 if ( !
mValue.toString().isEmpty() )
326 if ( !
mValue.toString().isEmpty() )
328 val =
mValue.toDouble( &ok );
348 const QVariant::Type type,
const QVariant defaultValue )
358 return QStringLiteral(
"VERSION" );
362 const QMetaEnum metaEnum( QMetaEnum::fromType<QgsServerParameter::Name>() );
363 return metaEnum.valueToKey( name );
369 if ( name.compare( QLatin1String(
"VERSION" ) ) == 0 )
375 const QMetaEnum metaEnum( QMetaEnum::fromType<QgsServerParameter::Name>() );
382 const QString msg = QString(
"%1 ('%2') cannot be converted into %3" ).arg(
name(
mName ),
mValue.toString(),
typeName() );
407 mParameters[ parameter.
mName ] = parameter;
413 query.addQueryItem( key, value );
419 QUrlQuery query = mUrlQuery;
421 if ( query.isEmpty() )
425 for (
auto param :
toMap().toStdMap() )
427 query.addQueryItem( param.first, param.second );
448 if ( mParameters.contains( paramName ) )
450 mParameters.take( paramName );
474 if ( serviceValue.isEmpty() )
477 if (
request() == QLatin1String(
"GetMap" ) \
478 ||
request() == QLatin1String(
"GetFeatureInfo" ) )
480 serviceValue =
"WMS";
491 for (
const auto ¶meter : mParameters.toStdMap() )
493 if ( parameter.second.
mValue.isNull() )
498 params[
"VERSION"] = parameter.second.
mValue.toString();
503 params[paramName] = parameter.second.
mValue.toString();
522 return mParameters[name].mValue;
528 QUrlQuery cleanQuery( query );
529 cleanQuery.setQuery( query.query().replace(
'+', QStringLiteral(
"%20" ) ) );
532 for (
const auto &item : cleanQuery.queryItems( QUrl::FullyDecoded ) )
537 mParameters[name].mValue = item.second;
538 if ( ! mParameters[name].isValid() )
540 mParameters[name].raiseError();
543 else if ( item.first.compare( QLatin1String(
"VERSION" ), Qt::CaseInsensitive ) == 0 )
546 mParameters[name].mValue = item.second;
547 if ( ! mParameters[name].isValid() )
549 mParameters[name].raiseError();
QMap< QString, QString > toMap() const
Returns all parameters in a map.
QString request() const
Returns REQUEST parameter as a string or an empty string if not defined.
A rectangle specified with double values.
Name
Parameter's name common to all services.
QString typeName() const
Returns the type of the parameter as a string.
QMap< QString, QString > mUnmanagedParameters
Exception thrown in case of malformed request.
QString value(const QString &key) const
Returns the value of a parameter.
static void raiseError(const QString &msg)
Raises an exception in case of an invalid parameters.
void remove(const QString &key)
Removes a parameter.
QString loadUrl(bool &ok) const
Loads the data associated to the parameter converted into an url.
A geometry is the spatial representation of a feature.
void load(const QUrlQuery &query)
Loads new parameters.
QNetworkReply * reply()
Returns a reference to the network reply.
QString contentAsString() const
Returns the fetched content as a string.
bool isGeosValid() const
Checks validity of the geometry using GEOS.
QgsServerParameter(const QgsServerParameter::Name name=QgsServerParameter::UNKNOWN, const QVariant::Type type=QVariant::String, const QVariant defaultValue=QVariant(""))
Constructor for QgsServerParameter.
QString map() const
Returns MAP parameter as a string or an empty string if not defined.
double toDouble(bool &ok) const
Converts the parameter into a double.
static QString name(const QgsServerParameter::Name name)
Converts a parameter's name into its string representation.
QgsServerParameters()
Constructor.
QUrl toUrl(bool &ok) const
Converts the parameter into an url.
QList< double > toDoubleList(bool &ok, char delimiter=',') const
Converts the parameter into a list of doubles.
HTTP network content fetcher.
QList< int > toIntList(bool &ok, char delimiter=',') const
Converts the parameter into a list of integers.
QgsRectangle toRectangle(bool &ok) const
Converts the parameter into a rectangle.
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.
QUrlQuery urlQuery() const
Returns a url query with underlying parameters.
virtual bool loadParameter(const QString &name, const QString &value)
Loads a parameter with a specific value.
void finished()
Emitted when content has loaded.
Parameter common to all services (WMS, WFS, ...)
QgsServerParameterDefinition(const QVariant::Type type=QVariant::String, const QVariant defaultValue=QVariant(""))
Constructor for QgsServerParameterDefinition.
QColor toColor(bool &ok) const
Converts the parameter into a color.
QgsServerParameter::Name mName
static QgsGeometry fromWkt(const QString &wkt)
Creates a new geometry from a WKT string.
QString service() const
Returns SERVICE parameter as a string or an empty string if not defined.
int toInt(bool &ok) const
Converts the parameter into an integer.
void add(const QString &key, const QString &value)
Adds a parameter.
QgsServerParameters provides an interface to retrieve and manipulate global parameters received from ...
virtual bool isValid() const
Returns true if the parameter is valid, false otherwise.
QString toString() const
Converts the parameter into a string.
QList< QgsGeometry > toGeomList(bool &ok, char delimiter=',') const
Converts the parameter into a list of geometries.
void clear()
Removes all parameters.
void raiseError() const
Raises an error in case of an invalid conversion.
void fetchContent(const QUrl &url)
Fetches content from a remote URL and handles redirects.
QList< QColor > toColorList(bool &ok, char delimiter=',') const
Converts the parameter into a list of colors.
bool toBool() const
Converts the parameter into a boolean.
QStringList toStringList(char delimiter=',') const
Converts the parameter into a list of strings.
QString fileName() const
Returns FILE_NAME parameter as a string or an empty string if not defined.
Definition of a parameter with basic conversion methods.