예제 #1
0
        public override ActionResult GetTemplate(RequestParameters requestParameters, string templateName)
        {
            var templates = GetTemplatesByName(templateName).ToList();

            if (!templates.Any())
            {
                return(new HttpStatusCodeResult(HttpStatusCode.NotFound, "Template not found"));
            }

            if (templates.Count > 1)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.Ambiguous, "Ambiguous template name"));
            }

            var templateItem = templates.First();

            if (templateItem == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.NotFound, "Template not found"));
            }

            if (templateItem.TemplateId != TemplateIDs.Template.Guid)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.NotFound, "Item found, but it is not a template"));
            }

            var output = new JsonContentResultWriter(new StringWriter());

            WriteMetaData(output);

            WriteItemHeader(output, templateItem);

            output.WriteStartArray("fields");

            foreach (var field in templateItem.Children.SelectMany(section => section.Children))
            {
                var isSystemField = field.Name.StartsWith("__");

                if (isSystemField && !requestParameters.IncludeSystemFields)
                {
                    continue;
                }

                output.WriteStartObject();
                output.WritePropertyString("id", field.Id.ToString("B").ToUpperInvariant());
                output.WritePropertyString("uri", DatabaseName + "/" + templateItem.Id + "/" + field.Id.ToString("B").ToUpperInvariant());
                output.WritePropertyString("name", field.Name);
                output.WritePropertyString("displayName", field.DisplayName);
                output.WritePropertyString("type", field["Type"]);
                output.WritePropertyString("source", field["Source"]);
                output.WritePropertyString("sharing", field["Shared"] == "1" ? "shared" : field["Unversioned"] == "1" ? "unversioned" : "versioned");
                output.WritePropertyString("section", field.Parent?.Name ?? string.Empty);
                output.WritePropertyString("kind", "own");
                output.WriteEndObject();
            }

            output.WriteEndArray();

            return(output.ToContentResult());
        }
예제 #2
0
        protected virtual void WriteItems([NotNull] JsonContentResultWriter output, [NotNull] RequestParameters requestParameters, IEnumerable <Item> items)
        {
            items = FilterEnumerable(requestParameters, items);

            var count = items.Count();

            if (requestParameters.Skip > 0)
            {
                items = items.Skip(requestParameters.Skip);
            }

            if (requestParameters.Take > 0)
            {
                items = items.Take(requestParameters.Take);
            }

            WriteMetaData(output);

            output.WritePropertyString("count", count);
            output.WritePropertyString("skip", requestParameters.Skip);
            output.WritePropertyString("take", requestParameters.Take);

            output.WriteStartArray("items");

            foreach (var child in items)
            {
                output.WriteStartObject();
                WriteItemHeader(output, child, requestParameters.Flatten == 0);
                WriteItemFields(output, requestParameters, child);
                WriteItemChildren(output, requestParameters, child, requestParameters.Children);
                output.WriteEndObject();
            }

            output.WriteEndArray();
        }
예제 #3
0
        public override ActionResult GetItems(RequestParameters requestParameters)
        {
            SetContext(requestParameters);

            List <ID>          result;
            IEnumerable <Item> items;
            var index = ContentSearchManager.GetIndex("sitecore_" + Database.Name.ToLowerInvariant() + "_index");

            using (var context = index.CreateSearchContext())
            {
                var queryable = FilterQueryable(context.GetQueryable <SearchResultItem>(), requestParameters);

                result = queryable.Where(i => i.Name != "$name" && i.Name != "__Standard Values").Select(item => item.ItemId).ToList().Distinct().ToList();

                items = result.Select(Database.GetItem).Where(i => i != null).OrderBy(t => t.Name).ThenBy(t => t.Paths.Path);

                if (requestParameters.Skip > 0)
                {
                    items = items.Skip(requestParameters.Skip);
                }

                if (requestParameters.Take > 0)
                {
                    items = items.Take(requestParameters.Take);
                }

                items = items.ToList();
            }

            var output = new JsonContentResultWriter(new StringWriter());

            WriteMetaData(output);

            output.WritePropertyString("count", result.Count);
            output.WritePropertyString("skip", requestParameters.Skip);
            output.WritePropertyString("take", requestParameters.Take);

            output.WriteStartArray("items");

            foreach (var item in items)
            {
                output.WriteStartObject();
                WriteItemHeader(output, item);
                WriteItemFields(output, requestParameters, item);
                WriteItemChildren(output, requestParameters, item, requestParameters.Children);
                output.WriteEndObject();
            }

            output.WriteEndArray();

            return(output.ToContentResult());
        }
        public virtual ActionResult GetDatabases()
        {
            var output = new JsonContentResultWriter(new StringWriter());

            output.WriteStartObject("metadata");
            output.WritePropertyString("version", Constants.Version);
            output.WriteEndObject();

            output.WriteStartArray("databases");

            foreach (var database in ContentDeliveryManager.Databases)
            {
                output.WriteStartObject();
                output.WritePropertyString("name", database.DatabaseName);
                output.WritePropertyString("type", database.GetType().FullName ?? string.Empty);
                output.WriteEndObject();
            }

            output.WriteEndArray();

            return(output.ToContentResult());
        }
