Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

434 lines
11 KiB

  1. using System;
  2. using System.Web;
  3. using System.IO;
  4. using System.Data;
  5. using System.Text;
  6. using System.Collections;
  7. using System.Data.SqlClient;
  8. using System.Xml.Serialization;
  9. using UDDI;
  10. using UDDI.Diagnostics;
  11. namespace UDDI.API.Business
  12. {
  13. public class DiscoveryUrl
  14. {
  15. [XmlAttribute("useType")]
  16. public string UseType;
  17. [XmlText]
  18. public string Value;
  19. public DiscoveryUrl()
  20. {
  21. }
  22. public DiscoveryUrl( string url )
  23. {
  24. this.Value = url;
  25. this.UseType = "";
  26. }
  27. public DiscoveryUrl( string url, string useType )
  28. {
  29. this.Value = url;
  30. this.UseType = useType;
  31. }
  32. internal void Validate()
  33. {
  34. if( null == UseType )
  35. UseType = "";
  36. Utility.ValidateLength( ref UseType, "useType", UDDI.Constants.Lengths.UseType );
  37. Utility.ValidateLength( ref Value, "discoveryURL", UDDI.Constants.Lengths.DiscoveryURL );
  38. }
  39. public void Save( string businessKey )
  40. {
  41. //
  42. // Create a command object to invoke the stored procedure
  43. //
  44. SqlCommand cmd = new SqlCommand( "net_businessEntity_discoveryUrl_save", ConnectionManager.GetConnection() );
  45. cmd.Transaction = ConnectionManager.GetTransaction();
  46. cmd.CommandType = CommandType.StoredProcedure;
  47. //
  48. // Input parameters
  49. //
  50. cmd.Parameters.Add( new SqlParameter( "@businessKey", SqlDbType.UniqueIdentifier ) );
  51. cmd.Parameters.Add( new SqlParameter( "@useType", SqlDbType.NVarChar, UDDI.Constants.Lengths.UseType ) );
  52. cmd.Parameters.Add( new SqlParameter( "@discoveryUrl", SqlDbType.NVarChar, UDDI.Constants.Lengths.DiscoveryURL ) );
  53. //
  54. // Set parameters
  55. //
  56. SqlParameterAccessor parmacc = new SqlParameterAccessor( cmd.Parameters );
  57. parmacc.SetGuidFromString( "@businessKey", businessKey );
  58. parmacc.SetString( "@useType", UseType );
  59. parmacc.SetString( "@discoveryUrl", Value );
  60. //
  61. // Execute save
  62. //
  63. cmd.ExecuteNonQuery();
  64. }
  65. public bool IsDefault( string businessKey )
  66. {
  67. Debug.VerifySetting( "DefaultDiscoveryURL" );
  68. string defaultDiscoveryUrl = Config.GetString( "DefaultDiscoveryURL" ) + businessKey;
  69. return ( null != UseType ) &&
  70. ( "businessentity" == UseType.ToLower() ) &&
  71. ( string.Compare( defaultDiscoveryUrl, Value, true ) == 0 );
  72. }
  73. }
  74. public class DiscoveryUrlCollection : CollectionBase
  75. {
  76. public DiscoveryUrl this[int index]
  77. {
  78. get
  79. { return (DiscoveryUrl)List[index]; }
  80. set
  81. { List[index] = value; }
  82. }
  83. public void Get( string businessKey )
  84. {
  85. //
  86. // This procedure add the discoveryURLs that were persisted to the database
  87. // this does not include the default discoveryURL, it is added by the the businessEntity.Get()
  88. // method since it has the visibility of the operator name who owns the entity.
  89. //
  90. //
  91. // Create a command object to invoke the stored procedure
  92. //
  93. SqlStoredProcedureAccessor cmd = new SqlStoredProcedureAccessor( "net_businessEntity_discoveryURLs_get" );
  94. //
  95. // Add parameters
  96. //
  97. cmd.Parameters.Add( "@businessKey", SqlDbType.UniqueIdentifier );
  98. cmd.Parameters.SetGuidFromString( "@businessKey", businessKey );
  99. //
  100. // Execute query
  101. //
  102. SqlDataReaderAccessor reader = cmd.ExecuteReader();
  103. try
  104. {
  105. Read( reader );
  106. }
  107. finally
  108. {
  109. reader.Close();
  110. }
  111. #if never
  112. //
  113. // This procedure add the discoveryURLs that were persisted to the database
  114. // this does not include the default discoveryURL, it is added by the the businessEntity.Get()
  115. // method since it has the visibility of the operator name who owns the entity.
  116. //
  117. const int UseTypeIndex = 0;
  118. const int UrlIndex = 1;
  119. //
  120. // Create a command object to invoke the stored procedure
  121. //
  122. SqlCommand cmd = new SqlCommand( "net_businessEntity_discoveryURLs_get", ConnectionManager.GetConnection() );
  123. cmd.Transaction = ConnectionManager.GetTransaction();
  124. cmd.CommandType = CommandType.StoredProcedure;
  125. //
  126. // Add parameters
  127. //
  128. cmd.Parameters.Add( new SqlParameter( "@businessKey", SqlDbType.UniqueIdentifier ) );
  129. SqlParameterAccessor paramacc = new SqlParameterAccessor( cmd.Parameters );
  130. paramacc.SetGuidFromString( "@businessKey", businessKey );
  131. //
  132. // Execute query
  133. //
  134. SqlDataReader rdr = cmd.ExecuteReader();
  135. try
  136. {
  137. SqlDataReaderAccessor rdracc = new SqlDataReaderAccessor( rdr );
  138. //
  139. // The discoveryUrls will be contained in the result set
  140. //
  141. while( rdr.Read() )
  142. {
  143. string useType = rdracc.GetString( UseTypeIndex );
  144. if( null == useType )
  145. useType = "";
  146. Add( rdracc.GetString( UrlIndex ), useType );
  147. }
  148. }
  149. finally
  150. {
  151. rdr.Close();
  152. }
  153. #endif
  154. }
  155. public void Read( SqlDataReaderAccessor reader )
  156. {
  157. const int UseTypeIndex = 0;
  158. const int UrlIndex = 1;
  159. //
  160. // The discoveryUrls will be contained in the result set
  161. //
  162. while( reader.Read() )
  163. {
  164. string useType = reader.GetString( UseTypeIndex );
  165. if( null == useType )
  166. useType = "";
  167. Add( reader.GetString( UrlIndex ), useType );
  168. }
  169. }
  170. internal void Validate()
  171. {
  172. foreach( DiscoveryUrl discoveryUrl in this )
  173. {
  174. discoveryUrl.Validate();
  175. }
  176. }
  177. public void Save( string businessKey )
  178. {
  179. //
  180. // Keep a separate remove list.
  181. //
  182. DiscoveryUrlCollection duplicateUrls = null;
  183. foreach( DiscoveryUrl discoveryUrl in this )
  184. {
  185. //
  186. // If we are not doing replication, then we want to check to make sure that we don't persist the
  187. // default discovery url.
  188. //
  189. if( ContextType.Replication != Context.ContextType )
  190. {
  191. //
  192. // Make sure we don't persist the default discoveryURL
  193. //
  194. if( !discoveryUrl.IsDefault( businessKey ) )
  195. {
  196. discoveryUrl.Save( businessKey );
  197. }
  198. else
  199. {
  200. if( null == duplicateUrls )
  201. {
  202. duplicateUrls = new DiscoveryUrlCollection();
  203. }
  204. duplicateUrls.Add( discoveryUrl );
  205. }
  206. }
  207. else
  208. {
  209. discoveryUrl.Save( businessKey );
  210. }
  211. }
  212. //
  213. // Remove duplicates if we have any.
  214. //
  215. if( null != duplicateUrls )
  216. {
  217. foreach( DiscoveryUrl duplicateUrl in duplicateUrls )
  218. {
  219. this.Remove( duplicateUrl );
  220. }
  221. }
  222. }
  223. internal void AddDefaultDiscoveryUrl( string businessKey )
  224. {
  225. Debug.VerifySetting( "DefaultDiscoveryURL" );
  226. string defaultDiscoveryUrl = Config.GetString( "DefaultDiscoveryURL" ) + businessKey;
  227. //
  228. // Check to see if the collection already contains the default
  229. // discovery URL. If so, we don't need to add one.
  230. // This check is needed since some of the legacy code used to
  231. // permit the persistence of the default discovery URL.
  232. //
  233. foreach( DiscoveryUrl discoveryUrl in this )
  234. {
  235. if( discoveryUrl.IsDefault( businessKey ) )
  236. return;
  237. }
  238. Add( defaultDiscoveryUrl, "businessEntity" );
  239. }
  240. public int Add()
  241. {
  242. return List.Add( new DiscoveryUrl() );
  243. }
  244. public int Add( DiscoveryUrl value )
  245. {
  246. return List.Add( value );
  247. }
  248. public int Add( string strUrl )
  249. {
  250. return List.Add( new DiscoveryUrl( strUrl ) );
  251. }
  252. public int Add( string strUrl, string strUseType )
  253. {
  254. return List.Add( new DiscoveryUrl( strUrl, strUseType ) );
  255. }
  256. public void Insert( int index, DiscoveryUrl value )
  257. {
  258. List.Insert( index, value );
  259. }
  260. public int IndexOf( DiscoveryUrl value )
  261. {
  262. return List.IndexOf( value );
  263. }
  264. public bool Contains( DiscoveryUrl value )
  265. {
  266. return List.Contains( value );
  267. }
  268. public void Remove( DiscoveryUrl value )
  269. {
  270. List.Remove(value);
  271. }
  272. public void CopyTo( DiscoveryUrl[] array )
  273. {
  274. foreach( DiscoveryUrl discoveryUrl in array )
  275. Add( discoveryUrl );
  276. }
  277. public DiscoveryUrl[] ToArray()
  278. {
  279. return (DiscoveryUrl[])InnerList.ToArray( typeof( DiscoveryUrl ) );
  280. }
  281. }
  282. /// ****************************************************************
  283. /// public class DiscoveryUrlHandler
  284. /// ----------------------------------------------------------------
  285. /// <summary>
  286. /// DiscoveryUrlHandler implements the IHttpHandler interface.
  287. /// It is designed to retrieve businessEntity details for a businessKey
  288. /// specified as part of the HTTP query string
  289. /// e.g. http://uddi.microsoft.com/discovery?businessKey=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
  290. /// </summary>
  291. /// ****************************************************************
  292. ///
  293. public class DiscoveryUrlHandler : IHttpHandler
  294. {
  295. public void ProcessRequest( HttpContext ctx )
  296. {
  297. try
  298. {
  299. //
  300. // Verify the GET verb was used and that a query string is
  301. // specified.
  302. //
  303. if( "GET" != ctx.Request.RequestType.ToUpper() )
  304. {
  305. ctx.Response.AddHeader( "Allow", "GET" );
  306. ctx.Response.StatusCode = 405; // Method Not Allowed
  307. return;
  308. }
  309. if( null == ctx.Request.QueryString[ "businessKey" ] )
  310. {
  311. ctx.Response.StatusCode = 400; // Bad Request
  312. ctx.Response.Write( "<h1>400 Bad Request</h1>Missing required argument 'businessKey'" );
  313. return;
  314. }
  315. //
  316. // Attempt to retrieve the business entity.
  317. //
  318. ConnectionManager.Open( false, false );
  319. string businessKey = ctx.Request.QueryString[ "businessKey" ];
  320. BusinessEntity be = new BusinessEntity( businessKey );
  321. be.Get();
  322. //
  323. // Serialize the business Entity to the response stream
  324. // using UTF-8 encoding
  325. //
  326. // XmlSerializer serializer = new XmlSerializer( be.GetType() );
  327. XmlSerializer serializer = XmlSerializerManager.GetSerializer( be.GetType() );
  328. UTF8Encoding utf8 = new UTF8Encoding( false );
  329. StreamWriter sr = new StreamWriter( ctx.Response.OutputStream, utf8 );
  330. serializer.Serialize( sr, be );
  331. ctx.Response.AddHeader( "Content-Type", "text/xml; charset=utf-8" );
  332. }
  333. catch( FormatException e )
  334. {
  335. //
  336. // We get a FormatException when passed a bad GUID.
  337. //
  338. ctx.Response.StatusCode = 400; // Bad Request
  339. ctx.Response.Write( "<h1>400 Bad Request</h1>" + e.Message );
  340. }
  341. catch( SqlException e )
  342. {
  343. //
  344. // We get a SqlException when we could not get the data.
  345. //
  346. ctx.Response.StatusCode = 400; // Bad Request
  347. ctx.Response.Write( "<h1>400 Bad Request</h1>" + e.Message );
  348. }
  349. catch( UDDIException e )
  350. {
  351. ctx.Response.StatusCode = 400; // Bad Request
  352. ctx.Response.Write( "<h1>400 Bad Request</h1>" + e.Message );
  353. }
  354. catch( Exception e )
  355. {
  356. ctx.Response.StatusCode = 500; // Internal Server Error
  357. ctx.Response.Write( "<h1>500 Internal Server Error</h1>" );
  358. Debug.OperatorMessage( SeverityType.Error, CategoryType.None, OperatorMessageType.None, e.ToString() );
  359. }
  360. finally
  361. {
  362. ConnectionManager.Close();
  363. }
  364. }
  365. public bool IsReusable
  366. {
  367. get
  368. {
  369. return true;
  370. }
  371. }
  372. }
  373. }