コード例 #1
0
        private static void HandleException(Exception ex)
        {
            if (ex is ResourceNotFoundException)
            {
                WebResponseHelper.ThrowResourceNotFoundException();
            }

            if (ex is AuthorizationRequiredException exception)
            {
                WebResponseHelper.ThrowAuthorizationRequired(exception);
            }

            if (ex is PermissionDeniedException permissionDeniedException)
            {
                WebResponseHelper.ThrowPermissionDeniedException(permissionDeniedException);
            }

            if (ex is ArgumentException argumentException)
            {
                WebResponseHelper.ThrowArgumentException(argumentException);
            }

            if (ex is ResourceManagementException managementException)
            {
                WebResponseHelper.ThrowResourceManagementException(managementException);
            }

            WebResponseHelper.ThrowServerException(ex);
        }
コード例 #2
0
        private static SearchResultPager GetSearchResultPager(IncomingWebRequestContext context, int pageSize, string token)
        {
            SearchResultPager p;

            if (token == null)
            {
                string               filter     = WebResponseHelper.GetFilterText(context);
                CultureInfo          locale     = WebResponseHelper.GetLocale();
                IEnumerable <string> attributes = WebResponseHelper.GetAttributes(context);

                if (attributes != null)
                {
                    p = Global.Client.GetResourcesPaged(filter, pageSize, attributes, locale);
                }
                else
                {
                    p = Global.Client.GetResourcesPaged(filter, pageSize, locale);
                }
            }
            else
            {
                p = (SearchResultPager)ResourceManagementWebServicev2.searchCache.Remove(ResourceManagementWebServicev2.BuildCacheKey(token));

                if (p == null)
                {
                    throw new ArgumentException("Invalid token");
                }
            }
            return(p);
        }
コード例 #3
0
        public Stream GetApprovalRequestsByStatus(string status)
        {
            try
            {
                if (Enum.TryParse(status, true, out Client.ApprovalStatus approvalStatus))
                {
                    return(WebResponseHelper.GetResponse(Global.Client.GetApprovals(approvalStatus).ToList(), false));
                }

                throw new ArgumentException("Invalid value for status parameter", nameof(status));
            }
            catch (WebFaultException)
            {
                throw;
            }
            catch (WebFaultException <Error> )
            {
                throw;
            }
            catch (Exception ex)
            {
                ResourceManagementWebServicev2.HandleException(ex);
                throw;
            }
        }
コード例 #4
0
        public Stream GetResourceByID(string id)
        {
            try
            {
                ResourceManagementWebServicev2.ValidateID(id);
                CultureInfo locale = WebResponseHelper.GetLocale();
                bool        includePermissionHints = WebResponseHelper.IsParameterSet(ParameterNames.IncludePermissionHints);

                ResourceObject resource = Global.Client.GetResource(id, locale, includePermissionHints);

                if (resource == null)
                {
                    throw new ResourceNotFoundException();
                }

                return(WebResponseHelper.GetResponse(resource, true));
            }
            catch (WebFaultException)
            {
                throw;
            }
            catch (WebFaultException <Error> )
            {
                throw;
            }
            catch (Exception ex)
            {
                ResourceManagementWebServicev2.HandleException(ex);
                throw;
            }
        }
コード例 #5
0
        public Stream GetResourceByKey(string objectType, string key, string keyValue)
        {
            try
            {
                ResourceManagementSchema.ValidateAttributeName(key);
                ResourceManagementSchema.ValidateObjectTypeName(objectType);
                CultureInfo locale = WebResponseHelper.GetLocale();

                ResourceObject resource = Global.Client.GetResourceByKey(objectType, key, keyValue, locale);

                if (resource == null)
                {
                    throw new ResourceNotFoundException();
                }

                return(WebResponseHelper.GetResponse(resource, false));
            }
            catch (WebFaultException)
            {
                throw;
            }
            catch (WebFaultException <Error> )
            {
                throw;
            }
            catch (Exception ex)
            {
                ResourceManagementWebServicev2.HandleException(ex);
                throw;
            }
        }
コード例 #6
0
        public static ResourceSerializationSettings GetResourceSerializationSettings(bool allowPermissions)
        {
            ResourceSerializationSettings settings = new ResourceSerializationSettings();

            if (WebResponseHelper.IsParameterSet(ParameterNames.ResourceFormat, "fixed"))
            {
                settings.ResourceFormat = ResourceSerializationHandling.FixedStructure;
            }

            if (WebResponseHelper.IsParameterSet(ParameterNames.ValueFormat, "string"))
            {
                settings.ValueFormat = AttributeValueSerializationHandling.ConvertToString;
            }

            if (WebResponseHelper.IsParameterSet(ParameterNames.ArrayHandling, "all"))
            {
                settings.ArrayHandling = ArraySerializationHandling.AllAttributes;
            }

            if (WebResponseHelper.IsParameterSet(ParameterNames.ArrayHandling, "whenRequired"))
            {
                settings.ArrayHandling = ArraySerializationHandling.WhenRequired;
            }

            settings.IncludePermissionHints = allowPermissions && WebResponseHelper.IsParameterSet(ParameterNames.IncludePermissionHints);
            settings.IncludeNullValues      = WebResponseHelper.IsParameterSet(ParameterNames.IncludeNullValues);

            return(settings);
        }