예제 #5
0
        public override ActionResult GetDatabase(RequestParameters requestParameters)
        {
            var rootItem = Items.FirstOrDefault(i => i.Parent == null);

            if (rootItem == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.NotFound, "Root item not found"));
            }

            var output = new JsonContentResultWriter(new StringWriter());

            WriteMetaData(output);

            output.WritePropertyString("type", "items");
            output.WritePropertyString("name", DatabaseName);
            output.WritePropertyString("icon16x16", string.Empty);
            output.WritePropertyString("icon32x32", string.Empty);

            output.WriteStartArray("languages");
            foreach (var language in Items.First(i => i.Path == "/sitecore/system/Languages").Children.OrderBy(l => l.Name))
            {
                output.WriteStartObject();
                output.WritePropertyString("name", language.Name);
                output.WritePropertyString("displayName", language.Name);
                output.WritePropertyString("cultureName", language["Iso"]);
                output.WriteEndObject();
            }

            output.WriteEndArray();

            output.WriteStartObject("root");
            WriteItemHeader(output, rootItem);
            WriteItemFields(output, requestParameters, rootItem);
            output.WriteEndObject();

            return(output.ToContentResult());
        }
예제 #6
0
        public override ActionResult GetItems(RequestParameters requestParameters)
        {
            var result = Filter(Items.AsQueryable(), requestParameters).Where(i => i.Name != "$name" && i.Name != "__Standard Values").ToList();
            var items  = result.OrderBy(t => t.Name).ThenBy(i => i.Path) as IEnumerable <FileDatabaseItem>;

            if (requestParameters.Skip > 0)
            {
                items = items.Skip(requestParameters.Skip);
            }

            if (requestParameters.Take > 0)
            {
                items = items.Take(requestParameters.Take);
            }

            items = items.ToList();

            var output = new JsonContentResultWriter(new StringWriter());

            WriteMetaData(output);

            output.WritePropertyString("count", result.Count);
            output.WritePropertyString("skip", requestParameters.Skip);
            output.WritePropertyString("take", requestParameters.Take);

            output.WriteStartArray("items");

            foreach (var item in items)
            {
                output.WriteStartObject();
                WriteItemHeader(output, item);
                WriteItemFields(output, requestParameters, item);
                WriteItemChildren(output, requestParameters, item, requestParameters.Children);
                output.WriteEndObject();
            }

            output.WriteEndArray();

            return(output.ToContentResult());
        }
예제 #7
0
        public override ActionResult GetTemplates(RequestParameters requestParameters)
        {
            var result = Filter(Items.AsQueryable(), requestParameters).Where(t => t.TemplateId == TemplateIDs.Template.Guid).Distinct().ToList();
            var items  = result.OrderBy(t => t.Name).ThenBy(i => i.Path) as IEnumerable <FileDatabaseItem>;

            if (requestParameters.Skip > 0)
            {
                items = items.Skip(requestParameters.Skip);
            }

            if (requestParameters.Take > 0)
            {
                items = items.Take(requestParameters.Take);
            }

            items = items.ToList();

            var output = new JsonContentResultWriter(new StringWriter());

            WriteMetaData(output);

            output.WritePropertyString("count", result.Count);
            output.WritePropertyString("skip", requestParameters.Skip);
            output.WritePropertyString("take", requestParameters.Take);

            output.WriteStartArray("templates");

            foreach (var item in items)
            {
                output.WriteStartObject();
                WriteItemHeader(output, item);
                output.WriteEndObject();
            }

            output.WriteEndArray();

            return(output.ToContentResult());
        }
