Exemple #1
0
        public HttpResponseMessage GetJsonXmlTranslation()
        {
            //get content as string so we can inspect and convert it
            HttpPostedFile file       = HttpContext.Current.Request.Files[0];
            StreamReader   reader     = new StreamReader(file.InputStream);
            string         rawContent = reader.ReadToEnd();

            //easy call for conversion!
            IFormattedContent convertedContent = _converter.Convert(rawContent);

            //now build a new response and send back the converted content
            HttpResponseMessage result = Request.CreateResponse(HttpStatusCode.OK);

            result.Content = new StreamContent(getStream(convertedContent.Content));

            //label the response content type appropriately depending on the convertedContent
            switch (convertedContent.FileType)
            {
            case FileType.Json:
                result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                break;

            case FileType.Xml:
                result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/xml");
                break;

            default:     //uh oh, reassign result to error
                result = Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Unknown file type.");
                result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                break;
            }

            return(result);
        }
Exemple #2
0
        public IFormattedContent Convert(string s)
        {
            IFormattedContent content = FormattedContent.CreateFormattedContent(s);
            IFormattedContent convertedContent;

            if (content.FileType == FileType.Json)
            {
                convertedContent = content.ToXmlContent();
            }
            else if (content.FileType == FileType.Xml)
            {
                convertedContent = content.ToJsonContent();
            }
            else
            {
                convertedContent = content; //content is already unknown, so nothing to convert, just use the same object
            }

            return(convertedContent);
        }