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 return toString().split( delimiter, Qt::SkipEmptyParts );
87 list =
toString().split( delimiter, Qt::KeepEmptyParts );
96 QList<QgsGeometry> geoms;
99 for (
const auto &wkt : constStringList )
110 return QList<QgsGeometry>();
123 while ( pos < filter.size() )
125 if ( pos + 1 < filter.size() && filter[pos] ==
'(' && filter[pos + 1] ==
'<' )
128 int posEnd = filter.indexOf(
"Filter>)", pos );
131 posEnd = filter.size();
133 filters.append( filter.mid( pos + 1, posEnd - pos + 6 ) );
136 else if ( pos + 1 < filter.size() && filter[pos] ==
'(' && filter[pos + 1] ==
')' )
139 filters.append(
"" );
142 else if ( filter[pos] ==
'<' && pos + 7 < filter.size() && filter.mid( pos + 1, 6 ).compare( QLatin1String(
"Filter" ) ) == 0 )
145 filters.append( filter.mid( pos ) );
163 auto isOgcFilter = [filter]()
165 return filter.contains( QStringLiteral(
"<Filter>" ) ) || filter.contains( QStringLiteral(
"()" ) );
168 while ( pos < filter.size() )
170 int posEnd = filter.indexOf(
';', pos );
172 if ( posEnd == pos + 1 )
174 if ( ! isOgcFilter() )
175 filters.append( QString() );
180 if ( ! isOgcFilter() )
181 filters.append( filter.mid( pos, posEnd - pos ) );
193 if ( ! filter.isEmpty() && filter.back() ==
';' )
195 filters.append( QString() );
204 QList<QColor> colors;
206 const auto constStringList(
toStringList( delimiter ) );
207 for (
const auto &part : constStringList )
209 QString cStr( part );
210 if ( !cStr.isEmpty() )
213 if ( cStr.startsWith( QLatin1String(
"0x" ), Qt::CaseInsensitive ) )
215 cStr.replace( 0, 2, QStringLiteral(
"#" ) );
218 const QColor color = QColor( cStr );
219 ok = color.isValid();
223 return QList<QColor>();
226 colors.append( color );
238 const auto constStringList(
toStringList( delimiter ) );
239 for (
const auto &part : constStringList )
241 const int val = part.toInt( &ok );
259 const auto constStringList(
toStringList( delimiter ) );
260 for (
const auto &part : constStringList )
262 const double val = part.toDouble( &ok );
266 return QList<double>();
280 if ( !
mValue.toString().isEmpty() )
284 if ( corners.size() == 4 )
288 for (
int i = 0; i < 4; i++ )
290 corners[i].replace(
' ',
'+' );
291 d[i] = corners[i].toDouble( &ok );
298 if ( d[0] > d[2] || d[1] > d[3] )
321 const QUrl url =
toUrl( ok );
327 QNetworkRequest request( url );
328 request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache );
329 request.setAttribute( QNetworkRequest::CacheSaveControlAttribute,
true );
339 QObject::tr(
"Request failed [error: %1 - url: %2]" ).arg( newReq.
errorMessage(), url.toString() ),
340 QStringLiteral(
"Server" ) );
346 ok = !reply.
content().isEmpty();
355 if ( !
mValue.toString().isEmpty() )
360 ok = ( !val.isEmpty() && val.isValid() );
369 if ( !
mValue.toString().isEmpty() )
371 val =
mValue.toInt( &ok );
381 if ( !
mValue.toString().isEmpty() )
394 if ( !
mValue.toString().isEmpty() )
396 val =
mValue.toDouble( &ok );
416 const QVariant::Type type,
const QVariant defaultValue )
426 return QStringLiteral(
"VERSION" );
430 const QMetaEnum metaEnum( QMetaEnum::fromType<QgsServerParameter::Name>() );
431 return metaEnum.valueToKey(
name );
437 if (
name.compare( QLatin1String(
"VERSION" ) ) == 0 )
443 const QMetaEnum metaEnum( QMetaEnum::fromType<QgsServerParameter::Name>() );
450 const QString msg = QString(
"%1 ('%2') cannot be converted into %3" ).arg(
name(
mName ),
mValue.toString(),
typeName() );
475 mParameters[ parameter.
mName ] = parameter;
481 query.addQueryItem( key,
value );
487 QUrlQuery query = mUrlQuery;
489 if ( query.isEmpty() )
493 const auto constMap(
toMap().toStdMap() );
494 for (
const auto ¶m : constMap )
496 const QString
value = QUrl::toPercentEncoding( QString( param.second ) );
497 query.addQueryItem( param.first,
value );
518 if ( mParameters.contains( paramName ) )
520 mParameters.take( paramName );
544 if ( serviceValue.isEmpty() )
547 if (
request() == QLatin1String(
"GetMap" ) \
548 ||
request() == QLatin1String(
"GetFeatureInfo" ) )
550 serviceValue =
"WMS";
561 for (
const auto ¶meter : mParameters.toStdMap() )
568 params[
"VERSION"] = parameter.second.mValue.toString();
573 params[paramName] = parameter.second.mValue.toString();
599 return mParameters[name].mValue;
605 QUrlQuery cleanQuery( query );
606 cleanQuery.setQuery( query.query().replace(
'+', QLatin1String(
"%20" ) ) );
609 const auto constQueryItems( cleanQuery.queryItems( QUrl::FullyDecoded ) );
610 for (
const auto &item : constQueryItems )
615 mParameters[name].mValue = item.second;
616 if ( ! mParameters[name].isValid() )
618 mParameters[name].raiseError();
621 else if ( item.first.compare( QLatin1String(
"VERSION" ), Qt::CaseInsensitive ) == 0 )
624 mParameters[name].mValue = item.second;
625 if ( ! mParameters[name].isValid() )
627 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, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.