コード例 #7
0
        public static Stream GetResponse(object r, bool allowPermissions)
        {
            ResourceSerializationSettings settings = WebResponseHelper.GetResourceSerializationSettings(allowPermissions);

            JsonSerializerSettings d = new JsonSerializerSettings
            {
                Context = new StreamingContext(StreamingContextStates.Other, settings),
                ReferenceLoopHandling = ReferenceLoopHandling.Serialize
            };

            WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8";

            return(new MemoryStream(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(r, d))));
        }
コード例 #8
0
        public void RemoveAttributeValue(string id, string attribute, string value)
        {
            try
            {
                ResourceManagementSchema.ValidateAttributeName(attribute);
                ResourceManagementWebServicev2.ValidateID(id);
                CultureInfo locale = WebResponseHelper.GetLocale();

                ResourceObject resource;

                if (ResourceManagementSchema.IsAttributeMultivalued(attribute))
                {
                    resource = Global.Client.GetResource(id, locale);
                }
                else
                {
                    resource = Global.Client.GetResource(id, new[] { attribute }, locale);
                }

                if (resource == null)
                {
                    throw new ResourceNotFoundException();
                }

                if (!resource.Attributes.ContainsAttribute(attribute))
                {
                    WebResponseHelper.ThrowAttributeNotFoundException(attribute);
                }

                resource.Attributes[attribute].RemoveValue(value, true);

                Global.Client.SaveResource(resource, locale);
                WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.NoContent;
            }
            catch (WebFaultException)
            {
                throw;
            }
            catch (WebFaultException <Error> )
            {
                throw;
            }
            catch (Exception ex)
            {
                ResourceManagementWebServicev2.HandleException(ex);
                throw;
            }
        }
コード例 #9
0
        public Stream GetRequestParameters(string id)
        {
            try
            {
                ResourceManagementWebServicev2.ValidateID(id);

                ResourceObject request = Global.Client.GetResourceByKey(ObjectTypeNames.Request, AttributeNames.ObjectID, id, new[] { AttributeNames.RequestParameter });

                if (request == null)
                {
                    throw new WebFaultException(HttpStatusCode.NotFound);
                }

                if (!request.Attributes.ContainsAttribute(AttributeNames.RequestParameter) || request.Attributes[AttributeNames.RequestParameter].IsNull)
                {
                    return(new MemoryStream());
                }

                IList <string>          parameters        = request.Attributes[AttributeNames.RequestParameter].StringValues;
                List <RequestParameter> requestParameters = new List <RequestParameter>();

                foreach (string param in parameters)
                {
                    requestParameters.Add(XmlDeserializeFromString <RequestParameter>(param));
                }

                return(WebResponseHelper.GetResponse(requestParameters, false));
            }
            catch (WebFaultException)
            {
                throw;
            }
            catch (WebFaultException <Error> )
            {
                throw;
            }
            catch (Exception ex)
            {
                ResourceManagementWebServicev2.HandleException(ex);
                throw;
            }
        }
コード例 #10
0
        public void UpdateResource(string id, ResourceUpdateRequest request)
        {
            try
            {
                ResourceManagementWebServicev2.ValidateID(id);
                CultureInfo locale = WebResponseHelper.GetLocale();

                ResourceObject resource = Global.Client.GetResource(id, locale);
                foreach (AttributeValueUpdate kvp in request.Attributes)
                {
                    if (kvp.Value.Length > 1)
                    {
                        resource.Attributes[kvp.Name].SetValue(kvp.Value);
                    }
                    else if (kvp.Value.Length == 1)
                    {
                        resource.Attributes[kvp.Name].SetValue(kvp.Value[0]);
                    }
                    else
                    {
                        resource.Attributes[kvp.Name].RemoveValues();
                    }
                }

                Global.Client.SaveResource(resource, locale);
            }
            catch (WebFaultException)
            {
                throw;
            }
            catch (WebFaultException <Error> )
            {
                throw;
            }
            catch (Exception ex)
            {
                ResourceManagementWebServicev2.HandleException(ex);
                throw;
            }
        }
