20#include "moc_qgsserverparameters.cpp"
26#include <QNetworkReply>
27#include <QNetworkRequest>
34 const QVariant defaultValue )
36 , mDefaultValue( defaultValue )
47 return QVariant::typeToName(
mType );
54 QString cStr =
mValue.toString();
56 if ( !cStr.isEmpty() )
59 if ( cStr.startsWith( QLatin1String(
"0x" ), Qt::CaseInsensitive ) )
61 cStr.replace( 0, 2, QStringLiteral(
"#" ) );
64 color = QColor( cStr );
74 QString value =
mValue.toString();
76 if ( value.isEmpty() && defaultValue )
86 return toString().split( delimiter, Qt::SkipEmptyParts );
93 list =
toString().split( delimiter, Qt::KeepEmptyParts );
102 QList<QgsGeometry> geoms;
104 const auto constStringList(
toStringList( delimiter ) );
105 for (
const auto &wkt : constStringList )
116 return QList<QgsGeometry>();
129 while ( pos < filter.size() )
131 if ( pos + 1 < filter.size() && filter[pos] ==
'(' && filter[pos + 1] ==
'<' )
134 int posEnd = filter.indexOf(
"Filter>)", pos );
137 posEnd = filter.size();
139 filters.append( filter.mid( pos + 1, posEnd - pos + 6 ) );
142 else if ( pos + 1 < filter.size() && filter[pos] ==
'(' && filter[pos + 1] ==
')' )
145 filters.append(
"" );
148 else if ( filter[pos] ==
'<' && pos + 7 < filter.size() && filter.mid( pos + 1, 6 ).compare( QLatin1String(
"Filter" ) ) == 0 )
151 filters.append( filter.mid( pos ) );
169 auto isOgcFilter = [filter]()
171 return filter.contains( QStringLiteral(
"<Filter>" ) ) || filter.contains( QStringLiteral(
"()" ) );
174 while ( pos < filter.size() )
176 int posEnd = filter.indexOf(
';', pos );
178 if ( posEnd == pos + 1 )
180 if ( ! isOgcFilter() )
181 filters.append( QString() );
186 if ( ! isOgcFilter() )
187 filters.append( filter.mid( pos, posEnd - pos ) );
199 if ( ! filter.isEmpty() && filter.back() ==
';' )
201 filters.append( QString() );
210 QList<QColor> colors;
212 const auto constStringList(
toStringList( delimiter ) );
213 for (
const auto &part : constStringList )
215 QString cStr( part );
216 if ( !cStr.isEmpty() )
219 if ( cStr.startsWith( QLatin1String(
"0x" ), Qt::CaseInsensitive ) )
221 cStr.replace( 0, 2, QStringLiteral(
"#" ) );
224 const QColor color = QColor( cStr );
225 ok = color.isValid();
229 return QList<QColor>();
232 colors.append( color );
244 const auto constStringList(
toStringList( delimiter ) );
245 for (
const auto &part : constStringList )
247 const int val = part.toInt( &ok );
265 const auto constStringList(
toStringList( delimiter ) );
266 for (
const auto &part : constStringList )
268 const double val = part.toDouble( &ok );
272 return QList<double>();
286 if ( !
mValue.toString().isEmpty() )
290 if ( corners.size() == 4 )
294 for (
int i = 0; i < 4; i++ )
296 corners[i].replace(
' ',
'+' );
297 d[i] = corners[i].toDouble( &ok );
304 if ( d[0] > d[2] || d[1] > d[3] )
327 const QUrl url =
toUrl( ok );
333 QNetworkRequest request( url );
334 request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache );
335 request.setAttribute( QNetworkRequest::CacheSaveControlAttribute,
true );
345 QObject::tr(
"Request failed [error: %1 - url: %2]" ).arg( newReq.
errorMessage(), url.toString() ),
346 QStringLiteral(
"Server" ) );
352 ok = !reply.
content().isEmpty();
361 if ( !
mValue.toString().isEmpty() )
366 ok = ( !val.isEmpty() && val.isValid() );
375 if ( !
mValue.toString().isEmpty() )
377 val =
mValue.toInt( &ok );
387 if ( !
mValue.toString().isEmpty() )
400 if ( !
mValue.toString().isEmpty() )
402 val =
mValue.toDouble( &ok );
422 const QMetaType::Type type,
const QVariant defaultValue )
429 const QVariant::Type type,
430 const QVariant defaultValue )
439 return QStringLiteral(
"VERSION" );
443 const QMetaEnum metaEnum( QMetaEnum::fromType<QgsServerParameter::Name>() );
444 return metaEnum.valueToKey(
name );
450 if (
name.compare( QLatin1String(
"VERSION" ) ) == 0 )
456 const QMetaEnum metaEnum( QMetaEnum::fromType<QgsServerParameter::Name>() );
463 const QString msg = QString(
"%1 ('%2') cannot be converted into %3" ).arg(
name(
mName ),
mValue.toString(),
typeName() );
488 mParameters[ parameter.
mName ] = parameter;
494 query.addQueryItem( key,
value );
500 QUrlQuery query = mUrlQuery;
502 if ( query.isEmpty() )
506 const auto constMap(
toMap().toStdMap() );
507 for (
const auto ¶m : constMap )
509 const QString
value = QUrl::toPercentEncoding( QString( param.second ) );
510 query.addQueryItem( param.first,
value );
531 if ( mParameters.contains( paramName ) )
533 mParameters.take( paramName );
557 if ( serviceValue.isEmpty() )
560 if (
request() == QLatin1String(
"GetMap" ) \
561 ||
request() == QLatin1String(
"GetFeatureInfo" ) )
563 serviceValue =
"WMS";
574 for (
const auto ¶meter : mParameters.toStdMap() )
581 params[
"VERSION"] = parameter.second.mValue.toString();
586 params[paramName] = parameter.second.mValue.toString();
612 return mParameters[name].mValue;
618 QUrlQuery cleanQuery( query );
619 cleanQuery.setQuery( query.query().replace(
'+', QLatin1String(
"%20" ) ) );
622 const auto constQueryItems( cleanQuery.queryItems( QUrl::FullyDecoded ) );
623 for (
const auto &item : constQueryItems )
628 mParameters[name].mValue = item.second;
629 if ( ! mParameters[name].isValid() )
631 mParameters[name].raiseError();
634 else if ( item.first.compare( QLatin1String(
"VERSION" ), Qt::CaseInsensitive ) == 0 )
637 mParameters[name].mValue = item.second;
638 if ( ! mParameters[name].isValid() )
640 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.