/// <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> /// 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.</param> /// <param name="statusCode">Contains the status of the request.</param> /// <returns>A string containing the response for the specified AtomPub request.</returns> /// <remarks>If a zip file is sent with the POST request to the collection uri, /// the contents of the zip file are extracted and the individual files will be uploaded /// in the repository. /// The zip file should contain the mets.xml containing the metadata of the individual files. /// If mets.xml is not found in the specified zip file, status code 'PreconditionFailed' will /// be returned. /// </remarks> 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); try { if (isAtomEntryType) { response = base.ProcessRequest(context, out statusCode); System.Xml.XmlTextReader responseReader = null; try { responseReader = new System.Xml.XmlTextReader(response, System.Xml.XmlNodeType.Document, null); SyndicationItem responseItem = SyndicationItem.Load(responseReader); string collectionName = AtomPubHelper.GetValueOfParameterFromUri(context, base.BaseUri, AtomPubParameterType.CollectionName); // Add <sword:treatment>Successfully created a {Collection Name}</sword:treatment> element. SwordPostProcessor.AddTreatmentElement(collectionName, responseItem); // Create atom entry document from syndication item. atomEntryDocument = new AtomEntryDocument(responseItem); } finally { responseReader.Close(); } context.Response.ContentType = AtomPubConstants.AtomEntryContentType; } else { // If request stream contains something other than AtomEntryDocument, // then client wants to create new member with Media. atomEntryDocument = this.CreateMedia(context); // Done this separately because if AtomEntry is null due to some reasons, // then also it will set the content type. context.Response.ContentType = AtomPubConstants.AtomEntryContentType; } // return the atom entry response. if (null != atomEntryDocument) { response = atomEntryDocument.AtomEntry; statusCode = System.Net.HttpStatusCode.Created; } else { statusCode = System.Net.HttpStatusCode.InternalServerError; } } catch (MetsException ex) { statusCode = System.Net.HttpStatusCode.InternalServerError; response = ex.Message; } catch (SwordException ex) { statusCode = System.Net.HttpStatusCode.UnsupportedMediaType; response = ex.Message; } finally { string zipExtractedPath = context.Items[SwordConstants.ZipExtractedPath] as string; if (!string.IsNullOrEmpty(zipExtractedPath) && Directory.Exists(zipExtractedPath)) { Directory.Delete(zipExtractedPath, true); context.Items[SwordConstants.ZipExtractedPath] = null; } } return(response); }