Beispiel #1
0
        /// <summary>
        /// Processes the request sent to the specified Uri.
        /// This method assumes that the request is already validated.
        /// </summary>
        /// <param name="context">An instance of HttpContext containing the request details.</param>
        /// <param name="statusCode">The HttpStatusCode indicating status of the request.</param>
        /// <returns>
        /// A string containing the response to the request.
        /// </returns>
        public string ProcessRequest(HttpContext context, out HttpStatusCode statusCode)
        {
            string response = string.Empty;
            SyndicationRequestType requestType = SyndicationHelper.GetSyndicationRequestType(context, this.baseUri);

            if (SyndicationRequestType.Help == requestType)
            {
                statusCode = HttpStatusCode.OK;
                response   = Properties.Resources.Help;
            }
            else
            {
                string searchQuery = HttpUtility.UrlDecode(context.Request.QueryString.ToString());
                int    pageSize    = int.Parse(ConfigurationManager.AppSettings["DefaultPageSize"], CultureInfo.InvariantCulture);
                int    maxPageSize = int.Parse(ConfigurationManager.AppSettings["MaxPageSize"], CultureInfo.InvariantCulture);

                if (SyndicationRequestType.DefaultSearchWithPageNo == requestType ||
                    SyndicationRequestType.RSSSearchWithPageNo == requestType ||
                    SyndicationRequestType.ATOMSearchWithPageNo == requestType)
                {
                    pageSize = int.Parse(SyndicationHelper.GetValueOfParameterFromUri(context, this.baseUri,
                                                                                      requestType, SyndicationParameterType.PageSize), CultureInfo.InvariantCulture);
                    pageSize = (pageSize > maxPageSize) ? maxPageSize : pageSize;
                }

                AuthenticatedToken token = (AuthenticatedToken)context.Items["AuthenticatedToken"];
                List <Resource>    resources;
                try
                {
                    SearchEngine searchEngin =
                        new SearchEngine(pageSize, true, token);
                    int            totalRecords;
                    ZentityContext zentityContext = CoreHelper.CreateZentityContext();
                    SortProperty   sortProperty   =
                        new SortProperty("dateModified", SortDirection.Descending);

                    resources = searchEngin.SearchResources(searchQuery,
                                                            zentityContext, sortProperty, 0, out totalRecords).ToList();
                    ;
                }
                catch (SearchException exception)
                {
                    statusCode = HttpStatusCode.BadRequest;
                    response   = exception.Message;
                    return(response);
                }
                SyndicationFeed feed = SyndicationHelper.CreateSyndicationFeed(resources, searchQuery, context.Request.Url);

                response = SyndicationHelper.GetResponseDocument(feed, requestType);

                statusCode = HttpStatusCode.OK;
            }
            return(response);
        }
Beispiel #2
0
        /// <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="requestType">Type of Syndication request.</param>
        /// <param name="parameterName"><typeref name="SyndicationParameterType"/>
        /// 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,
                                                          SyndicationRequestType requestType, SyndicationParameterType parameterName)
        {
            UriTemplateMatch matchResult = SyndicationTemplates[requestType].Match(baseUri, context.Request.Url);

            if (null == matchResult)
            {
                return(string.Empty);
            }

            return(matchResult.BoundVariables[parameterName.ToString()]);
        }
Beispiel #3
0
        /// <summary>
        /// Validates the specified 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)
        {
            errorMessage = string.Empty;
            string httpRequestType = context.Request.RequestType.ToUpperInvariant();

            // Verify request type is GET
            if (!(PlatformConstants.GetRequestType.Equals(httpRequestType)))
            {
                errorMessage = Resources.SYNDICATION_INVALID_METHOD;
                return(false);
            }

            SyndicationRequestType requestType = SyndicationHelper.GetSyndicationRequestType(context, this.baseUri);

            if (SyndicationRequestType.Unknown == requestType)
            {
                errorMessage = Resources.SYNDICATION_BAD_REQUEST;
                return(false);
            }

            return(true);
        }
Beispiel #4
0
        /// <summary>
        /// Creates a RSS document from a syndication feed using Rss20FeedFormatter
        /// </summary>
        /// <param name="feed">Syndication feed</param>
        /// <param name="requestType">Type of Syndication request</param>
        /// <returns>string, containing ATOM/RSS feed in XML format</returns>
        internal static string GetResponseDocument(SyndicationFeed feed, SyndicationRequestType requestType)
        {
            XmlWriter     writer   = null;
            StringBuilder feedData = new StringBuilder();

            try
            {
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Encoding           = Encoding.UTF8;
                settings.ConformanceLevel   = ConformanceLevel.Auto;
                settings.OmitXmlDeclaration = false;

                writer = XmlWriter.Create(feedData, settings);
                SyndicationFeedFormatter formatter;

                if (SyndicationRequestType.RSSSearch == requestType ||
                    SyndicationRequestType.RSSSearchWithPageNo == requestType)
                {
                    formatter = feed.GetRss20Formatter();
                }
                else
                {
                    formatter = feed.GetAtom10Formatter();
                }
                formatter.WriteTo(writer);
            }
            finally
            {
                if (null != writer)
                {
                    writer.Close();
                }
            }

            return(feedData.ToString());
        }