Exemplo n.º 1
0
        public void CachingStreamReadsEntireBookTest()
        {
            const int bufferSize = 4096;

            using (var sourceStream = TestHelpers.GetResourceStream(ResourceName))
            {
                using (var cachingStream = new CachingStream(sourceStream))
                {
                    using (var notcachingStream = TestHelpers.GetResourceStream(ResourceName))
                    {
                        while (true)
                        {
                            var bufferedTempBuffer    = new byte[bufferSize];
                            var notBufferedTempBuffer = new byte[bufferSize];

                            var bytesReadFromcachingStream     = cachingStream.Read(bufferedTempBuffer, 0, bufferedTempBuffer.Length);
                            var bytesReadFromNiotcachingStream = notcachingStream.Read(notBufferedTempBuffer, 0,
                                                                                       notBufferedTempBuffer.Length);

                            Assert.AreEqual(bytesReadFromcachingStream, bytesReadFromNiotcachingStream);
                            Assert.IsTrue(bufferedTempBuffer.SequenceEqual(notBufferedTempBuffer));

                            if (bytesReadFromcachingStream == 0)
                            {
                                break;
                            }
                        }

                        Assert.AreEqual(cachingStream.Length, notcachingStream.Length);
                        Assert.AreEqual(cachingStream.Position, notcachingStream.Position);
                    }
                }
            }
        }
Exemplo n.º 2
0
 public void CachingStreamDoesNothingOnFlushingTest()
 {
     using (var sourceStream = TestHelpers.GetResourceStream(ResourceName))
     {
         using (var cachingStream = new CachingStream(sourceStream))
         {
             cachingStream.Flush();
         }
     }
 }
Exemplo n.º 3
0
 public void CreateCachingStreamTest()
 {
     using (var stream = TestHelpers.GetResourceStream(ResourceName))
     {
         using (var cachingStream = new CachingStream(stream))
         {
             Assert.IsNotNull(cachingStream);
         }
     }
 }
		public void BufferedStreamFeatures()
		{
			using (var stream = GetResourceStream(ResourceName))
			{
				using (var bufferedStream = new CachingStream(stream))
				{
					Assert.IsNotNull(bufferedStream);
				}
			}
		}
		public void CreateBufferedInputStream()
		{
			using (var stream = GetResourceStream(ResourceName))
			{
				using (var bufferedStream = new CachingStream(stream))
				{
					Assert.IsNotNull(bufferedStream);
				}
			}
		}
        public void PositionOutOfRange(int position)
        {
            var data = GenerateData(1000);

            using (var memoryStream = new MemoryStream(data, writable: false))
                using (var cachingStream = new CachingStream(memoryStream, Ownership.Owns))
                {
                    Assert.Throws <ArgumentOutOfRangeException>(() => cachingStream.Position = position);
                }
        }
        public void Position(int position)
        {
            var data = GenerateData(1000);

            using (var memoryStream = new MemoryStream(data, writable: false))
                using (var cachingStream = new CachingStream(memoryStream, Ownership.Owns))
                {
                    cachingStream.Position = position;
                    Assert.AreEqual(position, cachingStream.Position);
                }
        }
        public void SeekAndReadAfterEnd(int position)
        {
            var data = GenerateData(1000);

            using (var memoryStream = new MemoryStream(data, writable: false))
                using (var cachingStream = new CachingStream(memoryStream, Ownership.Owns))
                {
                    cachingStream.Position = position;
                    byte[] buffer = new byte[10];
                    Assert.AreEqual(0, cachingStream.Read(buffer, 0, buffer.Length));
                }
        }
Exemplo n.º 9
0
        public void CachingStreamSeeksToEndTest()
        {
            using (var stream = TestHelpers.GetResourceStream(ResourceName))
            {
                using (var cachingStream = new CachingStream(stream))
                {
                    cachingStream.Seek(0, SeekOrigin.End);

                    Assert.AreEqual(cachingStream.Length, cachingStream.Position);
                }
            }
        }
Exemplo n.º 10
0
 public void CachingStreamFailsToSeekWithUnknownOriginTest()
 {
     using (var sourceStream = TestHelpers.GetResourceStream(ResourceName))
     {
         using (var cachingStream = new CachingStream(sourceStream))
         {
             Assert.Throws <NotSupportedException>
             (
                 () => { cachingStream.Seek(0, (SeekOrigin)100); }
             );
         }
     }
 }