コード例 #11
0
        public Stream GetResourceAttributeByID(string id, string attribute)
        {
            try
            {
                ResourceManagementSchema.ValidateAttributeName(attribute);
                ResourceManagementWebServicev2.ValidateID(id);
                CultureInfo locale = WebResponseHelper.GetLocale();

                ResourceObject resource = Global.Client.GetResource(id, new[] { attribute }, locale);

                if (resource == null)
                {
                    throw new ResourceNotFoundException();
                }

                if (!resource.Attributes.ContainsAttribute(attribute))
                {
                    WebResponseHelper.ThrowAttributeNotFoundException(attribute);
                }

                List <string> result = resource.Attributes[attribute].ToStringValues().ToList();

                return(WebResponseHelper.GetResponse(result, true));
            }
            catch (WebFaultException)
            {
                throw;
            }
            catch (WebFaultException <Error> )
            {
                throw;
            }
            catch (Exception ex)
            {
                ResourceManagementWebServicev2.HandleException(ex);
                throw;
            }
        }
コード例 #12
0
        public Stream GetResourcesPaged()
        {
            try
            {
                IncomingWebRequestContext context = WebOperationContext.Current?.IncomingRequest;
                if (context == null)
                {
                    throw new InvalidOperationException();
                }

                string currentIndexParam = context.UriTemplateMatch.QueryParameters["index"];
                string token             = context.UriTemplateMatch.QueryParameters["token"];

                int pageSize = WebResponseHelper.GetPageSize(context);
                int index    = currentIndexParam == null ? -1 : Convert.ToInt32(currentIndexParam);

                SearchResultPager p = ResourceManagementWebServicev2.GetSearchResultPager(context, pageSize, token);

                token = token ?? Guid.NewGuid().ToString();

                if (index >= 0)
                {
                    p.CurrentIndex = index;
                }

                p.PageSize = pageSize;

                int oldIndex = p.CurrentIndex;

                PagedResultSet results = new PagedResultSet
                {
                    Results = p.GetNextPage().ToList(),
                };

                ResourceManagementWebServicev2.GetPageUris(context, oldIndex, pageSize, token, p, out Uri previousPageUri, out Uri nextPageUri);

                results.NextPage     = nextPageUri?.ToString();
                results.PreviousPage = previousPageUri?.ToString();
                results.TotalCount   = p.TotalCount;
                results.HasMoreItems = results.NextPage != null;

                ResourceManagementWebServicev2.searchCache.Add(ResourceManagementWebServicev2.BuildCacheKey(token), p, new CacheItemPolicy()
                {
                    SlidingExpiration = new TimeSpan(0, 5, 0)
                });

                return(WebResponseHelper.GetResponse(results, false));
            }
            catch (WebFaultException)
            {
                throw;
            }
            catch (WebFaultException <Error> )
            {
                throw;
            }
            catch (Exception ex)
            {
                ResourceManagementWebServicev2.HandleException(ex);
                throw;
            }
        }
コード例 #13
0
        public Stream CreateResource(ResourceUpdateRequest request)
        {
            try
            {
                AttributeValueUpdate objectTypeUpdate = request.Attributes.FirstOrDefault(t => t.Name == AttributeNames.ObjectType);

                if (objectTypeUpdate == null)
                {
                    throw new ArgumentException("An object type must be specified");
                }

                string objectType = objectTypeUpdate.Value?[0] as string;

                if (objectType == null)
                {
                    throw new ArgumentException("An object type must be specified");
                }

                ResourceObject resource = Global.Client.CreateResource(objectType);

                foreach (AttributeValueUpdate kvp in request.Attributes)
                {
                    if (kvp.Value.Length > 1)
                    {
                        resource.Attributes[kvp.Name].SetValue(kvp.Value);
                    }
                    else if (kvp.Value.Length == 1)
                    {
                        resource.Attributes[kvp.Name].SetValue(kvp.Value[0]);
                    }
                    else
                    {
                        resource.Attributes[kvp.Name].RemoveValues();
                    }
                }

                Global.Client.SaveResource(resource);

                string bareID = resource.ObjectID.ToString().Replace("urn:uuid:", string.Empty);

                Uri url = new Uri(WebOperationContext.Current.IncomingRequest.UriTemplateMatch.RequestUri, bareID);
                WebOperationContext.Current.OutgoingResponse.Headers.Add(HttpResponseHeader.Location, url.ToString());
                WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.Created;

                if (WebResponseHelper.RequestNoBody())
                {
                    return(null);
                }
                else
                {
                    resource.Refresh();
                    return(WebResponseHelper.GetResponse(resource, false));
                }
            }
            catch (WebFaultException)
            {
                throw;
            }
            catch (WebFaultException <Error> )
            {
                throw;
            }
            catch (Exception ex)
            {
                ResourceManagementWebServicev2.HandleException(ex);
                throw;
            }
        }