Exemple #1
0
        /// <summary>
        /// Create a WebServiceContext instance.
        /// </summary>
        /// <param name='clientInformation'>Client information.</param>
        public WebServiceContext(WebClientInformation clientInformation)
        {
            // Init context.
            _database    = null;
            _clientToken = new WebClientToken(clientInformation.Token,
                                              WebServiceData.WebServiceManager.Key);
            _locale = clientInformation.Locale;
            if (_locale.IsNull())
            {
                _locale = GetDefaultLocale();
            }
            _currentRole = clientInformation.Role;
            _requestId   = GetNextRequestId();
            if (_isTracing)
            {
                _traceStart = DateTime.Now;
            }
            _transactionTimeout = Settings.Default.TransactionDefaultTimeout; // Unit is seconds.

            // Check arguments.
            try
            {
                clientInformation.CheckNotNull("clientInformation");
                CheckUser();
                CheckClientIpAddress();
                CheckHttpsProtocol();
                CheckWebServiceName();
                CheckCurrentRole();
            }
            catch (Exception exception)
            {
                WebServiceData.LogManager.LogSecurityError(this, exception);
                throw;
            }
        }
Exemple #2
0
        /// <summary>
        /// Create a WebServiceContext instance.
        /// This contructor should only be used during login.
        /// </summary>
        /// <param name="userName">User name.</param>
        /// <param name="applicationIdentifier">
        /// Identifier of the application that the user uses.
        /// </param>
        public WebServiceContext(String userName,
                                 String applicationIdentifier)
        {
            // Init object.
            _database    = null;
            _clientToken = new WebClientToken(userName,
                                              applicationIdentifier,
                                              WebServiceData.WebServiceManager.Key);
            _requestId = GetNextRequestId();
            if (_isTracing)
            {
                _traceStart = DateTime.Now;
            }
            _transactionTimeout = Settings.Default.TransactionDefaultTimeout; // Unit is seconds.

            // Check arguments.
            try
            {
                CheckHttpsProtocol();
            }
            catch (Exception exception)
            {
                WebServiceData.LogManager.LogSecurityError(this, exception);
                throw;
            }
            _currentRole = null;

            // This is only a temporary value.
            // Real value is set by UserManager when user has logged in.
            _locale = GetDefaultLocale();
        }
 public void Ping()
 {
     using (WebServiceDataServer database = GetDatabase(true))
     {
         Assert.IsTrue(database.Ping());
     }
 }
Exemple #4
0
        /// <summary>
        /// Rollback a transaction.
        /// </summary>
        public void RollbackTransaction()
        {
            WebServiceDataServer transactionDatabase = null;

            lock (_transactionInformation)
            {
                // Undo transaction.
                if (_transactionInformation.ContainsKey(TransactionKey))
                {
                    transactionDatabase = (WebServiceDataServer)(_transactionInformation[TransactionKey]);
                    _transactionInformation.Remove(TransactionKey);
                }
                if (_transactionInformation.ContainsKey(TransactionIdKey))
                {
                    _transactionInformation.Remove(TransactionIdKey);
                }
            }

            if (transactionDatabase.IsNotNull())
            {
                lock (transactionDatabase)
                {
                    transactionDatabase.RollbackTransaction();
                    transactionDatabase.Disconnect();
                }

                // Cache is cleared since data changes that
                // is rollbacked could have been cached.
                ClearCache(false);
            }
        }
 /// <summary>
 /// Add an entry to the web service log
 /// </summary>
 /// <param name="context">Web service request context.</param>
 /// <param name="text">Log text.</param>
 /// <param name="type">Type of log entry.</param>
 /// <param name="information">Extended information about the log entry.</param>
 private void UpdateLog(WebServiceContext context,
                        String text,
                        String type,
                        String information)
 {
     if (context.GetDatabase().HasPendingTransaction())
     {
         // Create new database connection that has no transaction.
         // The log entry may be removed (rollback of transaction)
         // if the database connection has an active transaction.
         using (WebServiceDataServer database = WebServiceData.DatabaseManager.GetDatabase(context))
         {
             database.UpdateLog(text,
                                type,
                                information,
                                context.ClientToken.UserName,
                                context.ClientToken.ClientIpAddress,
                                context.ClientToken.ApplicationIdentifier);
         }
     }
     else
     {
         context.GetDatabase().UpdateLog(text,
                                         type,
                                         information,
                                         context.ClientToken.UserName,
                                         context.ClientToken.ClientIpAddress,
                                         context.ClientToken.ApplicationIdentifier);
     }
 }
 public void TestCleanup()
 {
     if (_database.IsNotNull())
     {
         _database.Dispose();
         _database = null;
     }
 }
 public void TestCleanup()
 {
     if (_database.IsNotNull())
     {
         _database.RollbackTransaction();
         _database.Dispose();
         _database = null;
     }
 }