Exemplo n.º 11
0
 public void CachingStreamFailsToSetLengthTest()
 {
     using (var sourceStream = TestHelpers.GetResourceStream(ResourceName))
     {
         using (var cachingStream = new CachingStream(sourceStream))
         {
             Assert.Throws <NotSupportedException>
             (
                 () => { cachingStream.SetLength(100); }
             );
         }
     }
 }
Exemplo n.º 12
0
        public void CachingStreamSetsPositionTest()
        {
            using (var sourceStream = TestHelpers.GetResourceStream(ResourceName))
            {
                using (var cachingStream = new CachingStream(sourceStream))
                {
                    const int position = 100;

                    cachingStream.Position = position;
                    Assert.AreEqual(position, cachingStream.Position);
                }
            }
        }
Exemplo n.º 13
0
        public void Write()
        {
            var data = GenerateData(1000);

            using (var memoryStream = new MemoryStream(data, writable: true))
                using (var cachingStream = new CachingStream(memoryStream, Ownership.Owns))
                {
                    Assert.IsFalse(cachingStream.CanWrite);
                    Assert.Throws <NotSupportedException>(() => cachingStream.SetLength(2000));
                    Assert.Throws <NotSupportedException>(() => cachingStream.Write(new byte[1], 0, 1));
                    Assert.Throws <NotSupportedException>(() => cachingStream.WriteByte(1));
                    Assert.Throws <NotSupportedException>(() => cachingStream.BeginWrite(new byte[1], 0, 1, null, null));
                }
        }
Exemplo n.º 14
0
		public void SeekToBegin()
		{
			using (var stream = GetResourceStream(ResourceName))
			{
				using (var bufferedStream = new CachingStream(stream))
				{
					bufferedStream.Seek(0, SeekOrigin.Begin);
					Assert.AreEqual(0, bufferedStream.Position);

					bufferedStream.Seek(10, SeekOrigin.Begin);
					Assert.AreEqual(10, bufferedStream.Position);
				}
			}
		}
        public void ReadBlock(int size)
        {
            var data = GenerateData(size);

            using (var memoryStream = new MemoryStream(data, writable: false))
                using (var cachingStream = new CachingStream(memoryStream, Ownership.Owns))
                {
                    var buffer = new byte[size + 1];
                    var read   = cachingStream.ReadBlock(buffer, 0, buffer.Length);
                    Assert.AreEqual(size, read);
                    Assert.AreEqual(data, buffer.Take(size));
                    Assert.AreEqual(size, (int)cachingStream.Position);
                }
        }
Exemplo n.º 16
0
 public void CachingStreamFailsToSeekBeforeBeginTest()
 {
     using (var sourceStream = TestHelpers.GetResourceStream(ResourceName))
     {
         using (var cachingStream = new CachingStream(sourceStream))
         {
             var exception = Assert.Throws <ArgumentOutOfRangeException>
                             (
                 () => { cachingStream.Seek(-1, SeekOrigin.Begin); }
                             );
             Assert.AreEqual(@"offset", exception.ParamName);
         }
     }
 }
Exemplo n.º 17
0
        public void CachingStreamFeaturesTest()
        {
            using (var stream = TestHelpers.GetResourceStream(ResourceName))
            {
                using (var cachingStream = new CachingStream(stream))
                {
                    Assert.IsNotNull(cachingStream);

                    Assert.IsTrue(cachingStream.CanRead);
                    Assert.IsTrue(cachingStream.CanSeek);
                    Assert.IsFalse(cachingStream.CanWrite);
                }
            }
        }
Exemplo n.º 18
0
 public void CachingStreamFailsToSetTooLargePositionTest()
 {
     using (var sourceStream = TestHelpers.GetResourceStream(ResourceName))
     {
         using (var cachingStream = new CachingStream(sourceStream))
         {
             var exception = Assert.Throws <ArgumentOutOfRangeException>
                             (
                 () => { cachingStream.Position = int.MaxValue; }
                             );
             Assert.AreEqual(@"Position", exception.ParamName);
         }
     }
 }
