예제 #1
0
        public async Task ReadCalendarObjectResource(Dictionary <string, string> propertiesAndHeaders,
                                                     HttpResponse response)
        {
            #region Extracting Properties

            string url;
            propertiesAndHeaders.TryGetValue("url", out url);

            #endregion

            //An easy way of accessing the headers of the http response
            response.GetTypedHeaders();

            //StorageManagement.SetUserAndCollection(principalUrl, collectionName);
            //Must return the Etag header of the COR

            var calendarRes = _resourceRespository.Get(url);

            if (calendarRes == null || !StorageManagement.ExistCalendarObjectResource(url))
            {
                response.StatusCode = (int)HttpStatusCode.NotFound;
                return;
            }

            var resourceBody = await StorageManagement.GetCalendarObjectResource(url);

            var etagProperty = calendarRes.Properties.FirstOrDefault(x => x.Name == "getetag");
            if (etagProperty != null)
            {
                var etag = XmlTreeStructure.Parse(etagProperty.Value).Value;
                response.Headers["etag"] = etag;
            }

            await response.WriteAsync(resourceBody);
        }
 public AdminProductController()
 {
     categoryManagement = new CategoryManagement();
     storageManagement  = new StorageManagement();
     cargoManagement    = new CargoManagement();
     productManagement  = new ProductManagement();
 }
예제 #3
0
        public async Task <bool> DeleteCalendarCollection(Dictionary <string, string> propertiesAndHeaders,
                                                          HttpResponse response)
        {
            #region Extracting Properties

            string url;
            propertiesAndHeaders.TryGetValue("url", out url);

            #endregion

            //The delete method default status code
            response.StatusCode = (int)HttpStatusCode.NoContent;
            //If the collection already is gone it is treated as a successful operation.
            if (!StorageManagement.ExistCalendarCollection(url))
            {
                return(true);
            }

            //The collection is retrieve and if something unexpected happened an internal error is reflected.
            var collection = _collectionRespository.Get(url);
            if (collection == null)
            {
                StorageManagement.DeleteCalendarCollection(url);
                response.StatusCode = (int)HttpStatusCode.InternalServerError;
                return(false);
            }


            await _collectionRespository.Remove(collection);

            return(StorageManagement.DeleteCalendarCollection(url));
        }
예제 #4
0
        private async Task CreateDefaultCalendar(Dictionary <string, string> propertiesAndHeaders)
        {
            #region Extracting Properties

            string principalId;
            propertiesAndHeaders.TryGetValue("principalId", out principalId);

            string collectionName;
            propertiesAndHeaders.TryGetValue("collectionName", out collectionName);

            string url;
            propertiesAndHeaders.TryGetValue("url", out url);

            #endregion

            //Adding the collection to the database

            var principal = await _principalRepository.GetByIdentifierAsync(principalId);

            var collection = new CalendarCollection(url, collectionName);

            principal.CalendarCollections.Add(collection);
            await _principalRepository.SaveChangesAsync();

            //Adding the collection folder.
            StorageManagement.AddCalendarCollectionFolder(url);
        }