Exemple #8
0
        /// <summary>
        /// Start a transaction.
        /// </summary>
        /// <param name="timeout">
        /// Time before transaction timeouts if has not already finished.
        /// Unit is seconds.
        /// </param>
        public void StartTransaction(Int32 timeout)
        {
            Boolean abortTransaction = false;
            WebServiceDataServer transactionDatabase = null;
            Thread transactionThread;

            _transactionTimeout = timeout;
            lock (_transactionInformation)
            {
                if (IsInTransaction())
                {
                    // StartTransaction should only be called once
                    // to start the transaction.
                    abortTransaction = true;

                    // Undo transaction.
                    if (_transactionInformation.ContainsKey(TransactionKey))
                    {
                        transactionDatabase = (WebServiceDataServer)(_transactionInformation[TransactionKey]);
                        _transactionInformation.Remove(TransactionKey);
                    }
                    if (_transactionInformation.ContainsKey(TransactionIdKey))
                    {
                        _transactionInformation.Remove(TransactionIdKey);
                    }
                }
                else
                {
                    // Start transaction.
                    transactionDatabase = WebServiceData.DatabaseManager.GetDatabase(this);
                    transactionDatabase.CommandTimeout = _transactionTimeout;
                    transactionDatabase.BeginTransaction();
                    _transactionInformation.Add(TransactionKey, transactionDatabase);
                    _transactionInformation.Add(TransactionIdKey, RequestId);
                }
            }

            if (abortTransaction)
            {
                if (transactionDatabase.IsNotNull())
                {
                    lock (transactionDatabase)
                    {
                        transactionDatabase.RollbackTransaction();
                        transactionDatabase.Disconnect();
                    }
                }
                throw new ApplicationException("Transaction has already been started!");
            }

            // Start thread that control time out of transaction.
            transactionThread          = new Thread(CheckTransactionTimeOut);
            transactionThread.Priority = ThreadPriority.Highest;
            transactionThread.Start();
        }
Exemple #9
0
        /// <summary>
        /// Clear data cache in web service.
        /// </summary>
        /// <param name='checkAccessRight'>
        /// Indicates if user access rights should be checked or not.
        /// Requests to clear cache that are made from Internet should always
        /// check access rights.
        /// </param>
        public virtual void ClearCache(Boolean checkAccessRight = true)
        {
            // Check access rights.
            if (checkAccessRight)
            {
                WebServiceData.AuthorizationManager.CheckAuthorization(this, AuthorityIdentifier.WebServiceAdministrator);
            }

            // Clear cached data.
            ClearAspDotNetCache();
            WebServiceDataServer.ClearCache();
        }
 private WebServiceDataServer GetDatabase(Boolean refresh)
 {
     if (_database.IsNull() || refresh)
     {
         if (_database.IsNotNull())
         {
             _database.Dispose();
         }
         _database = new TaxonServer();
         _database.BeginTransaction();
     }
     return(_database);
 }
        private WebServiceDataServer GetDatabase(Boolean refresh = false)
        {
            if (_database.IsNull() || refresh)
            {
                if (_database.IsNotNull())
                {
                    _database.Dispose();
                }

                _database = new SpeciesObservationHarvestServer();
            }

            return(_database);
        }
Exemple #12
0
        /// <summary>
        /// Get database instance.
        /// </summary>
        /// <returns>Requested database instance.</returns>
        public WebServiceDataServer GetDatabase()
        {
            if (IsInTransaction())
            {
                return(GetTransactionDatabase());
            }

            if (_database.IsNull())
            {
                _database = WebServiceData.DatabaseManager.GetDatabase(this);
            }

            return(_database);
        }
Exemple #13
0
        /// <summary>
        /// Get database instance that is used in a transaction.
        /// </summary>
        /// <returns>The database instance.</returns>
        public WebServiceDataServer GetTransactionDatabase()
        {
            WebServiceDataServer database = null;

            lock (_transactionInformation)
            {
                if (_transactionInformation.Contains(TransactionKey))
                {
                    database = (WebServiceDataServer)(_transactionInformation[TransactionKey]);
                }
            }

            return(database);
        }
        public void ClearCache()
        {
            Int32  columnLength;
            String columnName, tableName;

            WebServiceDataServer.ClearCache();

            tableName    = WebServiceLogData.TABLE_NAME;
            columnName   = WebServiceLogData.TEXT;
            columnLength = GetDatabase(true).GetColumnLength(tableName, columnName);
            Assert.IsTrue(0 < columnLength);

            WebServiceDataServer.ClearCache();

            tableName    = WebServiceLogData.TABLE_NAME;
            columnName   = WebServiceLogData.SQL_SERVER_USER;
            columnLength = GetDatabase().GetColumnLength(tableName, columnName);
            Assert.IsTrue(0 < columnLength);
        }
