public static void WriteMetadata(TextWriter writer, ODataFormatter formatter) { if (_edmx == null) { CreateEdmx(); } formatter.WriteMetadataInternal(writer); }
public void ProcessRequest(HttpContext context, string httpMethod, Stream inputStream) { ODataRequest odataReq = null; ODataFormatter formatter = null; var portalContext = (PortalContext)context.Items[PortalContext.CONTEXT_ITEM_KEY]; try { Content content; Exception requestError = null; try { odataReq = ODataRequest.Parse(portalContext.RequestedUri.GetComponents(UriComponents.Path, UriFormat.Unescaped), portalContext); this.ODataRequest = odataReq; } catch (Exception e) { requestError = e; } formatter = ODataFormatter.Create(portalContext, odataReq); if (formatter == null) { formatter = ODataFormatter.Create("json", portalContext); throw new ODataException(ODataExceptionCode.InvalidFormatParameter); } if (requestError != null) { var innerOdataError = requestError as ODataException; var message = "An error occured during request parsing. " + requestError.Message + " See inner exception for details."; var code = innerOdataError == null ? ODataExceptionCode.RequestError : innerOdataError.ODataExceptionCode; throw new ODataException(message, code, requestError); } odataReq.Format = formatter.FormatName; formatter.Initialize(odataReq); // Cross-Origin Resource Sharing (CORS) // Do this after the formatter was initialized to be able to provide a proper error message. HttpHeaderTools.AssertOriginHeader(); var exists = Node.Exists(odataReq.RepositoryPath); if (httpMethod != "POST" && !exists) { ContentNotFound(context, odataReq.RepositoryPath); return; } JObject model = null; switch (httpMethod) { case "GET": if (odataReq.IsServiceDocumentRequest) { formatter.WriteServiceDocument(portalContext, odataReq); } else if (odataReq.IsMetadataRequest) { formatter.WriteMetadata(context, odataReq); } else { if (!Node.Exists(odataReq.RepositoryPath)) { ContentNotFound(context, odataReq.RepositoryPath); } if (odataReq.HasContentQuery) { formatter.WriteQueryResult(portalContext, odataReq); } else if (odataReq.IsCollection) { formatter.WriteChildrenCollection(odataReq.RepositoryPath, portalContext, odataReq); } else if (odataReq.IsMemberRequest) { formatter.WriteContentProperty(odataReq.RepositoryPath, odataReq.PropertyName, odataReq.IsRawValueRequest, portalContext, odataReq); } else { formatter.WriteSingleContent(odataReq.RepositoryPath, portalContext); } } break; case "PUT": // update if (odataReq.IsMemberRequest) { throw new ODataException("Cannot access a member with HTTP PUT.", ODataExceptionCode.IllegalInvoke); } else { model = Read(inputStream); content = Content.Load(odataReq.RepositoryPath); ResetContent(content); UpdateContent(content, model, odataReq); formatter.WriteSingleContent(content, portalContext); } break; case "MERGE": case "PATCH": // update if (odataReq.IsMemberRequest) { throw new ODataException(String.Concat("Cannot access a member with HTTP ", httpMethod, "."), ODataExceptionCode.IllegalInvoke); } else { model = Read(inputStream); content = Content.Load(odataReq.RepositoryPath); UpdateContent(content, model, odataReq); formatter.WriteSingleContent(content, portalContext); } break; case "POST": // invoke an action, create content if (odataReq.IsMemberRequest) { formatter.WriteOperationResult(inputStream, portalContext, odataReq); } else { model = Read(inputStream); content = CreateContent(model, odataReq); formatter.WriteSingleContent(content, portalContext); } break; case "DELETE": if (odataReq.IsMemberRequest) { throw new ODataException(String.Concat("Cannot access a member with HTTP ", httpMethod, "."), ODataExceptionCode.IllegalInvoke); } else { //model = Read(inputStream); Content.Delete(odataReq.RepositoryPath); } break; } } catch (ContentNotFoundException e) { var oe = new ODataException(ODataExceptionCode.ResourceNotFound, e); formatter.WriteErrorResponse(context, oe); } catch (ODataException e) { if (e.HttpStatusCode == 500) { Logger.WriteException(e); } formatter.WriteErrorResponse(context, e); } catch (SecurityException e) { // In case of a visitor we should not expose the information that this content actually exists. We return // a simple 404 instead to provide exactly the same response as the regular 404, where the content // really does not exist. But do this only if the visitor really does not have permission for the // requested content (because security exception could be thrown by an action or something else too). if (odataReq != null && User.Current.Id == User.Visitor.Id) { var head = NodeHead.Get(odataReq.RepositoryPath); if (head != null && !SecurityHandler.HasPermission(head, PermissionType.Open)) { ContentNotFound(context, odataReq.RepositoryPath); return; } } var oe = new ODataException(ODataExceptionCode.NotSpecified, e); Logger.WriteException(oe); formatter.WriteErrorResponse(context, oe); } catch (InvalidContentActionException ex) { var oe = new ODataException(ODataExceptionCode.NotSpecified, ex); if (ex.Reason != InvalidContentActionReason.NotSpecified) { oe.ErrorCode = Enum.GetName(typeof(InvalidContentActionReason), ex.Reason); } // it is unnecessary to log this exception as this is not a real error //Logger.WriteException(oe); formatter.WriteErrorResponse(context, oe); } catch (Exception ex) { var oe = new ODataException(ODataExceptionCode.NotSpecified, ex); Logger.WriteException(oe); formatter.WriteErrorResponse(context, oe); } finally { context.Response.End(); } }
internal override Dictionary <string, object> Project(Content content) { var fields = new Dictionary <string, object>(); var selfurl = GetSelfUrl(content); if (this.Request.EntityMetadata != MetadataFormat.None) { fields.Add("__metadata", GetMetadata(content, selfurl, this.Request.EntityMetadata)); } IEnumerable <string> fieldNames = null; if (Request.HasSelect) { fieldNames = Request.Select; } else { if (IsCollectionItem) { if (_fieldNamesForPaths.ContainsKey(content.ContentHandler.ParentPath)) { fieldNames = _fieldNamesForPaths[content.ContentHandler.ParentPath]; } else { _fieldNamesForPaths[content.ContentHandler.ParentPath] = fieldNames = content.GetFieldNamesInParentTable(); } if (content.AspectFields != null && content.AspectFields.Count > 0) { fieldNames = fieldNames.Concat(content.AspectFields.Keys); } } else { fieldNames = content.Fields.Keys; } } if (Request.HasSelect) { foreach (var selectItem in Request.Select) { if (selectItem.Contains("/")) { throw new ODataException("Bad item in $select: " + selectItem, ODataExceptionCode.InvalidSelectParameter); } } } if (!Request.HasSelect) { fieldNames = fieldNames.Concat(new[] { ACTIONSPROPERTY, ISFILEPROPERTY }); } foreach (var fieldName in fieldNames) { if (fields.ContainsKey(fieldName)) { continue; } if (ODataHandler.DisabledFieldNames.Contains(fieldName)) { fields.Add(fieldName, null); continue; } if (base.IsAllowedField(content, fieldName)) { Field field; if (content.Fields.TryGetValue(fieldName, out field)) { fields.Add(fieldName, ODataFormatter.GetJsonObject(field, selfurl)); } else if (fieldName == ACTIONSPROPERTY) { fields.Add(ACTIONSPROPERTY, ODataReference.Create(String.Concat(selfurl, "/", ODataHandler.PROPERTY_ACTIONS))); } else if (fieldName == ISFILEPROPERTY) { fields.Add(ISFILEPROPERTY, content.Fields.ContainsKey(ODataHandler.PROPERTY_BINARY)); } else if (fieldName == ICONPROPERTY) { fields.Add(fieldName, content.Icon ?? content.ContentType.Icon); } else { fields.Add(fieldName, null); } } else { fields.Add(fieldName, null); } } return(fields); }
private Dictionary <string, object> Project(Content content, List <Property> expandTree, List <Property> selectTree) { Field field; var outfields = new Dictionary <string, object>(); var selfurl = GetSelfUrl(content); if (this.Request.EntityMetadata != MetadataFormat.None) { outfields.Add("__metadata", GetMetadata(content, selfurl, this.Request.EntityMetadata)); } var hasJoker = false; foreach (var property in selectTree) { var propertyName = property.Name; if (propertyName == "*") { hasJoker = true; continue; } if (!content.Fields.TryGetValue(propertyName, out field)) { switch (propertyName) { case ACTIONSPROPERTY: var actionExpansion = GetPropertyFromList(ACTIONSPROPERTY, expandTree); if (actionExpansion == null) { outfields.Add(ACTIONSPROPERTY, ODataReference.Create(String.Concat(selfurl, "/", ODataHandler.PROPERTY_ACTIONS))); } else { outfields.Add(ACTIONSPROPERTY, GetActions(content)); } break; case ICONPROPERTY: outfields.Add(ICONPROPERTY, content.Icon ?? content.ContentType.Icon); break; case ISFILEPROPERTY: outfields.Add(ISFILEPROPERTY, content.Fields.ContainsKey(ODataHandler.PROPERTY_BINARY)); break; default: outfields.Add(propertyName, null); break; } } else { if (ODataHandler.DisabledFieldNames.Contains(field.Name)) { outfields.Add(propertyName, null); } else { var expansion = GetPropertyFromList(propertyName, expandTree); if (expansion != null) { outfields.Add(propertyName, Project(field, expansion.Children, property.Children ?? Property.JokerList)); } else { if (base.IsAllowedField(content, field.Name)) { outfields.Add(propertyName, ODataFormatter.GetJsonObject(field, selfurl)); } else { outfields.Add(propertyName, null); } } } } } if (hasJoker) { foreach (var contentField in content.Fields.Values) { if (outfields.ContainsKey(contentField.Name)) { continue; } var propertyName = contentField.Name; var expansion = GetPropertyFromList(propertyName, expandTree); if (expansion != null) { outfields.Add(propertyName, Project(contentField, expansion.Children, Property.JokerList)); } else { outfields.Add(propertyName, ODataFormatter.GetJsonObject(contentField, selfurl)); } } } //var actionSelection = GetPropertyFromList(ACTIONSPROPERTY, selectTree); //if (!outfields.ContainsKey(ACTIONSPROPERTY) && actionSelection != null) //{ // var actionExpansion = GetPropertyFromList(ACTIONSPROPERTY, expandTree); // if (actionExpansion == null) // outfields.Add(ACTIONSPROPERTY, JsonDeferred.Create(String.Concat(selfurl, "/", ODataHandler.PROPERTY_ACTIONS))); // else // outfields.Add(ACTIONSPROPERTY, GetActions(content)); //} //if (!outfields.ContainsKey(ICONPROPERTY) && null != GetPropertyFromList(ICONPROPERTY, selectTree)) // outfields.Add(ICONPROPERTY, content.Icon ?? content.ContentType.Icon); //if (null != GetPropertyFromList(ISFILEPROPERTY, selectTree)) // outfields.Add(ISFILEPROPERTY, content.Fields.ContainsKey(ODataHandler.PROPERTY_BINARY)); //if (null != GetPropertyFromList(ISCONTAINERPROPERTY, selectTree)) // outfields.Add(ISCONTAINERPROPERTY, content.ContentHandler is IFolder); return(outfields); }
private Dictionary <string, object> Project(Content content, List <Property> expandTree) { Property expansion; var outfields = new Dictionary <string, object>(); var selfurl = GetSelfUrl(content); if (this.Request.EntityMetadata != MetadataFormat.None) { outfields.Add("__metadata", GetMetadata(content, selfurl, this.Request.EntityMetadata)); } var fields = content.Fields.Values; var expansionEnabled = !content.ContentHandler.IsHeadOnly; foreach (var field in fields) { if (ODataHandler.DisabledFieldNames.Contains(field.Name)) { continue; } var propertyName = field.Name; expansion = expansionEnabled ? GetExpansion(propertyName, expandTree) : null; if (expansion != null) { outfields.Add(propertyName, Project(field, expansion.Children)); } else { if (base.IsAllowedField(content, field.Name)) { outfields.Add(propertyName, ODataFormatter.GetJsonObject(field, selfurl)); } else { outfields.Add(propertyName, null); } } } var actionExpansion = GetExpansion(ACTIONSPROPERTY, expandTree); if (actionExpansion == null) { outfields.Add(ACTIONSPROPERTY, ODataReference.Create(String.Concat(selfurl, "/", ODataHandler.PROPERTY_ACTIONS))); } else { outfields.Add(ACTIONSPROPERTY, GetActions(content)); } if (!outfields.ContainsKey(ICONPROPERTY)) { outfields.Add(ICONPROPERTY, content.Icon ?? content.ContentType.Icon); } outfields.Add(ISFILEPROPERTY, content.Fields.ContainsKey(ODataHandler.PROPERTY_BINARY)); return(outfields); }
public static void WriteMetadata(TextWriter writer, ODataFormatter formatter, Content content, bool isCollection) { var edmx = CreateEdmx(content, isCollection); formatter.WriteMetadataInternal(writer, edmx); }