Ejemplo n.º 1
0
        /// <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);
        }
Ejemplo n.º 2
0
        /// <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);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Processes the GET and POST requests sent to the Sword service.
        /// </summary>
        /// <param name="context">HttpContext object containing the request information.</param>
        /// <param name="statusCode">Out parameter returns the status of the request.</param>
        /// <returns>Response in string format.</returns>
        public string ProcessRequest(HttpContext context, out System.Net.HttpStatusCode statusCode)
        {
            var unknownContentHeaders = context.Request.Headers.AllKeys
                                        .Intersect(AtomPubHelper.UnknownContentHeaders);

            if (0 < unknownContentHeaders.Count())
            {
                statusCode = HttpStatusCode.NotImplemented;
                return(string.Format(CultureInfo.InvariantCulture,
                                     Resources.ATOMPUB_UNKNOWN_CONTENT_HEADER,
                                     unknownContentHeaders.First()));
            }

            HttpRequestProcessor processor = null;

            switch (context.Request.RequestType)
            {
            case PlatformConstants.GetRequestType:
                processor = new SwordGetProcessor();
                break;

            case PlatformConstants.PostRequestType:
                processor = new SwordPostProcessor();
                break;

            case PlatformConstants.PutRequestType:
                processor = new AtomPubPutProcessor(SwordHelper.GetBaseUri());
                break;

            case PlatformConstants.DeleteRequestType:
                processor = new AtomPubDeleteProcessor(SwordHelper.GetBaseUri());
                break;

            default:
                processor = null;
                break;
            }

            if (null == processor)
            {
                statusCode = HttpStatusCode.NotImplemented;
                return(Resources.SWORD_UNSUPPORTED_REQUEST_TYPE);
            }

            try
            {
                string responseString = string.Empty;
                // Check if it is a valid request
                if (processor.ValidateRequest(context, out responseString))
                {
                    return(processor.ProcessRequest(context, out statusCode));
                }
                else
                {
                    statusCode = HttpStatusCode.BadRequest;
                    return(responseString);
                }
            }
            catch (UnauthorizedException ex)
            {
                statusCode = HttpStatusCode.Forbidden;
                return(ex.Message);
            }
        }