Exemplo n.º 19
0
        public void CachingStreamSeeksToBeginTest()
        {
            using (var stream = TestHelpers.GetResourceStream(ResourceName))
            {
                using (var cachingStream = new CachingStream(stream))
                {
                    cachingStream.Seek(0, SeekOrigin.Begin);
                    Assert.AreEqual(0, cachingStream.Position);

                    cachingStream.Seek(10, SeekOrigin.Begin);
                    Assert.AreEqual(10, cachingStream.Position);
                }
            }
        }
Exemplo n.º 20
0
        public void CachingStreamFailsToWriteTest()
        {
            using (var sourceStream = TestHelpers.GetResourceStream(ResourceName))
            {
                using (var cachingStream = new CachingStream(sourceStream))
                {
                    byte[] buffer = { 0, 1 };

                    Assert.Throws <NotSupportedException>
                    (
                        () => { cachingStream.Write(buffer, 0, buffer.Length); }
                    );
                }
            }
        }
        public void SeekAndReadByte()
        {
            Random random = new Random(2);
            var    data   = GenerateData(123456);

            using (var memoryStream = new MemoryStream(data, writable: false))
                using (var cachingStream = new CachingStream(memoryStream, Ownership.Owns))
                {
                    for (int i = 0; i < 100; i++)
                    {
                        int offset = random.Next(data.Length - 1);
                        cachingStream.Position = offset;
                        Assert.AreEqual(data[offset], cachingStream.ReadByte());
                    }
                }
        }
Exemplo n.º 22
0
 public void ClosedCachingStreamFailsOnReadTest()
 {
     Assert.Throws <ObjectDisposedException>
     (
         () =>
     {
         using (var sourceStream = TestHelpers.GetResourceStream(ResourceName))
         {
             using (var cachingStream = new CachingStream(sourceStream))
             {
                 cachingStream.Close();
                 Assert.AreEqual(0, cachingStream.Position);
             }
         }
     }
     );
 }
Exemplo n.º 23
0
 public void CachingStreamFailsToGetLengthBeforeEofTest()
 {
     using (var sourceStream = TestHelpers.GetResourceStream(ResourceName))
     {
         using (var cachingStream = new CachingStream(sourceStream))
         {
             Assert.Throws <NotSupportedException>
             (
                 // ReSharper disable once UnusedVariable
                 () =>
             {
                 var length = cachingStream.Length;
             }
             );
         }
     }
 }
        public void ReadByte(int size)
        {
            var data = GenerateData(size);

            using (var memoryStream = new MemoryStream(data, writable: false))
                using (var cachingStream = new CachingStream(memoryStream, Ownership.Owns))
                {
                    for (int i = 0; i < size; i++)
                    {
                        Assert.AreEqual(data[i], cachingStream.ReadByte());
                    }
                    Assert.AreEqual(size, (int)cachingStream.Position);

                    Assert.AreEqual(-1, cachingStream.ReadByte());
                    Assert.AreEqual(size, (int)cachingStream.Position);
                }
        }
        public void Dispose()
        {
            var data = GenerateData(1000);

            using (var memoryStream = new MemoryStream(data, writable: false))
                using (var cachingStream = new CachingStream(memoryStream, Ownership.Owns))
                {
                    cachingStream.Dispose();

                    Assert.Throws <ObjectDisposedException>(() => { cachingStream.Flush(); });
                    Assert.Throws <ObjectDisposedException>(() => { var temp = cachingStream.Length; });
                    Assert.Throws <ObjectDisposedException>(() => { var temp = cachingStream.Position; });
                    Assert.Throws <ObjectDisposedException>(() => { cachingStream.Position = 1; });
                    Assert.Throws <ObjectDisposedException>(() => cachingStream.ReadByte());
                    Assert.Throws <ObjectDisposedException>(() => cachingStream.Read(new byte[10], 0, 1));
                    Assert.Throws <ObjectDisposedException>(() => cachingStream.Seek(1, SeekOrigin.Begin));
                }
        }
        public void SeekAndRead()
        {
            Random random = new Random(1);
            var    data   = GenerateData(1234567);

            using (var memoryStream = new MemoryStream(data, writable: false))
                using (var cachingStream = new CachingStream(memoryStream, Ownership.Owns))
                {
                    for (int i = 0; i < 100; i++)
                    {
                        int offset = random.Next(data.Length - 100);
                        int length = random.Next(1, data.Length - offset);

                        Assert.AreEqual(offset, cachingStream.Seek(offset, SeekOrigin.Begin));
                        var read = cachingStream.ReadExactly(length);
                        Assert.AreEqual(data.Skip(offset).Take(length), read);
                    }
                }
        }