예제 #5
0
        /// <summary>
        ///     Updates an existing COR from a PUT when a "If-Match" header is included using the corresponding etag.
        /// </summary>
        /// <param name="propertiesAndHeaders"></param>
        /// <param name="response"></param>
        private async Task UpdateCalendarObjectResource(Dictionary <string, string> propertiesAndHeaders,
                                                        HttpResponse response)
        {
            #region Extracting Properties

            string url;
            propertiesAndHeaders.TryGetValue("url", out url);

            string body;
            propertiesAndHeaders.TryGetValue("body", out body);

            //var headers = response.GetTypedHeaders();

            #endregion

            //var iCal = new VCalendar(body);

            //Fill the resource
            //var resource = FillResource(propertiesAndHeaders, iCal, response);

            var etag = $"\"{Guid.NewGuid()}\"";
            response.Headers["etag"] = etag;
            //headers.ETag = new EntityTagHeaderValue(etag, false);

            var errorStack = new Stack <string>();

            //updating the etag
            await _resourceRespository.CreateOrModifyProperty(url, "getetag", _namespacesSimple["D"],
                                                              $"<D:getetag {_namespaces["D"]}>{etag}</D:getetag>",
                                                              errorStack, true);

            //updating the ctag
            await
            _collectionRespository.CreateOrModifyProperty(
                url?.Remove(url.LastIndexOf("/", StringComparison.Ordinal) + 1), "getctag", _namespacesSimple["S"],
                $@"<S:getctag {_namespaces["S"]} >{Guid.NewGuid()}</S:getctag>", errorStack, true);

            //updating the lastmodified
            await _resourceRespository.CreateOrModifyProperty(url, "getlastmodified", _namespacesSimple["D"],
                                                              $"<D:getlastmodified {_namespaces["D"]}>{DateTime.Now}</D:getlastmodified>", errorStack, true);


            //Removing old File
            StorageManagement.DeleteCalendarObjectResource(url);
            //Adding New File
            await StorageManagement.AddCalendarObjectResourceFile(url, body);

            await _resourceRespository.CreateOrModifyProperty(url, "getcontentlength", "DAV:",
                                                              $"<D:getcontentlength {_namespaces["D"]}>{StorageManagement.GetFileSize(url)}</D:getcontentlength>",
                                                              errorStack, true);

            //the response for this methos is NO CONTENT
            response.StatusCode = (int)HttpStatusCode.NoContent;

            //Adding to the dataBase
            await _resourceRespository.SaveChangeAsync();
        }
예제 #6
0
        /// <summary>
        ///     Creates a new COR from a PUT when a "If-Non-Match" header is included
        /// </summary>
        /// <param name="propertiesAndHeaders"></param>
        /// <param name="response"></param>
        /// <param></param>
        private async Task CreateCalendarObjectResource(Dictionary <string, string> propertiesAndHeaders,
                                                        HttpResponse response)
        {
            #region Extracting Properties

            string url;
            propertiesAndHeaders.TryGetValue("url", out url);

            string body;
            propertiesAndHeaders.TryGetValue("body", out body);


            response.GetTypedHeaders();

            #endregion

            var iCal = new VCalendar(body);


            //filling the resource
            var resource = await FillResource(propertiesAndHeaders, iCal, response);

            //adding the resource to the db
            var collection = _collectionRespository.Get(url?.Remove(url.LastIndexOf("/", StringComparison.Ordinal) + 1));
            collection.CalendarResources.Add(resource);

            //adding the file
            await StorageManagement.AddCalendarObjectResourceFile(url, body);

            response.StatusCode = (int)HttpStatusCode.Created;

            //setting the content lenght property.
            var errorStack = new Stack <string>();
            await _resourceRespository.CreateOrModifyProperty(resource.Href, "getcontentlength", "DAV:",
                                                              $"<D:getcontentlength {_namespaces["D"]}>{StorageManagement.GetFileSize(url)}</D:getcontentlength>",
                                                              errorStack, true);

            await _collectionRespository.SaveChangeAsync();
        }
예제 #7
0
 public AdminStorageController()
 {
     storageManagement       = new StorageManagement();
     countyAndCityManagement = new CountyAndCityManagement();
 }
