|
17 | 17 |
|
18 | 18 | #include "qgsserverparameters.h" |
19 | 19 | #include "qgsserverexception.h" |
| 20 | +#include "qgsnetworkcontentfetcher.h" |
| 21 | +#include "qgsmessagelog.h" |
| 22 | +#include <QObject> |
| 23 | +#include <QUrl> |
| 24 | +#include <QNetworkReply> |
| 25 | +#include <QNetworkRequest> |
20 | 26 |
|
21 | 27 | // |
22 | 28 | // QgsServerParameterDefinition |
@@ -200,6 +206,92 @@ QgsRectangle QgsServerParameterDefinition::toRectangle( bool &ok ) const |
200 | 206 | return extent; |
201 | 207 | } |
202 | 208 |
|
| 209 | +QString QgsServerParameterDefinition::loadUrl( bool &ok ) const |
| 210 | +{ |
| 211 | + ok = true; |
| 212 | + |
| 213 | + // Get URL |
| 214 | + QUrl url = toUrl( ok ); |
| 215 | + if ( !ok ) |
| 216 | + { |
| 217 | + return QString(); |
| 218 | + } |
| 219 | + |
| 220 | + // fetching content |
| 221 | + QgsNetworkContentFetcher fetcher; |
| 222 | + QEventLoop loop; |
| 223 | + QObject::connect( &fetcher, &QgsNetworkContentFetcher::finished, &loop, &QEventLoop::quit ); |
| 224 | + |
| 225 | + QgsMessageLog::logMessage( |
| 226 | + QObject::tr( "Request started [url: %1]" ).arg( url.toString() ), |
| 227 | + QStringLiteral( "Server" ) ); |
| 228 | + QNetworkRequest request( url ); |
| 229 | + request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache ); |
| 230 | + request.setAttribute( QNetworkRequest::CacheSaveControlAttribute, true ); |
| 231 | + fetcher.fetchContent( request ); |
| 232 | + |
| 233 | + //wait until content fetched |
| 234 | + loop.exec( QEventLoop::ExcludeUserInputEvents ); |
| 235 | + |
| 236 | + QNetworkReply *reply = fetcher.reply(); |
| 237 | + if ( !reply ) |
| 238 | + { |
| 239 | + ok = false; |
| 240 | + QgsMessageLog::logMessage( |
| 241 | + QObject::tr( "Request failed [error: no reply - url: %1]" ).arg( url.toString() ), |
| 242 | + QStringLiteral( "Server" ) ); |
| 243 | + return QString(); |
| 244 | + } |
| 245 | + |
| 246 | + QVariant status = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute ); |
| 247 | + if ( !status.isNull() && status.toInt() >= 400 ) |
| 248 | + { |
| 249 | + ok = false; |
| 250 | + if ( reply->error() != QNetworkReply::NoError ) |
| 251 | + { |
| 252 | + QgsMessageLog::logMessage( |
| 253 | + QObject::tr( "Request failed [error: %1 - url: %2]" ).arg( reply->errorString(), reply->url().toString() ), |
| 254 | + QStringLiteral( "Server" ) ); |
| 255 | + } |
| 256 | + QVariant phrase = reply->attribute( QNetworkRequest::HttpReasonPhraseAttribute ); |
| 257 | + QgsMessageLog::logMessage( |
| 258 | + QObject::tr( "Request error [status: %1 - reason phrase: %2] for %3" ).arg( status.toInt() ).arg( phrase.toString(), reply->url().toString() ), |
| 259 | + QStringLiteral( "Server" ) ); |
| 260 | + return QString(); |
| 261 | + } |
| 262 | + |
| 263 | + if ( reply->error() != QNetworkReply::NoError ) |
| 264 | + { |
| 265 | + ok = false; |
| 266 | + QgsMessageLog::logMessage( |
| 267 | + QObject::tr( "Request failed [error: %1 - url: %2]" ).arg( reply->errorString(), reply->url().toString() ), |
| 268 | + QStringLiteral( "Server" ) ); |
| 269 | + return QString(); |
| 270 | + } |
| 271 | + |
| 272 | + QgsMessageLog::logMessage( |
| 273 | + QObject::tr( "Request finished [url: %1]" ).arg( url.toString() ), |
| 274 | + QStringLiteral( "Server" ) ); |
| 275 | + |
| 276 | + QString content = fetcher.contentAsString(); |
| 277 | + ok = ( !content.isEmpty() ); |
| 278 | + return content; |
| 279 | +} |
| 280 | + |
| 281 | +QUrl QgsServerParameterDefinition::toUrl( bool &ok ) const |
| 282 | +{ |
| 283 | + ok = true; |
| 284 | + QUrl val; |
| 285 | + |
| 286 | + if ( !mValue.toString().isEmpty() ) |
| 287 | + { |
| 288 | + val = mValue.toUrl(); |
| 289 | + } |
| 290 | + |
| 291 | + ok = ( !val.isEmpty() && val.isValid() ); |
| 292 | + return val; |
| 293 | +} |
| 294 | + |
203 | 295 | int QgsServerParameterDefinition::toInt( bool &ok ) const |
204 | 296 | { |
205 | 297 | ok = true; |
|
0 commit comments