QGIS API Documentation 3.39.0-Master (67e056379ed)
Loading...
Searching...
No Matches
qgsserverparameters.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsserverparameters.cpp
3 --------------------
4 begin : Jun 27, 2018
5 copyright : (C) 2018 by Paul Blottiere
6 email : paul dot blottiere at oslandia dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19#include "qgsserverparameters.h"
20#include "qgsserverexception.h"
21#include "qgsmessagelog.h"
22#include "qgsvariantutils.h"
23#include <QObject>
24#include <QUrl>
25#include <QNetworkReply>
26#include <QNetworkRequest>
27#include <QEventLoop>
28
29//
30// QgsServerParameterDefinition
31//
33 const QVariant defaultValue )
34 : mType( type )
35 , mDefaultValue( defaultValue )
36{
37}
38
39QgsServerParameterDefinition::QgsServerParameterDefinition( const QVariant::Type type, const QVariant defaultValue )
40 : QgsServerParameterDefinition( QgsVariantUtils::variantTypeToMetaType( type ), defaultValue )
41{
42}
43
45{
46 return QVariant::typeToName( mType );
47}
48
50{
51 ok = true;
52 QColor color = mDefaultValue.value<QColor>();
53 QString cStr = mValue.toString();
54
55 if ( !cStr.isEmpty() )
56 {
57 // support hexadecimal notation to define colors
58 if ( cStr.startsWith( QLatin1String( "0x" ), Qt::CaseInsensitive ) )
59 {
60 cStr.replace( 0, 2, QStringLiteral( "#" ) );
61 }
62
63 color = QColor( cStr );
64
65 ok = color.isValid();
66 }
67
68 return color;
69}
70
71QString QgsServerParameterDefinition::toString( const bool defaultValue ) const
72{
73 QString value = mValue.toString();
74
75 if ( value.isEmpty() && defaultValue )
76 value = mDefaultValue.toString();
77
78 return value;
79}
80
81QStringList QgsServerParameterDefinition::toStringList( const char delimiter, const bool skipEmptyParts ) const
82{
83 if ( skipEmptyParts )
84 {
85 return toString().split( delimiter, Qt::SkipEmptyParts );
86 }
87 else
88 {
89 QStringList list;
90 if ( !toString().isEmpty() )
91 {
92 list = toString().split( delimiter, Qt::KeepEmptyParts );
93 }
94 return list;
95 }
96}
97
98QList<QgsGeometry> QgsServerParameterDefinition::toGeomList( bool &ok, const char delimiter ) const
99{
100 ok = true;
101 QList<QgsGeometry> geoms;
102
103 const auto constStringList( toStringList( delimiter ) );
104 for ( const auto &wkt : constStringList )
105 {
106 const QgsGeometry g( QgsGeometry::fromWkt( wkt ) );
107
108 if ( g.isGeosValid() )
109 {
110 geoms.append( g );
111 }
112 else
113 {
114 ok = false;
115 return QList<QgsGeometry>();
116 }
117 }
118
119 return geoms;
120}
121
123{
124 int pos = 0;
125 QStringList filters;
126 const QString filter = toString();
127
128 while ( pos < filter.size() )
129 {
130 if ( pos + 1 < filter.size() && filter[pos] == '(' && filter[pos + 1] == '<' )
131 {
132 // OGC filter on multiple layers
133 int posEnd = filter.indexOf( "Filter>)", pos );
134 if ( posEnd < 0 )
135 {
136 posEnd = filter.size();
137 }
138 filters.append( filter.mid( pos + 1, posEnd - pos + 6 ) );
139 pos = posEnd + 8;
140 }
141 else if ( pos + 1 < filter.size() && filter[pos] == '(' && filter[pos + 1] == ')' )
142 {
143 // empty OGC filter
144 filters.append( "" );
145 pos += 2;
146 }
147 else if ( filter[pos] == '<' && pos + 7 < filter.size() && filter.mid( pos + 1, 6 ).compare( QLatin1String( "Filter" ) ) == 0 )
148 {
149 // Single OGC filter
150 filters.append( filter.mid( pos ) );
151 break;
152 }
153 else
154 {
155 pos += 1;
156 }
157 }
158
159 return filters;
160}
161
163{
164 int pos = 0;
165 QStringList filters;
166 const QString filter = toString();
167
168 auto isOgcFilter = [filter]()
169 {
170 return filter.contains( QStringLiteral( "<Filter>" ) ) || filter.contains( QStringLiteral( "()" ) );
171 };
172
173 while ( pos < filter.size() )
174 {
175 int posEnd = filter.indexOf( ';', pos );
176
177 if ( posEnd == pos + 1 )
178 {
179 if ( ! isOgcFilter() )
180 filters.append( QString() );
181 pos = posEnd;
182 continue;
183 }
184
185 if ( ! isOgcFilter() )
186 filters.append( filter.mid( pos, posEnd - pos ) );
187
188 if ( posEnd < 0 )
189 {
190 pos = filter.size();
191 }
192 else
193 {
194 pos = posEnd + 1;
195 }
196 }
197
198 if ( ! filter.isEmpty() && filter.back() == ';' )
199 {
200 filters.append( QString() );
201 }
202
203 return filters;
204}
205
206QList<QColor> QgsServerParameterDefinition::toColorList( bool &ok, const char delimiter ) const
207{
208 ok = true;
209 QList<QColor> colors;
210
211 const auto constStringList( toStringList( delimiter ) );
212 for ( const auto &part : constStringList )
213 {
214 QString cStr( part );
215 if ( !cStr.isEmpty() )
216 {
217 // support hexadecimal notation to define colors
218 if ( cStr.startsWith( QLatin1String( "0x" ), Qt::CaseInsensitive ) )
219 {
220 cStr.replace( 0, 2, QStringLiteral( "#" ) );
221 }
222
223 const QColor color = QColor( cStr );
224 ok = color.isValid();
225
226 if ( !ok )
227 {
228 return QList<QColor>();
229 }
230
231 colors.append( color );
232 }
233 }
234
235 return colors;
236}
237
238QList<int> QgsServerParameterDefinition::toIntList( bool &ok, const char delimiter ) const
239{
240 ok = true;
241 QList<int> ints;
242
243 const auto constStringList( toStringList( delimiter ) );
244 for ( const auto &part : constStringList )
245 {
246 const int val = part.toInt( &ok );
247
248 if ( !ok )
249 {
250 return QList<int>();
251 }
252
253 ints.append( val );
254 }
255
256 return ints;
257}
258
259QList<double> QgsServerParameterDefinition::toDoubleList( bool &ok, const char delimiter ) const
260{
261 ok = true;
262 QList<double> vals;
263
264 const auto constStringList( toStringList( delimiter ) );
265 for ( const auto &part : constStringList )
266 {
267 const double val = part.toDouble( &ok );
268
269 if ( !ok )
270 {
271 return QList<double>();
272 }
273
274 vals.append( val );
275 }
276
277 return vals;
278}
279
281{
282 ok = true;
283 QgsRectangle extent;
284
285 if ( !mValue.toString().isEmpty() )
286 {
287 QStringList corners = mValue.toString().split( ',' );
288
289 if ( corners.size() == 4 )
290 {
291 double d[4];
292
293 for ( int i = 0; i < 4; i++ )
294 {
295 corners[i].replace( ' ', '+' );
296 d[i] = corners[i].toDouble( &ok );
297 if ( !ok )
298 {
299 return QgsRectangle();
300 }
301 }
302
303 if ( d[0] > d[2] || d[1] > d[3] )
304 {
305 ok = false;
306 return QgsRectangle();
307 }
308
309 extent = QgsRectangle( d[0], d[1], d[2], d[3] );
310 }
311 else
312 {
313 ok = false;
314 return QgsRectangle();
315 }
316 }
317
318 return extent;
319}
320
322{
323 ok = true;
324
325 // Get URL
326 const QUrl url = toUrl( ok );
327 if ( !ok )
328 {
329 return QString();
330 }
331
332 QNetworkRequest request( url );
333 request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache );
334 request.setAttribute( QNetworkRequest::CacheSaveControlAttribute, true );
335
336 // fetching content
338 const QgsBlockingNetworkRequest::ErrorCode errorCode = newReq.get( request, false );
339
340 if ( errorCode != QgsBlockingNetworkRequest::NoError )
341 {
342 ok = false;
344 QObject::tr( "Request failed [error: %1 - url: %2]" ).arg( newReq.errorMessage(), url.toString() ),
345 QStringLiteral( "Server" ) );
346 return QString();
347 }
348
349 QgsNetworkReplyContent reply = newReq.reply();
350
351 ok = !reply.content().isEmpty();
352 return reply.content();
353}
354
356{
357 ok = true;
358 QUrl val;
359
360 if ( !mValue.toString().isEmpty() )
361 {
362 val = mValue.toUrl();
363 }
364
365 ok = ( !val.isEmpty() && val.isValid() );
366 return val;
367}
368
370{
371 ok = true;
372 int val = mDefaultValue.toInt();
373
374 if ( !mValue.toString().isEmpty() )
375 {
376 val = mValue.toInt( &ok );
377 }
378
379 return val;
380}
381
383{
384 int val = mDefaultValue.toBool();
385
386 if ( !mValue.toString().isEmpty() )
387 {
388 val = mValue.toBool();
389 }
390
391 return val;
392}
393
395{
396 ok = true;
397 double val = mDefaultValue.toDouble();
398
399 if ( !mValue.toString().isEmpty() )
400 {
401 val = mValue.toDouble( &ok );
402 }
403
404 return val;
405}
406
408{
409 return mValue.canConvert( mType );
410}
411
413{
414 throw QgsBadRequestException( QStringLiteral( "Invalid Parameter" ), msg );
415}
416
417//
418// QgsServerParameter
419//
421 const QMetaType::Type type, const QVariant defaultValue )
422 : QgsServerParameterDefinition( type, defaultValue )
423 , mName( name )
424{
425}
426
428 const QVariant::Type type,
429 const QVariant defaultValue )
430 : QgsServerParameter( name, QgsVariantUtils::variantTypeToMetaType( type ), defaultValue )
431{
432}
433
435{
437 {
438 return QStringLiteral( "VERSION" );
439 }
440 else
441 {
442 const QMetaEnum metaEnum( QMetaEnum::fromType<QgsServerParameter::Name>() );
443 return metaEnum.valueToKey( name );
444 }
445}
446
448{
449 if ( name.compare( QLatin1String( "VERSION" ) ) == 0 )
450 {
452 }
453 else
454 {
455 const QMetaEnum metaEnum( QMetaEnum::fromType<QgsServerParameter::Name>() );
456 return ( QgsServerParameter::Name ) metaEnum.keyToValue( name.toUpper().toStdString().c_str() );
457 }
458}
459
461{
462 const QString msg = QString( "%1 ('%2') cannot be converted into %3" ).arg( name( mName ), mValue.toString(), typeName() );
464}
465
466//
467// QgsServerParameters
468//
477
480{
481 mUrlQuery = query;
482 load( query );
483}
484
485void QgsServerParameters::save( const QgsServerParameter &parameter )
486{
487 mParameters[ parameter.mName ] = parameter;
488}
489
490void QgsServerParameters::add( const QString &key, const QString &value )
491{
492 QUrlQuery query;
493 query.addQueryItem( key, value );
494 load( query );
495}
496
498{
499 QUrlQuery query = mUrlQuery;
500
501 if ( query.isEmpty() )
502 {
503 query.clear();
504
505 const auto constMap( toMap().toStdMap() );
506 for ( const auto &param : constMap )
507 {
508 const QString value = QUrl::toPercentEncoding( QString( param.second ) );
509 query.addQueryItem( param.first, value );
510 }
511 }
512
513 return query;
514}
515
520
521void QgsServerParameters::remove( const QString &key )
522{
523 if ( mUnmanagedParameters.contains( key ) )
524 {
525 mUnmanagedParameters.take( key );
526 }
527 else
528 {
529 const QgsServerParameter::Name paramName = QgsServerParameter::name( key );
530 if ( mParameters.contains( paramName ) )
531 {
532 mParameters.take( paramName );
533 }
534 }
535}
536
538{
539 return value( QgsServerParameter::MAP ).toString();
540}
541
543{
544 return value( QgsServerParameter::VERSION_SERVICE ).toString();
545}
546
548{
549 return value( QgsServerParameter::FILE_NAME ).toString();
550}
551
553{
554 QString serviceValue = value( QgsServerParameter::SERVICE ).toString();
555
556 if ( serviceValue.isEmpty() )
557 {
558 // SERVICE not mandatory for WMS 1.3.0 GetMap & GetFeatureInfo
559 if ( request() == QLatin1String( "GetMap" ) \
560 || request() == QLatin1String( "GetFeatureInfo" ) )
561 {
562 serviceValue = "WMS";
563 }
564 }
565
566 return serviceValue;
567}
568
569QMap<QString, QString> QgsServerParameters::toMap() const
570{
571 QMap<QString, QString> params = mUnmanagedParameters;
572
573 for ( const auto &parameter : mParameters.toStdMap() )
574 {
575 if ( QgsVariantUtils::isNull( parameter.second.mValue ) )
576 continue;
577
578 if ( parameter.second.mName == QgsServerParameter::VERSION_SERVICE )
579 {
580 params["VERSION"] = parameter.second.mValue.toString();
581 }
582 else
583 {
584 const QString paramName = QgsServerParameter::name( parameter.first );
585 params[paramName] = parameter.second.mValue.toString();
586 }
587 }
588
589 return params;
590}
591
593{
594 return value( QgsServerParameter::REQUEST ).toString();
595}
596
597QString QgsServerParameters::value( const QString &key ) const
598{
599 if ( ! mParameters.contains( QgsServerParameter::name( key ) ) )
600 {
601 return mUnmanagedParameters[key];
602 }
603 else
604 {
605 return value( QgsServerParameter::name( key ) ).toString();
606 }
607}
608
610{
611 return mParameters[name].mValue;
612}
613
614void QgsServerParameters::load( const QUrlQuery &query )
615{
616 // clean query string first
617 QUrlQuery cleanQuery( query );
618 cleanQuery.setQuery( query.query().replace( '+', QLatin1String( "%20" ) ) );
619
620 // load parameters
621 const auto constQueryItems( cleanQuery.queryItems( QUrl::FullyDecoded ) );
622 for ( const auto &item : constQueryItems )
623 {
624 const QgsServerParameter::Name name = QgsServerParameter::name( item.first );
625 if ( name >= 0 )
626 {
627 mParameters[name].mValue = item.second;
628 if ( ! mParameters[name].isValid() )
629 {
630 mParameters[name].raiseError();
631 }
632 }
633 else if ( item.first.compare( QLatin1String( "VERSION" ), Qt::CaseInsensitive ) == 0 )
634 {
636 mParameters[name].mValue = item.second;
637 if ( ! mParameters[name].isValid() )
638 {
639 mParameters[name].raiseError();
640 }
641 }
642 else if ( ! loadParameter( item.first, item.second ) )
643 {
644 mUnmanagedParameters[item.first.toUpper()] = item.second;
645 }
646 }
647}
648
649bool QgsServerParameters::loadParameter( const QString &, const QString & )
650{
651 return false;
652}
653
655{
656 mParameters.clear();
657 mUnmanagedParameters.clear();
658}
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 Q_INVOKABLE 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.
QgsServerParameterDefinition(const QMetaType::Type type=QMetaType::Type::QString, const QVariant defaultValue=QVariant(""))
Constructor for QgsServerParameterDefinition.
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.
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 QMetaType::Type type=QMetaType::Type::QString, 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.
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.
Contains utility functions for working with QVariants and QVariant types.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.