private Type GetMemberType(Type type) { if (type.IsArray) { return(type.GetElementType()); } if (InheritedGenericInterfacesHelper.ImplementsGenericInterface(typeof(IEnumerable <>), type)) { Type[] interfaces = type.GetInterfaces(); foreach (Type interfac in interfaces) { if (interfac.IsGenericType && interfac.GetGenericTypeDefinition() == typeof(IEnumerable <object>).GetGenericTypeDefinition()) { Type memberType = interfac.GetGenericArguments()[0]; if (memberType.GetCustomAttributes(typeof(OslcResourceShape), false).Length > 0) { return(memberType); } return(null); } } } return(null); }
/// <summary> /// Write a .NET object to an output stream /// </summary> /// <param name="type"></param> /// <param name="value"></param> /// <param name="writeStream"></param> /// <param name="content"></param> /// <param name="transportContext"></param> /// <returns></returns> public override Task WriteToStreamAsync( Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext ) { return(Task.Factory.StartNew(() => { if ((Graph == null) || (Graph.IsEmpty) || RebuildGraph) { if (ImplementsGenericType(typeof(FilteredResource <>), type)) { PropertyInfo resourceProp = value.GetType().GetProperty("Resource"); Type[] actualTypeArguments = GetChildClassParameterArguments(typeof(FilteredResource <>), type); object objects = resourceProp.GetValue(value, null); PropertyInfo propertiesProp = value.GetType().GetProperty("Properties"); if (!ImplementsICollection(actualTypeArguments[0])) { objects = new EnumerableWrapper(objects); } if (ImplementsGenericType(typeof(ResponseInfo <>), type)) { //Subject URI for the collection is the query capability //TODO: should this be set by the app based on service provider info int portNum = httpRequest.RequestUri.Port; string portString = null; if (portNum == 80 || portNum == 443) { portString = ""; } else { portString = ":" + portNum.ToString(); } string descriptionAbout = httpRequest.RequestUri.Scheme + "://" + httpRequest.RequestUri.Host + portString + httpRequest.RequestUri.LocalPath; //Subject URI for the responseInfo is the full request URI string responseInfoAbout = httpRequest.RequestUri.ToString(); PropertyInfo totalCountProp = value.GetType().GetProperty("TotalCount"); PropertyInfo nextPageProp = value.GetType().GetProperty("NextPage"); Graph = DotNetRdfHelper.CreateDotNetRdfGraph(descriptionAbout, responseInfoAbout, (string)nextPageProp.GetValue(value, null), (int)totalCountProp.GetValue(value, null), objects as IEnumerable <object>, (IDictionary <string, object>)propertiesProp.GetValue(value, null)); } else { Graph = DotNetRdfHelper.CreateDotNetRdfGraph(null, null, null, null, objects as IEnumerable <object>, (IDictionary <string, object>)propertiesProp.GetValue(value, null)); } } else if (InheritedGenericInterfacesHelper.ImplementsGenericInterface(typeof(IEnumerable <>), value.GetType())) { Graph = DotNetRdfHelper.CreateDotNetRdfGraph(value as IEnumerable <object>); } else if (type.GetCustomAttributes(typeof(OslcResourceShape), false).Length > 0) { Graph = DotNetRdfHelper.CreateDotNetRdfGraph(new object[] { value }); } else { Graph = DotNetRdfHelper.CreateDotNetRdfGraph(new EnumerableWrapper(value)); } } IRdfWriter rdfWriter; if (content == null || content.Headers == null || content.Headers.ContentType.MediaType.Equals(OslcMediaType.APPLICATION_RDF_XML)) { RdfXmlWriter rdfXmlWriter = new RdfXmlWriter(); rdfXmlWriter.UseDtd = false; rdfXmlWriter.PrettyPrintMode = false; rdfXmlWriter.CompressionLevel = 20; //turtlelWriter.UseTypedNodes = false; rdfWriter = rdfXmlWriter; } else if (content.Headers.ContentType.MediaType.Equals(OslcMediaType.TEXT_TURTLE)) { TurtleWriter turtlelWriter = new TurtleWriter(TurtleSyntax.W3C); turtlelWriter.PrettyPrintMode = false; rdfWriter = turtlelWriter; } else { //For now, use the dotNetRDF RdfXmlWriter for application/xml //OslcXmlWriter oslcXmlWriter = new OslcXmlWriter(); RdfXmlWriter oslcXmlWriter = new RdfXmlWriter(); oslcXmlWriter.UseDtd = false; oslcXmlWriter.PrettyPrintMode = false; oslcXmlWriter.CompressionLevel = 20; rdfWriter = oslcXmlWriter; } StreamWriter streamWriter = new NonClosingStreamWriter(writeStream); rdfWriter.Save(Graph, streamWriter); })); }
/// <summary> /// Write a .NET object to an output stream /// </summary> /// <param name="type"></param> /// <param name="value"></param> /// <param name="writeStream"></param> /// <param name="content"></param> /// <param name="transportContext"></param> /// <returns></returns> public override Task WriteToStreamAsync( Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext ) { return(Task.Factory.StartNew(() => { if ((Json == null) || (Json.Count() == 0) || RebuildJson) { if (ImplementsGenericType(typeof(FilteredResource <>), type)) { PropertyInfo resourceProp = value.GetType().GetProperty("Resource"); Type[] actualTypeArguments = GetChildClassParameterArguments(typeof(FilteredResource <>), type); object objects = resourceProp.GetValue(value, null); PropertyInfo propertiesProp = value.GetType().GetProperty("Properties"); if (!ImplementsICollection(actualTypeArguments[0])) { objects = new EnumerableWrapper(objects); } if (ImplementsGenericType(typeof(ResponseInfo <>), type)) { //Subject URI for the collection is the query capability //TODO: should this be set by the app based on service provider info int portNum = httpRequest.RequestUri.Port; string portString = null; if (portNum == 80 || portNum == 443) { portString = ""; } else { portString = ":" + portNum.ToString(); } string descriptionAbout = httpRequest.RequestUri.Scheme + "://" + httpRequest.RequestUri.Host + portString + httpRequest.RequestUri.LocalPath; //Subject URI for the responseInfo is the full request URI string responseInfoAbout = httpRequest.RequestUri.ToString(); PropertyInfo totalCountProp = value.GetType().GetProperty("TotalCount"); PropertyInfo nextPageProp = value.GetType().GetProperty("NextPage"); Json = JsonHelper.CreateJson(descriptionAbout, responseInfoAbout, (string)nextPageProp.GetValue(value, null), (int)totalCountProp.GetValue(value, null), objects as IEnumerable <object>, (IDictionary <string, object>)propertiesProp.GetValue(value, null)); } else { Json = JsonHelper.CreateJson(null, null, null, null, objects as IEnumerable <object>, (IDictionary <string, object>)propertiesProp.GetValue(value, null)); } } else if (InheritedGenericInterfacesHelper.ImplementsGenericInterface(typeof(IEnumerable <>), value.GetType())) { Json = JsonHelper.CreateJson(value as IEnumerable <object>); } else if (type.GetCustomAttributes(typeof(OslcResourceShape), false).Length > 0) { Json = JsonHelper.CreateJson(new object[] { value }); } else { Json = JsonHelper.CreateJson(new EnumerableWrapper(value)); } Debug.WriteLine("JsonMediaTypeFormatter.WriteToStreamAsync(): Generated JSON: " + Json); } Json.Save(writeStream); })); }