Exemplo n.º 27
0
		public void SeekCurrent()
		{
			using (var stream = GetResourceStream(ResourceName))
			{
				using (var bufferedStream = new CachingStream(stream))
				{
					var tempBuffer = new byte[100];
					var bytesRead = bufferedStream.Read(tempBuffer, 0, tempBuffer.Length);
					Assert.AreEqual(tempBuffer.Length, bytesRead);

					var position = bufferedStream.Position;
					Assert.AreEqual(position, bufferedStream.Seek(0, SeekOrigin.Current));

					position = bufferedStream.Position;
					Assert.AreEqual(position + 100, bufferedStream.Seek(100, SeekOrigin.Current));

					position = bufferedStream.Position;
					Assert.AreEqual(position - 55, bufferedStream.Seek(-55, SeekOrigin.Current));
				}
			}
		}
Exemplo n.º 28
0
        public void CachingStreamSeeksCurrentTest()
        {
            using (var stream = TestHelpers.GetResourceStream(ResourceName))
            {
                using (var cachingStream = new CachingStream(stream))
                {
                    var tempBuffer = new byte[100];
                    var bytesRead  = cachingStream.Read(tempBuffer, 0, tempBuffer.Length);
                    Assert.AreEqual(tempBuffer.Length, bytesRead);

                    var position = cachingStream.Position;
                    Assert.AreEqual(position, cachingStream.Seek(0, SeekOrigin.Current));

                    position = cachingStream.Position;
                    Assert.AreEqual(position + 100, cachingStream.Seek(100, SeekOrigin.Current));

                    position = cachingStream.Position;
                    Assert.AreEqual(position - 55, cachingStream.Seek(-55, SeekOrigin.Current));
                }
            }
        }
        public void SeekAndCopyTo()
        {
            Random random = new Random(1);
            var    data   = GenerateData(54321);

            using (var memoryStream = new MemoryStream(data, writable: false))
                using (var cachingStream = new CachingStream(memoryStream, Ownership.Owns))
                {
                    for (int i = 0; i < 100; i++)
                    {
                        int offset = random.Next(data.Length - 100);

                        Assert.AreEqual(offset, cachingStream.Seek(offset, SeekOrigin.Begin));

                        using (var destination = new MemoryStream(data.Length))
                        {
                            cachingStream.CopyTo(destination);
                            Assert.AreEqual(data.Skip(offset), destination.ToArray());
                        }
                    }
                }
        }
Exemplo n.º 30
0
        /// <summary>
        /// Gets the media.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="cacheConnector">The cache connector.</param>
        /// <returns>A memory stream for the media object.</returns>
        /// <remarks>Documented by Dev03, 2008-08-05</remarks>
        /// <remarks>Documented by Dev03, 2009-01-13</remarks>
        public Stream GetMediaStream(int id, IDbMediaConnector cacheConnector)
        {
            CachingStream stream = null;

            using (NpgsqlConnection conn = PostgreSQLConn.CreateConnection(Parent.CurrentUser))
            {
                int noid = 0;
                using (NpgsqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "SELECT data FROM \"MediaContent\" WHERE id=:id;";
                    cmd.Parameters.Add("id", id);
                    noid = Convert.ToInt32(PostgreSQLConn.ExecuteScalar(cmd, Parent.CurrentUser));
                }

                NpgsqlTransaction  tran        = conn.BeginTransaction();
                LargeObjectManager lbm         = new LargeObjectManager(conn);
                LargeObject        largeObject = lbm.Open(noid, LargeObjectManager.READWRITE);
                byte[]             buffer      = LargeObjectToBuffer(largeObject);
                stream = new CachingStream(buffer, id, cacheConnector);
                largeObject.Close();
                tran.Commit();
            }
            return(stream);
        }
