Esempio n. 1
0
        /// <summary>
        /// This method prepares the binary body for output from the content.
        /// </summary>
        /// <param name="context">The current context.</param>
        /// <param name="content">The content to be prepared.</param>
        /// <returns>Returns a ResourceManagerMessageFragmentBody object with the binary representation of the content.</returns>
        protected virtual ResourceManagerMessageFragmentBody PrepareBodyOutput(ResourceManagerContext context, BinaryContent content)
        {
            ResourceManagerMessageFragmentBody
                body = context.GetObjectPool<ResourceManagerMessageFragmentBody>().Get();

            byte[] blob = content.ToArray();

            switch (context.Request.Data.RequestPreferredCompression)
            {
                case "gzip":
                    using (MemoryStream ms = new MemoryStream())
                    {
                        using (Stream gz = new GZipStream(ms, CompressionMode.Compress, true))
                        {
                            gz.Write(blob, 0, blob.Length);
                            gz.Close();
                        }
                        ms.Position = 0;
                        body.Load(ms);
                    }
                    body.ContentEncoding = "gzip";
                    break;
                case "deflate":
                    using (MemoryStream ms = new MemoryStream())
                    {
                        using (Stream gz = new DeflateStream(ms, CompressionMode.Compress, true))
                        {
                            gz.Write(blob, 0, blob.Length);
                            gz.Close();
                        }
                        ms.Position = 0;
                        body.Load(ms);
                    }
                    body.ContentEncoding = "deflate";
                    break;
                default:
                    body.Load(blob, 0, blob.Length);
                    break;
            }

            blob = null;

            if (content.MimeType == null || content.MimeType == "")
            {
                if (context.Request.Settings.OutputColl.Count > 0)
                    body.ContentType = context.Request.Settings.OutputColl[0].OutputMIMEType;
                else
                    body.ContentType = "application/octet-stream";
            }
            else
                body.ContentType = content.MimeType;

            return body;
        }
        /// <summary>
        /// This method selects the appropriate node and creates a Body content.
        /// </summary>
        /// <param name="context">The current context.</param>
        public override void OutputPrepare(ResourceManagerContext context)
        {
            BinaryContent content = null;
            try
            {
                Uri resource = new Uri(context.Request.Settings.OutputColl[0].Output);

                GuidHolder holder;
                lock (syncLockCache)
                {
                    byte[] blob;
                    if (!mInternalResourceCache.ContainsKey(resource))
                    {
                        blob = RH.ResourceLoadFromUri(resource);
                        holder = new GuidHolder(Guid.NewGuid(), Guid.NewGuid(), blob);
                        mInternalResourceCache.Add(resource, holder);
                    }
                    else
                        holder = mInternalResourceCache[resource];
                }

                content = context.GetObjectPool<BinaryContent>().Get();
                content.Load();
                content.Data = holder.blob;
                //content.Load(holder.blob, 0, holder.blob.Length);
                content.IDContent = holder.ID;
                content.IDVersion = holder.Version;

                string statusBody;
                context.Response.Body = PrepareBody(context, content, out statusBody);
                context.Response.Status = statusBody;
            }
            catch (Exception ex)
            {
                context.Response.Body = null;
                context.Response.Status = CH.HTTPCodes.InternalServerError_500;
                context.Response.Substatus = ex.Message;
            }
            finally
            {
                if (content != null && content.ObjectPoolCanReturn)
                    content.ObjectPoolReturn();
                content = null;
            }
        }