/// <summary> /// Processes the GET request sent to the sword service. /// </summary> /// <param name="context">Object of HttpContext containing the request information.</param> /// <param name="statusCode">Out parameter returns status of the request.</param> /// <returns>String containing the response to the specified request.</returns> public override string ProcessRequest(HttpContext context, out System.Net.HttpStatusCode statusCode) { string response = string.Empty; AtomPubRequestType requestType = AtomPubHelper.GetAtomPubRequestType(context, base.BaseUri); switch (requestType) { case AtomPubRequestType.ServiceDocument: statusCode = System.Net.HttpStatusCode.OK; response = this.GetServiceDocument(context); // Set content type for Service Document - Bug Fix : 183173 context.Response.ContentType = AtomPubConstants.ServiceDocumentContentType; break; case AtomPubRequestType.Collection: case AtomPubRequestType.CollectionWithPageNo: case AtomPubRequestType.EditMember: case AtomPubRequestType.EditMedia: response = base.ProcessRequest(context, out statusCode); break; case AtomPubRequestType.Unknwon: default: statusCode = System.Net.HttpStatusCode.BadRequest; response = Properties.Resources.ATOMPUB_BAD_REQUEST; break; } return(response); }
/// <summary> /// Processes the request. /// </summary> /// <param name="context">The HttpContext for the incoming HTTP request.</param> /// <param name="statusCode">The HTTP Status Code that must be returned to the client.</param> /// <returns>The response that must be sent to the client.</returns> public override string ProcessRequest(HttpContext context, out HttpStatusCode statusCode) { if (!AtomPubHelper.ValidatePrecondition(context, base.BaseUri, out statusCode)) { if (HttpStatusCode.PreconditionFailed == statusCode) { return(Properties.Resources.ATOMPUB_PRECONDITION_FAILED); } else { return(Properties.Resources.ATOMPUB_RESOURCE_NOT_MODIFIED); } } string errorMessage = string.Empty; // used to hold updated member metadata. AtomEntryDocument atomDocument = null; AtomPubRequestType requestType = AtomPubHelper.GetAtomPubRequestType(context, base.BaseUri); try { switch (requestType) { case AtomPubRequestType.EditMember: atomDocument = UpdateMember(context); break; case AtomPubRequestType.EditMedia: atomDocument = UpdateMedia(context); break; case AtomPubRequestType.ServiceDocument: case AtomPubRequestType.Collection: case AtomPubRequestType.CollectionWithPageNo: case AtomPubRequestType.Unknwon: default: statusCode = HttpStatusCode.BadRequest; errorMessage = Resources.ATOMPUB_INVALID_URL; break; } } catch (ResourceNotFoundException) { statusCode = HttpStatusCode.NotFound; return(Properties.Resources.ATOMPUB_RESOURCE_NOT_FOUND); } if (null != atomDocument) { statusCode = HttpStatusCode.OK; string memberId = AtomPubHelper.GetValueOfParameterFromUri(context, base.BaseUri, AtomPubParameterType.Id); string eTag = AtomPubHelper.CalculateETag(memberId); context.Response.AddHeader(AtomPubConstants.KeyETag, eTag); return(atomDocument.AtomEntry); } else { return(errorMessage); } }
/// <summary> /// Updates the metadata for the specified resource. /// </summary> /// <param name="collectionName">Name of the target collection.</param> /// <param name="memberResourceId">The Id of the member resource.</param> /// <param name="atomEntry">Describes the resource to be modified.</param> /// <returns>A SyndicationItem that describes the updated resource.</returns> /// <exception cref="ArgumentNullException">Throws exception if collectionName is null/empty /// or atomEntry is null.</exception> /// <exception cref="ArgumentException">Throws exception if requested memberResourceId is not a unique identifier.</exception> SyndicationItem IAtomPubStoreWriter.UpdateMemberInfo(string collectionName, string memberResourceId, AtomEntryDocument atomEntry) { if (string.IsNullOrEmpty(collectionName)) { throw new ArgumentNullException("collectionName"); } if (string.IsNullOrEmpty(memberResourceId)) { throw new ArgumentNullException("memberResourceId"); } if (null == atomEntry) { throw new ArgumentNullException("atomEntry"); } using (ZentityContext context = CoreHelper.CreateZentityContext()) { ScholarlyWork resource = (ScholarlyWork)AtomPubHelper.GetMember(context, collectionName, memberResourceId, "Update"); resource.Files.Load(); // Bug Fix : 177689 - AtomPub (M2): Author node is appending after every PUT request instead of overwriting it. resource.Authors.Load(); resource.Contributors.Load(); UpdateResourceProperty(context, resource, atomEntry); context.SaveChanges(); return(ZentityAtomPubStoreReader.GenerateSyndicationItem(this.BaseUri, resource)); } }
/// <summary> /// Gets the resource property. /// </summary> /// <param name="resource">The resource.</param> /// <param name="propertyName">Name of the property.</param> /// <returns>A <see cref="ResourceProperty"/>.</returns> internal static ResourceProperty GetResourceProperty(ScholarlyWork resource, string propertyName) { if (!resource.ResourceProperties.IsLoaded) { try { resource.ResourceProperties.Load(); foreach (ResourceProperty resourceProperties in resource.ResourceProperties) { if (!resourceProperties.PropertyReference.IsLoaded) { resourceProperties.PropertyReference.Load(); } } } catch (InvalidOperationException) { // Do nothing if resource is not attached to the context. } } string propertyUri = AtomPubHelper.GetPropertyUri(propertyName); return(resource.ResourceProperties .Where(tuple => tuple.Property.Uri == propertyUri) .FirstOrDefault()); }
/// <summary> /// Gets the specified resource. /// </summary> /// <param name="collectionName">The name of the resource type.</param> /// <param name="memberResourceId">The Guid of the resource to return.</param> /// <returns>A SyndicationItem for the specified resource.</returns> /// <exception cref="ArgumentNullException">Throws exception if collectionName is null/empty.</exception> SyndicationItem IAtomPubStoreReader.GetMember(string collectionName, string memberResourceId) { using (ZentityContext context = CoreHelper.CreateZentityContext()) { ScholarlyWork resource = (ScholarlyWork)AtomPubHelper.GetMember(context, collectionName, memberResourceId, "Read"); return(ZentityAtomPubStoreReader.GenerateSyndicationItem(this.BaseUri, resource)); } }
/// <summary> /// Adds the child resources. /// </summary> /// <param name="extractionPath">The extraction path.</param> /// <param name="document">The document.</param> /// <param name="resource">The resource.</param> /// <param name="zentityContext">The zentity context.</param> private static void AddChildResources( string extractionPath, MetsDocument document, ScholarlyWork resource, ZentityContext zentityContext) { resource.Container = new ScholarlyWorkContainer(); ScholarlyWorkContainer childContainer = null; string[] fileNames = Directory.GetFiles(extractionPath) .Select(path => GetFileName(path)) .Where(name => SwordConstants.MetsDocumentName != name) .ToArray(); if (0 < fileNames.Length) { childContainer = new ScholarlyWorkContainer(); resource.Container.ContainedWorks.Add(childContainer); } // Loop though all files which are extracted. foreach (string fileName in fileNames) { // Get the extension int dotIndex = fileName.LastIndexOf('.'); string fileExtension = (0 < dotIndex) ? fileName.Substring(dotIndex + 1) : string.Empty; #region Upload Zip File Contents // Get Metadata for the specified fileName MetadataSection dataSection = document.Files[fileName]; // Create resource against each type as specified in the METS document. ScholarlyWork individualResource = CreateResouceUsingMetsMetadata(dataSection); UpdateResourceProeprties(zentityContext, individualResource, dataSection); // Create Media and Upload file contents. Core.File individualMediaResource = AddFileResource(zentityContext, individualResource, extractionPath + "\\" + fileName); individualMediaResource.MimeType = AtomPubHelper.GetMimeTypeFromFileExtension(fileExtension); individualMediaResource.FileExtension = fileExtension; // Save file name in notes for future references. individualMediaResource.Description = fileName; // Associate with the main resource. childContainer.ContainedWorks.Add(individualResource); #endregion AuthenticatedToken authenticatedToken = CoreHelper.GetAuthenticationToken(); individualResource.GrantDefaultPermissions(zentityContext, authenticatedToken); individualMediaResource.GrantDefaultPermissions(zentityContext, authenticatedToken); } }
/// <summary> /// Updates the Resource.File of the specified resource. /// </summary> /// <param name="collectionName">The type of the resource.</param> /// <param name="memberResourceId">The resource whose File needs to be updated.</param> /// <param name="mimeType">The MIME type of media.</param> /// <param name="media">The new File contents.</param> /// <returns>A SyndicationItem that describes the updated resource.</returns> /// <exception cref="ArgumentNullException">Throws exception if collectionName is null/empty /// or media is null.</exception> /// <exception cref="ArgumentException">Throws exception if requested memberResourceId is not a unique identifier.</exception> SyndicationItem IAtomPubStoreWriter.UpdateMedia(string collectionName, string memberResourceId, string mimeType, byte[] media) { if (string.IsNullOrEmpty(collectionName)) { throw new ArgumentNullException("collectionName"); } if (null == media) { throw new ArgumentNullException("media"); } if (string.IsNullOrEmpty(memberResourceId)) { throw new ArgumentNullException("memberResourceId"); } if (!AtomPubHelper.IsValidGuid(memberResourceId)) { throw new ArgumentException(Resources.ATOMPUB_INVALID_RESOURCE_ID, "memberResourceId"); } using (ZentityContext context = CoreHelper.CreateZentityContext()) { Type collectionType = CoreHelper.GetSystemResourceType(collectionName); // Prepare a query to get a resource with specified Id and specified type. string commandText = string.Format(CultureInfo.InvariantCulture, AtomPubConstants.EsqlToGetFileContents, collectionType.FullName); ObjectQuery <Core.File> query = new ObjectQuery <Core.File>(commandText, context); query.Parameters.Add(new ObjectParameter("Id", new Guid(memberResourceId))); Core.File mediaResource = query.FirstOrDefault(); if (null == mediaResource) { throw new ResourceNotFoundException(Resources.ATOMPUB_RESOURCE_NOT_FOUND); } if (!mediaResource.Authorize("Update", context, CoreHelper.GetAuthenticationToken())) { throw new UnauthorizedException(Resources.ATOMPUB_UNAUTHORIZED); } mediaResource.Resources.Load(); ScholarlyWork resource = (ScholarlyWork)mediaResource.Resources.First(); resource.DateModified = DateTime.Now; mediaResource.MimeType = mimeType; mediaResource.FileExtension = AtomPubHelper.GetFileExtension(mimeType); MemoryStream mediaStream = ZentityAtomPubStoreWriter.GetMediaStream(media); context.UploadFileContent(mediaResource, mediaStream); // Bug Fix : 180811 - Save Changes once mime type and contents are set. context.SaveChanges(); return(ZentityAtomPubStoreReader.GenerateSyndicationItem(this.BaseUri, resource)); } }
/// <summary> /// Deletes a media resource using the information present in the HttpContext. /// </summary> /// <param name="context">The HttpContext for the incoming HTTP request.</param> /// <returns>True if the operation succeeds, False otherwise.</returns> private bool DeleteMedia(HttpContext context) { // Gets the atom request template match result to get requested collection name. string collectionName = AtomPubHelper.GetValueOfParameterFromUri(context, base.BaseUri, AtomPubParameterType.CollectionName); string memberId = AtomPubHelper.GetValueOfParameterFromUri(context, base.BaseUri, AtomPubParameterType.Id); IAtomPubStoreWriter atomPubStoreWriter = AtomPubStoreFactory.GetAtomPubStoreWriter(base.BaseUri.OriginalString); return(atomPubStoreWriter.DeleteMedia(collectionName, memberId)); }
/// <summary> /// Gets a media contents of a member resource. /// </summary> /// <param name="collectionName">Name of the target collection.</param> /// <param name="memberResourceId">The Id of the member resource.</param> /// <param name="outputStream">A stream containing the media corresponding to the specified resource. If no matching media is found, null is returned.</param> /// <exception cref="ArgumentNullException">Throws exception if memberResourceId is null/empty /// or outputStream is null.</exception> /// <exception cref="ArgumentException">Throws exception if outputStream stream is not readable.</exception> void IAtomPubStoreReader.GetMedia(string collectionName, string memberResourceId, Stream outputStream) { if (string.IsNullOrEmpty(collectionName)) { throw new ArgumentNullException("collectionName"); } if (string.IsNullOrEmpty(memberResourceId)) { throw new ArgumentNullException("memberResourceId"); } if (null == outputStream) { throw new ArgumentNullException("outputStream"); } if (!AtomPubHelper.IsValidGuid(memberResourceId)) { throw new ArgumentException(Resources.ATOMPUB_INVALID_RESOURCE_ID); } if (!outputStream.CanWrite) { throw new ArgumentException(Properties.Resources.ATOMPUB_CANNOT_WRITE_ON_STREAM, "outputStream"); } using (ZentityContext context = CoreHelper.CreateZentityContext()) { ResourceType collectionType = coreHelper.GetResourceType(collectionName); // Prepare a query to get a resource with specified Id and specified type. string commandText = string.Format(CultureInfo.InvariantCulture, AtomPubConstants.EsqlToGetFileContents, collectionType.FullName); ObjectQuery <Core.File> query = new ObjectQuery <Core.File>(commandText, context); query.Parameters.Add(new ObjectParameter("Id", new Guid(memberResourceId))); Core.File mediaFile = query.FirstOrDefault(); if (null == mediaFile) { throw new ResourceNotFoundException(Resources.ATOMPUB_RESOURCE_NOT_FOUND); } if (!mediaFile.Authorize("Read", context, CoreHelper.GetAuthenticationToken())) { throw new UnauthorizedException(Resources.ATOMPUB_UNAUTHORIZED); } context.DownloadFileContent(mediaFile, outputStream); } }
/// <summary> /// Creates a member resource using the information present in the HttpContext. /// </summary> /// <param name="context">The HttpContext for the incoming HTTP request.</param> /// <returns>An AtomEntryDocument corresponding to the newly created member resource.</returns> private AtomEntryDocument CreateMember(HttpContext context) { string collectionName = AtomPubHelper.GetValueOfParameterFromUri(context, base.BaseUri, AtomPubParameterType.CollectionName); //Get AtomEntryDocument for request to update the member information. AtomEntryDocument atomEntry = new AtomEntryDocument(context.Request.InputStream); IAtomPubStoreWriter atomPubStoreWriter = AtomPubStoreFactory.GetAtomPubStoreWriter(base.BaseUri.OriginalString); SyndicationItem item = atomPubStoreWriter.CreateMember(collectionName, atomEntry); // Create atom entry document from syndication item. return(new AtomEntryDocument(item)); }
/// <summary> /// Creates a new Resource.File for a specified resource of type collectionName in the repository. /// </summary> /// <param name="collectionName">The resource type.</param> /// <param name="mimeType">The MIME type of media.</param> /// <param name="media">The new File contents.</param> /// <param name="fileExtension">The media file extension.</param> /// <returns>A SyndicationItem that describes the newly created resource.</returns> /// <exception cref="ArgumentNullException">Throws exception if collectionName is null/empty /// or mimeType is null/empty or media is null.</exception> protected SyndicationItem CreateMedia(string collectionName, string mimeType, byte[] media, string fileExtension) { if (string.IsNullOrEmpty(collectionName)) { throw new ArgumentNullException("collectionName"); } if (string.IsNullOrEmpty(mimeType)) { throw new ArgumentNullException("mimeType"); } if (null == media) { throw new ArgumentNullException("media"); } AuthenticatedToken authenticatedToken = CoreHelper.GetAuthenticationToken(); using (ZentityContext context = CoreHelper.CreateZentityContext()) { if (!authenticatedToken.HasCreatePermission(context)) { throw new UnauthorizedException(Resources.ATOMPUB_UNAUTHORIZED); } ScholarlyWork resource = CreateScholarlyWork(collectionName); resource.DateModified = DateTime.Now; Core.File mediaResource = new Core.File(); mediaResource.MimeType = mimeType; mediaResource.FileExtension = string.IsNullOrEmpty(fileExtension) ? AtomPubHelper.GetFileExtension(mimeType) : fileExtension; context.AddToResources(mediaResource); context.SaveChanges(); resource.Files.Add(mediaResource); MemoryStream mediaStream = ZentityAtomPubStoreWriter.GetMediaStream(media); context.UploadFileContent(mediaResource, mediaStream); mediaStream.Close(); resource.GrantDefaultPermissions(context, authenticatedToken); mediaResource.GrantDefaultPermissions(context, authenticatedToken); context.SaveChanges(); return(ZentityAtomPubStoreReader.GenerateSyndicationItem(this.BaseUri, resource)); } }
/// <summary> /// Updates a member resource using the information present in the HttpContext. /// </summary> /// <param name="context">The HttpContext for the incoming HTTP request.</param> /// <returns>An AtomEntryDocument corresponding to the updated member resource.</returns> private AtomEntryDocument UpdateMember(HttpContext context) { // Gets the atom request template match result to get requested collection name. string collectionName = AtomPubHelper.GetValueOfParameterFromUri(context, base.BaseUri, AtomPubParameterType.CollectionName); string memberId = AtomPubHelper.GetValueOfParameterFromUri(context, base.BaseUri, AtomPubParameterType.Id); IAtomPubStoreWriter atomPubStoreWriter = AtomPubStoreFactory.GetAtomPubStoreWriter(base.BaseUri.OriginalString); //Get AtomEntryDocument for request to update the member information. AtomEntryDocument atomDocument = new AtomEntryDocument(context.Request.InputStream); SyndicationItem item = atomPubStoreWriter.UpdateMemberInfo(collectionName, memberId, atomDocument); // Get AtomEntryDocument for response. return(new AtomEntryDocument(item)); }
/// <summary> /// Gets the property. /// </summary> /// <param name="context">The context.</param> /// <param name="propertyName">Name of the property.</param> /// <returns>A <see cref="Property"/> type.</returns> private static Property GetProperty(ZentityContext context, string propertyName) { string propertyUri = AtomPubHelper.GetPropertyUri(propertyName); Property property = context.Properties.FirstOrDefault(prp => prp.Uri == propertyUri); if (null == property) { property = new Property { Name = propertyName, Uri = propertyUri }; } return(property); }
/// <summary> /// Add Core.Person items to Navigation property of a resource. /// </summary> /// <param name="context">ZentityContext to search a person in database</param> /// <param name="contacts">Navigation property of resource to add persons</param> /// <param name="authors">List of persons to add to navigation property.</param> protected static void AddPersons(ZentityContext context, EntityCollection <Contact> contacts, IEnumerable <Person> authors) { foreach (Person author in authors) { if (!string.IsNullOrEmpty(author.Title)) { Match nameMatch = Regex.Match(author.Title, AtomPubConstants.PersonNamePattern, RegexOptions.IgnoreCase); author.FirstName = nameMatch.Groups[AtomPubConstants.FirstName].Value; author.MiddleName = nameMatch.Groups[AtomPubConstants.MiddleName].Value; author.LastName = nameMatch.Groups[AtomPubConstants.LastName].Value; } System.Linq.Expressions.Expression filter = null; ParameterExpression param = System.Linq.Expressions.Expression.Parameter(typeof(Person), "person"); if (!string.IsNullOrEmpty(author.Uri)) { AtomPubHelper.GeneratePersonFilterExpression("Uri", author.Uri, param, ref filter); } else if (!string.IsNullOrEmpty(author.Email)) { AtomPubHelper.GeneratePersonFilterExpression("Email", author.Email, param, ref filter); } else { AtomPubHelper.GeneratePersonFilterExpression(AtomPubConstants.FirstName, author.FirstName, param, ref filter); AtomPubHelper.GeneratePersonFilterExpression(AtomPubConstants.MiddleName, author.MiddleName, param, ref filter); AtomPubHelper.GeneratePersonFilterExpression(AtomPubConstants.LastName, author.LastName, param, ref filter); } if (null == filter) { continue; } Expression <Func <Person, bool> > predicate = System.Linq.Expressions.Expression.Lambda(filter, param) as Expression <Func <Person, bool> >; Person contact = context.People().FirstOrDefault(predicate); if (null == contact) { contact = author; } contacts.Add(contact); } }
/// <summary> /// Processes the request. /// </summary> /// <param name="context">The HttpContext for the incoming HTTP request.</param> /// <param name="statusCode">The HTTP Status Code that must be returned to the client.</param> /// <returns>The response that must be sent to the client.</returns> public override string ProcessRequest(HttpContext context, out System.Net.HttpStatusCode statusCode) { string response = string.Empty; bool isDeleted; AtomPubRequestType requestType = AtomPubHelper.GetAtomPubRequestType(context, base.BaseUri); try { switch (requestType) { case AtomPubRequestType.EditMember: isDeleted = DeleteMember(context); break; case AtomPubRequestType.EditMedia: isDeleted = DeleteMedia(context); break; case AtomPubRequestType.ServiceDocument: case AtomPubRequestType.Collection: case AtomPubRequestType.CollectionWithPageNo: case AtomPubRequestType.Unknwon: default: isDeleted = false; response = Resources.ATOMPUB_INVALID_URL; break; } } catch (ResourceNotFoundException) { statusCode = HttpStatusCode.NotFound; return(Properties.Resources.ATOMPUB_RESOURCE_NOT_FOUND); } if (isDeleted) { statusCode = HttpStatusCode.OK; } else { statusCode = HttpStatusCode.BadRequest; } return(response); }
/// <summary> /// Handles the POST request send to the collection Uri. The method assumes that the request is /// already validated using ValidateRequest method. /// </summary> /// <param name="context">HttpContext containing the request object.</param> /// <param name="statusCode">returns the status of the request.</param> /// <returns>A string containing the response for the specified AtomPub request.</returns> public override string ProcessRequest(HttpContext context, out System.Net.HttpStatusCode statusCode) { string response = string.Empty; AtomEntryDocument atomEntryDocument = null; bool isAtomEntryType = AtomPubHelper.IsAtomEntryMediaType(context.Request.ContentType); if (isAtomEntryType) { // If request stream contains AtomEntryDocument, then client wants to create new Member. atomEntryDocument = this.CreateMember(context); } else { // If request stream contains something other than AtomEntryDocument, // then client wants to create new member with Media. atomEntryDocument = this.CreateMedia(context); } // Add Location Header SyndicationLink editLink = atomEntryDocument.Links .Where(link => link.RelationshipType == AtomPubConstants.Edit) .FirstOrDefault(); if (null != editLink) { context.Response.AddHeader(AtomPubConstants.KeyLocation, editLink.Uri.AbsoluteUri); } // Add ETag header string[] memberIds = atomEntryDocument.Id.Split(new string[] { AtomPubConstants.IdPrefix }, StringSplitOptions.RemoveEmptyEntries); if (0 < memberIds.Length) { string eTag = AtomPubHelper.CalculateETag(memberIds[0]); context.Response.AddHeader(AtomPubConstants.KeyETag, eTag); } response = atomEntryDocument.AtomEntry; statusCode = HttpStatusCode.Created; return(response); }
/// <summary> /// Validates the incoming request. /// </summary> /// <param name="context">The HttpContext for the incoming HTTP request.</param> /// <param name="errorMessage">The HTTP Status Code if the request is invalid.</param> /// <returns>True if the request is valid, False otherwise.</returns> public override bool ValidateRequest(HttpContext context, out string errorMessage) { // Verify that type of request is edit member or edit media request. AtomPubRequestType requestType = AtomPubHelper.GetAtomPubRequestType(context, base.BaseUri); if (!(AtomPubRequestType.EditMember == requestType || AtomPubRequestType.EditMedia == requestType)) { errorMessage = Resources.ATOMPUB_INVALID_URL; return(false); } bool isAtomEntryType = AtomPubHelper.IsAtomEntryMediaType(context.Request.ContentType); if (requestType == AtomPubRequestType.EditMember && !isAtomEntryType) { errorMessage = Resources.ATOMPUB_UNSUPPORTED_CONTENT_TYPE; return(false); } // Get the requested member type and its id for verifying it existents. string collectionName = AtomPubHelper.GetValueOfParameterFromUri(context, base.BaseUri, AtomPubParameterType.CollectionName); if (!string.IsNullOrEmpty(collectionName) && AtomPubHelper.IsValidCollectionType(collectionName)) { string memberId = AtomPubHelper.GetValueOfParameterFromUri(context, base.BaseUri, AtomPubParameterType.Id); if (!string.IsNullOrEmpty(memberId) && AtomPubHelper.IsValidGuid(memberId)) { errorMessage = string.Empty; return(true); } else { errorMessage = Resources.ATOMPUB_INVALID_RESOURCE_ID; return(false); } } else { errorMessage = Resources.ATOMPUB_UNSUPPORTED_COLLECTION_NAME; return(false); } }
/// <summary> /// Updates a media resource using the information present in the HttpContext. /// </summary> /// <param name="context">The HttpContext for the incoming HTTP request.</param> /// <returns>An AtomEntryDocument corresponding to the updated media resource.</returns> private AtomEntryDocument UpdateMedia(HttpContext context) { // Gets the atom request template match result to get requested collection name. string collectionName = AtomPubHelper.GetValueOfParameterFromUri(context, base.BaseUri, AtomPubParameterType.CollectionName); string memberId = AtomPubHelper.GetValueOfParameterFromUri(context, base.BaseUri, AtomPubParameterType.Id); IAtomPubStoreWriter atomPubStoreWriter = AtomPubStoreFactory.GetAtomPubStoreWriter(base.BaseUri.OriginalString); // Get byte array to update media. BinaryReader reader = new BinaryReader(context.Request.InputStream); byte[] media = new byte[context.Request.InputStream.Length]; reader.Read(media, 0, media.Length); SyndicationItem item = atomPubStoreWriter.UpdateMedia(collectionName, memberId, context.Request.ContentType, media); // Get AtomEntryDocument for response. return(new AtomEntryDocument(item)); }
/// <summary> /// Gets the resource properties. /// </summary> /// <param name="resource">The resource.</param> /// <param name="propertyName">Name of the property.</param> /// <returns>List of <see cref="ResourceProperty"/>.</returns> internal static List <ResourceProperty> GetResourceProperties(ScholarlyWork resource, string propertyName) { if (!resource.ResourceProperties.IsLoaded) { try { resource.ResourceProperties.Load(); } catch (InvalidOperationException) { // Do nothing if resource is not attached to the context. } } string propertyUri = AtomPubHelper.GetPropertyUri(propertyName); return(resource.ResourceProperties .Where(tuple => tuple.Property.Uri == propertyUri) .ToList()); }
/// <summary> /// Validates the specified Sword request. /// </summary> /// <param name="context">An instance of HttpContext containing the request details.</param> /// <param name="errorMessage">An error message describing the reason if validation is failed.</param> /// <returns>True if the request is valid, otherwise false.</returns> public bool ValidateRequest(HttpContext context, out string errorMessage) { string httpRequestType = context.Request.RequestType.ToUpperInvariant(); // Verify request type is GET, PUT, POST, DELETE if (!(PlatformConstants.GetRequestType == httpRequestType || PlatformConstants.PutRequestType == httpRequestType || PlatformConstants.PostRequestType == httpRequestType || PlatformConstants.DeleteRequestType == httpRequestType)) { errorMessage = Resources.ATOMPUB_INVALID_METHOD; return(false); } Uri baseUri = new Uri(SwordHelper.GetBaseUri()); AtomPubRequestType requestType = AtomPubHelper.GetAtomPubRequestType(context, baseUri); bool isValidRequest = AtomPubRequestType.Unknwon != requestType; errorMessage = (isValidRequest) ? string.Empty : Resources.ATOMPUB_BAD_REQUEST; return(isValidRequest); }
/// <summary> /// Gets the member. /// </summary> /// <param name="context">The context.</param> /// <param name="collectionName">Name of the collection.</param> /// <param name="memberResourceId">The member resource id.</param> /// <returns>The <see cref="Resource"/>.</returns> internal static Resource GetMember(ZentityContext context, string collectionName, string memberResourceId) { if (string.IsNullOrEmpty(collectionName)) { throw new ArgumentNullException("collectionName"); } if (string.IsNullOrEmpty(memberResourceId)) { throw new ArgumentNullException("memberResourceId"); } if (!AtomPubHelper.IsValidGuid(memberResourceId)) { throw new ArgumentException(Resources.ATOMPUB_INVALID_RESOURCE_ID, "memberResourceId"); } Type collectionType = CoreHelper.GetSystemResourceType(collectionName); if (collectionType == null) { throw new ResourceNotFoundException(Resources.ATOMPUB_RESOURCE_NOT_FOUND); } string commandText = string.Format(CultureInfo.InvariantCulture, AtomPubConstants.EsqlToGetResourceById, collectionType.FullName); ObjectQuery <ScholarlyWork> resourceQuery = new ObjectQuery <ScholarlyWork>(commandText, context); resourceQuery.Parameters.Add(new ObjectParameter("Id", new Guid(memberResourceId))); Resource resource = resourceQuery.FirstOrDefault(); if (resource == null) { throw new ResourceNotFoundException(Resources.ATOMPUB_RESOURCE_NOT_FOUND); } return(resource); }
/// <summary> /// Deletes the Resource.File for the specified resource. /// </summary> /// <param name="collectionName">The type of the resource.</param> /// <param name="memberResourceId">The Guid of the resource.</param> /// <returns>True if the operation succeeds, False otherwise.</returns> /// <exception cref="ArgumentNullException">Throws exception if collectionName is null/empty.</exception> /// <exception cref="ArgumentException">Throws exception if requested memberResourceId is not a unique identifier.</exception> bool IAtomPubStoreWriter.DeleteMedia(string collectionName, string memberResourceId) { if (string.IsNullOrEmpty(collectionName)) { throw new ArgumentNullException("collectionName"); } if (!AtomPubHelper.IsValidGuid(memberResourceId)) { throw new ArgumentException(Resources.ATOMPUB_INVALID_RESOURCE_ID, "memberResourceId"); } using (ZentityContext context = CoreHelper.CreateZentityContext()) { Type collectionType = CoreHelper.GetSystemResourceType(collectionName); string commandText = string.Format(CultureInfo.InvariantCulture, AtomPubConstants.EsqlToGetFileContents, collectionType.FullName); ObjectQuery <Core.File> query = new ObjectQuery <Core.File>(commandText, context); query.Parameters.Add(new ObjectParameter("Id", new Guid(memberResourceId))); Core.File mediaFile = query.FirstOrDefault(); if (null == mediaFile) { throw new ResourceNotFoundException(Resources.ATOMPUB_RESOURCE_NOT_FOUND); } if (!mediaFile.Authorize("Delete", context, CoreHelper.GetAuthenticationToken())) { throw new UnauthorizedException(Resources.ATOMPUB_UNAUTHORIZED); } DeleteRelationships(context, mediaFile); context.DeleteObject(mediaFile); context.SaveChanges(); return(true); } }
/// <summary> /// Create resource using metadata. /// </summary> /// <param name="dataSection">Metadata Section containing metadata of the resource.</param> /// <returns>A resource of type ScholarlyWork or its derived type.</returns> private static ScholarlyWork CreateResouceUsingMetsMetadata(MetadataSection dataSection) { if (null == dataSection) { throw new ArgumentNullException("dataSection"); } ScholarlyWork resource = null; Type resourceType = null; // Get the resource type specified in METS document. if (null != dataSection.DescriptiveMetadata && null != dataSection.DescriptiveMetadata.ResourceType && 0 < dataSection.DescriptiveMetadata.ResourceType.Count) { string resourceTypeName = dataSection.DescriptiveMetadata.ResourceType[0]; // Check if the resource type is derived from ScholarlyWork, // as Sword & AtomPub supports only ScholarlyWork and its derivatives. if (AtomPubHelper.IsValidCollectionType(resourceTypeName)) { resourceType = CoreHelper.GetSystemResourceType(resourceTypeName); } } // Take ScholarlyWork as default resource type. if (null == resourceType) { resourceType = typeof(ScholarlyWork); } // Create resource of type ScholarlyWork or its derived types. resource = Activator.CreateInstance(resourceType, false) as ScholarlyWork; if (null == resource) { throw new SwordException(Properties.Resources.UNSUPPORTED_RESOURCE_TYPE); } return(resource); }
/// <summary> /// Creates a media resource using the information present in the HttpContext. /// </summary> /// <param name="context">The HttpContext for the incoming HTTP request.</param> /// <returns>An AtomEntryDocument corresponding to the newly created media resource.</returns> private AtomEntryDocument CreateMedia(HttpContext context) { SyndicationItem item = null; string collectionName = AtomPubHelper.GetValueOfParameterFromUri(context, base.BaseUri, AtomPubParameterType.CollectionName); // Get byte array to update media. BinaryReader reader = new BinaryReader(context.Request.InputStream); byte[] media = new byte[context.Request.InputStream.Length]; reader.Read(media, 0, media.Length); string fileExtention = string.Empty; if (context.Request.Headers.AllKeys.Contains("Content-Disposition")) { fileExtention = AtomPubHelper.GetFileExtentionFromContentDisposition( context.Request.Headers["Content-Disposition"]); } IAtomPubStoreWriter swordStoreWriter = AtomPubStoreFactory.GetSwordStoreWriter(); item = swordStoreWriter.CreateMedia(collectionName, context.Request.ContentType, media, fileExtention); AtomEntryDocument atomEntryDocument = null; if (null != item) { // Add <sword:treatment>Successfully created a {Collection Name}</sword:treatment> element. SwordPostProcessor.AddTreatmentElement(collectionName, item); // Create atom entry document from syndication item. atomEntryDocument = new AtomEntryDocument(item); } return(atomEntryDocument); }
/// <summary> /// Validates the request uri and returns the appropriate error message. /// If the request is valid, 'error' with empty string is returned. /// </summary> /// <param name="context">HttpContext containing the request object.</param> /// <param name="errorMessage">Contains the error message.</param> /// <returns>True if the request is valid, else false.</returns> public override bool ValidateRequest(HttpContext context, out string errorMessage) { bool isValid = false; AtomPubRequestType requestType = AtomPubHelper.GetAtomPubRequestType(context, base.BaseUri); switch (requestType) { case AtomPubRequestType.Collection: // Get collection name string collectionName = AtomPubHelper.GetValueOfParameterFromUri(context, base.BaseUri, AtomPubParameterType.CollectionName); // If valid collection name, then proceed. if (!string.IsNullOrEmpty(collectionName) && AtomPubHelper.IsValidCollectionType(collectionName)) { errorMessage = string.Empty; isValid = true; } else { errorMessage = Properties.Resources.ATOMPUB_UNSUPPORTED_COLLECTION_NAME; isValid = false; } break; case AtomPubRequestType.Unknwon: default: errorMessage = Properties.Resources.ATOMPUB_INVALID_URL; isValid = false; break; } return(isValid); }
/// <summary> /// Gets the value of specified parameter from the request Uri. /// </summary> /// <param name="context">HttpContext containing the request uri.</param> /// <param name="baseUri">Base Uri for the specified request.</param> /// <param name="parameterName"><typeref name="AtomPubParameterType"/> name of the parameter whose value is to be retrieved.</param> /// <returns>String containing value for the specified parameter.</returns> internal static string GetValueOfParameterFromUri(HttpContext context, Uri baseUri, AtomPubParameterType parameterName) { UriTemplateMatch matchResult = AtomPubHelper.GetTemplateMatchFromUri(baseUri, context.Request.Url); if (null == matchResult) { return(string.Empty); } string paramName = parameterName.ToString(); string paramValue = string.Empty; foreach (string paramKey in matchResult.BoundVariables.Keys) { if (paramKey.Equals(paramName, StringComparison.OrdinalIgnoreCase)) { paramValue = matchResult.BoundVariables[paramKey]; break; } } return(paramValue); }
/// <summary> /// Gets all the resource types that inherit from ScholarlyWork, including ScholarlyWork. /// </summary> /// <returns>An array of strings containing the resource type names.</returns> string[] IAtomPubStoreReader.GetCollectionNames() { List <string> resourceTypes = new List <string>(); using (ZentityContext zentityContext = CoreHelper.CreateZentityContext()) { resourceTypes = new List <string>(); // Get all resource types which are derived from ScholarlyWork as // only ScholarlyWork type supports authors list. foreach (ResourceType typeInfo in CoreHelper.GetResourceTypes(zentityContext)) { if (AtomPubHelper.IsValidCollectionType(typeInfo.Name)) { resourceTypes.Add(typeInfo.Name); } } } return(resourceTypes .OrderBy(name => name) .ToArray()); }
/// <summary> /// Deletes the specified resource. /// </summary> /// <param name="collectionName">The type of the resource.</param> /// <param name="memberResourceId">The Guid of the resource.</param> /// <returns>True if the operation succeeds, False otherwise.</returns> /// <exception cref="ArgumentNullException">Throws exception if collectionName is null/empty.</exception> /// <exception cref="ArgumentException">Throws exception if requested memberResourceId is not a unique identifier.</exception> bool IAtomPubStoreWriter.DeleteMember(string collectionName, string memberResourceId) { using (ZentityContext context = CoreHelper.CreateZentityContext()) { ScholarlyWork resource = (ScholarlyWork)AtomPubHelper.GetMember(context, collectionName, memberResourceId, "Delete"); // Load to delete all Core.File resource associated to requested scholarlywork. if (!resource.Files.IsLoaded) { resource.Files.Load(); } Zentity.Core.File[] resourceFiles = resource.Files.ToArray(); for (int i = 0; i < resourceFiles.Length; i++) { DeleteRelationships(context, resourceFiles[i]); context.DeleteObject(resourceFiles[i]); } DeleteRelationships(context, resource); // Delete associated Resource propertes resource.ResourceProperties.Load(); List <ResourceProperty> resProperties = resource.ResourceProperties.ToList(); foreach (ResourceProperty property in resProperties) { resource.ResourceProperties.Remove(property); context.DeleteObject(property); } context.DeleteObject(resource); context.SaveChanges(); return(true); } }
/// <summary> /// Creates a media resource using the information present in the HttpContext. /// </summary> /// <param name="context">The HttpContext for the incoming HTTP request.</param> /// <returns>An AtomEntryDocument corresponding to the newly created media resource.</returns> private AtomEntryDocument CreateMedia(HttpContext context) { string collectionName = AtomPubHelper.GetValueOfParameterFromUri(context, base.BaseUri, AtomPubParameterType.CollectionName); string fileExtention = string.Empty; if (context.Request.Headers.AllKeys.Contains("Content-Disposition")) { fileExtention = AtomPubHelper.GetFileExtentionFromContentDisposition( context.Request.Headers["Content-Disposition"]); } // Get byte array to update media. BinaryReader reader = new BinaryReader(context.Request.InputStream); byte[] media = new byte[context.Request.InputStream.Length]; reader.Read(media, 0, media.Length); IAtomPubStoreWriter atomPubStoreWriter = AtomPubStoreFactory.GetAtomPubStoreWriter(base.BaseUri.OriginalString); SyndicationItem item = atomPubStoreWriter.CreateMedia(collectionName, context.Request.ContentType, media, fileExtention); // Create atom entry document from syndication item. return(new AtomEntryDocument(item)); }
/// <summary> /// Validates the request uri and returns the appropriate error message. /// If the request is valid, 'error' with empty string is returned. /// Following requests are considered as valid : /// 1. Service Document Uri /// 2. Collection Uri /// 3. Collection Uri for next / previous page /// 4. Member resource Uri /// 5. Media Entry resource Uri /// </summary> /// <param name="context">HttpContext containing the request object.</param> /// <param name="errorMessage">Contains the error message.</param> /// <returns>True if the request is valid, else false.</returns> public override bool ValidateRequest(HttpContext context, out string errorMessage) { errorMessage = string.Empty; AtomPubRequestType requestType = AtomPubHelper.GetAtomPubRequestType(context, base.BaseUri); if (AtomPubRequestType.Unknwon == requestType) { errorMessage = Resources.ATOMPUB_INVALID_URL; return(false); } // If ServiceDocument uri then no need of further validations. if (AtomPubRequestType.ServiceDocument == requestType) { return(true); } // Get collection name string collectionName = AtomPubHelper.GetValueOfParameterFromUri(context, base.BaseUri, AtomPubParameterType.CollectionName); bool isValid = false; // If valid collection name, then proceed. if (!string.IsNullOrEmpty(collectionName) && AtomPubHelper.IsValidCollectionType(collectionName)) { switch (requestType) { case AtomPubRequestType.Collection: isValid = true; break; case AtomPubRequestType.CollectionWithPageNo: string strPageNo = AtomPubHelper.GetValueOfParameterFromUri(context, base.BaseUri, AtomPubParameterType.PageNo); long pageNumber = 0; if (!long.TryParse(strPageNo, out pageNumber)) { errorMessage = Resources.ATOMPUB_INVALID_PAGE_NUMBER; } else { isValid = true; } break; case AtomPubRequestType.EditMember: case AtomPubRequestType.EditMedia: // Get the requested id for verify it is GUID string memberId = AtomPubHelper.GetValueOfParameterFromUri(context, base.BaseUri, AtomPubParameterType.Id); if (AtomPubHelper.IsValidGuid(memberId)) { isValid = true; } else { errorMessage = Resources.ATOMPUB_INVALID_RESOURCE_ID; } break; default: isValid = false; errorMessage = Resources.ATOMPUB_INVALID_URL; break; } } else { errorMessage = Resources.ATOMPUB_UNSUPPORTED_COLLECTION_NAME; } return(isValid); }