Exemple #15
0
        /// <summary>
        /// Implementation of the IDisposable interface.
        /// Releases all resources related to this request.
        /// </summary>
        public void Dispose()
        {
            Int32    index;
            String   method;
            TimeSpan duration;

            if (_isTracing)
            {
                if (_traceUsers.IsEmpty() ||
                    _traceUsers.ContainsKey(ClientToken.UserName))
                {
                    duration = DateTime.Now - _traceStart;
                    method   = Environment.StackTrace;
                    index    = method.IndexOf(":line"); // English.
                    if (index < 0)
                    {
                        index = method.IndexOf(":rad"); // Swedish.
                    }

                    if (0 <= index)
                    {
                        method = method.Substring(index);
                        method = method.Substring(method.IndexOf("."));
                        method = method.Substring(1, method.IndexOf("(") - 1) + "()";
                    }
                    WebServiceData.LogManager.Log(this, method + ", duration = " + duration.Milliseconds + " milliseconds.", LogType.Trace, null);
                }
            }

            if (_database.IsNotNull())
            {
                try
                {
                    _database.Disconnect();
                }
                catch
                {
                }
                _database = null;
            }
        }
Exemple #16
0
        /// <summary>
        /// Check if transaction has timed out and should be rollbacked.
        /// </summary>
        private void CheckTransactionTimeOut()
        {
            WebServiceDataServer transactionDatabase = null;
            Int32 transactionId;
            Int32 transactionTimeout;

            // Get transaction information.
            transactionTimeout = _transactionTimeout;
            if (transactionTimeout > Settings.Default.TransactionMaxTimeout)
            {
                // Unit is seconds.
                transactionTimeout = Settings.Default.TransactionMaxTimeout;
            }
            lock (_transactionInformation)
            {
                if (_transactionInformation.ContainsKey(TransactionIdKey))
                {
                    transactionId = (Int32)(_transactionInformation[TransactionIdKey]);
                }
                else
                {
                    // Transaction has already been commited or aborted.
                    return;
                }
            }

            // Wait for timeout.
            Thread.Sleep(transactionTimeout * 1000);

            // Check if we have a transaction to abort.
            lock (_transactionInformation)
            {
                if (_transactionInformation.ContainsKey(TransactionIdKey))
                {
                    if (transactionId == ((Int32)(_transactionInformation[TransactionIdKey])))
                    {
                        if (IsInTransaction())
                        {
                            // Undo transaction.
                            if (_transactionInformation.ContainsKey(TransactionKey))
                            {
                                transactionDatabase = (WebServiceDataServer)(_transactionInformation[TransactionKey]);
                                _transactionInformation.Remove(TransactionKey);
                            }
                            if (_transactionInformation.ContainsKey(TransactionIdKey))
                            {
                                _transactionInformation.Remove(TransactionIdKey);
                            }
                        }
                        else
                        {
                            _transactionInformation.Remove(TransactionIdKey);
                        }
                    }
                    // else: A new transaction has started.
                }
                // else: Transaction id has been removed by another
                //       transaction call from the same client.
            }

            if (transactionDatabase.IsNotNull())
            {
                lock (transactionDatabase)
                {
                    transactionDatabase.RollbackTransaction();
                    transactionDatabase.Disconnect();
                }
            }
        }
 public WebServiceDataServerTest()
 {
     _database = null;
 }
 /// <summary>
 /// Clear data cache in web service.
 /// </summary>
 /// <param name='checkAccessRight'>
 /// Indicates if user access rights should be checked or not.
 /// Requests to clear cache that are made from Internet should always
 /// check access rights.
 /// </param>
 public override void ClearCache(Boolean checkAccessRight = true)
 {
     // Clear cached data.
     Cache.Clear();
     WebServiceDataServer.ClearCache();
 }
Exemple #19
0
        public void GetDatabase()
        {
            WebServiceDataServer databaseServer = GetDatabaseManager(true).GetDatabase(GetContext());

            Assert.IsNotNull(databaseServer);
        }