/// <summary>
		/// Checks whether a resource exists at the specified paths.
		/// </summary>
		/// <param name="context">The <see cref="IMansionContext"/>.</param>
		/// <param name="path">The path to the resource.</param>
		/// <returns>Returns true when a resource exists, otherwise false.</returns>
		public bool Exists(IMansionContext context, IResourcePath path)
		{
			// validate arguments
			if (context == null)
				throw new ArgumentNullException("context");
			if (path == null)
				throw new ArgumentNullException("path");

			// check if the lookup table contains any of the paths
			return path.Paths.Select(TranslatePath).Any(candidate => lookupTable.ContainsKey(candidate));
		}
				/// <summary>
				/// Constructs a <see cref="FileResource.FileInputPipe"/>.
				/// </summary>
				/// <param name="response">The <see cref="GetObjectResponse"/>.</param>
				/// <param name="path">The <see cref="IResourcePath"/>.</param>
				public S3InputPipe(GetObjectResponse response, IResourcePath path)
				{
					// validate arguments
					if (response == null)
						throw new ArgumentNullException("response");
					if (path == null)
						throw new ArgumentNullException("path");

					// set values
					this.response = response;
					reader = new StreamReader(response.ResponseStream);
				}
示例#3
0
		/// <summary>
		/// Constructs a new template.
		/// </summary>
		/// <param name="sections">The sections defined in this template.</param>
		/// <param name="path">The resource path from which this template is loaded.</param>
		public Template(IEnumerable<Section> sections, IResourcePath path)
		{
			// validate arguments
			if (sections == null)
				throw new ArgumentNullException("sections");
			if (path == null)
				throw new ArgumentNullException("path");

			// initialize all the sections
			sections = sections.ToList();
			foreach (var section in sections)
				section.Template = this;

			// set values
			this.sections = sections;
			Path = path;
		}
		/// <summary>
		/// Tries to get resources based on the path. When not resource is found no exception is thrown.
		/// </summary>
		/// <param name="context">The <see cref="IMansionContext"/>.</param>
		/// <param name="path">The path to the resources.</param>
		/// <param name="resources">The resources found.</param>
		/// <returns>Returns true when a resource was found, otherwise false.</returns>
		public bool TryGet(IMansionContext context, IResourcePath path, out IEnumerable<IResource> resources)
		{
			// validate arguments
			if (context == null)
				throw new ArgumentNullException("context");
			if (path == null)
				throw new ArgumentNullException("path");

			// locate the resources by their path
			var resourceList = LocateResources(path).ToList();
			resources = resourceList;

			// return the resouces
			return resourceList.Count > 0;
		}
		/// <summary>
		/// Locates all the <see cref="IResource"/>s for the given <paramref name="path"/>.
		/// </summary>
		/// <param name="path">The <see cref="IResourcePath"/>.</param>
		/// <returns>Returns the resource.</returns>
		private IEnumerable<IResource> LocateResources(IResourcePath path)
		{
			// loop over all the paths
			foreach (var translatedPath in path.Paths.Select(TranslatePath))
			{
				//  check if the resource exists
				AssemblyName[] assemblyNames;
				if (!lookupTable.TryGetValue(translatedPath, out assemblyNames))
					continue;
				if (assemblyNames.Length == 0)
					throw new InvalidOperationException(string.Format("No assemblies registered for resource '{0}'", translatedPath));

				// if the path is not overridable just return the last resource
				if (!path.Overridable)
				{
					yield return new EmbeddedResource(resourceSubFolder + "." + translatedPath, assemblyNames.Last(), path);
					yield break;
				}

				// loop over all the assemblies to open the resources
				foreach (var assembly in assemblyNames)
					yield return new EmbeddedResource(resourceSubFolder + "." + translatedPath, assembly, path);
			}
		}
		/// <summary>
		/// Gets the resource from it's path.
		/// </summary>
		/// <param name="context">The <see cref="IMansionContext"/>.</param>
		/// <param name="path">The path to the resource.</param>
		/// <returns>Returns the resource.</returns>
		/// <exception cref="ResourceNotFoundException">Thrown when a resource can not be resolved from it's path.</exception>
		public IResource GetSingle(IMansionContext context, IResourcePath path)
		{
			// validate arguments
			if (context == null)
				throw new ArgumentNullException("context");
			if (path == null)
				throw new ArgumentNullException("path");

			return Get(context, path).Single();
		}
		/// <summary>
		/// Gets the resources from their path.
		/// </summary>
		/// <param name="context">The <see cref="IMansionContext"/>.</param>
		/// <param name="path">The path to the resources.</param>
		/// <returns>Returns the resources.</returns>
		/// <exception cref="ResourceNotFoundException">Thrown when a resource can not be resolved from it's path.</exception>
		public IEnumerable<IResource> Get(IMansionContext context, IResourcePath path)
		{
			// validate arguments
			if (context == null)
				throw new ArgumentNullException("context");
			if (path == null)
				throw new ArgumentNullException("path");

			// locate the resources by their path
			var resources = LocateResources(path).ToList();

			//if there are no resources for this path, throw an exception
			if (resources.Count == 0)
				throw new ResourceNotFoundException(new[] {path});

			// return the resouces
			return resources;
		}
		/// <summary>
		/// Gets the first and most important relative path of <paramref name="path"/>.
		/// </summary>
		/// <param name="context">The <see cref="IMansionContext"/>.</param>
		/// <param name="path">The <see cref="IResourcePath"/>.</param>
		/// <returns>Returns a string version of the most important relative path.</returns>
		public string GetFirstRelativePath(IMansionContext context, IResourcePath path)
		{
			// validate arguments
			if (context == null)
				throw new ArgumentNullException("context");
			if (path == null)
				throw new ArgumentNullException("path");

			// just return the first path
			return path.Paths.First();
		}
		/// <summary>
		/// Tries to get resources based on the path. When not resource is found no exception is thrown.
		/// </summary>
		/// <param name="context">The <see cref="IMansionContext"/>.</param>
		/// <param name="path">The path to the resources.</param>
		/// <param name="resources">The resources found.</param>
		/// <returns>Returns true when a resource was found, otherwise false.</returns>
		public bool TryGet(IMansionContext context, IResourcePath path, out IEnumerable<IResource> resources)
		{
			// validate arguments
			if (context == null)
				throw new ArgumentNullException("context");
			if (path == null)
				throw new ArgumentNullException("path");
			CheckDisposed();

			// retrieve the resources
			resources = (from physicalPath in TryLocate(path)
			             select new FileResource(physicalPath, path));

			// check if any resource was found
			return resources.Any(x => true);
		}
		/// <summary>
		/// Constructs this embedded resource.
		/// </summary>
		/// <param name="name">The name of the resource.</param>
		/// <param name="assemblyName">The <see cref="Assembly"/> from which to load the resource.</param>
		/// <param name="path">The <see cref="IResourcePath"/>.</param>
		/// <exception cref="ArgumentNullException">Thrown if either <paramref name="name"/>, <paramref name="assemblyName"/> or <paramref name="path"/> is null.</exception>
		public EmbeddedResource(string name, AssemblyName assemblyName, IResourcePath path)
		{
			// validate arguments
			if (string.IsNullOrEmpty(name))
				throw new ArgumentNullException("name");
			if (assemblyName == null)
				throw new ArgumentNullException("assemblyName");
			if (path == null)
				throw new ArgumentNullException("path");

			// set values
			resourceName = assemblyName.Name + "." + name;
			this.assemblyName = assemblyName;
			Path = path;
		}
		/// <summary>
		/// Deletes a resource idefentified by the specified <paramref name="path"/>.
		/// </summary>
		/// <param name="context">The <see cref="IMansionContext"/>.</param>
		/// <param name="path">The <see cref="IResourcePath"/> identifying the resource.</param>
		public void DeleteResource(IMansionContext context, IResourcePath path)
		{
			// validate arguments
			if (context == null)
				throw new ArgumentNullException("context");
			if (path == null)
				throw new ArgumentNullException("path");

			// delete the file if it exists
			var info = new FileInfo(ResourceUtils.Combine(physicalBasePath, relativeBasePath, path.Paths.Single()));
			if (info.Exists)
				info.Delete();
		}
			/// <summary>
			/// Constructs an S3 resource.
			/// </summary>
			/// <param name="service">The <see cref="S3ContentResourceService"/>.</param>
			/// <param name="metadata">The <see cref="GetObjectMetadataResponse"/> describing the data.</param>
			/// <param name="path">The <see cref="IResourcePath"/> of the resource.</param>
			public S3Resource(S3ContentResourceService service, GetObjectMetadataResponse metadata, IResourcePath path)
			{
				// validate arguments
				if (service == null)
					throw new ArgumentNullException("service");
				if (path == null)
					throw new ArgumentNullException("path");

				// set values
				this.service = service;
				this.metadata = metadata;
				this.path = path;
			}
		/// <summary>
		/// Checks whether a resource exists at the specified paths.
		/// </summary>
		/// <param name="context">The <see cref="IMansionContext"/>.</param>
		/// <param name="path">The path to the resource.</param>
		/// <returns>Returns true when a resource exists, otherwise false.</returns>
		public bool Exists(IMansionContext context, IResourcePath path)
		{
			// validate arguments
			if (path == null)
				throw new ArgumentNullException("path");
			CheckDisposed();

			try
			{
				client.Value.GetObjectMetadata(new GetObjectMetadataRequest().WithBucketName(bucketName).WithKey(path.Paths.Single())).Dispose();
				return true;
			}
			catch (AmazonS3Exception ex)
			{
				if (ex.StatusCode == System.Net.HttpStatusCode.NotFound)
					return false;

				//status wasn't not found, so throw the exception
				throw;
			}
		}
				/// <summary>
				/// Constructs a <see cref="FileResource.FileInputPipe"/>.
				/// </summary>
				/// <param name="service">The <see cref="S3ContentResourceService"/>.</param>
				/// <param name="path">The <see cref="IResourcePath"/>.</param>
				public S3OutputPipe(S3ContentResourceService service, IResourcePath path)
				{
					// validate arguments
					if (service == null)
						throw new ArgumentNullException("service");
					if (path == null)
						throw new ArgumentNullException("path");

					// set values
					this.service = service;
					this.path = path;
					stream = new MemoryStream();
					writer = new StreamWriter(stream);
				}
		/// <summary>
		/// Gets the resource from it's path.
		/// </summary>
		/// <param name="context">The <see cref="IMansionContext"/>.</param>
		/// <param name="path">The path to the resource.</param>
		/// <returns>Returns the resource.</returns>
		/// <exception cref="ResourceNotFoundException">Thrown when a resource can not be resolved from it's path.</exception>
		public IResource GetSingle(IMansionContext context, IResourcePath path)
		{
			return Get(context, path).Single();
		}
		/// <summary>
		/// Checks whether a resource exists at the specified paths.
		/// </summary>
		/// <param name="context">The <see cref="IMansionContext"/>.</param>
		/// <param name="path">The path to the resource.</param>
		/// <returns>Returns true when a resource exists, otherwise false.</returns>
		public bool Exists(IMansionContext context, IResourcePath path)
		{
			return TryLocate(path).Any();
		}
		/// <summary>
		/// Tries to locate a path.
		/// </summary>
		/// <param name="path"></param>
		/// <returns></returns>
		private IEnumerable<FileInfo> TryLocate(IResourcePath path)
		{
			// validate arguments
			if (path == null)
				throw new ArgumentNullException("path");
			if (path.Paths == null)
				throw new InvalidOperationException("A path must return an enumerable of paths");
			CheckDisposed();

			// get the appropriate path order
			IEnumerable<string> applicationPaths = path.Overridable ? rootPaths : rootPathsReverse;

			// loop through all the paths
			foreach (var fileInfo in path.Paths.SelectMany(resourcePath => applicationPaths.Select(applicationPath => ResourceUtils.Combine(applicationPath, resourcePath)).Select(candicatePath => new FileInfo(candicatePath)).Where(fileInfo => fileInfo.Exists)))
			{
				// return the found file
				yield return fileInfo;

				// check if 
				if (!path.Overridable)
					yield break;
			}
		}
		/// <summary>
		/// Gets the first and most important relative path of <paramref name="path"/>.
		/// </summary>
		/// <param name="context">The <see cref="IMansionContext"/>.</param>
		/// <param name="path">The <see cref="IResourcePath"/>.</param>
		/// <returns>Returns a string version of the most important relative path.</returns>
		public string GetFirstRelativePath(IMansionContext context, IResourcePath path)
		{
			// validate arguments
			if (context == null)
				throw new ArgumentNullException("context");
			if (path == null)
				throw new ArgumentNullException("path");
			CheckDisposed();

			return TryLocate(path).First().FullName.Substring(physicalBasePath.Length);
		}
