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>();
127 QList<QColor> colors;
129 const auto constStringList(
toStringList( delimiter ) );
130 for (
const auto &part : constStringList )
132 QString cStr( part );
133 if ( !cStr.isEmpty() )
136 if ( cStr.startsWith( QLatin1String(
"0x" ), Qt::CaseInsensitive ) )
138 cStr.replace( 0, 2, QStringLiteral(
"#" ) );
141 const QColor color = QColor( cStr );
142 ok = color.isValid();
146 return QList<QColor>();
149 colors.append( color );
161 const auto constStringList(
toStringList( delimiter ) );
162 for (
const auto &part : constStringList )
164 const int val = part.toInt( &ok );
182 const auto constStringList(
toStringList( delimiter ) );
183 for (
const auto &part : constStringList )
185 const double val = part.toDouble( &ok );
189 return QList<double>();
203 if ( !
mValue.toString().isEmpty() )
205 QStringList corners =
mValue.toString().split(
',' );
207 if ( corners.size() == 4 )
211 for (
int i = 0; i < 4; i++ )
213 corners[i].replace(
' ',
'+' );
214 d[i] = corners[i].toDouble( &ok );
221 if ( d[0] > d[2] || d[1] > d[3] )
244 QUrl url =
toUrl( ok );
256 QObject::tr(
"Request started [url: %1]" ).arg( url.toString() ),
257 QStringLiteral(
"Server" ) );
258 QNetworkRequest request( url );
259 request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache );
260 request.setAttribute( QNetworkRequest::CacheSaveControlAttribute,
true );
264 loop.exec( QEventLoop::ExcludeUserInputEvents );
266 QNetworkReply *reply = fetcher.
reply();
271 QObject::tr(
"Request failed [error: no reply - url: %1]" ).arg( url.toString() ),
272 QStringLiteral(
"Server" ) );
276 QVariant status = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute );
277 if ( !status.isNull() && status.toInt() >= 400 )
280 if ( reply->error() != QNetworkReply::NoError )
283 QObject::tr(
"Request failed [error: %1 - url: %2]" ).arg( reply->errorString(), reply->url().toString() ),
284 QStringLiteral(
"Server" ) );
286 QVariant phrase = reply->attribute( QNetworkRequest::HttpReasonPhraseAttribute );
288 QObject::tr(
"Request error [status: %1 - reason phrase: %2] for %3" ).arg( status.toInt() ).arg( phrase.toString(), reply->url().toString() ),
289 QStringLiteral(
"Server" ) );
293 if ( reply->error() != QNetworkReply::NoError )
297 QObject::tr(
"Request failed [error: %1 - url: %2]" ).arg( reply->errorString(), reply->url().toString() ),
298 QStringLiteral(
"Server" ) );
303 QObject::tr(
"Request finished [url: %1]" ).arg( url.toString() ),
304 QStringLiteral(
"Server" ) );
307 ok = ( !content.isEmpty() );
316 if ( !
mValue.toString().isEmpty() )
321 ok = ( !val.isEmpty() && val.isValid() );
330 if ( !
mValue.toString().isEmpty() )
332 val =
mValue.toInt( &ok );
342 if ( !
mValue.toString().isEmpty() )
355 if ( !
mValue.toString().isEmpty() )
357 val =
mValue.toDouble( &ok );
377 const QVariant::Type type,
const QVariant defaultValue )
387 return QStringLiteral(
"VERSION" );
391 const QMetaEnum metaEnum( QMetaEnum::fromType<QgsServerParameter::Name>() );
392 return metaEnum.valueToKey(
name );
398 if (
name.compare( QLatin1String(
"VERSION" ) ) == 0 )
404 const QMetaEnum metaEnum( QMetaEnum::fromType<QgsServerParameter::Name>() );
411 const QString msg = QString(
"%1 ('%2') cannot be converted into %3" ).arg(
name(
mName ),
mValue.toString(),
typeName() );
436 mParameters[ parameter.
mName ] = parameter;
442 query.addQueryItem( key,
value );
448 QUrlQuery query = mUrlQuery;
450 if ( query.isEmpty() )
454 const auto constMap(
toMap().toStdMap() );
455 for (
const auto ¶m : constMap )
457 const QString
value = QString( param.second ).replace(
'+', QLatin1String(
"%2B" ) );
458 query.addQueryItem( param.first,
value );
479 if ( mParameters.contains( paramName ) )
481 mParameters.take( paramName );
505 if ( serviceValue.isEmpty() )
508 if (
request() == QLatin1String(
"GetMap" ) \
509 ||
request() == QLatin1String(
"GetFeatureInfo" ) )
511 serviceValue =
"WMS";
522 for (
const auto ¶meter : mParameters.toStdMap() )
524 if ( parameter.second.mValue.isNull() )
529 params[
"VERSION"] = parameter.second.mValue.toString();
534 params[paramName] = parameter.second.mValue.toString();
560 return mParameters[name].mValue;
566 QUrlQuery cleanQuery( query );
567 cleanQuery.setQuery( query.query().replace(
'+', QLatin1String(
"%20" ) ) );
570 const auto constQueryItems( cleanQuery.queryItems( QUrl::FullyDecoded ) );
571 for (
const auto &item : constQueryItems )
576 mParameters[name].mValue = item.second;
577 if ( ! mParameters[name].isValid() )
579 mParameters[name].raiseError();
582 else if ( item.first.compare( QLatin1String(
"VERSION" ), Qt::CaseInsensitive ) == 0 )
585 mParameters[name].mValue = item.second;
586 if ( ! mParameters[name].isValid() )
588 mParameters[name].raiseError();
Exception thrown in case of malformed request.
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(QgsGeometry::ValidityFlags flags=QgsGeometry::ValidityFlags()) 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).
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.
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.
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.