public object DeleteRedirect(string redirectId) { try { // Get a reference to the redirect RedirectItem redirect = Repository.GetRedirectById(redirectId); if (redirect == null) { throw new RedirectNotFoundException(); } // Delete the redirect Repository.DeleteRedirect(redirect); // Return the redirect return(redirect); } catch (RedirectsException ex) { // Generate the error response return(Request.CreateResponse(JsonMetaResponse.GetError(HttpStatusCode.InternalServerError, ex.Message))); } }
public object GetRedirectsForMedia(int contentId) { IMedia media = ApplicationContext.Services.MediaService.GetById(contentId); if (media == null) { throw new RedirectsException(HttpStatusCode.NotFound, "A media item with the specified ID could not be found."); } try { return(JsonMetaResponse.GetSuccess(new { media = new { id = media.Id, name = media.Name }, redirects = Repository.GetRedirectsByMediaId(contentId) })); } catch (RedirectsException ex) { return(Request.CreateResponse(JsonMetaResponse.GetError(HttpStatusCode.InternalServerError, ex.Message))); } }
public object GetLastEditedData(int siteId, int max = 5) { try { // Look for the site with the specified ID IDashboardSite site = DashboardContext.Current.GetSiteById(siteId); // Throw an exception if the site wasn't found if (site == null) { throw new DashboardException(HttpStatusCode.NotFound, "Det efterspurgte site blev ikke fundet."); } // Get a reference to the current user //User user = umbraco.BusinessLogic.User.GetCurrent(); // Get the last editied pages var pages = LastEditedItem.GetLastEditedData(site, null, max); // Return a nice JSON response return(new { culture = CultureInfo.CurrentCulture.Name, data = pages, }); } catch (DashboardException ex) { LogHelper.Error <DashboardController>("Unable to load last edited pages for site with ID " + siteId, ex); return(Request.CreateResponse(JsonMetaResponse.GetError(HttpStatusCode.InternalServerError, ex.Message))); } catch (Exception ex) { LogHelper.Error <DashboardController>("Unable to load last edited pages for site with ID " + siteId + ": " + ex.Message, ex); return(Request.CreateResponse(JsonMetaResponse.GetError( HttpStatusCode.InternalServerError, "Unknown server error. Check the log for further information." ))); } }
public object GetBlocks(int pageId) { IPublishedContent content = Umbraco.Content(pageId); if (content == null) { return(Request.CreateResponse(JsonMetaResponse.GetError("Page not found or not published."))); } return(new { blocks = new [] { new { title = "History", view = "/App_Plugins/Skybrud.Analytics/Views/Blocks/History.html", periods = new [] { new { name = "Yesterday", alias = "yesterday" }, new { name = "Last week", alias = "lastweek" }, new { name = "Last month", alias = "lastmonth" }, new { name = "Last year", alias = "lastyear" } } } } }); }
public object GetBlocksForSite(int siteId, int save = 0) { try { // Look for the site with the specified ID IDashboardSite site = DashboardContext.Current.GetSiteById(siteId); // Throw an exception if the site wasn't found if (site == null) { throw new DashboardException(HttpStatusCode.NotFound, "Det efterspurgte site blev ikke fundet."); } // Set the active site of the user. //if (save == 1) DashboardHelpers.UserSettings.SetCurrentSite(umbraco.helper.GetCurrentUmbracoUser(), site); // Get the blocks for the site IDashboardBlock[] blocks = site.GetBlocks(); // Return the blocks return(new { site, blocks }); } catch (DashboardException ex) { LogHelper.Error <DashboardController>("Unable to load blocks for site with ID " + siteId, ex); return(Request.CreateResponse(JsonMetaResponse.GetError(HttpStatusCode.InternalServerError, ex.Message))); } catch (Exception ex) { LogHelper.Error <DashboardController>("Unable to load blocks for site with ID " + siteId, ex); return(Request.CreateResponse(JsonMetaResponse.GetError( HttpStatusCode.InternalServerError, "Unknown server error. Check the log for further information." ))); } }
public object SetDefaultSite(int siteId) { try { // Look for the site with the specified ID IDashboardSite site = DashboardContext.Current.GetSiteById(siteId); // Throw an exception if the site wasn't found if (site == null) { throw new DashboardException(HttpStatusCode.NotFound, "Det efterspurgte site blev ikke fundet."); } DashboardHelpers.UserSettings.SetDefaultSite(UmbracoContext.Security.CurrentUser, site); return(site); } catch (DashboardException ex) { return(Request.CreateResponse(JsonMetaResponse.GetError(HttpStatusCode.InternalServerError, ex.Message))); } catch (Exception ex) { return(Request.CreateResponse(JsonMetaResponse.GetError( HttpStatusCode.InternalServerError, "Unknown server error. Check the log for further information." ))); } }
public object GetVideoFromUrl(string url) { // Validate that we have an URL if (String.IsNullOrWhiteSpace(url)) { return(Request.CreateResponse(JsonMetaResponse.GetError(HttpStatusCode.BadRequest, "No URL specified"))); } Match m1 = Regex.Match(url, "vimeo.com/([0-9]+)$"); Match m2 = Regex.Match(url, @"youtu(?:\.be|be\.com)/(?:.*v(?:/|=)|(?:.*/)?)([a-zA-Z0-9-_]+)", RegexOptions.IgnoreCase); if (m1.Success) { string videoId = m1.Groups[1].Value; if (String.IsNullOrWhiteSpace(Config.VimeoConsumerKey)) { return(Request.CreateResponse(JsonMetaResponse.GetError(HttpStatusCode.InternalServerError, "Vimeo is not configured"))); } if (String.IsNullOrWhiteSpace(Config.VimeoConsumerSecret)) { return(Request.CreateResponse(JsonMetaResponse.GetError(HttpStatusCode.InternalServerError, "Vimeo is not configured"))); } VimeoService vimeo = VimeoService.CreateFromConsumerKey(Config.VimeoConsumerKey, Config.VimeoConsumerSecret); try { VimeoVideoResponse response = vimeo.Videos.GetInfo(Int32.Parse(videoId)); VimeoVideo video = response.Video; return(new { url = "https://vimeo.com/" + videoId, type = "vimeo", details = new { id = videoId, published = TimeUtils.GetUnixTimeFromDateTime(video.UploadDate), title = video.Title, description = video.Description, duration = (int)video.Duration.TotalSeconds, thumbnails = ( from thumbnail in video.Thumbnails select new { url = thumbnail.Url, width = thumbnail.Width, height = thumbnail.Height } ) } }); } catch (VimeoException ex) { return(Request.CreateResponse(JsonMetaResponse.GetError(HttpStatusCode.InternalServerError, "Something went wrong: " + ex.Message))); } } if (m2.Success) { if (String.IsNullOrWhiteSpace(Config.GoogleServerKey)) { return(Request.CreateResponse(JsonMetaResponse.GetError(HttpStatusCode.InternalServerError, "YouTube is not configured"))); } string videoId = m2.Groups[1].Value; GoogleService service = GoogleService.CreateFromServerKey(Config.GoogleServerKey); YouTubeVideoListResponse response = service.YouTube.Videos.GetVideos(new YouTubeVideoListOptions { Part = YouTubeVideoPart.Snippet + YouTubeVideoPart.ContentDetails, Ids = new[] { videoId } }); if (response.Body.Items.Length == 0) { return(Request.CreateResponse(JsonMetaResponse.GetError(HttpStatusCode.BadRequest, "Video not found"))); } YouTubeVideo video = response.Body.Items[0]; return(new { url = "https://www.youtube.com/watch?v=" + videoId, type = "youtube", details = new { id = videoId, published = TimeUtils.GetUnixTimeFromDateTime(video.Snippet.PublishedAt), title = video.Snippet.Title, description = video.Snippet.Description, duration = (int)video.ContentDetails.Duration.Value.TotalSeconds, thumbnails = new[] { GetThumbnail("default", video.Snippet.Thumbnails.Default), GetThumbnail("medium", video.Snippet.Thumbnails.Medium), GetThumbnail("high", video.Snippet.Thumbnails.High), GetThumbnail("standard", video.Snippet.Thumbnails.Standard) }.WhereNotNull() } }); } return(Request.CreateResponse(JsonMetaResponse.GetError(HttpStatusCode.BadRequest, "Unknown URL syntax"))); }
/// <summary> /// Returns a new JSON based error response with the specified <paramref name="statusCode"/>. /// </summary> /// <param name="statusCode">The status code to be used for the response.</param> /// <returns>An instance of <see cref="HttpResponseMessage"/>.</returns> protected virtual HttpResponseMessage ReturnError(HttpStatusCode statusCode) { return(CreateSpaResponse(JsonMetaResponse.GetError(statusCode, null))); }
/// <summary> /// Returns an instance of <see cref="HttpResponseMessage"/> representing a server error. /// </summary> /// <returns>An instance of <see cref="HttpResponseMessage"/>.</returns> protected virtual HttpResponseMessage ReturnServerError() { return(CreateSpaResponse(JsonMetaResponse.GetError(HttpStatusCode.InternalServerError, "Internal server error"))); }
/// <summary> /// Returns a new JSON based instance of <see cref="HttpResponseMessage"/> for the specified /// <paramref name="data"/>. /// /// If <paramref name="data"/> is an instance of <see cref="JsonMetaResponse"/>, the status code will be /// inherited from the <see cref="JsonMetaData.Code"/> property. In any other cases, the status code will be /// <see cref="HttpStatusCode.OK"/>. /// </summary> /// <param name="data">The data to be serialized to JSON and returned as the response body.</param> /// <returns>An instance of <see cref="HttpResponseMessage"/>.</returns> protected virtual HttpResponseMessage CreateSpaResponse(object data) { JsonMetaResponse meta = data as JsonMetaResponse; return(meta != null?CreateSpaResponse(meta.Meta.Code, meta) : CreateSpaResponse(HttpStatusCode.OK, data)); }
/// <summary> /// Creates a new <see cref="HttpResponseMessage"/> based on the specified <see cref="JsonMetaResponse"/>. The /// status code of the server response will automatically be derived from the <see cref="JsonMetaResponse"/>. /// </summary> /// <param name="request">The current request.</param> /// <param name="response">The meta response object to be returned.</param> public static HttpResponseMessage CreateResponse(this HttpRequestMessage request, JsonMetaResponse response) { return(request.CreateResponse(response.Meta.Code, response)); }
public object EditRedirect(int rootNodeId, string redirectId, string url, string linkMode, int linkId, string linkUrl, string linkName = null, bool permanent = true, bool regex = false, bool forward = false) { try { // Get a reference to the redirect RedirectItem redirect = Repository.GetRedirectById(redirectId); if (redirect == null) { throw new RedirectNotFoundException(); } // Some input validation if (String.IsNullOrWhiteSpace(url)) { throw new RedirectsException(Localize("redirects/errorNoUrl")); } if (String.IsNullOrWhiteSpace(linkUrl)) { throw new RedirectsException(Localize("redirects/errorNoDestination")); } if (String.IsNullOrWhiteSpace(linkMode)) { throw new RedirectsException(Localize("redirects/errorNoDestination")); } // Parse the link mode RedirectLinkMode mode; switch (linkMode) { case "content": mode = RedirectLinkMode.Content; break; case "media": mode = RedirectLinkMode.Media; break; case "url": mode = RedirectLinkMode.Url; break; default: throw new RedirectsException(Localize("redirects/errorUnknownLinkMode")); } // Initialize a new link item RedirectLinkItem destination = new RedirectLinkItem(linkId, linkName, linkUrl, mode); // Split the URL and query string string[] urlParts = url.Split('?'); url = urlParts[0].TrimEnd('/'); string query = urlParts.Length == 2 ? urlParts[1] : ""; // Update the properties of the redirect redirect.RootNodeId = rootNodeId; redirect.Url = url; redirect.QueryString = query; redirect.Link = destination; redirect.IsPermanent = permanent; redirect.IsRegex = regex; redirect.ForwardQueryString = forward; // Save/update the redirect Repository.SaveRedirect(redirect); // Return the redirect return(redirect); } catch (RedirectsException ex) { // Generate the error response return(Request.CreateResponse(JsonMetaResponse.GetError(HttpStatusCode.InternalServerError, ex.Message))); } }
/// <summary> /// Returns a new JSON based error response with the specified <paramref name="message"/>, <paramref name="message"/> and <paramref name="data"/>. /// </summary> /// <param name="statusCode">The status code to be used for the response.</param> /// <param name="message">The message of the error response.</param> /// <param name="data">The value of the <c>data</c> property in the JSON response.</param> /// <returns>An instance of <see cref="HttpResponseMessage"/>.</returns> protected virtual HttpResponseMessage ReturnError(HttpStatusCode statusCode, string message, object data) { return(CreateSpaResponse(JsonMetaResponse.GetError(statusCode, message, data))); }
public object GetData(int pageId, string period = "yesterday") { IPublishedContent content = Umbraco.Content(pageId); if (content == null) { return(Request.CreateResponse(JsonMetaResponse.GetError("Page not found or not published."))); } IPublishedContent site = GetSiteNode(content); if (site == null) { return(Request.CreateResponse(JsonMetaResponse.GetError("Unable to determine site node."))); } AnalyticsProfileSelection selection = site.Value("analyticsProfile") as AnalyticsProfileSelection; // Get a reference to the configuration of this package AnalyticsConfig config = AnalyticsConfig.Current; string profileId = null; GoogleService service = null; if (selection != null && selection.IsValid) { profileId = selection.Profile.Id; AnalyticsConfigUser user = config.GetUserById(selection.User.Id); if (user != null) { service = GoogleService.CreateFromRefreshToken( user.Client.ClientId, user.Client.ClientSecret, user.RefreshToken ); } } // Fallback to app settings (if specified) if (service == null && config.HasAppSettings) { profileId = config.AppSettings.AnalyticsProfileId; service = GoogleService.CreateFromRefreshToken( config.AppSettings.GoogleClientId, config.AppSettings.GoogleClientSecret, config.AppSettings.GoogleRefreshToken ); } if (String.IsNullOrWhiteSpace(profileId) || service == null) { return(Request.CreateResponse(JsonMetaResponse.GetError("The Analytics package is not configured."))); } AnalyticsDataMode mode = content.Id == site.Id ? AnalyticsDataMode.Site : AnalyticsDataMode.Page; Period p = Period.Parse(period); return(new { period = p, page = new { id = content.Id, name = content.Name, url = content.Url }, history = GetHistory(service.Analytics, profileId, content, mode, p) }); }
public object Search(int contextId, string keywords = "", int offset = 0, int limit = 10, string year = "") { HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*"); if (!contextId.IsExistingNode()) { return(Request.CreateResponse(JsonMetaResponse.GetError("contextId is not a valid Umbraco-node"))); } List <IPublishedContent> results; int total; try { SpecificFields fields = new SpecificFields { Fields = new List <ISpecificField> { new SpecificField { FieldName = "year", SearchTerms = new [] { year } } } }; SearchOptions initialSo = new SearchOptions { Text = keywords, DocumentTypes = new string[] { Constants.SkyConstants.DocumentTypes.NewsPage }, RootIds = new int[] { contextId }, Fields = { FieldList = new List <Field> { Field.GetFieldOption("nodeName_lci", 15, null), Field.GetFieldOption("titel_lci", 15, null), Field.GetFieldOption("teaser_lci", 15, null), Field.GetFieldOption(Constants.SkyConstants.Properties.ContentGrid, null, null) } }, Order = new Order() { FieldName = string.Format("{0}_range", Constants.SkyConstants.Properties.ContentDate), OrderDirection = OrderDirection.Descending, OrderType = OrderType.String }, DateRange = new DateRange() { FieldName = Constants.SkyConstants.Properties.ContentDate, End = DateTime.MaxValue, Start = DateTime.MinValue.AddDays(1) }, Limit = limit, Offset = offset, Debug = HttpContext.Current.IsDebuggingEnabled }; if (!string.IsNullOrEmpty(year)) { initialSo.SpecificFields = fields; } results = Skybrud.Umbraco.Search.SkybrudSearch.SearchDocuments( out total, initialSo ).ToList(); } catch (Exception ex) { Error error = new Error("Der skete en fejl på serveren"); LogHelper.Error <NewsController>(error.ToString(), ex); return(Request.CreateResponse(JsonMetaResponse.GetError(HttpStatusCode.InternalServerError, error.Message, error))); } var returnResults = results.Select(x => NewsSearchResult.GetFromContent(x)); JsonMetaResponse response = JsonMetaResponse.GetSuccess(returnResults); response.SetPagination(new JsonPagination { Total = total, Limit = limit, Offset = offset }); return(Request.CreateResponse(response)); }
/// <summary> /// Creates a new <code>HttpResponseMessage</code> based on the specified /// <code>JsonMetaResponse</code>. The status code of the server response will /// automatically be derived from the <code>JsonMetaResponse</code>. /// </summary> /// <param name="request">The current request.</param> /// <param name="response">The meta response object to be returned.</param> public static HttpResponseMessage CreateResponse(this HttpRequestMessage request, JsonMetaResponse response) { return request.CreateResponse(response.Meta.Code, response); }
public object Search(int siteid, string keywords = "", int offset = 0, int limit = 10) { HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*"); if (!siteid.IsExistingNode()) { return(Request.CreateResponse(JsonMetaResponse.GetError("siteid is not valid"))); } int total = 0; int pageTotal = 0; int jobTotal = 0; int employeeTotal = 0; try { #region Normal Search SearchOptions initialSo = new SearchOptions { Text = keywords, RootIds = new int[] { siteid }, DocumentTypes = new string[] { Constants.SkyConstants.DocumentTypes.SubPage, Constants.SkyConstants.DocumentTypes.FrontPage }, // add your own doctypes Fields = { FieldList = new List <Field> { Field.GetFieldOption("nodeName_lci", 15, null), Field.GetFieldOption("title_lci", 15, null), Field.GetFieldOption("teaser_lci", 15, null), Field.GetFieldOption(Constants.SkyConstants.Properties.ContentGrid, null, null), // add more fields to search here } }, Order = new Order { OrderType = OrderType.Score }, Limit = limit, Offset = offset, Debug = HttpContext.Current.IsDebuggingEnabled }; List <IPublishedContent> results = SkybrudSearch.SearchDocuments( out pageTotal, initialSo ).ToList(); #endregion // create response body List <SiteSearchResult> returnResults = results.Select(x => SiteSearchResult.GetFromContent(x)).ToList(); JsonMetaResponse response = JsonMetaResponse.GetSuccess(returnResults.Take(limit)); total = pageTotal + jobTotal + employeeTotal; response.SetPagination(new JsonPagination { Total = total, Limit = limit, Offset = offset }); //Set groups JObject obj = JObject.FromObject(response); return(obj); } catch (Exception ex) { Error error = new Error("Der skete en fejl på serveren"); LogHelper.Error <SiteController>(error.ToString(), ex); return(Request.CreateResponse(JsonMetaResponse.GetError(HttpStatusCode.InternalServerError, error.Message, error))); } }
/// <summary> /// Returns a new JSON based error response with the specified <paramref name="message"/>. /// </summary> /// <param name="message">The message of the error response.</param> /// <returns>An instance of <see cref="HttpResponseMessage"/>.</returns> protected virtual HttpResponseMessage ReturnError(string message) { return(CreateSpaResponse(JsonMetaResponse.GetError(HttpStatusCode.InternalServerError, message))); }
public object GetData(string url = "", [FromUri] string parts = "", int navLevels = 1, bool navContext = false, int nodeId = -1, int appSiteId = -1, string appHost = "", string appProtocol = "") { // Use the current URL as fallback for "appHost" and "appProtocol" appHost = String.IsNullOrWhiteSpace(appHost) ? Request.RequestUri.Host : appHost; appProtocol = String.IsNullOrWhiteSpace(appProtocol) ? Request.RequestUri.Scheme : appProtocol; HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*"); Stopwatch watch = Stopwatch.StartNew(); HttpStatusCode statusCode = HttpStatusCode.OK; try { HttpResponseMessage response; // if nodeId exists, prefer content from that node if (nodeId > 0) { IPublishedContent c = UmbracoContext.Current.ContentCache.GetById(nodeId); if (c != null) { url = c.Url; } } // Try get siteId from domain if (appSiteId == -1 && !string.IsNullOrWhiteSpace(appHost)) { appSiteId = Domain.GetRootFromDomain(appHost); } #region Setup // Get a reference to the site node IPublishedContent site = UmbracoContext.ContentCache.GetById(appSiteId); if (site == null) { return(ReturnServerError()); } // Parse the options from the query string SpaApiRequest options = new SpaApiRequest(appSiteId, url, parts) { Protocol = appProtocol, HostName = appHost }; if (BeforeSetup(options, site, out response)) { return(response); } #region Content lookup IPublishedContent content = null; if (options.IsPreview) { // Get the ID from the request int id = url.GetPreviewId(); // Attempt to set the preview context if (SpaHelpers.SetPreviewContext(id)) { // Look up the page in the content cache (with preview enabled) content = UmbracoContext.ContentCache.GetById(true, id); } // Show the 404 page if we don't have a content page at this point if (content == null) { // Get the culture ID int cultureNodeId = GetCultureIdFromUrl(options); // Lookup the culture node in the content cache IPublishedContent cultureNode = UmbracoContext.ContentCache.GetById(cultureNodeId); // Look for a 404 page configuration on the culture node if (cultureNode != null) { content = cultureNode.TypedContent(SkyConstants.Properties.NotFoundPage); } // If we have no content at this point, we just return a simple if (content == null) { return(CreateSpaResponse(JsonMetaResponse.GetError(statusCode, "Page not found"))); } } } else { // Get a reference to the current page (fetched regardless of "parts" as the URL determines the culture) content = GetContentFromInput(site, nodeId, url); // Handle "umbracoInternalRedirectId" when present if (content != null && content.HasValue(global::Umbraco.Core.Constants.Conventions.Content.InternalRedirectId)) { content = Umbraco.TypedContent(global::Umbraco.Core.Constants.Conventions.Content.InternalRedirectId); } } #endregion #region Handle any 404/redirects URLs if (content == null) { // Got any redirects? if (HandleRedirects(options, out response)) { return(response); } // Set the status code (404) statusCode = HttpStatusCode.NotFound; // Get the culture ID int cultureNodeId = GetCultureIdFromUrl(options); // Lookup the culture node in the content cache IPublishedContent cultureNode = UmbracoContext.ContentCache.GetById(cultureNodeId); // Look for a 404 page configuration on the culture node if (cultureNode != null) { content = cultureNode.GetPropertyValue <IPublishedContent>(SkyConstants.Properties.NotFoundPage); } // If we have no content at this point, we just return a simple if (content == null) { return(CreateSpaResponse(JsonMetaResponse.GetError(statusCode, "Page not found"))); } } #endregion // If "content" matches the locale node, we get the content of the front page instead if (content.DocumentTypeAlias == SkyConstants.DocumentTypes.Culture) { content = content.FirstChild() ?? content; } // Set the current culture Thread.CurrentThread.CurrentCulture = content.GetCulture(); Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture; // Initialize the main model SpaPartDataModel model = new SpaPartDataModel(appSiteId, content.Id); // Get a reference to the culture node (eg. "Dansk" or "English") IPublishedContent culture = content.AncestorOrSelf(2); // Initialize the site/culture settings SpaSiteModel siteModel = new SpaSiteModel(site, culture); #endregion #region Content if (options.Parts.Contains(SpaApiPart.Content)) { SpaContentModel spaContent = new SpaContentModel(siteModel, appHost, appProtocol); model.Content = spaContent.GetContent(siteModel, content); } #endregion #region Navigation if (options.Parts.Contains(SpaApiPart.Navigation)) { // to handle navigation right, we try to load the initial nodeId, so we dont get FirstChild on cultureNode //IPublishedContent navContent = UmbracoContext.Current.ContentCache.GetById(nodeId) ?? content; model.Navigation = new SpaNavigationModel(content, navLevels, navContext); } #endregion #region Site if (options.Parts.Contains(SpaApiPart.Site) && appSiteId > 0) { model.Site = siteModel; } #endregion watch.Stop(); model.ExecuteTimeMs = watch.ElapsedMilliseconds; return(CreateSpaResponse(statusCode, model)); } catch (Exception ex) { LogHelper.Error <SpaController>("SpaController Exception: ", ex); return(ReturnServerError()); } }