예제 #8
0
        public override ActionResult GetChildren(RequestParameters requestParameters, string itemName)
        {
            var items = GetItemsByName(itemName);

            if (!items.Any())
            {
                return(new HttpStatusCodeResult(HttpStatusCode.NotFound, "Item not found"));
            }

            if (items.Count() > 1)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.Ambiguous, "Ambiguous item name"));
            }

            var item = items.First();

            if (item == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.NotFound, "Item not found"));
            }

            var children = item.Children;

            foreach (var pair in requestParameters.Parameters)
            {
                var fieldName = pair.Key;
                var value     = pair.Value;

                if (!fieldName.EndsWith("]"))
                {
                    children = children.Where(i => i[fieldName] == value);
                    continue;
                }

                var n = fieldName.LastIndexOf('[');
                if (n < 0)
                {
                    throw new SyntaxErrorException("[ expected");
                }

                var op = fieldName.Mid(n + 1, fieldName.Length - n - 2);
                fieldName = fieldName.Left(n).Trim();

                switch (op)
                {
                case "not":
                    children = children.Where(i => i[fieldName] != value);
                    break;

                case "in":
                    var l1 = value.Split('|');
                    children = children.Where(i => l1.Contains(i[fieldName]));
                    break;

                case "not in":
                    var l2 = value.Split('|');
                    children = children.Where(i => !l2.Contains(i[fieldName]));
                    break;

                case "has":
                    children = children.Where(t => !string.IsNullOrEmpty(t[fieldName]));
                    break;
                }
            }

            children = children.ToList();
            var count = children.Count();

            if (requestParameters.Skip > 0)
            {
                children = children.Skip(requestParameters.Skip);
            }

            if (requestParameters.Take > 0)
            {
                children = children.Take(requestParameters.Take);
            }

            var output = new JsonContentResultWriter(new StringWriter());

            WriteMetaData(output);

            output.WritePropertyString("count", count);
            output.WritePropertyString("skip", requestParameters.Skip);
            output.WritePropertyString("take", requestParameters.Take);

            output.WriteStartArray("items");

            foreach (var child in children)
            {
                output.WriteStartObject();
                WriteItemHeader(output, child);
                WriteItemFields(output, requestParameters, child);
                WriteItemChildren(output, requestParameters, child, requestParameters.Children);
                output.WriteEndObject();
            }

            output.WriteEndArray();

            return(output.ToContentResult());
        }
예제 #9
0
        public override ActionResult AddItem(RequestParameters requestParameters, string itemPath, string templateName)
        {
            var output = new JsonContentResultWriter(new StringWriter());

            WriteMetaData(output);

            var n = itemPath.LastIndexOf('/');

            if (n < 0)
            {
                output.WriteError("Parent path not found: " + itemPath);
                return(output.ToContentResult());
            }

            var parentPath = itemPath.Left(n);

            if (!ID.IsID(parentPath) && !parentPath.StartsWith("/", System.StringComparison.OrdinalIgnoreCase))
            {
                parentPath = "/" + parentPath;
            }

            var parentItem = Database.GetItem(parentPath);

            if (parentItem == null)
            {
                output.WriteError("Parent item not found: " + parentPath);
                return(output.ToContentResult());
            }

            if (!parentItem.Security.CanCreate(Context.User))
            {
                output.WriteError("You do not have permission to create items under item: " + parentItem.Paths.Path);
                return(output.ToContentResult());
            }

            var templateItem = Database.GetItem(templateName);

            if (templateItem == null)
            {
                output.WriteError("Template not found: " + templateName);
                return(output.ToContentResult());
            }

            var itemName = itemPath.Mid(n + 1);

            try
            {
                var newItem = parentItem.Add(itemName, new TemplateID(templateItem.ID));
                if (newItem != null)
                {
                    output.WriteStartObject("item");
                    WriteItemHeader(output, newItem);
                    output.WriteEndObject();
                }
                else
                {
                    output.WriteError("Failed to create item under " + parentItem.Paths.Path);
                }
            }
            catch (System.Exception ex)
            {
                output.WriteError("Failed to create item under " + parentItem.Paths.Path + ": " + ex.Message);
            }

            return(output.ToContentResult());
        }
예제 #10
0
        public override ActionResult GetTemplate(RequestParameters requestParameters, string templateName)
        {
            SetContext(requestParameters);

            var templates = GetTemplatesByName(templateName).ToList();

            if (!templates.Any())
            {
                return(new HttpStatusCodeResult(HttpStatusCode.NotFound, "Template not found"));
            }

            if (templates.Count > 1)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.Ambiguous, "Ambiguous template name"));
            }

            var templateItem = templates.First();

            if (templateItem == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.NotFound, "Template not found"));
            }

            var template = new TemplateItem(templateItem);

            if (template.InnerItem.TemplateID != TemplateIDs.Template)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.NotFound, "Item found, but it is not a template"));
            }

            var output = new JsonContentResultWriter(new StringWriter());

            WriteMetaData(output);

            WriteItemHeader(output, template);

            output.WriteStartArray("fields");

            foreach (var field in template.Fields.OrderBy(f => f.Section.Sortorder).ThenBy(f => f.Section.Name).ThenBy(f => f.Sortorder).ThenBy(f => f.Name))
            {
                var isOwnField    = field.InnerItem.Parent.Parent == template.InnerItem;
                var isSystemField = field.Name.StartsWith("__");

                if (isSystemField && !requestParameters.IncludeSystemFields)
                {
                    continue;
                }

                output.WriteStartObject();
                output.WritePropertyString("id", field.ID.ToString());
                output.WritePropertyString("uri", template.Database.Name + "/" + template.ID + "/" + field.ID);
                output.WritePropertyString("name", field.Name);
                output.WritePropertyString("displayName", field.DisplayName);
                output.WritePropertyString("type", field.Type);
                output.WritePropertyString("source", field.Source);
                output.WritePropertyString("sharing", field.IsShared ? "shared" : field.IsUnversioned ? "unversioned" : "versioned");
                output.WritePropertyString("section", field.Section.Name);
                output.WritePropertyString("kind", isOwnField ? "own" : isSystemField ? "system" : "inherited");
                output.WriteEndObject();
            }

            output.WriteEndArray();

            return(output.ToContentResult());
        }
