Example #1
0
        public void VerifyConcurrencyValues(IEnumerable <KeyValuePair <string, object> > values)
        {
            DSResource originalResource = this.GetOriginalResource();

            if (originalResource != null)
            {
                foreach (KeyValuePair <string, object> value in values)
                {
                    object obj = originalResource.GetValue(value.Key, null);
                    if (obj.Equals(value.Value))
                    {
                        continue;
                    }
                    object[] key = new object[2];
                    key[0] = value.Key;
                    key[1] = value.Value;
                    throw new DataServiceException(0x19c, ExceptionHelpers.GetDataServiceExceptionMessage(HttpStatusCode.PreconditionFailed, Resources.PropertyKey, key));
                }
                return;
            }
            else
            {
                throw new OptimisticConcurrencyException(ExceptionHelpers.GetExceptionMessage(Resources.GetCmdletNotReturningAnObject, new object[0]));
            }
        }
Example #2
0
 public static void AddParametersToCommand(ICommand command, Uri uri)
 {
     try
     {
         NameValueCollection nameValueCollection = HttpUtility.ParseQueryString(uri.Query);
         foreach (string key in nameValueCollection.Keys)
         {
             if (key == null || string.IsNullOrWhiteSpace(key))
             {
                 object[] str = new object[1];
                 str[0] = uri.ToString();
                 throw new DataServiceException(0x190, ExceptionHelpers.GetDataServiceExceptionMessage(HttpStatusCode.BadRequest, Resources.InvalidQueryParameterMessage, str));
             }
             else
             {
                 string[] values = nameValueCollection.GetValues(key);
                 if ((int)values.Length == 1)
                 {
                     string str1 = values[0];
                     if (!string.IsNullOrWhiteSpace(str1))
                     {
                         string str2 = key.Trim();
                         if (str2.StartsWith("$", StringComparison.OrdinalIgnoreCase))
                         {
                             continue;
                         }
                         try
                         {
                             command.AddParameter(str2, str1.Trim(), true);
                         }
                         catch (ArgumentException argumentException1)
                         {
                             ArgumentException argumentException = argumentException1;
                             object[]          objArray          = new object[1];
                             objArray[0] = uri.ToString();
                             throw new DataServiceException(0x190, string.Empty, ExceptionHelpers.GetDataServiceExceptionMessage(HttpStatusCode.BadRequest, Resources.InvalidQueryParameterMessage, objArray), string.Empty, argumentException);
                         }
                     }
                     else
                     {
                         object[] objArray1 = new object[1];
                         objArray1[0] = uri.ToString();
                         throw new DataServiceException(0x190, ExceptionHelpers.GetExceptionMessage(Resources.InvalidQueryParameterMessage, objArray1));
                     }
                 }
                 else
                 {
                     object[] objArray2 = new object[1];
                     objArray2[0] = uri.ToString();
                     throw new DataServiceException(0x190, ExceptionHelpers.GetDataServiceExceptionMessage(HttpStatusCode.BadRequest, Resources.InvalidQueryParameterMessage, objArray2));
                 }
             }
         }
     }
     catch (Exception exception)
     {
         TraceHelper.Current.UriParsingFailed(uri.ToString());
         throw;
     }
 }
Example #3
0
        private DSResource GetOriginalResource()
        {
            DSResource dSResource;
            ICommand   command = DataServiceController.Current.GetCommand(CommandType.Read, this.userContext, this.resourceType, this.metadata, this.membershipId);

            using (command)
            {
                UriParametersHelper.AddParametersToCommand(command, DataServiceController.Current.GetCurrentResourceUri());
                CommandArgumentVisitor commandArgumentVisitor = new CommandArgumentVisitor(command);
                commandArgumentVisitor.Visit(this.query.Expression);
                List <DSResource> dSResources = new List <DSResource>();
                DataServiceController.Current.QuotaSystem.CheckCmdletExecutionQuota(this.userContext);
                IEnumerator <DSResource> enumerator = command.InvokeAsync(dSResources.AsQueryable <DSResource>().Expression, true);
                while (enumerator.MoveNext())
                {
                    dSResources.Add(enumerator.Current);
                }
                if (dSResources.Count != 0)
                {
                    dSResource = dSResources.First <DSResource>();
                }
                else
                {
                    throw new DataServiceException(0x194, ExceptionHelpers.GetDataServiceExceptionMessage(HttpStatusCode.NotFound, Resources.ResourceInstanceNotFound, new object[0]));
                }
            }
            return(dSResource);
        }
Example #4
0
 public void VerifyMandatoryParameterAdded(IEnumerable <string> parameters, ulong parameterSetMask)
 {
     if (parameters.Count <string>() == 0)
     {
         parameterSetMask = (ulong)(~((long)-1 << (this.ParameterSets.Count & 63)));
     }
     if (parameterSetMask != 0)
     {
         List <int> .Enumerator enumerator = this.GetParameterSetsFromBitmask(parameterSetMask).GetEnumerator();
         try
         {
             while (enumerator.MoveNext())
             {
                 int            current = enumerator.Current;
                 PSParameterSet item    = this.ParameterSets[current];
                 bool           flag    = false;
                 Dictionary <string, PSParameterInfo> .KeyCollection.Enumerator enumerator1 = item.Parameters.Keys.GetEnumerator();
                 try
                 {
                     while (enumerator1.MoveNext())
                     {
                         string str = enumerator1.Current;
                         if (!item.Parameters[str].IsMandatory || parameters.Contains <string>(str))
                         {
                             continue;
                         }
                         flag = true;
                         break;
                     }
                 }
                 finally
                 {
                     enumerator1.Dispose();
                 }
                 if (flag)
                 {
                     continue;
                 }
                 return;
             }
             object[] cmdletName = new object[1];
             cmdletName[0] = this.CmdletName;
             throw new DataServiceException(0x190, ExceptionHelpers.GetDataServiceExceptionMessage(HttpStatusCode.BadRequest, Resources.MandatoryParameterMissing, cmdletName));
         }
         finally
         {
             enumerator.Dispose();
         }
         return;
     }
     else
     {
         return;
     }
 }
