25#include <QNetworkReply>
26#include <QNetworkRequest>
33 const QVariant defaultValue )
35 , mDefaultValue( defaultValue )
41 return QVariant::typeToName(
mType );
48 QString cStr =
mValue.toString();
50 if ( !cStr.isEmpty() )
53 if ( cStr.startsWith( QLatin1String(
"0x" ), Qt::CaseInsensitive ) )
55 cStr.replace( 0, 2, QStringLiteral(
"#" ) );
58 color = QColor( cStr );
68 QString value =
mValue.toString();
70 if ( value.isEmpty() && defaultValue )
80#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
81 return toString().split( delimiter, QString::SkipEmptyParts );
83 return toString().split( delimiter, Qt::SkipEmptyParts );
91#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
92 list =
toString().split( delimiter, QString::KeepEmptyParts );
94 list =
toString().split( delimiter, Qt::KeepEmptyParts );
104 QList<QgsGeometry> geoms;
106 const auto constStringList(
toStringList( delimiter ) );
107 for (
const auto &wkt : constStringList )
118 return QList<QgsGeometry>();
131 while ( pos < filter.size() )
133 if ( pos + 1 < filter.size() && filter[pos] ==
'(' && filter[pos + 1] ==
'<' )
136 int posEnd = filter.indexOf(
"Filter>)", pos );
139 posEnd = filter.size();
141 filters.append( filter.mid( pos + 1, posEnd - pos + 6 ) );
144 else if ( pos + 1 < filter.size() && filter[pos] ==
'(' && filter[pos + 1] ==
')' )
147 filters.append(
"" );
150 else if ( filter[pos] ==
'<' && pos + 7 < filter.size() && filter.mid( pos + 1, 6 ).compare( QLatin1String(
"Filter" ) ) == 0 )
153 filters.append( filter.mid( pos ) );
171 auto isOgcFilter = [filter]()
173 return filter.contains( QStringLiteral(
"<Filter>" ) ) || filter.contains( QStringLiteral(
"()" ) );
176 while ( pos < filter.size() )
178 int posEnd = filter.indexOf(
';', pos );
180 if ( posEnd == pos + 1 )
182 if ( ! isOgcFilter() )
183 filters.append( QString() );
188 if ( ! isOgcFilter() )
189 filters.append( filter.mid( pos, posEnd - pos ) );
201 if ( ! filter.isEmpty() && filter.back() ==
';' )
203 filters.append( QString() );
212 QList<QColor> colors;
214 const auto constStringList(
toStringList( delimiter ) );
215 for (
const auto &part : constStringList )
217 QString cStr( part );
218 if ( !cStr.isEmpty() )
221 if ( cStr.startsWith( QLatin1String(
"0x" ), Qt::CaseInsensitive ) )
223 cStr.replace( 0, 2, QStringLiteral(
"#" ) );
226 const QColor color = QColor( cStr );
227 ok = color.isValid();
231 return QList<QColor>();
234 colors.append( color );
246 const auto constStringList(
toStringList( delimiter ) );
247 for (
const auto &part : constStringList )
249 const int val = part.toInt( &ok );
267 const auto constStringList(
toStringList( delimiter ) );
268 for (
const auto &part : constStringList )
270 const double val = part.toDouble( &ok );
274 return QList<double>();
288 if ( !
mValue.toString().isEmpty() )
292 if ( corners.size() == 4 )
296 for (
int i = 0; i < 4; i++ )
298 corners[i].replace(
' ',
'+' );
299 d[i] = corners[i].toDouble( &ok );
306 if ( d[0] > d[2] || d[1] > d[3] )
329 const QUrl url =
toUrl( ok );
335 QNetworkRequest request( url );
336 request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache );
337 request.setAttribute( QNetworkRequest::CacheSaveControlAttribute,
true );
347 QObject::tr(
"Request failed [error: %1 - url: %2]" ).arg( newReq.
errorMessage(), url.toString() ),
348 QStringLiteral(
"Server" ) );
354 ok = !reply.
content().isEmpty();
363 if ( !
mValue.toString().isEmpty() )
368 ok = ( !val.isEmpty() && val.isValid() );
377 if ( !
mValue.toString().isEmpty() )
379 val =
mValue.toInt( &ok );
389 if ( !
mValue.toString().isEmpty() )
402 if ( !
mValue.toString().isEmpty() )
404 val =
mValue.toDouble( &ok );
424 const QVariant::Type type,
const QVariant defaultValue )
434 return QStringLiteral(
"VERSION" );
438 const QMetaEnum metaEnum( QMetaEnum::fromType<QgsServerParameter::Name>() );
439 return metaEnum.valueToKey(
name );
445 if (
name.compare( QLatin1String(
"VERSION" ) ) == 0 )
451 const QMetaEnum metaEnum( QMetaEnum::fromType<QgsServerParameter::Name>() );
458 const QString msg = QString(
"%1 ('%2') cannot be converted into %3" ).arg(
name(
mName ),
mValue.toString(),
typeName() );
483 mParameters[ parameter.
mName ] = parameter;
489 query.addQueryItem( key,
value );
495 QUrlQuery query = mUrlQuery;
497 if ( query.isEmpty() )
501 const auto constMap(
toMap().toStdMap() );
502 for (
const auto ¶m : constMap )
504 const QString
value = QUrl::toPercentEncoding( QString( param.second ) );
505 query.addQueryItem( param.first,
value );
526 if ( mParameters.contains( paramName ) )
528 mParameters.take( paramName );
552 if ( serviceValue.isEmpty() )
555 if (
request() == QLatin1String(
"GetMap" ) \
556 ||
request() == QLatin1String(
"GetFeatureInfo" ) )
558 serviceValue =
"WMS";
569 for (
const auto ¶meter : mParameters.toStdMap() )
576 params[
"VERSION"] = parameter.second.mValue.toString();
581 params[paramName] = parameter.second.mValue.toString();
607 return mParameters[name].mValue;
613 QUrlQuery cleanQuery( query );
614 cleanQuery.setQuery( query.query().replace(
'+', QLatin1String(
"%20" ) ) );
617 const auto constQueryItems( cleanQuery.queryItems( QUrl::FullyDecoded ) );
618 for (
const auto &item : constQueryItems )
623 mParameters[name].mValue = item.second;
624 if ( ! mParameters[name].isValid() )
626 mParameters[name].raiseError();
629 else if ( item.first.compare( QLatin1String(
"VERSION" ), Qt::CaseInsensitive ) == 0 )
632 mParameters[name].mValue = item.second;
633 if ( ! mParameters[name].isValid() )
635 mParameters[name].raiseError();
Exception thrown in case of malformed request.
A thread safe class for performing blocking (sync) network requests, with full support for QGIS proxy...
ErrorCode get(QNetworkRequest &request, bool forceRefresh=false, QgsFeedback *feedback=nullptr)
Performs a "get" operation on the specified request.
QString errorMessage() const
Returns the error message string, after a get(), post(), head() or put() request has been made.
@ NoError
No error was encountered.
QgsNetworkReplyContent reply() const
Returns the content of the network reply, after a get(), post(), head() or put() request has been mad...
A geometry is the spatial representation of a feature.
static QgsGeometry fromWkt(const QString &wkt)
Creates a new geometry from a WKT string.
bool isGeosValid(Qgis::GeometryValidityFlags flags=Qgis::GeometryValidityFlags()) const
Checks validity of the geometry using GEOS.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
Encapsulates a network reply within a container which is inexpensive to copy and safe to pass between...
QByteArray content() const
Returns the reply content.
A rectangle specified with double values.
QString toString(int precision=16) const
Returns a string representation of form xmin,ymin : xmax,ymax Coordinates will be truncated to the sp...
Definition of a parameter with basic conversion methods.
QList< QgsGeometry > toGeomList(bool &ok, char delimiter=',') const
Converts the parameter into a list of geometries.
QString loadUrl(bool &ok) const
Loads the data associated to the parameter converted into an url.
QUrl toUrl(bool &ok) const
Converts the parameter into an url.
QString toString(bool defaultValue=false) const
Converts the parameter into a string.
bool toBool() const
Converts the parameter into a boolean.
QList< double > toDoubleList(bool &ok, char delimiter=',') const
Converts the parameter into a list of doubles.
QStringList toStringList(char delimiter=',', bool skipEmptyParts=true) const
Converts the parameter into a list of strings.
virtual bool isValid() const
Returns true if the parameter is valid, false otherwise.
QgsServerParameterDefinition(const QVariant::Type type=QVariant::String, const QVariant defaultValue=QVariant(""))
Constructor for QgsServerParameterDefinition.
QString typeName() const
Returns the type of the parameter as a string.
static void raiseError(const QString &msg)
Raises an exception in case of an invalid parameters.
QStringList toExpressionList() const
Converts the parameter into a list of QGIS expressions.
int toInt(bool &ok) const
Converts the parameter into an integer.
QList< int > toIntList(bool &ok, char delimiter=',') const
Converts the parameter into a list of integers.
QColor toColor(bool &ok) const
Converts the parameter into a color.
double toDouble(bool &ok) const
Converts the parameter into a double.
QgsRectangle toRectangle(bool &ok) const
Converts the parameter into a rectangle.
QList< QColor > toColorList(bool &ok, char delimiter=',') const
Converts the parameter into a list of colors.
QStringList toOgcFilterList() const
Converts the parameter into a list of OGC filters.
Parameter common to all services (WMS, WFS, ...)
QgsServerParameter::Name mName
QgsServerParameter(const QgsServerParameter::Name name=QgsServerParameter::UNKNOWN, const QVariant::Type type=QVariant::String, const QVariant defaultValue=QVariant(""))
Constructor for QgsServerParameter.
Name
Parameter's name common to all services.
void raiseError() const
Raises an error in case of an invalid conversion.
static QString name(const QgsServerParameter::Name name)
Converts a parameter's name into its string representation.
QgsServerParameters provides an interface to retrieve and manipulate global parameters received from ...
QMap< QString, QString > toMap() const
Returns all parameters in a map.
QString map() const
Returns MAP parameter as a string or an empty string if not defined.
QgsServerParameters()
Constructor.
void add(const QString &key, const QString &value)
Adds a parameter.
QString service() const
Returns SERVICE parameter as a string or an empty string if not defined.
virtual QString request() const
Returns REQUEST parameter as a string or an empty string if not defined.
void clear()
Removes all parameters.
QString fileName() const
Returns FILE_NAME parameter as a string or an empty string if not defined.
virtual bool loadParameter(const QString &name, const QString &value)
Loads a parameter with a specific value.
QUrlQuery urlQuery() const
Returns a url query with underlying parameters.
QMap< QString, QString > mUnmanagedParameters
void load(const QUrlQuery &query)
Loads new parameters.
void remove(const QString &key)
Removes a parameter.
virtual QString version() const
Returns VERSION parameter as a string or an empty string if not defined.
QString value(const QString &key) const
Returns the value of a parameter.
static bool isNull(const QVariant &variant)
Returns true if the specified variant should be considered a NULL value.