Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / wpf / src / Core / CSharp / MS / Internal / AppModel / SiteOfOriginPart.cs / 1305600 / SiteOfOriginPart.cs
//------------------------------------------------------------------------------
//
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//
// Description:
// SiteOfOriginPart is an implementation of the abstract PackagePart class. It contains an override for GetStreamCore.
//
// History:
// 6/16/2005: [....] - Initial creation.
//
//-----------------------------------------------------------------------------
using System;
using System.Net;
using System.IO.Packaging;
using System.Windows;
using System.Windows.Navigation;
using System.Windows.Resources;
using System.IO;
using System.Resources;
using System.Globalization;
using MS.Internal.PresentationCore;
namespace MS.Internal.AppModel
{
///
/// SiteOfOriginPart is an implementation of the abstract PackagePart class. It contains an override for GetStreamCore.
///
internal class SiteOfOriginPart : System.IO.Packaging.PackagePart
{
//-----------------------------------------------------
//
// Public Constructors
//
//-----------------------------------------------------
#region Public Constructors
internal SiteOfOriginPart(Package container, Uri uri) :
base(container, uri)
{
}
#endregion
//------------------------------------------------------
//
// Protected Methods
//
//-----------------------------------------------------
#region Protected Methods
protected override Stream GetStreamCore(FileMode mode, FileAccess access)
{
#if DEBUG
if (SiteOfOriginContainer._traceSwitch.Enabled)
System.Diagnostics.Trace.TraceInformation(
DateTime.Now.ToLongTimeString() + " " + DateTime.Now.Millisecond + " " +
System.Threading.Thread.CurrentThread.ManagedThreadId +
": SiteOfOriginPart: Getting stream.");
#endif
return GetStreamAndSetContentType(false);
}
protected override string GetContentTypeCore()
{
#if DEBUG
if (SiteOfOriginContainer._traceSwitch.Enabled)
System.Diagnostics.Trace.TraceInformation(
DateTime.Now.ToLongTimeString() + " " + DateTime.Now.Millisecond + " " +
System.Threading.Thread.CurrentThread.ManagedThreadId +
": SiteOfOriginPart: Getting content type.");
#endif
GetStreamAndSetContentType(true);
return _contentType.ToString();
}
#endregion
//------------------------------------------------------
//
// Private Methods
//
//------------------------------------------------------
#region Private Methods
private Stream GetStreamAndSetContentType(bool onlyNeedContentType)
{
lock (_globalLock)
{
if (onlyNeedContentType && _contentType != MS.Internal.ContentType.Empty)
{
#if DEBUG
if (SiteOfOriginContainer._traceSwitch.Enabled)
System.Diagnostics.Trace.TraceInformation(
DateTime.Now.ToLongTimeString() + " " + DateTime.Now.Millisecond + " " +
System.Threading.Thread.CurrentThread.ManagedThreadId +
": SiteOfOriginPart: Getting content type and using previously determined value");
#endif
return null;
}
// If GetContentTypeCore is called before GetStream()
// then we need to retrieve the stream to get the mime type.
// That stream is then stored as _cacheStream and returned
// the next time GetStreamCore() is called.
if (_cacheStream != null)
{
#if DEBUG
if (SiteOfOriginContainer._traceSwitch.Enabled)
System.Diagnostics.Trace.TraceInformation(
DateTime.Now.ToLongTimeString() + " " + DateTime.Now.Millisecond + " " +
System.Threading.Thread.CurrentThread.ManagedThreadId +
"SiteOfOriginPart: Using Cached stream");
#endif
Stream temp = _cacheStream;
_cacheStream = null;
return temp;
}
if (_absoluteLocation == null)
{
#if DEBUG
if (SiteOfOriginContainer._traceSwitch.Enabled)
System.Diagnostics.Trace.TraceInformation(
DateTime.Now.ToLongTimeString() + " " + DateTime.Now.Millisecond + " " +
System.Threading.Thread.CurrentThread.ManagedThreadId +
": SiteOfOriginPart: Determining absolute uri for this resource");
#endif
string original = Uri.ToString();
Invariant.Assert(original[0] == '/');
string uriMinusInitialSlash = original.Substring(1); // trim leading '/'
_absoluteLocation = new Uri(SiteOfOriginContainer.SiteOfOrigin, uriMinusInitialSlash);
}
#if DEBUG
if (SiteOfOriginContainer._traceSwitch.Enabled)
System.Diagnostics.Trace.TraceInformation(
DateTime.Now.ToLongTimeString() + " " + DateTime.Now.Millisecond + " " +
System.Threading.Thread.CurrentThread.ManagedThreadId +
": SiteOfOriginPart: Making web request to " + _absoluteLocation);
#endif
// For performance reasons it is better to open local files directly
// rather than make a FileWebRequest.
Stream responseStream;
if (SecurityHelper.AreStringTypesEqual(_absoluteLocation.Scheme, Uri.UriSchemeFile))
{
responseStream = HandleFileSource(onlyNeedContentType);
}
else
{
responseStream = HandleWebSource(onlyNeedContentType);
}
return responseStream;
}
}
private Stream HandleFileSource(bool onlyNeedContentType)
{
#if DEBUG
if (SiteOfOriginContainer._traceSwitch.Enabled)
System.Diagnostics.Trace.TraceInformation(
DateTime.Now.ToLongTimeString() + " " + DateTime.Now.Millisecond + " " +
System.Threading.Thread.CurrentThread.ManagedThreadId +
": Opening local file " + _absoluteLocation);
#endif
if (_contentType == MS.Internal.ContentType.Empty)
{
_contentType = MS.Internal.MimeTypeMapper.GetMimeTypeFromUri(Uri);
}
if (!onlyNeedContentType)
{
return File.OpenRead(_absoluteLocation.LocalPath);
}
return null;
}
private Stream HandleWebSource(bool onlyNeedContentType)
{
WebResponse response = WpfWebRequestHelper.CreateRequestAndGetResponse(_absoluteLocation);
Stream responseStream = response.GetResponseStream();
#if DEBUG
if (SiteOfOriginContainer._traceSwitch.Enabled)
System.Diagnostics.Trace.TraceInformation(
DateTime.Now.ToLongTimeString() + " " + DateTime.Now.Millisecond + " " +
System.Threading.Thread.CurrentThread.ManagedThreadId +
": Successfully retrieved stream from " + _absoluteLocation);
#endif
if (_contentType == MS.Internal.ContentType.Empty)
{
#if DEBUG
if (SiteOfOriginContainer._traceSwitch.Enabled)
System.Diagnostics.Trace.TraceInformation(
DateTime.Now.ToLongTimeString() + " " + DateTime.Now.Millisecond + " " +
System.Threading.Thread.CurrentThread.ManagedThreadId +
": SiteOfOriginPart: Setting _contentType");
#endif
_contentType = WpfWebRequestHelper.GetContentType(response);
}
if (onlyNeedContentType)
{
_cacheStream = responseStream;
}
return responseStream;
}
#endregion
//-----------------------------------------------------
//
// Private Fields
//
//------------------------------------------------------
#region Private Members
Uri _absoluteLocation = null;
ContentType _contentType = MS.Internal.ContentType.Empty;
Stream _cacheStream = null;
private Object _globalLock = new Object();
#endregion Private Members
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------------------------
//
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//
// Description:
// SiteOfOriginPart is an implementation of the abstract PackagePart class. It contains an override for GetStreamCore.
//
// History:
// 6/16/2005: [....] - Initial creation.
//
//-----------------------------------------------------------------------------
using System;
using System.Net;
using System.IO.Packaging;
using System.Windows;
using System.Windows.Navigation;
using System.Windows.Resources;
using System.IO;
using System.Resources;
using System.Globalization;
using MS.Internal.PresentationCore;
namespace MS.Internal.AppModel
{
///
/// SiteOfOriginPart is an implementation of the abstract PackagePart class. It contains an override for GetStreamCore.
///
internal class SiteOfOriginPart : System.IO.Packaging.PackagePart
{
//-----------------------------------------------------
//
// Public Constructors
//
//-----------------------------------------------------
#region Public Constructors
internal SiteOfOriginPart(Package container, Uri uri) :
base(container, uri)
{
}
#endregion
//------------------------------------------------------
//
// Protected Methods
//
//-----------------------------------------------------
#region Protected Methods
protected override Stream GetStreamCore(FileMode mode, FileAccess access)
{
#if DEBUG
if (SiteOfOriginContainer._traceSwitch.Enabled)
System.Diagnostics.Trace.TraceInformation(
DateTime.Now.ToLongTimeString() + " " + DateTime.Now.Millisecond + " " +
System.Threading.Thread.CurrentThread.ManagedThreadId +
": SiteOfOriginPart: Getting stream.");
#endif
return GetStreamAndSetContentType(false);
}
protected override string GetContentTypeCore()
{
#if DEBUG
if (SiteOfOriginContainer._traceSwitch.Enabled)
System.Diagnostics.Trace.TraceInformation(
DateTime.Now.ToLongTimeString() + " " + DateTime.Now.Millisecond + " " +
System.Threading.Thread.CurrentThread.ManagedThreadId +
": SiteOfOriginPart: Getting content type.");
#endif
GetStreamAndSetContentType(true);
return _contentType.ToString();
}
#endregion
//------------------------------------------------------
//
// Private Methods
//
//------------------------------------------------------
#region Private Methods
private Stream GetStreamAndSetContentType(bool onlyNeedContentType)
{
lock (_globalLock)
{
if (onlyNeedContentType && _contentType != MS.Internal.ContentType.Empty)
{
#if DEBUG
if (SiteOfOriginContainer._traceSwitch.Enabled)
System.Diagnostics.Trace.TraceInformation(
DateTime.Now.ToLongTimeString() + " " + DateTime.Now.Millisecond + " " +
System.Threading.Thread.CurrentThread.ManagedThreadId +
": SiteOfOriginPart: Getting content type and using previously determined value");
#endif
return null;
}
// If GetContentTypeCore is called before GetStream()
// then we need to retrieve the stream to get the mime type.
// That stream is then stored as _cacheStream and returned
// the next time GetStreamCore() is called.
if (_cacheStream != null)
{
#if DEBUG
if (SiteOfOriginContainer._traceSwitch.Enabled)
System.Diagnostics.Trace.TraceInformation(
DateTime.Now.ToLongTimeString() + " " + DateTime.Now.Millisecond + " " +
System.Threading.Thread.CurrentThread.ManagedThreadId +
"SiteOfOriginPart: Using Cached stream");
#endif
Stream temp = _cacheStream;
_cacheStream = null;
return temp;
}
if (_absoluteLocation == null)
{
#if DEBUG
if (SiteOfOriginContainer._traceSwitch.Enabled)
System.Diagnostics.Trace.TraceInformation(
DateTime.Now.ToLongTimeString() + " " + DateTime.Now.Millisecond + " " +
System.Threading.Thread.CurrentThread.ManagedThreadId +
": SiteOfOriginPart: Determining absolute uri for this resource");
#endif
string original = Uri.ToString();
Invariant.Assert(original[0] == '/');
string uriMinusInitialSlash = original.Substring(1); // trim leading '/'
_absoluteLocation = new Uri(SiteOfOriginContainer.SiteOfOrigin, uriMinusInitialSlash);
}
#if DEBUG
if (SiteOfOriginContainer._traceSwitch.Enabled)
System.Diagnostics.Trace.TraceInformation(
DateTime.Now.ToLongTimeString() + " " + DateTime.Now.Millisecond + " " +
System.Threading.Thread.CurrentThread.ManagedThreadId +
": SiteOfOriginPart: Making web request to " + _absoluteLocation);
#endif
// For performance reasons it is better to open local files directly
// rather than make a FileWebRequest.
Stream responseStream;
if (SecurityHelper.AreStringTypesEqual(_absoluteLocation.Scheme, Uri.UriSchemeFile))
{
responseStream = HandleFileSource(onlyNeedContentType);
}
else
{
responseStream = HandleWebSource(onlyNeedContentType);
}
return responseStream;
}
}
private Stream HandleFileSource(bool onlyNeedContentType)
{
#if DEBUG
if (SiteOfOriginContainer._traceSwitch.Enabled)
System.Diagnostics.Trace.TraceInformation(
DateTime.Now.ToLongTimeString() + " " + DateTime.Now.Millisecond + " " +
System.Threading.Thread.CurrentThread.ManagedThreadId +
": Opening local file " + _absoluteLocation);
#endif
if (_contentType == MS.Internal.ContentType.Empty)
{
_contentType = MS.Internal.MimeTypeMapper.GetMimeTypeFromUri(Uri);
}
if (!onlyNeedContentType)
{
return File.OpenRead(_absoluteLocation.LocalPath);
}
return null;
}
private Stream HandleWebSource(bool onlyNeedContentType)
{
WebResponse response = WpfWebRequestHelper.CreateRequestAndGetResponse(_absoluteLocation);
Stream responseStream = response.GetResponseStream();
#if DEBUG
if (SiteOfOriginContainer._traceSwitch.Enabled)
System.Diagnostics.Trace.TraceInformation(
DateTime.Now.ToLongTimeString() + " " + DateTime.Now.Millisecond + " " +
System.Threading.Thread.CurrentThread.ManagedThreadId +
": Successfully retrieved stream from " + _absoluteLocation);
#endif
if (_contentType == MS.Internal.ContentType.Empty)
{
#if DEBUG
if (SiteOfOriginContainer._traceSwitch.Enabled)
System.Diagnostics.Trace.TraceInformation(
DateTime.Now.ToLongTimeString() + " " + DateTime.Now.Millisecond + " " +
System.Threading.Thread.CurrentThread.ManagedThreadId +
": SiteOfOriginPart: Setting _contentType");
#endif
_contentType = WpfWebRequestHelper.GetContentType(response);
}
if (onlyNeedContentType)
{
_cacheStream = responseStream;
}
return responseStream;
}
#endregion
//-----------------------------------------------------
//
// Private Fields
//
//------------------------------------------------------
#region Private Members
Uri _absoluteLocation = null;
ContentType _contentType = MS.Internal.ContentType.Empty;
Stream _cacheStream = null;
private Object _globalLock = new Object();
#endregion Private Members
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- BufferModesCollection.cs
- SolidColorBrush.cs
- SID.cs
- IndexerNameAttribute.cs
- TextModifier.cs
- ByteFacetDescriptionElement.cs
- NetTcpSecurity.cs
- StateBag.cs
- XmlDeclaration.cs
- CopyOnWriteList.cs
- DataGridViewHeaderCell.cs
- SequentialUshortCollection.cs
- BitmapVisualManager.cs
- HtmlInputText.cs
- ProtectedConfigurationSection.cs
- PenLineJoinValidation.cs
- TransferRequestHandler.cs
- DbDataAdapter.cs
- DefaultHttpHandler.cs
- ForeignKeyFactory.cs
- PathFigure.cs
- XmlQuerySequence.cs
- ConnectionStringEditor.cs
- XXXInfos.cs
- LongTypeConverter.cs
- activationcontext.cs
- PeerCollaboration.cs
- HandlerBase.cs
- ForeignConstraint.cs
- Viewport2DVisual3D.cs
- StorageEntityContainerMapping.cs
- IntegerFacetDescriptionElement.cs
- ContextMenu.cs
- ZipIOExtraField.cs
- BasicViewGenerator.cs
- DodSequenceMerge.cs
- QueryReaderSettings.cs
- SiteOfOriginPart.cs
- DataView.cs
- MenuItem.cs
- DifferencingCollection.cs
- NonClientArea.cs
- Clause.cs
- Journaling.cs
- AnnotationAuthorChangedEventArgs.cs
- PageAdapter.cs
- SafeNativeMethods.cs
- DataControlImageButton.cs
- PeerConnector.cs
- DBSqlParserColumn.cs
- HttpWrapper.cs
- ObjectDataSourceDisposingEventArgs.cs
- ForeignKeyConstraint.cs
- ToolBarTray.cs
- MinMaxParagraphWidth.cs
- Propagator.JoinPropagator.cs
- ExtentJoinTreeNode.cs
- PieceDirectory.cs
- UrlParameterReader.cs
- SystemUnicastIPAddressInformation.cs
- CmsUtils.cs
- PolicyLevel.cs
- AdjustableArrowCap.cs
- ScriptingJsonSerializationSection.cs
- ObjectTag.cs
- HTTP_SERVICE_CONFIG_URLACL_PARAM.cs
- SolidColorBrush.cs
- XamlReader.cs
- _DomainName.cs
- dsa.cs
- ServiceObjectContainer.cs
- XPathDocumentBuilder.cs
- EntityCommandExecutionException.cs
- SqlInternalConnectionTds.cs
- DefaultAsyncDataDispatcher.cs
- MultipleViewProviderWrapper.cs
- TileBrush.cs
- Message.cs
- Simplifier.cs
- StickyNote.cs
- ListViewInsertionMark.cs
- RoutedEventHandlerInfo.cs
- UnsafeNativeMethods.cs
- FlowDocumentReader.cs
- SchemaDeclBase.cs
- TreePrinter.cs
- MergeFilterQuery.cs
- AngleUtil.cs
- Registry.cs
- TokenBasedSet.cs
- RuntimeConfigurationRecord.cs
- HttpRequest.cs
- DataRecordInternal.cs
- XmlIlGenerator.cs
- ListViewHitTestInfo.cs
- Variant.cs
- ExpressionBuilderContext.cs
- StickyNoteAnnotations.cs
- CodeBlockBuilder.cs
- HierarchicalDataBoundControlAdapter.cs