Ejemplo n.º 1
0
        /// <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);
        }
Ejemplo n.º 2
0
        /// <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);
            }
        }
Ejemplo n.º 3
0
        /// <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);
        }
Ejemplo n.º 4
0
        /// <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);
            }
        }
Ejemplo n.º 5
0
        /// <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);
        }
Ejemplo n.º 6
0
        /// <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);
        }
Ejemplo n.º 7
0
        /// <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);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Handles the GET request send to the following uri:
        /// 1. Service Document Uri
        /// 2. Collection Uri
        /// 3. Collection Uri for next / previous page
        /// 4. Member resource Uri
        /// 5. Media Entry resource 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;

            AtomPubRequestType requestType = AtomPubHelper.GetAtomPubRequestType(context, base.BaseUri);

            // Get collection name
            string collectionName = AtomPubHelper.GetValueOfParameterFromUri(context, base.BaseUri,
                                                                             AtomPubParameterType.CollectionName);

            statusCode = HttpStatusCode.OK;

            // This assumes that the collection name is verified by ValidateRequest method.
            switch (requestType)
            {
            case AtomPubRequestType.ServiceDocument:
                response = this.GetServiceDocument(context);
                context.Response.ContentType = AtomPubConstants.ServiceDocumentContentType;
                break;

            case AtomPubRequestType.Collection:
                response = this.GetAtomFeed(collectionName);
                break;

            case AtomPubRequestType.CollectionWithPageNo:
                string strPageNo = AtomPubHelper.GetValueOfParameterFromUri(context, base.BaseUri,
                                                                            AtomPubParameterType.PageNo);
                long pageNumber = long.Parse(strPageNo, CultureInfo.InvariantCulture);
                response = this.GetAtomFeed(collectionName, pageNumber);
                context.Response.ContentType = AtomPubConstants.AtomFeedContentType;
                break;

            case AtomPubRequestType.EditMember:
            {
                // Get the requested member type and its id for verifying it existents.
                string memberId = AtomPubHelper.GetValueOfParameterFromUri(context, base.BaseUri,
                                                                           AtomPubParameterType.Id);

                try
                {
                    if (AtomPubHelper.ValidatePrecondition(context, base.BaseUri, out statusCode))
                    {
                        string eTag = AtomPubHelper.CalculateETag(memberId);
                        context.Response.AddHeader(AtomPubConstants.KeyETag, eTag);
                        response = this.GetAtomEntryDocument(collectionName, memberId).AtomEntry;
                        context.Response.ContentType = AtomPubConstants.AtomEntryContentType;
                        statusCode = HttpStatusCode.OK;
                    }
                    else
                    {
                        if (HttpStatusCode.PreconditionFailed == statusCode)
                        {
                            response = Properties.Resources.ATOMPUB_PRECONDITION_FAILED;
                        }
                        else
                        {
                            response = Properties.Resources.ATOMPUB_RESOURCE_NOT_MODIFIED;
                        }
                    }
                }
                catch (ResourceNotFoundException)
                {
                    statusCode = HttpStatusCode.NotFound;
                    response   = Properties.Resources.ATOMPUB_RESOURCE_NOT_FOUND;
                }
            }
            break;

            case AtomPubRequestType.EditMedia:
            {
                // Get the requested member type and its id for verifying it existents.
                string memberId = AtomPubHelper.GetValueOfParameterFromUri(context, base.BaseUri,
                                                                           AtomPubParameterType.Id);
                try
                {
                    if (AtomPubHelper.ValidatePrecondition(context, base.BaseUri, out statusCode))
                    {
                        //context.Response.Clear();
                        string eTag = AtomPubHelper.CalculateETag(memberId);
                        context.Response.AddHeader(AtomPubConstants.KeyETag, eTag);

                        byte[] content = null;

                        // Write the media contents on stream.
                        this.GetMediaResource(collectionName, memberId, out content);
                        context.Response.OutputStream.Write(content, 0, content.Length);

                        string fileExtension = string.Empty;

                        context.Response.ContentType = GetMediaContentType(collectionName, memberId, out fileExtension);

                        string attachedFileName = string.Format(CultureInfo.InvariantCulture, "attachment; filename={0}.{1}", memberId, fileExtension);

                        context.Response.AddHeader("Content-Disposition", attachedFileName);
                        context.Response.AddHeader("Content-Length", content.Length.ToString(CultureInfo.InvariantCulture));
                        statusCode = HttpStatusCode.OK;
                    }
                    else
                    {
                        if (HttpStatusCode.PreconditionFailed == statusCode)
                        {
                            response = Properties.Resources.ATOMPUB_PRECONDITION_FAILED;
                        }
                        else
                        {
                            response = Properties.Resources.ATOMPUB_RESOURCE_NOT_MODIFIED;
                        }
                    }
                }
                catch (ResourceNotFoundException)
                {
                    statusCode = HttpStatusCode.NotFound;
                    response   = Properties.Resources.ATOMPUB_RESOURCE_NOT_FOUND;
                }
            }
            break;

            case AtomPubRequestType.Unknwon:
            default:
                statusCode = HttpStatusCode.BadRequest;
                response   = Properties.Resources.ATOMPUB_BAD_REQUEST;
                break;
            }

            return(response);
        }