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 );
67 QString value =
mValue.toString();
69 if ( value.isEmpty() && defaultValue )
79 #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
80 return toString().split( delimiter, QString::SkipEmptyParts );
82 return toString().split( delimiter, Qt::SkipEmptyParts );
90 #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
91 list =
toString().split( delimiter, QString::KeepEmptyParts );
93 list =
toString().split( delimiter, Qt::KeepEmptyParts );
103 QList<QgsGeometry> geoms;
105 const auto constStringList(
toStringList( delimiter ) );
106 for (
const auto &wkt : constStringList )
117 return QList<QgsGeometry>();
130 while ( pos < filter.size() )
132 if ( pos + 1 < filter.size() && filter[pos] ==
'(' && filter[pos + 1] ==
'<' )
135 int posEnd = filter.indexOf(
"Filter>)", pos );
138 posEnd = filter.size();
140 filters.append( filter.mid( pos + 1, posEnd - pos + 6 ) );
143 else if ( pos + 1 < filter.size() && filter[pos] ==
'(' && filter[pos + 1] ==
')' )
146 filters.append(
"" );
149 else if ( filter[pos] ==
'<' && pos + 7 < filter.size() && filter.mid( pos + 1, 6 ).compare( QLatin1String(
"Filter" ) ) == 0 )
152 filters.append( filter.mid( pos ) );
170 auto isOgcFilter = [filter]()
172 return filter.contains( QStringLiteral(
"<Filter>" ) ) or filter.contains( QStringLiteral(
"()" ) );
175 while ( pos < filter.size() )
177 int posEnd = filter.indexOf(
';', pos );
179 if ( posEnd == pos + 1 )
181 if ( ! isOgcFilter() )
182 filters.append( QString() );
187 if ( ! isOgcFilter() )
188 filters.append( filter.mid( pos, posEnd - pos ) );
200 if ( ! filter.isEmpty() && filter.back() ==
';' )
202 filters.append( QString() );
211 QList<QColor> colors;
213 const auto constStringList(
toStringList( delimiter ) );
214 for (
const auto &part : constStringList )
216 QString cStr( part );
217 if ( !cStr.isEmpty() )
220 if ( cStr.startsWith( QLatin1String(
"0x" ), Qt::CaseInsensitive ) )
222 cStr.replace( 0, 2, QStringLiteral(
"#" ) );
225 const QColor color = QColor( cStr );
226 ok = color.isValid();
230 return QList<QColor>();
233 colors.append( color );
245 const auto constStringList(
toStringList( delimiter ) );
246 for (
const auto &part : constStringList )
248 const int val = part.toInt( &ok );
266 const auto constStringList(
toStringList( delimiter ) );
267 for (
const auto &part : constStringList )
269 const double val = part.toDouble( &ok );
273 return QList<double>();
287 if ( !
mValue.toString().isEmpty() )
289 QStringList corners =
mValue.toString().split(
',' );
291 if ( corners.size() == 4 )
295 for (
int i = 0; i < 4; i++ )
297 corners[i].replace(
' ',
'+' );
298 d[i] = corners[i].toDouble( &ok );
305 if ( d[0] > d[2] || d[1] > d[3] )
328 const QUrl url =
toUrl( ok );
340 QObject::tr(
"Request started [url: %1]" ).arg( url.toString() ),
341 QStringLiteral(
"Server" ) );
342 QNetworkRequest request( url );
343 request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache );
344 request.setAttribute( QNetworkRequest::CacheSaveControlAttribute,
true );
348 loop.exec( QEventLoop::ExcludeUserInputEvents );
350 QNetworkReply *reply = fetcher.
reply();
355 QObject::tr(
"Request failed [error: no reply - url: %1]" ).arg( url.toString() ),
356 QStringLiteral(
"Server" ) );
360 const QVariant status = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute );
361 if ( !status.isNull() && status.toInt() >= 400 )
364 if ( reply->error() != QNetworkReply::NoError )
367 QObject::tr(
"Request failed [error: %1 - url: %2]" ).arg( reply->errorString(), reply->url().toString() ),
368 QStringLiteral(
"Server" ) );
370 const QVariant phrase = reply->attribute( QNetworkRequest::HttpReasonPhraseAttribute );
372 QObject::tr(
"Request error [status: %1 - reason phrase: %2] for %3" ).arg( status.toInt() ).arg( phrase.toString(), reply->url().toString() ),
373 QStringLiteral(
"Server" ) );
377 if ( reply->error() != QNetworkReply::NoError )
381 QObject::tr(
"Request failed [error: %1 - url: %2]" ).arg( reply->errorString(), reply->url().toString() ),
382 QStringLiteral(
"Server" ) );
387 QObject::tr(
"Request finished [url: %1]" ).arg( url.toString() ),
388 QStringLiteral(
"Server" ) );
391 ok = ( !content.isEmpty() );
400 if ( !
mValue.toString().isEmpty() )
405 ok = ( !val.isEmpty() && val.isValid() );
414 if ( !
mValue.toString().isEmpty() )
416 val =
mValue.toInt( &ok );
426 if ( !
mValue.toString().isEmpty() )
439 if ( !
mValue.toString().isEmpty() )
441 val =
mValue.toDouble( &ok );
461 const QVariant::Type type,
const QVariant defaultValue )
471 return QStringLiteral(
"VERSION" );
475 const QMetaEnum metaEnum( QMetaEnum::fromType<QgsServerParameter::Name>() );
476 return metaEnum.valueToKey(
name );
482 if (
name.compare( QLatin1String(
"VERSION" ) ) == 0 )
488 const QMetaEnum metaEnum( QMetaEnum::fromType<QgsServerParameter::Name>() );
495 const QString msg = QString(
"%1 ('%2') cannot be converted into %3" ).arg(
name(
mName ),
mValue.toString(),
typeName() );
520 mParameters[ parameter.
mName ] = parameter;
526 query.addQueryItem( key,
value );
532 QUrlQuery query = mUrlQuery;
534 if ( query.isEmpty() )
538 const auto constMap(
toMap().toStdMap() );
539 for (
const auto ¶m : constMap )
541 const QString
value = QUrl::toPercentEncoding( QString( param.second ) );
542 query.addQueryItem( param.first,
value );
563 if ( mParameters.contains( paramName ) )
565 mParameters.take( paramName );
589 if ( serviceValue.isEmpty() )
592 if (
request() == QLatin1String(
"GetMap" ) \
593 ||
request() == QLatin1String(
"GetFeatureInfo" ) )
595 serviceValue =
"WMS";
606 for (
const auto ¶meter : mParameters.toStdMap() )
608 if ( parameter.second.mValue.isNull() )
613 params[
"VERSION"] = parameter.second.mValue.toString();
618 params[paramName] = parameter.second.mValue.toString();
644 return mParameters[name].mValue;
650 QUrlQuery cleanQuery( query );
651 cleanQuery.setQuery( query.query().replace(
'+', QLatin1String(
"%20" ) ) );
654 const auto constQueryItems( cleanQuery.queryItems( QUrl::FullyDecoded ) );
655 for (
const auto &item : constQueryItems )
660 mParameters[name].mValue = item.second;
661 if ( ! mParameters[name].isValid() )
663 mParameters[name].raiseError();
666 else if ( item.first.compare( QLatin1String(
"VERSION" ), Qt::CaseInsensitive ) == 0 )
669 mParameters[name].mValue = item.second;
670 if ( ! mParameters[name].isValid() )
672 mParameters[name].raiseError();
Exception thrown in case of malformed request.
A geometry is the spatial representation of a feature.
bool isGeosValid(Qgis::GeometryValidityFlags flags=Qgis::GeometryValidityFlags()) const
Checks validity of the geometry using GEOS.
static QgsGeometry fromWkt(const QString &wkt)
Creates a new geometry from a WKT string.
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).
HTTP network content fetcher.
void finished()
Emitted when content has loaded.
QNetworkReply * reply()
Returns a reference to the network reply.
QString contentAsString() const
Returns the fetched content as a string.
void fetchContent(const QUrl &url, const QString &authcfg=QString())
Fetches content from a remote URL and handles redirects.
A rectangle specified with double values.
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.