Exemplo n.º 31
0
        /// <summary>
        /// Gets the media.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="cacheConnector">The cache connector.</param>
        /// <returns>A memory stream for the media object.</returns>
        /// <remarks>Documented by Dev03, 2008-08-05</remarks>
        /// <remarks>Documented by Dev03, 2009-01-13</remarks>
        public Stream GetMediaStream(int id, IDbMediaConnector cacheConnector)
        {
            CachingStream stream = null;
            using (NpgsqlConnection conn = PostgreSQLConn.CreateConnection(Parent.CurrentUser))
            {
                int noid = 0;
                using (NpgsqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "SELECT data FROM \"MediaContent\" WHERE id=:id;";
                    cmd.Parameters.Add("id", id);
                    noid = Convert.ToInt32(PostgreSQLConn.ExecuteScalar(cmd, Parent.CurrentUser));
                }

                NpgsqlTransaction tran = conn.BeginTransaction();
                LargeObjectManager lbm = new LargeObjectManager(conn);
                LargeObject largeObject = lbm.Open(noid, LargeObjectManager.READWRITE);
                byte[] buffer = LargeObjectToBuffer(largeObject);
                stream = new CachingStream(buffer, id, cacheConnector);
                largeObject.Close();
                tran.Commit();
            }
            return stream;
        }
Exemplo n.º 32
0
		public void SeekToEnd()
		{
			using (var stream = GetResourceStream(ResourceName))
			{
				using (var bufferedStream = new CachingStream(stream))
				{
					bufferedStream.Seek(0, SeekOrigin.End);

					Assert.AreEqual(bufferedStream.Length, bufferedStream.Position);
				}
			}
		}
Exemplo n.º 33
0
		public void ReadEntireBook()
		{
			const int bufferSize = 4096;

			using (var sourceStream = GetResourceStream(ResourceName))
			{
				using (var bufferedStream = new CachingStream(sourceStream))
				{
					using (var notBufferedStream = GetResourceStream(ResourceName))
					{
						while (true)
						{
							var bufferedTempBuffer = new byte[bufferSize];
							var notBufferedTempBuffer = new byte[bufferSize];

							var bytesReadFromBufferedStream = bufferedStream.Read(bufferedTempBuffer, 0, bufferedTempBuffer.Length);
							var bytesReadFromNiotBufferedStream = notBufferedStream.Read(notBufferedTempBuffer, 0,
								notBufferedTempBuffer.Length);

							Assert.AreEqual(bytesReadFromBufferedStream, bytesReadFromNiotBufferedStream);
							Assert.IsTrue(bufferedTempBuffer.SequenceEqual(notBufferedTempBuffer));

							if (bytesReadFromBufferedStream == 0)
							{
								break;
							}
						}

						Assert.AreEqual(bufferedStream.Length, notBufferedStream.Length);
						Assert.AreEqual(bufferedStream.Position, notBufferedStream.Position);
					}
				}
			}
		}