Example #5
0
        public void CheckCmdletExecutionQuota(UserContext user)
        {
            int num = DataServiceController.Current.IncrementCmdletExecutionCount(user);
            int maxCmdletsPerRequest = DataServiceController.Current.Configuration.PowerShell.Quotas.MaxCmdletsPerRequest;

            if (num <= maxCmdletsPerRequest)
            {
                return;
            }
            else
            {
                TraceHelper.Current.MaxCmdletQuotaViolation((uint)num);
                DataServiceController.Current.PerfCounters.SystemQuotaViolationsPerSec.Increment();
                DataServiceController.Current.QuotaSystem.SystemQuotaViolation.Increment();
                throw new DataServiceException(0x193, ExceptionHelpers.GetDataServiceExceptionMessage(HttpStatusCode.Forbidden, Resources.CmdletExecutionPerRequestQuotaExceeded, new object[0]));
            }
        }
Example #6
0
        public static void ProcessingRequestHandler(object source, DataServiceProcessingPipelineEventArgs args)
        {
            UserData userDatum = null;

            TraceHelper.Current.DebugMessage("QuotaSystem.ProcessingRequestHandler entered");
            if (args != null && args.OperationContext != null)
            {
                TraceHelper.CorrelateWithClientRequestId(args.OperationContext);
            }
            UserContext userContext = new UserContext(CurrentRequestHelper.Identity, CurrentRequestHelper.Certificate);

            if (!DataServiceController.Current.IsRequestProcessingStarted(userContext))
            {
                UserDataCache.UserDataEnvelope userDataEnvelope = DataServiceController.Current.UserDataCache.Get(userContext);
                using (userDataEnvelope)
                {
                    UserQuota userQuota = DataServiceController.Current.GetUserQuota(userContext);
                    if (args != null)
                    {
                        Guid activityId = EtwActivity.GetActivityId();
                        args.OperationContext.ResponseHeaders.Add("request-id", activityId.ToString());
                    }
                    if (userDataEnvelope.Data.Usage.QuotaCheckAndUpdate(userContext, userQuota))
                    {
                        DataServiceController.Current.UserDataCache.TryLockKey(userContext, out userDatum);
                    }
                    else
                    {
                        throw new DataServiceException(0x193, ExceptionHelpers.GetDataServiceExceptionMessage(HttpStatusCode.Forbidden, Resources.UserQuotaExceeded, new object[0]));
                    }
                }
                DataServiceController.Current.SetRequestProcessingState(userContext, true);
                TraceHelper.Current.RequestProcessingStart();
                DataServiceController.Current.UserDataCache.Trace();
                TraceHelper.Current.DebugMessage("QuotaSystem.ProcessingRequestHandler exited");
                return;
            }
            else
            {
                TraceHelper.Current.DebugMessage("QuotaSystem.ProcessingRequestHandler IsRequestProcessingStarted returned true");
                return;
            }
        }
Example #7
0
        private void ExecutionCompletionEventHandler(IAsyncResult ar)
        {
            Exception dataServiceException;

            lock (this.syncObject)
            {
                this.timer.Stop();
                this.isExecutionCompleted = true;
                if (!this.isExceedsMaxExecutionTime)
                {
                    dataServiceException = PSCommand.CheckPowershellForException(this.cmdletInfo.CmdletName, this.commandType, this.powerShell);
                }
                else
                {
                    object[] cmdletName = new object[1];
                    cmdletName[0]        = this.cmdletInfo.CmdletName;
                    dataServiceException = new DataServiceException(0x193, ExceptionHelpers.GetDataServiceExceptionMessage(HttpStatusCode.Forbidden, Resources.CmdletExecutionQuotaExceeded, cmdletName));
                    TraceHelper.Current.CommandExecutionTimeExceeded(this.cmdletInfo.CmdletName, this.runspace.Borrower.Name, DataServiceController.Current.Configuration.PowerShell.Quotas.MaxExecutionTime, 0);
                    DataServiceController.Current.PerfCounters.SystemQuotaViolationsPerSec.Increment();
                    DataServiceController.Current.QuotaSystem.SystemQuotaViolation.Increment();
                }
                if (this.output != null && dataServiceException == null)
                {
                    foreach (PSObject pSObject in this.output)
                    {
                        if (pSObject == null)
                        {
                            continue;
                        }
                        DSResource dSResource = SerializerBase.SerializeEntity(pSObject, this.entityType);
                        this.dataStore.Add(dSResource);
                    }
                }
                this.dataStore.Completed(dataServiceException);
                this.powerShell.Trace();
            }
            this.Dispose();
        }