Ejemplo n.º 1
0
        /// <summary>
        /// Calls an RSS service and returns the service response.
        ///   This method goes crazy with the Regex class to parse the response
        ///   from an RSS service. After testing, I discovered that most
        ///   RSS services do not return a valid XML response. That requires
        ///   parsing through possibly faulty XML (missing end tags, etc.)
        /// </summary>
        /// <param name="requestInfo">
        /// a filled ServiceRequestInfo class
        /// </param>
        /// <returns>
        /// The Service Response Info
        /// </returns>
        private static ServiceResponseInfo CallRssService(ServiceRequestInfo requestInfo)
        {
            var responseInfo = new ServiceResponseInfo();

            // Grab the page
            var rawResponse = GetWebPage(requestInfo.Url);
            if (rawResponse == string.Empty)
            {
                responseInfo.ServiceStatus = "No reply from URL (could be timeout)";
                return responseInfo;
            }

            // Get Channel
            var channel = Regex.Match(rawResponse, @"<channel>(.*?)</channel>", RegexOptions.Singleline);
            if (channel.Success)
            {
                // Get Service title
                var serviceTitle = Regex.Match(
                    channel.Result("$1"), @"<title>(.*?)</title>", RegexOptions.Singleline | RegexOptions.IgnoreCase);
                if (serviceTitle.Success)
                {
                    responseInfo.ServiceTitle = serviceTitle.Result("$1");
                }

                // Get Service Link
                var serviceLink = Regex.Match(
                    channel.Result("$1"), @"<link>(.*?)</link>", RegexOptions.Singleline | RegexOptions.IgnoreCase);
                if (serviceLink.Success)
                {
                    responseInfo.ServiceLink = serviceLink.Result("$1");
                }

                // Get Service Description
                var serviceDescription = Regex.Match(
                    channel.Result("$1"),
                    @"<description>(.*?)</description>",
                    RegexOptions.Singleline | RegexOptions.IgnoreCase);
                if (serviceDescription.Success)
                {
                    responseInfo.ServiceDescription = serviceDescription.Result("$1");
                }

                // Get Service Copyright
                var serviceCopyright = Regex.Match(
                    channel.Result("$1"),
                    @"<copyright>(.*?)</copyright>",
                    RegexOptions.Singleline | RegexOptions.IgnoreCase);
                if (serviceCopyright.Success)
                {
                    responseInfo.ServiceCopyright = serviceCopyright.Result("$1");
                }
            }

            // Get Image
            var image = Regex.Match(rawResponse, @"<image>(.*?)</image>", RegexOptions.Singleline);
            if (image.Success)
            {
                // Get Image title
                var imageTitle = Regex.Match(
                    image.Result("$1"), @"<title>(.*?)</title>", RegexOptions.Singleline | RegexOptions.IgnoreCase);
                if (imageTitle.Success)
                {
                    responseInfo.ServiceImageTitle = imageTitle.Result("$1");
                }

                // Get Image Url
                var imageUrl = Regex.Match(
                    image.Result("$1"), @"<url>(.*?)</url>", RegexOptions.Singleline | RegexOptions.IgnoreCase);
                if (imageUrl.Success)
                {
                    responseInfo.ServiceImageUrl = imageUrl.Result("$1");
                }

                // Get Image Link
                var imageLink = Regex.Match(
                    image.Result("$1"), @"<link>(.*?)</link>", RegexOptions.Singleline | RegexOptions.IgnoreCase);
                if (imageLink.Success)
                {
                    responseInfo.ServiceImageLink = imageLink.Result("$1");
                }
            }

            // Get Items
            var items = Regex.Matches(rawResponse, @"<item>(.*?)</item>", RegexOptions.Singleline);
            var counter = 0;
            foreach (Match item in items)
            {
                counter++;
                if (counter > requestInfo.MaxHits)
                {
                    break;
                }

                var responseInfoItem = new ServiceResponseInfoItem();

                // Get Item title
                var itemTitle = Regex.Match(
                    item.Result("$1"), @"<title>(.*?)</title>", RegexOptions.Singleline | RegexOptions.IgnoreCase);
                if (itemTitle.Success)
                {
                    responseInfoItem.Title = itemTitle.Result("$1");
                }

                // Get Item Link
                var itemLink = Regex.Match(
                    item.Result("$1"), @"<link>(.*?)</link>", RegexOptions.Singleline | RegexOptions.IgnoreCase);
                if (itemLink.Success)
                {
                    responseInfoItem.Link = itemLink.Result("$1");
                }

                // Get Item Description
                var itemDescription = Regex.Match(
                    item.Result("$1"),
                    @"<description>(.*?)</description>",
                    RegexOptions.Singleline | RegexOptions.IgnoreCase);
                if (itemDescription.Success)
                {
                    responseInfoItem.Description = itemDescription.Result("$1");
                }

                // Add new item to Response Info
                responseInfo.Items.Add(responseInfoItem);
            }

            return responseInfo;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Call local or external service
        /// </summary>
        /// <param name="portalId">
        /// The portal ID.
        /// </param>
        /// <param name="userId">
        /// The user ID.
        /// </param>
        /// <param name="applicationFullPath">
        /// The application full path.
        /// </param>
        /// <param name="requestInfo">
        /// a filled ServiceRequestInfo class
        /// </param>
        /// <param name="appleseedPage">
        /// The Appleseed page.
        /// </param>
        /// <returns>
        /// ServiceResponseInfo
        /// </returns>
        public static ServiceResponseInfo CallService(
            int portalId,
            Guid userId,
            string applicationFullPath,
            ref ServiceRequestInfo requestInfo,
            Page appleseedPage)
        {
            var responseInfo = new ServiceResponseInfo { ServiceStatus = string.Empty };

            if (requestInfo.Type != ServiceType.RSSService)
            {
                if (portalId < 0)
                {
                    if (!GetPortalIDViaAlias(requestInfo.PortalAlias, ref portalId, appleseedPage))
                    {
                        responseInfo.ServiceStatus += "WARNING! Unknown PortalAlias";
                    }
                }

                if (userId != Guid.Empty)
                {
                    // TBD!!
                    // Note: should be controlled by key in appSettings:
                    // <add key="AllowRSSServiceSignin" value="false" />
                    // <add key="AllowWebServiceSignin" value="false" />
                    // userID = GetUserID(requestInfo.UserName, requestInfo.UserPassword);
                    /* Jakob says: later
                    if (requestInfo.UserName == string.Empty)
                        userID = -1;
                    else
                    {
                        Appleseed.Framework.Security.User user;
                        UsersDB usersDB = new UsersDB();
                        user = usersDB.Login(requestInfo.UserName, requestInfo.UserPassword, portalID);
                        if (user != null)
                            userID = int.Parse(user.ID.ToString());
                        else
                            userID = -1;
                    }
                    */
                }

                if (requestInfo.LocalMode)
                {
                    if (appleseedPage != null)
                    {
                        responseInfo.ServiceTitle = appleseedPage.PortalSettings.PortalName;
                        responseInfo.ServiceLink = Path.ApplicationFullPath;
                        responseInfo.ServiceDescription = appleseedPage.PortalSettings.PortalTitle;

                        responseInfo.ServiceImageTitle = appleseedPage.PortalSettings.PortalTitle;
                        responseInfo.ServiceImageUrl = Path.ApplicationFullPath +
                                                       appleseedPage.PortalSettings.PortalPath + "/logo.gif";
                        responseInfo.ServiceImageLink = Path.ApplicationFullPath;
                    }
                    else
                    {
                        responseInfo.ServiceTitle = "ServiceTitle TBD!"; // JLH!!
                        responseInfo.ServiceLink = Path.ApplicationFullPath;
                        responseInfo.ServiceDescription = "ServiceDescription TBD!";

                        responseInfo.ServiceImageTitle = "ImageTitle TBD!";
                        responseInfo.ServiceImageUrl = Path.ApplicationFullPath + "_Appleseed/logo.gif"; // JLH!! TBD!
                        responseInfo.ServiceImageLink = Path.ApplicationFullPath;
                    }
                }
            }

            // For test
            // responseInfo.ServiceStatus = "TEST: UserID=" + userID + " PortalID=" + portalID + " LocalMode=" + requestInfo.LocalMode;
            try
            {
                switch (requestInfo.Type)
                {
                    case ServiceType.CommunityWebService:
                        if (requestInfo.LocalMode)
                        {
                            // Run local code (not calling service via http/soap)
                            responseInfo.Items = GetResponseItemList(
                                portalId, userId, applicationFullPath, ref requestInfo);
                            if (responseInfo.ServiceStatus == string.Empty)
                            {
                                responseInfo.ServiceStatus = "OK";
                            }
                        }
                        else
                        {
                            responseInfo = CallCommunityService(requestInfo, "CommunityService.asmx");
                        }

                        responseInfo.ServiceDescription += " (" + requestInfo.ListType + "List WebService)";
                        break;
                    case ServiceType.CommunityRSSService:
                        if (requestInfo.LocalMode)
                        {
                            switch (requestInfo.ListType)
                            {
                                case ServiceListType.Tab:
                                    responseInfo.Items = GetResponseTabList(
                                        portalId, userId, Path.ApplicationFullPath, ref requestInfo);
                                    break;
                                case ServiceListType.Module:
                                    responseInfo.Items = GetResponseModuleList(
                                        portalId, userId, Path.ApplicationFullPath, ref requestInfo);
                                    break;
                                case ServiceListType.Item:
                                    responseInfo.Items = GetResponseItemList(
                                        portalId, userId, Path.ApplicationFullPath, ref requestInfo);
                                    break;
                            }

                            if (responseInfo.ServiceStatus == string.Empty)
                            {
                                responseInfo.ServiceStatus = "OK";
                            }
                        }
                        else
                        {
                            var oldUrl = requestInfo.Url;
                            if (!requestInfo.Url.EndsWith("/"))
                            {
                                requestInfo.Url += "/";
                            }

                            requestInfo.Url += "CommunityRSS.aspx" + AddRSSRequestParameters(requestInfo);
                            responseInfo = CallRssService(requestInfo);
                            requestInfo.Url = oldUrl;
                        }

                        responseInfo.ServiceDescription += " (RSS " + requestInfo.ListType + "List Service)";
                        break;
                    case ServiceType.RSSService:
                        responseInfo = CallRssService(requestInfo);
                        if (responseInfo.ServiceStatus == string.Empty)
                        {
                            responseInfo.ServiceStatus = "OK";
                        }

                        break;
                    default:
                        responseInfo.ServiceStatus = "ERROR! The requested service type is not supported";
                        break;
                }
            }
            catch (Exception ex)
            {
                responseInfo.ServiceStatus = "FATAL ERROR! Can not call service. Problem: " + ex.Message;
            }

            return responseInfo;
        }