Exemplo n.º 34
0
        /// <summary>
        /// To be overwritten by the corresponding subclass.
        /// </summary>
        /// <param name="rq">The rq.</param>
        /// <param name="rp">The rp.</param>
        /// <remarks>Documented by Dev10, 2008-08-07</remarks>
        /// <remarks>Documented by Dev05, 2009-01-16</remarks>
        public override void OnResponse(ref HTTPRequestStruct rq, ref HTTPResponseStruct rp)
        {
            Stream responseStream;
            int cardId;
            string ContentTypeString;
            Side side;
            Guid extensionGuid;
            if (IsContent(rq.URL, out cardId, out side, out ContentTypeString))
            {
                string content = (side == Side.Question) ? DequeueQuestion(cardId) : DequeueAnswer(cardId);
                responseStream = new MemoryStream(Encoding.Unicode.GetBytes(content));

                rp.mediaStream = responseStream;
                rp.Headers["Content-Type"] = ContentTypeString + "; charset=UTF-16";
                rp.Headers["Content-Length"] = responseStream.Length;
            }
            else if (IsExtension(rq.URL, out extensionGuid))
            {
                if (extensionconnector.IsStreamAvailable(extensionGuid))
                    responseStream = extensionconnector.GetExtensionStream(extensionGuid);
                else if (parent.CurrentUser.ConnectionString.SyncType == SyncType.HalfSynchronizedWithDbAccess)
                    responseStream = direct_extensionconnector.GetExtensionStream(extensionGuid);
                else if (parent.CurrentUser.ConnectionString.SyncType == SyncType.HalfSynchronizedWithoutDbAccess || parent.CurrentUser.ConnectionString.SyncType == SyncType.FullSynchronized)
                {
                    lock (webClient)
                    {
                        try
                        {
                            responseStream = new CachingStream(webClient.DownloadData(new Uri(string.Format(parent.CurrentUser.ConnectionString.ExtensionURI, extensionGuid))), extensionGuid, extensionconnector);
                        }
                        catch (WebException exp)
                        {
                            if ((exp.Response as HttpWebResponse).StatusCode != HttpStatusCode.Forbidden)
                                throw exp;

                            string result = webClient.DownloadString(new Uri(string.Format(parent.CurrentUser.ConnectionString.ExtensionURI + "&user={1}&password={2}", -1,
                                parent.CurrentUser.ConnectionString.ServerUser.UserName, parent.CurrentUser.ConnectionString.ServerUser.Password)));

                            if (result != "TRUE")
                                throw new NoValidUserException();

                            responseStream = new CachingStream(webClient.DownloadData(new Uri(string.Format(parent.CurrentUser.ConnectionString.ExtensionURI, extensionGuid))), extensionGuid, extensionconnector);
                        }
                    }
                }
                else
                    throw new ArgumentException();

                rp.mediaStream = responseStream;
                rp.Headers["Content-Type"] = "application/bin";
                rp.Headers["Content-Length"] = responseStream.Length;
            }
            else
            {
                int mediaId = GetMediaID(rq.URL);

                if (connector.IsMediaAvailable(mediaId))
                    responseStream = connector.GetMediaStream(mediaId, null);
                else if (parent.CurrentUser.ConnectionString.SyncType == SyncType.HalfSynchronizedWithDbAccess)
                    responseStream = direct_connector.GetMediaStream(mediaId, connector);
                else if (parent.CurrentUser.ConnectionString.SyncType == SyncType.HalfSynchronizedWithoutDbAccess || parent.CurrentUser.ConnectionString.SyncType == SyncType.FullSynchronized)
                {
                    lock (webClient)
                    {
                        try
                        {
                            responseStream = new CachingStream(webClient.DownloadData(new Uri(string.Format(parent.CurrentUser.ConnectionString.LearningModuleFolder, mediaId))), mediaId, connector);
                        }
                        catch (WebException exp)
                        {
                            if ((exp.Response as HttpWebResponse).StatusCode != HttpStatusCode.Forbidden)
                                throw exp;

                            string result = webClient.DownloadString(new Uri(string.Format(parent.CurrentUser.ConnectionString.LearningModuleFolder + "&user={1}&password={2}", -1,
                                parent.CurrentUser.ConnectionString.ServerUser.UserName, parent.CurrentUser.ConnectionString.ServerUser.Password)));

                            if (result != "TRUE")
                                throw new NoValidUserException();

                            responseStream = new CachingStream(webClient.DownloadData(new Uri(string.Format(parent.CurrentUser.ConnectionString.LearningModuleFolder, mediaId))), mediaId, connector);
                        }
                    }
                }
                else
                    throw new ArgumentException();

                rp.mediaStream = responseStream;

                ContentTypeString = connector.GetPropertyValue(mediaId, MediaProperty.MimeType);
                if (ContentTypeString == null)
                {
                    Trace.WriteLine("MimeType delivered null, obviously the corresponding DB entry is missing...");
                    ContentTypeString = Helper.UnknownMimeType;
                }
                rp.Headers["Content-Type"] = ContentTypeString;
                rp.Headers["Content-Length"] = responseStream.Length;
            }
        }