예제 #11
0
        public override ActionResult GetDatabase(RequestParameters requestParameters)
        {
            SetContext(requestParameters);

            var output = new JsonContentResultWriter(new StringWriter());

            WriteMetaData(output);

            output.WritePropertyString("type", "items");
            output.WritePropertyString("name", DatabaseName);
            output.WritePropertyString("icon16x16", Images.GetThemedImageSource(Database.Icon, ImageDimension.id16x16));
            output.WritePropertyString("icon32x32", Images.GetThemedImageSource(Database.Icon, ImageDimension.id32x32));

            output.WriteStartArray("languages");
            foreach (var language in Database.GetLanguages().OrderBy(l => l.Name))
            {
                output.WriteStartObject();
                output.WritePropertyString("name", language.Name);
                output.WritePropertyString("displayName", language.GetDisplayName());
                output.WritePropertyString("cultureName", language.CultureInfo.Name);
                output.WriteEndObject();
            }

            output.WriteEndArray();

            output.WriteStartArray("roots");

            ICollection <Item> children = new List <Item>();

            if (string.IsNullOrEmpty(RootItemPath))
            {
                children.Add(Database.GetRootItem());
            }
            else
            {
                var rootItem = Database.GetItem(RootItemPath);
                if (rootItem == null)
                {
                    return(new HttpStatusCodeResult(HttpStatusCode.NotFound, "Root Item Path not found"));
                }

                foreach (Item child in rootItem.Children)
                {
                    children.Add(child);
                }
            }

            var rootItems = FilterEnumerable(requestParameters, children);

            foreach (var rootItem in rootItems)
            {
                output.WriteStartObject();
                WriteItemHeader(output, rootItem, requestParameters.Flatten == 0);
                WriteItemFields(output, requestParameters, rootItem);
                output.WriteEndObject();
            }

            output.WriteEndArray();

            return(output.ToContentResult());
        }
        public virtual ActionResult GetBundle()
        {
            var authenticationResult = AuthenticateUser();

            if (authenticationResult != null)
            {
                return(authenticationResult);
            }

            var output = new JsonContentResultWriter(new StringWriter());

            // run each bundle entry through the MVC pipeline
            foreach (var key in Request.Form.AllKeys)
            {
                if (string.IsNullOrEmpty(key))
                {
                    continue;
                }

                if (key == "username" || key == "password" || key == "domain")
                {
                    continue;
                }

                var url = Request.Form[key];

                var uri = new Uri(url, UriKind.RelativeOrAbsolute);
                if (!uri.IsAbsoluteUri)
                {
                    uri = new Uri("http://127.0.0.1" + url);
                }

                // find the route
                var textWriter  = new StringWriter();
                var httpContext = new HttpContextWrapper(new HttpContext(new HttpRequest(string.Empty, uri.AbsoluteUri, uri.Query.Mid(1)), new HttpResponse(textWriter)));
                var routeData   = RouteTable.Routes.GetRouteData(httpContext);
                if (routeData == null)
                {
                    return(new HttpStatusCodeResult(HttpStatusCode.NotFound, "Route not found: " + url));
                }

                // execute the controller
                var controllerName = routeData.Values["controller"].ToString();
                var requestContext = new RequestContext(httpContext, routeData);

                var controller = ControllerBuilder.Current.GetControllerFactory().CreateController(requestContext, controllerName);
                controller.Execute(requestContext);

                // if empty - write an empty object
                var result = textWriter.ToString();
                if (string.IsNullOrEmpty(result))
                {
                    output.WritePropertyName(key);
                    output.WriteStartObject();
                    output.WriteEndObject();
                    continue;
                }

                JObject jObject;
                try
                {
                    jObject = JObject.Parse(result);
                }
                catch
                {
                    return(new HttpStatusCodeResult(HttpStatusCode.NotFound, result));
                }

                output.WritePropertyName(key);
                jObject.WriteTo(output);
            }

            return(output.ToContentResult());
        }