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     return toString().split( delimiter, QString::SkipEmptyParts );
 
   86       list = 
toString().split( delimiter, QString::KeepEmptyParts );
 
   95   QList<QgsGeometry> geoms;
 
   98   for ( 
const auto &wkt : constStringList )
 
  109       return QList<QgsGeometry>();
 
  119   QList<QColor> colors;
 
  121   const auto constStringList( 
toStringList( delimiter ) );
 
  122   for ( 
const auto &part : constStringList )
 
  124     QString cStr( part );
 
  125     if ( !cStr.isEmpty() )
 
  128       if ( cStr.startsWith( QLatin1String( 
"0x" ), Qt::CaseInsensitive ) )
 
  130         cStr.replace( 0, 2, QStringLiteral( 
"#" ) );
 
  133       const QColor color = QColor( cStr );
 
  134       ok = color.isValid();
 
  138         return QList<QColor>();
 
  141       colors.append( color );
 
  153   const auto constStringList( 
toStringList( delimiter ) );
 
  154   for ( 
const auto &part : constStringList )
 
  156     const int val = part.toInt( &ok );
 
  174   const auto constStringList( 
toStringList( delimiter ) );
 
  175   for ( 
const auto &part : constStringList )
 
  177     const double val = part.toDouble( &ok );
 
  181       return QList<double>();
 
  195   if ( !
mValue.toString().isEmpty() )
 
  197     QStringList corners = 
mValue.toString().split( 
',' );
 
  199     if ( corners.size() == 4 )
 
  203       for ( 
int i = 0; i < 4; i++ )
 
  205         corners[i].replace( 
' ', 
'+' );
 
  206         d[i] = corners[i].toDouble( &ok );
 
  213       if ( d[0] > d[2] || d[1] > d[3] )
 
  236   QUrl url = 
toUrl( ok );
 
  248     QObject::tr( 
"Request started [url: %1]" ).arg( url.toString() ),
 
  249     QStringLiteral( 
"Server" ) );
 
  250   QNetworkRequest request( url );
 
  251   request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache );
 
  252   request.setAttribute( QNetworkRequest::CacheSaveControlAttribute, 
true );
 
  256   loop.exec( QEventLoop::ExcludeUserInputEvents );
 
  258   QNetworkReply *reply = fetcher.
reply();
 
  263       QObject::tr( 
"Request failed [error: no reply - url: %1]" ).arg( url.toString() ),
 
  264       QStringLiteral( 
"Server" ) );
 
  268   QVariant status = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute );
 
  269   if ( !status.isNull() && status.toInt() >= 400 )
 
  272     if ( reply->error() != QNetworkReply::NoError )
 
  275         QObject::tr( 
"Request failed [error: %1 - url: %2]" ).arg( reply->errorString(), reply->url().toString() ),
 
  276         QStringLiteral( 
"Server" ) );
 
  278     QVariant phrase = reply->attribute( QNetworkRequest::HttpReasonPhraseAttribute );
 
  280       QObject::tr( 
"Request error [status: %1 - reason phrase: %2] for %3" ).arg( status.toInt() ).arg( phrase.toString(), reply->url().toString() ),
 
  281       QStringLiteral( 
"Server" ) );
 
  285   if ( reply->error() != QNetworkReply::NoError )
 
  289       QObject::tr( 
"Request failed [error: %1 - url: %2]" ).arg( reply->errorString(), reply->url().toString() ),
 
  290       QStringLiteral( 
"Server" ) );
 
  295     QObject::tr( 
"Request finished [url: %1]" ).arg( url.toString() ),
 
  296     QStringLiteral( 
"Server" ) );
 
  299   ok = ( !content.isEmpty() );
 
  308   if ( !
mValue.toString().isEmpty() )
 
  313   ok = ( !val.isEmpty() && val.isValid() );
 
  322   if ( !
mValue.toString().isEmpty() )
 
  324     val = 
mValue.toInt( &ok );
 
  334   if ( !
mValue.toString().isEmpty() )
 
  347   if ( !
mValue.toString().isEmpty() )
 
  349     val = 
mValue.toDouble( &ok );
 
  369                                         const QVariant::Type type, 
const QVariant defaultValue )
 
  379     return QStringLiteral( 
"VERSION" );
 
  383     const QMetaEnum metaEnum( QMetaEnum::fromType<QgsServerParameter::Name>() );
 
  384     return metaEnum.valueToKey( 
name );
 
  390   if ( 
name.compare( QLatin1String( 
"VERSION" ) ) == 0 )
 
  396     const QMetaEnum metaEnum( QMetaEnum::fromType<QgsServerParameter::Name>() );
 
  403   const QString msg = QString( 
"%1 ('%2') cannot be converted into %3" ).arg( 
name( 
mName ), 
mValue.toString(), 
typeName() );
 
  428   mParameters[ parameter.
mName ] = parameter;
 
  434   query.addQueryItem( key, 
value );
 
  440   QUrlQuery query = mUrlQuery;
 
  442   if ( query.isEmpty() )
 
  446     const auto constMap( 
toMap().toStdMap() );
 
  447     for ( 
const auto ¶m : constMap )
 
  449       query.addQueryItem( param.first, param.second );
 
  470     if ( mParameters.contains( paramName ) )
 
  472       mParameters.take( paramName );
 
  496   if ( serviceValue.isEmpty() )
 
  499     if ( 
request() == QLatin1String( 
"GetMap" ) \
 
  500          || 
request() == QLatin1String( 
"GetFeatureInfo" ) )
 
  502       serviceValue = 
"WMS";
 
  513   for ( 
const auto ¶meter : mParameters.toStdMap() )
 
  515     if ( parameter.second.mValue.isNull() )
 
  520       params[
"VERSION"] = parameter.second.mValue.toString();
 
  525       params[paramName] = parameter.second.mValue.toString();
 
  551   return mParameters[name].mValue;
 
  557   QUrlQuery cleanQuery( query );
 
  558   cleanQuery.setQuery( query.query().replace( 
'+', QLatin1String( 
"%20" ) ) );
 
  561   const auto constQueryItems( cleanQuery.queryItems( QUrl::FullyDecoded ) );
 
  562   for ( 
const auto &item : constQueryItems )
 
  567       mParameters[name].mValue = item.second;
 
  568       if ( ! mParameters[name].isValid() )
 
  570         mParameters[name].raiseError();
 
  573     else if ( item.first.compare( QLatin1String( 
"VERSION" ),  Qt::CaseInsensitive ) == 0 )
 
  576       mParameters[name].mValue = item.second;
 
  577       if ( ! mParameters[name].isValid() )
 
  579         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::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.
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.
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.