1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
//------------------------------------------------------------------------------
// <copyright file="DiscoveryDocumentReference.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Web.Services.Discovery {
using System;
using System.Net;
using System.Xml;
using System.Diagnostics;
using System.IO;
using System.Xml.Serialization;
using System.Web.Services.Protocols;
using System.Web.Services.Configuration;
using System.Text;
using System.Globalization;
using System.Threading;
using System.Collections;
using System.Web.Services.Diagnostics;
/// <include file='doc\DiscoveryDocumentReference.uex' path='docs/doc[@for="DiscoveryDocumentReference"]/*' />
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
[XmlRoot("discoveryRef", Namespace = DiscoveryDocument.Namespace)]
public sealed class DiscoveryDocumentReference : DiscoveryReference {
private string reference;
/// <include file='doc\DiscoveryDocumentReference.uex' path='docs/doc[@for="DiscoveryDocumentReference.DiscoveryDocumentReference"]/*' />
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public DiscoveryDocumentReference() {
}
/// <include file='doc\DiscoveryDocumentReference.uex' path='docs/doc[@for="DiscoveryDocumentReference.DiscoveryDocumentReference1"]/*' />
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public DiscoveryDocumentReference(string href) {
Ref = href;
}
/// <include file='doc\DiscoveryDocumentReference.uex' path='docs/doc[@for="DiscoveryDocumentReference.Ref"]/*' />
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
[XmlAttribute("ref")]
public string Ref {
get {
return reference == null ? "" : reference;
}
set {
reference = value;
}
}
/// <include file='doc\DiscoveryDocumentReference.uex' path='docs/doc[@for="DiscoveryDocumentReference.DefaultFilename"]/*' />
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
[XmlIgnore]
public override string DefaultFilename {
get {
string filename = FilenameFromUrl(Url);
return Path.ChangeExtension(filename, ".disco"); // [Microsoft] change default extension
}
}
/// <include file='doc\DiscoveryDocumentReference.uex' path='docs/doc[@for="DiscoveryDocumentReference.Document"]/*' />
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
[XmlIgnore]
public DiscoveryDocument Document {
get {
if (ClientProtocol == null)
throw new InvalidOperationException(Res.GetString(Res.WebMissingClientProtocol));
object document = ClientProtocol.Documents[Url];
if (document == null) {
Resolve();
document = ClientProtocol.Documents[Url];
}
DiscoveryDocument discoDocument = document as DiscoveryDocument;
if (discoDocument == null) {
throw new InvalidOperationException(Res.GetString(Res.WebInvalidDocType,
typeof(DiscoveryDocument).FullName,
document == null ? string.Empty : document.GetType().FullName,
Url));
}
return discoDocument;
}
}
/// <include file='doc\DiscoveryDocumentReference.uex' path='docs/doc[@for="DiscoveryDocumentReference.WriteDocument"]/*' />
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public override void WriteDocument(object document, Stream stream) {
WebServicesSection.Current.DiscoveryDocumentSerializer.Serialize(new StreamWriter(stream, new UTF8Encoding(false)), document);
}
/// <include file='doc\DiscoveryDocumentReference.uex' path='docs/doc[@for="DiscoveryDocumentReference.ReadDocument"]/*' />
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public override object ReadDocument(Stream stream) {
return WebServicesSection.Current.DiscoveryDocumentSerializer.Deserialize(stream);
}
/// <include file='doc\DiscoveryDocumentReference.uex' path='docs/doc[@for="DiscoveryDocumentReference.Url"]/*' />
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
[XmlIgnore]
public override string Url {
get { return Ref; }
set { Ref = value; }
}
/// <include file='doc\DiscoveryDocumentReference.uex' path='docs/doc[@for="DiscoveryDocumentReference.GetDocumentNoParse"]/*' />
/// <devdoc>
/// Retrieves a discovery document from Url, either out of the ClientProtocol
/// or from a stream. Does not
/// </devdoc>
private static DiscoveryDocument GetDocumentNoParse(ref string url, DiscoveryClientProtocol client) {
DiscoveryDocument d = (DiscoveryDocument) client.Documents[url];
if (d != null) {
return d;
}
string contentType = null;
Stream stream = client.Download(ref url, ref contentType);
try {
XmlTextReader reader = new XmlTextReader(new StreamReader(stream, RequestResponseUtils.GetEncoding(contentType)));
reader.WhitespaceHandling = WhitespaceHandling.Significant;
reader.XmlResolver = null;
reader.DtdProcessing = DtdProcessing.Prohibit;
if (!DiscoveryDocument.CanRead(reader)) {
// there is no discovery document at this location
ArgumentException exception = new ArgumentException(Res.GetString(Res.WebInvalidFormat));
throw new InvalidOperationException(Res.GetString(Res.WebMissingDocument, url), exception);
}
return DiscoveryDocument.Read(reader);
}
finally {
stream.Close();
}
}
/// <include file='doc\DiscoveryDocumentReference.uex' path='docs/doc[@for="DiscoveryDocumentReference.Resolve"]/*' />
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
protected internal override void Resolve(string contentType, Stream stream) {
DiscoveryDocument document = null;
if (ContentType.IsHtml(contentType)) {
string newRef = LinkGrep.SearchForLink(stream);
if (newRef != null) {
string newUrl = UriToString(Url, newRef);
document = GetDocumentNoParse(ref newUrl, ClientProtocol);
Url = newUrl;
}
else
throw new InvalidContentTypeException(Res.GetString(Res.WebInvalidContentType, contentType), contentType);
}
if (document == null) { // probably xml...
XmlTextReader reader = new XmlTextReader(new StreamReader(stream, RequestResponseUtils.GetEncoding(contentType)));
reader.XmlResolver = null;
reader.WhitespaceHandling = WhitespaceHandling.Significant;
reader.DtdProcessing = DtdProcessing.Prohibit;
if (DiscoveryDocument.CanRead(reader)) {
// it's a discovery document, so just read it.
document = DiscoveryDocument.Read(reader);
}
else {
// check out the processing instructions before the first tag. if any of them
// match the form specified in the DISCO spec, save the href.
stream.Position = 0;
XmlTextReader newReader = new XmlTextReader(new StreamReader(stream, RequestResponseUtils.GetEncoding(contentType)));
newReader.XmlResolver = null;
newReader.DtdProcessing = DtdProcessing.Prohibit;
while (newReader.NodeType != XmlNodeType.Element) {
if (newReader.NodeType == XmlNodeType.ProcessingInstruction) {
// manually parse the PI contents since XmlTextReader won't automatically do it
StringBuilder sb = new StringBuilder("<pi ");
sb.Append(newReader.Value);
sb.Append("/>");
XmlTextReader piReader = new XmlTextReader(new StringReader(sb.ToString()));
piReader.XmlResolver = null;
piReader.DtdProcessing = DtdProcessing.Prohibit;
piReader.Read();
string type = piReader["type"];
string alternate = piReader["alternate"];
string href = piReader["href"];
if (type != null && ContentType.MatchesBase(type, ContentType.TextXml)
&& alternate != null && string.Compare(alternate, "yes", StringComparison.OrdinalIgnoreCase) == 0
&& href != null) {
// we got a PI with the right attributes
// there is a link to a discovery document. follow it after fully qualifying it.
string newUrl = UriToString(Url, href);
document = GetDocumentNoParse(ref newUrl, ClientProtocol);
Url = newUrl;
break;
}
}
newReader.Read();
}
}
}
if (document == null) {
// there is no discovery document at this location
Exception exception;
if (ContentType.IsXml(contentType)) {
exception = new ArgumentException(Res.GetString(Res.WebInvalidFormat));
}
else {
exception = new InvalidContentTypeException(Res.GetString(Res.WebInvalidContentType, contentType), contentType);
}
throw new InvalidOperationException(Res.GetString(Res.WebMissingDocument, Url), exception);
}
ClientProtocol.References[Url] = this;
ClientProtocol.Documents[Url] = document;
foreach (object o in document.References) {
if (o is DiscoveryReference) {
DiscoveryReference r = (DiscoveryReference) o;
if (r.Url.Length == 0) {
throw new InvalidOperationException(Res.GetString(Res.WebEmptyRef, r.GetType().FullName, Url));
}
r.Url = UriToString(Url, r.Url);
//All inheritors of DiscoveryReference that got URIs relative
//to Ref property should adjust them like ContractReference does here.
ContractReference cr = r as ContractReference;
if ( (cr != null) && (cr.DocRef != null) ) {
cr.DocRef = UriToString(Url, cr.DocRef);
}
r.ClientProtocol = ClientProtocol;
ClientProtocol.References[r.Url] = r;
}
else
ClientProtocol.AdditionalInformation.Add(o);
}
return;
}
/// <include file='doc\DiscoveryDocumentReference.uex' path='docs/doc[@for="DiscoveryDocumentReference.ResolveAll"]/*' />
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public void ResolveAll() {
ResolveAll(true);
}
internal void ResolveAll(bool throwOnError) {
try {
Resolve();
}
catch (Exception e) {
if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException) {
throw;
}
if (throwOnError)
throw;
if (Tracing.On) Tracing.ExceptionCatch(TraceEventType.Warning, this, "ResolveAll", e);
// can't continue, because we couldn't find a document.
return;
}
foreach (object o in Document.References) {
DiscoveryDocumentReference r = o as DiscoveryDocumentReference;
if (r == null)
continue;
if (ClientProtocol.Documents[r.Url] != null) {
continue;
}
r.ClientProtocol = ClientProtocol;
r.ResolveAll(throwOnError);
}
}
}
}
|