Exemplo n.º 35
0
        /// <summary>
        /// To be overwritten by the corresponding subclass.
        /// </summary>
        /// <param name="rq">The rq.</param>
        /// <param name="rp">The rp.</param>
        /// <remarks>Documented by Dev10, 2008-08-07</remarks>
        /// <remarks>Documented by Dev05, 2009-01-16</remarks>
        public override void OnResponse(ref HTTPRequestStruct rq, ref HTTPResponseStruct rp)
        {
            Stream responseStream;
            int    cardId;
            string ContentTypeString;
            Side   side;
            Guid   extensionGuid;

            if (IsContent(rq.URL, out cardId, out side, out ContentTypeString))
            {
                string content = (side == Side.Question) ? DequeueQuestion(cardId) : DequeueAnswer(cardId);
                responseStream = new MemoryStream(Encoding.Unicode.GetBytes(content));

                rp.mediaStream               = responseStream;
                rp.Headers["Content-Type"]   = ContentTypeString + "; charset=UTF-16";
                rp.Headers["Content-Length"] = responseStream.Length;
            }
            else if (IsExtension(rq.URL, out extensionGuid))
            {
                if (extensionconnector.IsStreamAvailable(extensionGuid))
                {
                    responseStream = extensionconnector.GetExtensionStream(extensionGuid);
                }
                else if (parent.CurrentUser.ConnectionString.SyncType == SyncType.HalfSynchronizedWithDbAccess)
                {
                    responseStream = direct_extensionconnector.GetExtensionStream(extensionGuid);
                }
                else if (parent.CurrentUser.ConnectionString.SyncType == SyncType.HalfSynchronizedWithoutDbAccess || parent.CurrentUser.ConnectionString.SyncType == SyncType.FullSynchronized)
                {
                    lock (webClient)
                    {
                        try
                        {
                            responseStream = new CachingStream(webClient.DownloadData(new Uri(string.Format(parent.CurrentUser.ConnectionString.ExtensionURI, extensionGuid))), extensionGuid, extensionconnector);
                        }
                        catch (WebException exp)
                        {
                            if ((exp.Response as HttpWebResponse).StatusCode != HttpStatusCode.Forbidden)
                            {
                                throw exp;
                            }

                            string result = webClient.DownloadString(new Uri(string.Format(parent.CurrentUser.ConnectionString.ExtensionURI + "&user={1}&password={2}", -1,
                                                                                           parent.CurrentUser.ConnectionString.ServerUser.UserName, parent.CurrentUser.ConnectionString.ServerUser.Password)));

                            if (result != "TRUE")
                            {
                                throw new NoValidUserException();
                            }

                            responseStream = new CachingStream(webClient.DownloadData(new Uri(string.Format(parent.CurrentUser.ConnectionString.ExtensionURI, extensionGuid))), extensionGuid, extensionconnector);
                        }
                    }
                }
                else
                {
                    throw new ArgumentException();
                }

                rp.mediaStream               = responseStream;
                rp.Headers["Content-Type"]   = "application/bin";
                rp.Headers["Content-Length"] = responseStream.Length;
            }
            else
            {
                int mediaId = GetMediaID(rq.URL);

                if (connector.IsMediaAvailable(mediaId))
                {
                    responseStream = connector.GetMediaStream(mediaId, null);
                }
                else if (parent.CurrentUser.ConnectionString.SyncType == SyncType.HalfSynchronizedWithDbAccess)
                {
                    responseStream = direct_connector.GetMediaStream(mediaId, connector);
                }
                else if (parent.CurrentUser.ConnectionString.SyncType == SyncType.HalfSynchronizedWithoutDbAccess || parent.CurrentUser.ConnectionString.SyncType == SyncType.FullSynchronized)
                {
                    lock (webClient)
                    {
                        try
                        {
                            responseStream = new CachingStream(webClient.DownloadData(new Uri(string.Format(parent.CurrentUser.ConnectionString.LearningModuleFolder, mediaId))), mediaId, connector);
                        }
                        catch (WebException exp)
                        {
                            if ((exp.Response as HttpWebResponse).StatusCode != HttpStatusCode.Forbidden)
                            {
                                throw exp;
                            }

                            string result = webClient.DownloadString(new Uri(string.Format(parent.CurrentUser.ConnectionString.LearningModuleFolder + "&user={1}&password={2}", -1,
                                                                                           parent.CurrentUser.ConnectionString.ServerUser.UserName, parent.CurrentUser.ConnectionString.ServerUser.Password)));

                            if (result != "TRUE")
                            {
                                throw new NoValidUserException();
                            }

                            responseStream = new CachingStream(webClient.DownloadData(new Uri(string.Format(parent.CurrentUser.ConnectionString.LearningModuleFolder, mediaId))), mediaId, connector);
                        }
                    }
                }
                else
                {
                    throw new ArgumentException();
                }

                rp.mediaStream = responseStream;

                ContentTypeString = connector.GetPropertyValue(mediaId, MediaProperty.MimeType);
                if (ContentTypeString == null)
                {
                    Trace.WriteLine("MimeType delivered null, obviously the corresponding DB entry is missing...");
                    ContentTypeString = Helper.UnknownMimeType;
                }
                rp.Headers["Content-Type"]   = ContentTypeString;
                rp.Headers["Content-Length"] = responseStream.Length;
            }
        }