예제 #8
0
        //TODO: Poner esto en la capa de datos
        public async Task AddCalendarObjectResource(Dictionary <string, string> propertiesAndHeaders,
                                                    HttpResponse response)
        {
            #region Extracting Properties

            string url;
            propertiesAndHeaders.TryGetValue("url", out url);

            string ifmatch;
            var    ifMatchEtags = new List <string>();
            propertiesAndHeaders.TryGetValue("If-Match", out ifmatch);
            if (ifmatch != null)
            {
                ifMatchEtags = ifmatch.Split(',').ToList();
            }


            string ifnonematch;
            var    ifNoneMatchEtags = new List <string>();
            propertiesAndHeaders.TryGetValue("If-None-Match", out ifnonematch);
            if (ifnonematch != null)
            {
                ifNoneMatchEtags = ifnonematch.Split(',').ToList();
            }
            string body;
            propertiesAndHeaders.TryGetValue("body", out body);

            #endregion

            //Note: calendar object resource = COR

            //CheckAllPreconditions

            PreconditionCheck = new PutPrecondition(StorageManagement, _collectionRespository, _resourceRespository);
            if (!await PreconditionCheck.PreconditionsOK(propertiesAndHeaders, response))
            {
                return;
            }

            var resourceExist = await _resourceRespository.Exist(url);

            //If the ifmatch is included i look for the etag in the resource, but first the resource has to exist.
            //If all is ok and the if-match etag matches the etag in the resource then i update the resource.
            //If the if-match dont match then i set that the precondition failed.
            if (ifMatchEtags.Count > 0)
            {
                if (resourceExist)
                {
                    var resource     = _resourceRespository.Get(url);
                    var resourceEtag =
                        XmlTreeStructure.Parse(resource.Properties.FirstOrDefault(x => x.Name == "getetag")?.Value)
                        .Value;
                    if (ifMatchEtags.Contains(resourceEtag))
                    {
                        await UpdateCalendarObjectResource(propertiesAndHeaders, response);

                        return;
                    }
                    response.StatusCode = (int)HttpStatusCode.PreconditionFailed;
                    return;
                }
            }

            if (ifNoneMatchEtags.Count > 0 && ifNoneMatchEtags.Contains("*"))
            {
                if (!resourceExist)
                {
                    await CreateCalendarObjectResource(propertiesAndHeaders, response);

                    return;
                }
                response.StatusCode = (int)HttpStatusCode.PreconditionFailed;
                return;
            }

            if (resourceExist && StorageManagement.ExistCalendarObjectResource(url))
            {
                await UpdateCalendarObjectResource(propertiesAndHeaders, response);

                return;
            }
            await CreateCalendarObjectResource(propertiesAndHeaders, response);

            //return HTTP 201 Created
        }
예제 #9
0
        public async Task <bool> DeleteCalendarObjectResource(Dictionary <string, string> propertiesAndHeaders,
                                                              HttpResponse response)
        {
            #region Extracting Properties

            string url;
            propertiesAndHeaders.TryGetValue("url", out url);

            string ifmatch;
            var    ifMatchEtags = new List <string>();
            propertiesAndHeaders.TryGetValue("If-Match", out ifmatch);
            if (ifmatch != null)
            {
                ifMatchEtags = ifmatch.Split(',').ToList();
            }

            #endregion

            //if the collection doesnt exist in the user folder
            // the can't do anything

            var collectionUrl = url?.Remove(url.LastIndexOf("/", StringComparison.Ordinal) + 1);
            if (!StorageManagement.ExistCalendarCollection(collectionUrl) &&
                !await _collectionRespository.Exist(collectionUrl))
            {
                return(true);
            }

            var resource =
                _resourceRespository.Get(url);

            if (ifMatchEtags.Count > 0)
            {
                if (resource != null)
                {
                    var resourceEtag =
                        XmlTreeStructure.Parse(resource.Properties.FirstOrDefault(x => x.Name == "getetag")?.Value)
                        .Value;
                    if (resourceEtag != null && ifMatchEtags.Contains(resourceEtag))
                    {
                        response.StatusCode = (int)HttpStatusCode.NoContent;
                        await _resourceRespository.Remove(resource);


                        //updating the ctag
                        var stack = new Stack <string>();
                        await
                        _collectionRespository.CreateOrModifyProperty(collectionUrl, "getctag",
                                                                      _namespacesSimple["S"],
                                                                      $@"<S:getctag {_namespaces["S"]} >{Guid.NewGuid()}</S:getctag>", stack, true);


                        return(StorageManagement.DeleteCalendarObjectResource(url));
                    }
                }
            }


            if (resource != null)
            {
                response.StatusCode = (int)HttpStatusCode.NoContent;
                await _resourceRespository.Remove(resource);

                //updating the ctag
                var stack = new Stack <string>();
                await _collectionRespository.CreateOrModifyProperty(collectionUrl, "getctag", _namespacesSimple["S"],
                                                                    $@"<S:getctag {_namespaces["S"]} >{Guid.NewGuid()}</S:getctag>", stack, true);


                return(StorageManagement.DeleteCalendarObjectResource(url));
            }

            return(StorageManagement.DeleteCalendarObjectResource(url));
        }