25#include <QNetworkReply>
26#include <QNetworkRequest>
33 const QVariant defaultValue )
35 , mDefaultValue( defaultValue )
46 return QVariant::typeToName(
mType );
53 QString cStr =
mValue.toString();
55 if ( !cStr.isEmpty() )
58 if ( cStr.startsWith( QLatin1String(
"0x" ), Qt::CaseInsensitive ) )
60 cStr.replace( 0, 2, QStringLiteral(
"#" ) );
63 color = QColor( cStr );
73 QString value =
mValue.toString();
75 if ( value.isEmpty() && defaultValue )
85 return toString().split( delimiter, Qt::SkipEmptyParts );
92 list =
toString().split( delimiter, Qt::KeepEmptyParts );
101 QList<QgsGeometry> geoms;
103 const auto constStringList(
toStringList( delimiter ) );
104 for (
const auto &wkt : constStringList )
115 return QList<QgsGeometry>();
128 while ( pos < filter.size() )
130 if ( pos + 1 < filter.size() && filter[pos] ==
'(' && filter[pos + 1] ==
'<' )
133 int posEnd = filter.indexOf(
"Filter>)", pos );
136 posEnd = filter.size();
138 filters.append( filter.mid( pos + 1, posEnd - pos + 6 ) );
141 else if ( pos + 1 < filter.size() && filter[pos] ==
'(' && filter[pos + 1] ==
')' )
144 filters.append(
"" );
147 else if ( filter[pos] ==
'<' && pos + 7 < filter.size() && filter.mid( pos + 1, 6 ).compare( QLatin1String(
"Filter" ) ) == 0 )
150 filters.append( filter.mid( pos ) );
168 auto isOgcFilter = [filter]()
170 return filter.contains( QStringLiteral(
"<Filter>" ) ) || filter.contains( QStringLiteral(
"()" ) );
173 while ( pos < filter.size() )
175 int posEnd = filter.indexOf(
';', pos );
177 if ( posEnd == pos + 1 )
179 if ( ! isOgcFilter() )
180 filters.append( QString() );
185 if ( ! isOgcFilter() )
186 filters.append( filter.mid( pos, posEnd - pos ) );
198 if ( ! filter.isEmpty() && filter.back() ==
';' )
200 filters.append( QString() );
209 QList<QColor> colors;
211 const auto constStringList(
toStringList( delimiter ) );
212 for (
const auto &part : constStringList )
214 QString cStr( part );
215 if ( !cStr.isEmpty() )
218 if ( cStr.startsWith( QLatin1String(
"0x" ), Qt::CaseInsensitive ) )
220 cStr.replace( 0, 2, QStringLiteral(
"#" ) );
223 const QColor color = QColor( cStr );
224 ok = color.isValid();
228 return QList<QColor>();
231 colors.append( color );
243 const auto constStringList(
toStringList( delimiter ) );
244 for (
const auto &part : constStringList )
246 const int val = part.toInt( &ok );
264 const auto constStringList(
toStringList( delimiter ) );
265 for (
const auto &part : constStringList )
267 const double val = part.toDouble( &ok );
271 return QList<double>();
285 if ( !
mValue.toString().isEmpty() )
289 if ( corners.size() == 4 )
293 for (
int i = 0; i < 4; i++ )
295 corners[i].replace(
' ',
'+' );
296 d[i] = corners[i].toDouble( &ok );
303 if ( d[0] > d[2] || d[1] > d[3] )
326 const QUrl url =
toUrl( ok );
332 QNetworkRequest request( url );
333 request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache );
334 request.setAttribute( QNetworkRequest::CacheSaveControlAttribute,
true );
344 QObject::tr(
"Request failed [error: %1 - url: %2]" ).arg( newReq.
errorMessage(), url.toString() ),
345 QStringLiteral(
"Server" ) );
351 ok = !reply.
content().isEmpty();
360 if ( !
mValue.toString().isEmpty() )
365 ok = ( !val.isEmpty() && val.isValid() );
374 if ( !
mValue.toString().isEmpty() )
376 val =
mValue.toInt( &ok );
386 if ( !
mValue.toString().isEmpty() )
399 if ( !
mValue.toString().isEmpty() )
401 val =
mValue.toDouble( &ok );
421 const QMetaType::Type type,
const QVariant defaultValue )
428 const QVariant::Type type,
429 const QVariant defaultValue )
438 return QStringLiteral(
"VERSION" );
442 const QMetaEnum metaEnum( QMetaEnum::fromType<QgsServerParameter::Name>() );
443 return metaEnum.valueToKey(
name );
449 if (
name.compare( QLatin1String(
"VERSION" ) ) == 0 )
455 const QMetaEnum metaEnum( QMetaEnum::fromType<QgsServerParameter::Name>() );
462 const QString msg = QString(
"%1 ('%2') cannot be converted into %3" ).arg(
name(
mName ),
mValue.toString(),
typeName() );
487 mParameters[ parameter.
mName ] = parameter;
493 query.addQueryItem( key,
value );
499 QUrlQuery query = mUrlQuery;
501 if ( query.isEmpty() )
505 const auto constMap(
toMap().toStdMap() );
506 for (
const auto ¶m : constMap )
508 const QString
value = QUrl::toPercentEncoding( QString( param.second ) );
509 query.addQueryItem( param.first,
value );
530 if ( mParameters.contains( paramName ) )
532 mParameters.take( paramName );
556 if ( serviceValue.isEmpty() )
559 if (
request() == QLatin1String(
"GetMap" ) \
560 ||
request() == QLatin1String(
"GetFeatureInfo" ) )
562 serviceValue =
"WMS";
573 for (
const auto ¶meter : mParameters.toStdMap() )
580 params[
"VERSION"] = parameter.second.mValue.toString();
585 params[paramName] = parameter.second.mValue.toString();
611 return mParameters[name].mValue;
617 QUrlQuery cleanQuery( query );
618 cleanQuery.setQuery( query.query().replace(
'+', QLatin1String(
"%20" ) ) );
621 const auto constQueryItems( cleanQuery.queryItems( QUrl::FullyDecoded ) );
622 for (
const auto &item : constQueryItems )
627 mParameters[name].mValue = item.second;
628 if ( ! mParameters[name].isValid() )
630 mParameters[name].raiseError();
633 else if ( item.first.compare( QLatin1String(
"VERSION" ), Qt::CaseInsensitive ) == 0 )
636 mParameters[name].mValue = item.second;
637 if ( ! mParameters[name].isValid() )
639 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...
QString errorMessage() const
Returns the error message string, after a get(), post(), head() or put() request has been made.
ErrorCode get(QNetworkRequest &request, bool forceRefresh=false, QgsFeedback *feedback=nullptr, RequestFlags requestFlags=QgsBlockingNetworkRequest::RequestFlags())
Performs a "get" operation on the specified request.
@ 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 Q_INVOKABLE 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.
QgsServerParameterDefinition(const QMetaType::Type type=QMetaType::Type::QString, const QVariant defaultValue=QVariant(""))
Constructor for QgsServerParameterDefinition.
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.
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 QMetaType::Type type=QMetaType::Type::QString, 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.
Contains utility functions for working with QVariants and QVariant types.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.