24 #include <QNetworkReply> 25 #include <QNetworkRequest> 31 const QVariant defaultValue )
33 , mDefaultValue( defaultValue )
39 return QVariant::typeToName(
mType );
46 QString cStr =
mValue.toString();
48 if ( !cStr.isEmpty() )
51 if ( cStr.startsWith( QLatin1String(
"0x" ), Qt::CaseInsensitive ) )
53 cStr.replace( 0, 2, QStringLiteral(
"#" ) );
56 color = QColor( cStr );
73 return toString().split( delimiter, QString::SkipEmptyParts );
80 list =
toString().split( delimiter, QString::KeepEmptyParts );
89 QList<QgsGeometry> geoms;
102 return QList<QgsGeometry>();
112 QList<QColor> colors;
116 QString cStr( part );
117 if ( !cStr.isEmpty() )
120 if ( cStr.startsWith( QLatin1String(
"0x" ), Qt::CaseInsensitive ) )
122 cStr.replace( 0, 2, QStringLiteral(
"#" ) );
125 const QColor color = QColor( cStr );
126 ok = color.isValid();
130 return QList<QColor>();
133 colors.append( color );
147 const int val = part.toInt( &ok );
167 const double val = part.toDouble( &ok );
171 return QList<double>();
185 if ( !
mValue.toString().isEmpty() )
187 QStringList corners =
mValue.toString().split(
',' );
189 if ( corners.size() == 4 )
193 for (
int i = 0; i < 4; i++ )
195 corners[i].replace(
' ',
'+' );
196 d[i] = corners[i].toDouble( &ok );
203 if ( d[0] > d[2] || d[1] > d[3] )
226 QUrl url =
toUrl( ok );
238 QObject::tr(
"Request started [url: %1]" ).arg( url.toString() ),
239 QStringLiteral(
"Server" ) );
240 QNetworkRequest request( url );
241 request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache );
242 request.setAttribute( QNetworkRequest::CacheSaveControlAttribute,
true );
246 loop.exec( QEventLoop::ExcludeUserInputEvents );
248 QNetworkReply *reply = fetcher.
reply();
253 QObject::tr(
"Request failed [error: no reply - url: %1]" ).arg( url.toString() ),
254 QStringLiteral(
"Server" ) );
258 QVariant status = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute );
259 if ( !status.isNull() && status.toInt() >= 400 )
262 if ( reply->error() != QNetworkReply::NoError )
265 QObject::tr(
"Request failed [error: %1 - url: %2]" ).arg( reply->errorString(), reply->url().toString() ),
266 QStringLiteral(
"Server" ) );
268 QVariant phrase = reply->attribute( QNetworkRequest::HttpReasonPhraseAttribute );
270 QObject::tr(
"Request error [status: %1 - reason phrase: %2] for %3" ).arg( status.toInt() ).arg( phrase.toString(), reply->url().toString() ),
271 QStringLiteral(
"Server" ) );
275 if ( reply->error() != QNetworkReply::NoError )
279 QObject::tr(
"Request failed [error: %1 - url: %2]" ).arg( reply->errorString(), reply->url().toString() ),
280 QStringLiteral(
"Server" ) );
285 QObject::tr(
"Request finished [url: %1]" ).arg( url.toString() ),
286 QStringLiteral(
"Server" ) );
289 ok = ( !content.isEmpty() );
298 if ( !
mValue.toString().isEmpty() )
303 ok = ( !val.isEmpty() && val.isValid() );
312 if ( !
mValue.toString().isEmpty() )
314 val =
mValue.toInt( &ok );
324 if ( !
mValue.toString().isEmpty() )
337 if ( !
mValue.toString().isEmpty() )
339 val =
mValue.toDouble( &ok );
359 const QVariant::Type type,
const QVariant defaultValue )
369 return QStringLiteral(
"VERSION" );
373 const QMetaEnum metaEnum( QMetaEnum::fromType<QgsServerParameter::Name>() );
374 return metaEnum.valueToKey( name );
380 if ( name.compare( QLatin1String(
"VERSION" ) ) == 0 )
386 const QMetaEnum metaEnum( QMetaEnum::fromType<QgsServerParameter::Name>() );
393 const QString msg = QString(
"%1 ('%2') cannot be converted into %3" ).arg(
name(
mName ),
mValue.toString(),
typeName() );
417 mParameters[ parameter.
mName ] = parameter;
423 query.addQueryItem( key, value );
431 for (
auto param :
toMap().toStdMap() )
433 query.addQueryItem( param.first, param.second );
453 if ( mParameters.contains( paramName ) )
455 mParameters.take( paramName );
479 if ( serviceValue.isEmpty() )
482 if (
request() == QLatin1String(
"GetMap" ) \
483 ||
request() == QLatin1String(
"GetFeatureInfo" ) )
485 serviceValue =
"WMS";
496 for (
const auto ¶meter : mParameters.toStdMap() )
498 if ( parameter.second.
mValue.isNull() )
503 params[
"VERSION"] = parameter.second.
mValue.toString();
508 params[paramName] = parameter.second.
mValue.toString();
534 return mParameters[name].mValue;
540 QUrlQuery cleanQuery( query );
541 cleanQuery.setQuery( query.query().replace(
'+', QStringLiteral(
"%20" ) ) );
544 for (
const auto &item : cleanQuery.queryItems( QUrl::FullyDecoded ) )
549 mParameters[name].mValue = item.second;
550 if ( ! mParameters[name].isValid() )
552 mParameters[name].raiseError();
555 else if ( item.first.compare( QLatin1String(
"VERSION" ) ) == 0 )
558 mParameters[name].mValue = item.second;
559 if ( ! mParameters[name].isValid() )
561 mParameters[name].raiseError();
int toInt(bool &ok) const
Converts the parameter into an integer.
QString toString() const
Converts the parameter into a string.
QString contentAsString() const
Returns the fetched content as a string.
bool isGeosValid(QgsGeometry::ValidityFlags flags=nullptr) const
Checks validity of the geometry using GEOS.
A rectangle specified with double values.
QMap< QString, QString > toMap() const
Returns all parameters in a map.
double toDouble(bool &ok) const
Converts the parameter into a double.
Name
Parameter's name common to all services.
QList< QgsGeometry > toGeomList(bool &ok, char delimiter= ',') const
Converts the parameter into a list of geometries.
QMap< QString, QString > mUnmanagedParameters
Exception thrown in case of malformed request.
virtual bool isValid() const
Returns true if the parameter is valid, false otherwise.
QString loadUrl(bool &ok) const
Loads the data associated to the parameter converted into an url.
QString value(const QString &key) const
Returns the value of a parameter.
QStringList toStringList(char delimiter= ',', bool skipEmptyParts=true) const
Converts the parameter into a list of strings.
static void raiseError(const QString &msg)
Raises an exception in case of an invalid parameters.
void remove(const QString &key)
Removes a parameter.
QColor toColor(bool &ok) const
Converts the parameter into a color.
A geometry is the spatial representation of a feature.
void load(const QUrlQuery &query)
Loads new parameters.
QNetworkReply * reply()
Returns a reference to the network reply.
QString typeName() const
Returns the type of the parameter as a string.
QList< double > toDoubleList(bool &ok, char delimiter= ',') const
Converts the parameter into a list of doubles.
QString fileName() const
Returns FILE_NAME parameter as a string or an empty string if not defined.
QgsServerParameter(const QgsServerParameter::Name name=QgsServerParameter::UNKNOWN, const QVariant::Type type=QVariant::String, const QVariant defaultValue=QVariant(""))
Constructor for QgsServerParameter.
static QString name(const QgsServerParameter::Name name)
Converts a parameter's name into its string representation.
QgsServerParameters()
Constructor.
HTTP network content fetcher.
QString version() const
Returns VERSION parameter as a string or an empty string if not defined.
QList< QColor > toColorList(bool &ok, char delimiter= ',') const
Converts the parameter into a list of colors.
QString request() const
Returns REQUEST parameter as a string or an empty string if not defined.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
virtual bool loadParameter(const QString &name, const QString &value)
Loads a parameter with a specific value.
void finished()
Emitted when content has loaded.
Parameter common to all services (WMS, WFS, ...)
QgsServerParameterDefinition(const QVariant::Type type=QVariant::String, const QVariant defaultValue=QVariant(""))
Constructor for QgsServerParameterDefinition.
void raiseError() const
Raises an error in case of an invalid conversion.
QgsRectangle toRectangle(bool &ok) const
Converts the parameter into a rectangle.
QString map() const
Returns MAP parameter as a string or an empty string if not defined.
QUrl toUrl(bool &ok) const
Converts the parameter into an url.
QgsServerParameter::Name mName
static QgsGeometry fromWkt(const QString &wkt)
Creates a new geometry from a WKT string.
void add(const QString &key, const QString &value)
Adds a parameter.
bool toBool() const
Converts the parameter into a boolean.
QgsServerParameters provides an interface to retrieve and manipulate global parameters received from ...
QUrlQuery urlQuery() const
Returns a url query with underlying parameters.
QList< int > toIntList(bool &ok, char delimiter= ',') const
Converts the parameter into a list of integers.
void clear()
Removes all parameters.
void fetchContent(const QUrl &url)
Fetches content from a remote URL and handles redirects.
QString service() const
Returns SERVICE parameter as a string or an empty string if not defined.
Definition of a parameter with basic conversion methods.