PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Raven.Database/Server/RavenFS/Controllers/ConfigController.cs

https://github.com/nwendel/ravendb
C# | 128 lines | 108 code | 20 blank | 0 comment | 2 complexity | 7e9f5c32bc9be38988e3f83c046f68a3 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-3-Clause, CC-BY-SA-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Specialized;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Net.Http;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Web.Http;
  11. using Raven.Database.Server.RavenFS.Extensions;
  12. using Raven.Abstractions.Logging;
  13. using Raven.Database.Server.RavenFS.Util;
  14. using Raven.Imports.Newtonsoft.Json;
  15. using Raven.Json.Linq;
  16. using Raven.Abstractions.Extensions;
  17. using System.Web.Http.ModelBinding;
  18. using System.Text.RegularExpressions;
  19. using Raven.Abstractions.FileSystem.Notifications;
  20. using Raven.Abstractions.FileSystem;
  21. namespace Raven.Database.Server.RavenFS.Controllers
  22. {
  23. public class ConfigController : RavenFsApiController
  24. {
  25. private static new readonly ILog Log = LogManager.GetCurrentClassLogger();
  26. [HttpGet]
  27. [Route("fs/{fileSystemName}/config")]
  28. public HttpResponseMessage Get()
  29. {
  30. string[] names = null;
  31. Storage.Batch(accessor => { names = accessor.GetConfigNames(Paging.Start, Paging.PageSize).ToArray(); });
  32. return this.GetMessageWithObject(names)
  33. .WithNoCache();
  34. }
  35. [HttpGet]
  36. [Route("fs/{fileSystemName}/config")]
  37. public HttpResponseMessage Get(string name)
  38. {
  39. try
  40. {
  41. RavenJObject config = null;
  42. Storage.Batch(accessor => { config = accessor.GetConfig(name); });
  43. return this.GetMessageWithObject(config, HttpStatusCode.OK)
  44. .WithNoCache();
  45. }
  46. catch (FileNotFoundException)
  47. {
  48. return this.GetEmptyMessage(HttpStatusCode.NotFound)
  49. .WithNoCache();
  50. }
  51. }
  52. [HttpGet]
  53. [Route("fs/{fileSystemName}/config/non-generated")]
  54. public HttpResponseMessage NonGeneratedConfigNames()
  55. {
  56. IEnumerable<string> configs = null;
  57. Storage.Batch(accessor => { configs = accessor.GetConfigNames(Paging.Start, Paging.PageSize).ToList(); });
  58. var searchPattern = new Regex("^(sync|deleteOp|raven\\/synchronization\\/sources|conflicted|renameOp)", RegexOptions.IgnoreCase);
  59. configs = configs.Where((c) => !searchPattern.IsMatch(c)).AsEnumerable();
  60. return this.GetMessageWithObject(configs)
  61. .WithNoCache();
  62. }
  63. [HttpGet]
  64. [Route("fs/{fileSystemName}/config/search")]
  65. public HttpResponseMessage ConfigNamesStartingWith(string prefix)
  66. {
  67. if (prefix == null)
  68. prefix = "";
  69. ConfigurationSearchResults results = null;
  70. Storage.Batch(accessor =>
  71. {
  72. int totalResults;
  73. var names = accessor.GetConfigNamesStartingWithPrefix(prefix, Paging.Start, Paging.PageSize,
  74. out totalResults);
  75. results = new ConfigurationSearchResults
  76. {
  77. ConfigNames = names,
  78. PageSize = Paging.PageSize,
  79. Start = Paging.Start,
  80. TotalCount = totalResults
  81. };
  82. });
  83. return this.GetMessageWithObject(results)
  84. .WithNoCache();
  85. }
  86. [HttpPut]
  87. [Route("fs/{fileSystemName}/config")]
  88. public async Task<HttpResponseMessage> Put(string name)
  89. {
  90. var json = await ReadJsonAsync();
  91. ConcurrencyAwareExecutor.Execute(() => Storage.Batch(accessor => accessor.SetConfig(name, json)), ConcurrencyResponseException);
  92. Publisher.Publish(new ConfigurationChangeNotification { Name = name, Action = ConfigurationChangeAction.Set });
  93. Log.Debug("Config '{0}' was inserted", name);
  94. return this.GetMessageWithObject(json, HttpStatusCode.Created)
  95. .WithNoCache();
  96. }
  97. [HttpDelete]
  98. [Route("fs/{fileSystemName}/config")]
  99. public HttpResponseMessage Delete(string name)
  100. {
  101. ConcurrencyAwareExecutor.Execute(() => Storage.Batch(accessor => accessor.DeleteConfig(name)),
  102. ConcurrencyResponseException);
  103. Publisher.Publish(new ConfigurationChangeNotification { Name = name, Action = ConfigurationChangeAction.Delete });
  104. Log.Debug("Config '{0}' was deleted", name);
  105. return GetEmptyMessage(HttpStatusCode.NoContent);
  106. }
  107. }
  108. }