示例#1
0
bool QgsWcsCapabilities::sendRequest( QString const &url )
{
  QgsDebugMsg( "url = " + url );
  mError.clear();
  QNetworkRequest request( url );
  QgsSetRequestInitiatorClass( request, QStringLiteral( "QgsWcsCapabilities" ) );
  if ( !setAuthorization( request ) )
  {
    mError = tr( "Download of capabilities failed: network request update failed for authentication config" );
    QgsMessageLog::logMessage( mError, tr( "WCS" ) );
    return false;
  }
  request.setAttribute( QNetworkRequest::CacheSaveControlAttribute, true );
  request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, mCacheLoadControl );
  QgsDebugMsg( QStringLiteral( "mCacheLoadControl = %1" ).arg( mCacheLoadControl ) );

  QgsDebugMsg( QStringLiteral( "getcapabilities: %1" ).arg( url ) );
  mCapabilitiesReply = QgsNetworkAccessManager::instance()->get( request );
  if ( !setAuthorizationReply( mCapabilitiesReply ) )
  {
    mCapabilitiesReply->deleteLater();
    mCapabilitiesReply = nullptr;
    mError = tr( "Download of capabilities failed: network reply update failed for authentication config" );
    QgsMessageLog::logMessage( mError, tr( "WCS" ) );
    return false;
  }

  connect( mCapabilitiesReply, &QNetworkReply::finished, this, &QgsWcsCapabilities::capabilitiesReplyFinished );
  connect( mCapabilitiesReply, &QNetworkReply::downloadProgress, this, &QgsWcsCapabilities::capabilitiesReplyProgress );

  QEventLoop loop;
  connect( this, &QgsWcsCapabilities::downloadFinished, &loop, &QEventLoop::quit );
  loop.exec( QEventLoop::ExcludeUserInputEvents );

  if ( mCapabilitiesResponse.isEmpty() )
  {
    if ( mError.isEmpty() )
    {
      mErrorFormat = QStringLiteral( "text/plain" );
      mError = tr( "empty capabilities document" );
    }
    return false;
  }

  if ( mCapabilitiesResponse.startsWith( "<html>" ) ||
       mCapabilitiesResponse.startsWith( "<HTML>" ) )
  {
    mErrorFormat = QStringLiteral( "text/html" );
    mError = mCapabilitiesResponse;
    return false;
  }
  return true;
}
示例#2
0
void QgsWcsCapabilities::capabilitiesReplyFinished()
{
  if ( mCapabilitiesReply->error() == QNetworkReply::NoError )
  {
    QVariant redirect = mCapabilitiesReply->attribute( QNetworkRequest::RedirectionTargetAttribute );
    if ( !redirect.isNull() )
    {
      emit statusChanged( tr( "Capabilities request redirected." ) );

      QNetworkRequest request( redirect.toUrl() );
      if ( !setAuthorization( request ) )
      {
        mCapabilitiesResponse.clear();
        mError = tr( "Download of capabilities failed: network request update failed for authentication config" );
        QgsMessageLog::logMessage( mError, tr( "WCS" ) );
        return;
      }
      request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferNetwork );
      request.setAttribute( QNetworkRequest::CacheSaveControlAttribute, true );

      mCapabilitiesReply->deleteLater();
      QgsDebugMsg( QString( "redirected getcapabilities: %1" ).arg( redirect.toString() ) );
      mCapabilitiesReply = QgsNetworkAccessManager::instance()->get( request );
      if ( !setAuthorizationReply( mCapabilitiesReply ) )
      {
        mCapabilitiesResponse.clear();
        mCapabilitiesReply->deleteLater();
        mCapabilitiesReply = nullptr;
        mError = tr( "Download of capabilities failed: network reply update failed for authentication config" );
        QgsMessageLog::logMessage( mError, tr( "WCS" ) );
        return;
      }

      connect( mCapabilitiesReply, &QNetworkReply::finished, this, &QgsWcsCapabilities::capabilitiesReplyFinished );
      connect( mCapabilitiesReply, &QNetworkReply::downloadProgress, this, &QgsWcsCapabilities::capabilitiesReplyProgress );
      return;
    }

    mCapabilitiesResponse = mCapabilitiesReply->readAll();

    if ( mCapabilitiesResponse.isEmpty() )
    {
      mErrorFormat = QStringLiteral( "text/plain" );
      mError = tr( "empty of capabilities: %1" ).arg( mCapabilitiesReply->errorString() );
    }
  }
  else
  {
    // Resend request if AlwaysCache
    QNetworkRequest request = mCapabilitiesReply->request();
    if ( request.attribute( QNetworkRequest::CacheLoadControlAttribute ).toInt() == QNetworkRequest::AlwaysCache )
    {
      QgsDebugMsg( "Resend request with PreferCache" );
      request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache );

      mCapabilitiesReply->deleteLater();

      mCapabilitiesReply = QgsNetworkAccessManager::instance()->get( request );
      if ( !setAuthorizationReply( mCapabilitiesReply ) )
      {
        mCapabilitiesResponse.clear();
        mCapabilitiesReply->deleteLater();
        mCapabilitiesReply = nullptr;
        mError = tr( "Download of capabilities failed: network reply update failed for authentication config" );
        QgsMessageLog::logMessage( mError, tr( "WCS" ) );
        return;
      }
      connect( mCapabilitiesReply, &QNetworkReply::finished, this, &QgsWcsCapabilities::capabilitiesReplyFinished );
      connect( mCapabilitiesReply, &QNetworkReply::downloadProgress, this, &QgsWcsCapabilities::capabilitiesReplyProgress );
      return;
    }

    mErrorFormat = QStringLiteral( "text/plain" );
    mError = tr( "Download of capabilities failed: %1" ).arg( mCapabilitiesReply->errorString() );
    QgsMessageLog::logMessage( mError, tr( "WCS" ) );
    mCapabilitiesResponse.clear();
  }

  mCapabilitiesReply->deleteLater();
  mCapabilitiesReply = nullptr;

  emit downloadFinished();
}