private RestReturnType CheckReturnType(HttpResponseMessage response) { if (null == response) { throw new ArgumentNullException("response"); } RestReturnType rt = RestReturnType.Detect; string contentType = ContentHelper.GetContentType(response); if (string.IsNullOrEmpty(contentType)) { rt = RestReturnType.Detect; } else if (ContentHelper.IsJson(contentType)) { rt = RestReturnType.Json; } else if (ContentHelper.IsXml(contentType)) { rt = RestReturnType.Xml; } return(rt); }
private Object ConvertOutput(string str, RestReturnType returnType) { if (RawOutput) { return(str); //nothing to do. } Exception ex = null;; object obj = null; bool convertSuccess = false; // determine the response type // On CoreCLR, we need to explicitly load Json.NET JsonObject.ImportJsonDotNetModule(this); if (returnType == RestReturnType.Json) { convertSuccess = TryConvertToJson(str, out obj, ref ex) || TryConvertToXml(str, out obj, ref ex); } // default to try xml first since it's more common else { convertSuccess = TryConvertToXml(str, out obj, ref ex) || TryConvertToJson(str, out obj, ref ex); } if (!convertSuccess) { // fallback to string return(str); } return(obj); }
/// <summary> /// Process the web response and output corresponding objects. /// </summary> /// <param name="response"></param> internal override void ProcessResponse(HttpResponseMessage response) { if (null == response) { throw new ArgumentNullException("response"); } using (BufferingStreamReader responseStream = new BufferingStreamReader(StreamHelper.GetResponseStream(response))) { if (ShouldWriteToPipeline) { // First see if it is an RSS / ATOM feed, in which case we can // stream it - unless the user has overridden it with a return type of "XML" if (TryProcessFeedStream(responseStream)) { // Do nothing, content has been processed. } else { // get the response encoding Encoding encoding = ContentHelper.GetEncoding(response); string str = StreamHelper.DecodeStream(responseStream, encoding); RestReturnType returnType = CheckReturnType(response); Object obj = ConvertOutput(str, returnType); WriteObject(obj); } } if (ShouldSaveToOutFile) { StreamHelper.SaveStreamToFile(responseStream, QualifiedOutFile, this); } } }
private Object ConvertOutput(string str, RestReturnType returnType) { if (RawOutput) { return(str); //nothing to do. } Exception ex = null;; object obj = null; bool convertSuccess = false; // determine the response type if (returnType == RestReturnType.Json) { convertSuccess = TryConvertToJson(str, out obj, ref ex) || TryConvertToXml(str, out obj, ref ex); } // default to try xml first since it's more common else { convertSuccess = TryConvertToXml(str, out obj, ref ex) || TryConvertToJson(str, out obj, ref ex); } if (!convertSuccess) { // fallback to string return(str); } return(obj); }
/// <summary> /// Process the web reponse and output corresponding objects. /// </summary> /// <param name="response"></param> internal override void ProcessResponse(HttpResponseMessage response) { if (null == response) { throw new ArgumentNullException("response"); } using (BufferingStreamReader responseStream = new BufferingStreamReader(StreamHelper.GetResponseStream(response))) { if (ShouldWriteToPipeline) { // First see if it is an RSS / ATOM feed, in which case we can // stream it - unless the user has overriden it with a return type of "XML" if (TryProcessFeedStream(responseStream)) { // Do nothing, content has been processed. } else { // determine the response type RestReturnType returnType = CheckReturnType(response); // get the response encoding Encoding encoding = ContentHelper.GetEncoding(response); object obj = null; Exception ex = null; string str = StreamHelper.DecodeStream(responseStream, encoding); bool convertSuccess = false; // On CoreCLR, we need to explicity load Json.NET JsonObject.ImportJsonDotNetModule(this); if (returnType == RestReturnType.Json) { convertSuccess = TryConvertToJson(str, out obj, ref ex) || TryConvertToXml(str, out obj, ref ex); } // default to try xml first since it's more common else { convertSuccess = TryConvertToXml(str, out obj, ref ex) || TryConvertToJson(str, out obj, ref ex); } if (!convertSuccess) { // fallback to string obj = str; } WriteObject(obj); } } if (ShouldSaveToOutFile) { StreamHelper.SaveStreamToFile(responseStream, QualifiedOutFile, this); } } }
internal override void ProcessResponse(WebResponse response) { if (response == null) { throw new ArgumentNullException("response"); } MemoryStream stream = new MemoryStream(); using (BufferingStreamReader reader = new BufferingStreamReader(StreamHelper.GetResponseStream(response))) { if (base.ShouldWriteToPipeline && !this.TryProcessFeedStream(reader)) { RestReturnType type = this.CheckReturnType(response); stream = StreamHelper.ReadStream(reader, response.ContentLength, this); Encoding encoding = ContentHelper.GetEncoding(response); object obj2 = null; Exception exRef = null; string json = StreamHelper.DecodeStream(stream, encoding); bool flag = false; if (type == RestReturnType.Json) { flag = this.TryConvertToJson(json, out obj2, ref exRef) || this.TryConvertToXml(json, out obj2, ref exRef); } else { flag = this.TryConvertToXml(json, out obj2, ref exRef) || this.TryConvertToJson(json, out obj2, ref exRef); } if (!flag) { obj2 = json; } base.WriteObject(obj2); } if (base.ShouldSaveToOutFile) { StreamHelper.SaveStreamToFile(StreamHelper.ReadStream(reader, response.ContentLength, this), base.QualifiedOutFile, this); } } }
private RestReturnType CheckReturnType(WebResponse response) { if (response == null) { throw new ArgumentNullException("response"); } RestReturnType detect = RestReturnType.Detect; string contentType = ContentHelper.GetContentType(response); if (string.IsNullOrEmpty(contentType)) { return(RestReturnType.Detect); } if (ContentHelper.IsJson(contentType)) { return(RestReturnType.Json); } if (ContentHelper.IsXml(contentType)) { detect = RestReturnType.Xml; } return(detect); }
/// <summary> /// Process the web response and output corresponding objects. /// </summary> /// <param name="response"></param> internal override void ProcessResponse(HttpResponseMessage response) { if (null == response) { throw new ArgumentNullException("response"); } using (BufferingStreamReader responseStream = new BufferingStreamReader(StreamHelper.GetResponseStream(response))) { if (ShouldWriteToPipeline) { // First see if it is an RSS / ATOM feed, in which case we can // stream it - unless the user has overridden it with a return type of "XML" if (TryProcessFeedStream(responseStream)) { // Do nothing, content has been processed. } else { // determine the response type RestReturnType returnType = CheckReturnType(response); // Try to get the response encoding from the ContentType header. Encoding encoding = null; string charSet = response.Content.Headers.ContentType?.CharSet; if (!string.IsNullOrEmpty(charSet)) { // NOTE: Don't use ContentHelper.GetEncoding; it returns a // default which bypasses checking for a meta charset value. StreamHelper.TryGetEncoding(charSet, out encoding); } object obj = null; Exception ex = null; string str = StreamHelper.DecodeStream(responseStream, ref encoding); // NOTE: Tests use this verbose output to verify the encoding. WriteVerbose(string.Format ( System.Globalization.CultureInfo.InvariantCulture, "Content encoding: {0}", string.IsNullOrEmpty(encoding.HeaderName) ? encoding.EncodingName : encoding.HeaderName) ); bool convertSuccess = false; // On CoreCLR, we need to explicitly load Json.NET JsonObject.ImportJsonDotNetModule(this); if (returnType == RestReturnType.Json) { convertSuccess = TryConvertToJson(str, out obj, ref ex) || TryConvertToXml(str, out obj, ref ex); } // default to try xml first since it's more common else { convertSuccess = TryConvertToXml(str, out obj, ref ex) || TryConvertToJson(str, out obj, ref ex); } if (!convertSuccess) { // fallback to string obj = str; } WriteObject(obj); } } if (ShouldSaveToOutFile) { StreamHelper.SaveStreamToFile(responseStream, QualifiedOutFile, this); } } }