示例#19
0
		/// <summary>
		/// Constructs a resource.
		/// </summary>
		/// <param name="fileInfo"></param>
		/// <param name="path"></param>
		public FileResource(FileInfo fileInfo, IResourcePath path)
		{
			// validate arguments
			if (fileInfo == null)
				throw new ArgumentNullException("fileInfo");
			if (path == null)
				throw new ArgumentNullException("path");

			// set values
			this.fileInfo = fileInfo;
			Path = path;
		}
		/// <summary>
		/// Opens the resource using the specified path. This will create the resource if it does not already exist.
		/// </summary>
		/// <param name="context">The <see cref="IMansionContext"/>.</param>
		/// <param name="path">The <see cref="IResourcePath"/> identifying the resource.</param>
		/// <returns>Returns the <see cref="IResource"/>.</returns>
		public IResource GetResource(IMansionContext context, IResourcePath path)
		{
			// validate arguments
			if (context == null)
				throw new ArgumentNullException("context");
			if (path == null)
				throw new ArgumentNullException("path");
			CheckDisposed();

			// get the meta data for the file and dispose the response streams inmediately
			GetObjectMetadataResponse metaData = null;
			try
			{
				metaData = client.Value.GetObjectMetadata(new GetObjectMetadataRequest().WithBucketName(bucketName).WithKey(path.Paths.Single()));
				metaData.Dispose();
			}
			catch (AmazonS3Exception ex)
			{
				//status wasn't not found, so throw the exception
				if (ex.StatusCode != System.Net.HttpStatusCode.NotFound)
					throw;
			}

			// create the resource
			return new S3Resource(this, metaData, path);
		}
		/// <summary>
		/// Checks whether a resource exists at the specified paths.
		/// </summary>
		/// <param name="context">The <see cref="IMansionContext"/>.</param>
		/// <param name="path">The path to the resource.</param>
		/// <returns>Returns true when a resource exists, otherwise false.</returns>
		public bool Exists(IMansionContext context, IResourcePath path)
		{
			// validate arguments
			if (context == null)
				throw new ArgumentNullException("context");
			if (path == null)
				throw new ArgumentNullException("path");

			return File.Exists(ResourceUtils.Combine(physicalBasePath, relativeBasePath, path.Paths.Single()));
		}
		/// <summary>
		/// Deletes a resource idefentified by the specified <paramref name="path"/>.
		/// </summary>
		/// <param name="context">The <see cref="IMansionContext"/>.</param>
		/// <param name="path">The <see cref="IResourcePath"/> identifying the resource.</param>
		public void DeleteResource(IMansionContext context, IResourcePath path)
		{
			// validate arguments
			if (context == null)
				throw new ArgumentNullException("context");
			if (path == null)
				throw new ArgumentNullException("path");
			CheckDisposed();

			// delete the data
			try
			{
				client.Value.DeleteObject(new DeleteObjectRequest().WithBucketName(bucketName).WithKey(path.Paths.Single())).Dispose();
			}
			catch (AmazonS3Exception ex)
			{
				// status wasn't not found, so throw the exception
				if (ex.StatusCode != System.Net.HttpStatusCode.NotFound)
					throw;
			}
		}
		/// <summary>
		/// Opens the resource using the specified path. This will create the resource if it does not already exist.
		/// </summary>
		/// <param name="context">The <see cref="IMansionContext"/>.</param>
		/// <param name="path">The <see cref="IResourcePath"/> identifying the resource.</param>
		/// <returns>Returns the <see cref="IResource"/>.</returns>
		public IResource GetResource(IMansionContext context, IResourcePath path)
		{
			// validate arguments
			if (context == null)
				throw new ArgumentNullException("context");
			if (path == null)
				throw new ArgumentNullException("path");

			return new FileResource(new FileInfo(ResourceUtils.Combine(physicalBasePath, relativeBasePath, path.Paths.Single())), path);
		}
		/// <summary>
		/// Gets the resources from their path.
		/// </summary>
		/// <param name="context">The <see cref="IMansionContext"/>.</param>
		/// <param name="path">The path to the resources.</param>
		/// <returns>Returns the resources.</returns>
		/// <exception cref="ResourceNotFoundException">Thrown when a resource can not be resolved from it's path.</exception>
		public IEnumerable<IResource> Get(IMansionContext context, IResourcePath path)
		{
			// validate arguments
			if (context == null)
				throw new ArgumentNullException("context");
			if (path == null)
				throw new ArgumentNullException("path");
			CheckDisposed();

			// loop through all the paths>
			var count = 0;

			// return the resolved resources
			foreach (var physicalPath in TryLocate(path))
			{
				count++;
				yield return new FileResource(physicalPath, path);
			}

			// make sure at least one resource was found
			if (count == 0)
				throw new ResourceNotFoundException(new[] {path});
		}