• No se han encontrado resultados

DERECHO A LA NO DISCRIMINACION EN EL DERECHO INTERNACIONAL

SECCIÓN II: DERECHO A NO SER DISCRIMINADO

B) DERECHO A LA NO DISCRIMINACION EN EL DERECHO INTERNACIONAL

This is the last of nine tasks required to create a basic REST-style queue and publication/subscription application that uses the Service Bus.

You can now build and run the application.

1. From the Build menu in Visual Studio, click Build Solution, or press F6 to confirm the accuracy of your work.

1. If there are no errors, press F5 to run the application. When prompted, enter your service namespace, issuer name, and default key that you obtained in Step 1.

Example Description

The following example is the complete code, as it should appear after following steps 1 through 8:

using System; using System.Collections.Specialized; using System.IO; using System.Net; using System.Text; using System.Xml; namespace Microsoft.ServiceBus.Samples {

To build the application

class Program {

static string serviceNamespace; static string baseAddress; static string token;

const string sbHostName = "servicebus.windows.net"; const string acsHostName = "accesscontrol.windows.net";

static void Main(string[] args) {

Console.Write("Enter your service namespace: "); serviceNamespace = Console.ReadLine();

Console.Write("Enter your issuer name: "); string issuerName = Console.ReadLine(); Console.Write("Enter your issuer secret: "); string issuerSecret = Console.ReadLine();

baseAddress = "https://" + serviceNamespace + "." + sbHostName + "/"; try

{

// Get a SWT token from the Access Control Service, given the issuerName and issuerSecret values.

token = GetToken(issuerName, issuerSecret);

string queueName = "Queue" + Guid.NewGuid().ToString(); // Create and put a message in the queue using the SWT token. CreateQueue(queueName, token);

string msg = ReceiveAndDeleteMessage(queueName); string topicName = "Topic" + Guid.NewGuid().ToString();

string subscriptionName = "Subscription" + Guid.NewGuid().ToString(); CreateTopic(topicName);

CreateSubscription(topicName, subscriptionName); SendMessage(topicName, "msg2");

// Wait for messages to post:

//System.Threading.Thread.Sleep(500);

Console.WriteLine(ReceiveAndDeleteMessage(topicName + "/Subscriptions/" + subscriptionName));

// Get an Atom feed with all the queues in the namespace Console.WriteLine(GetResources("$Resources/Queues")); // Get an Atom feed with all the topics in the namespace Console.WriteLine(GetResources("$Resources/Topics"));

// Get an Atom feed with all the subscriptions for the topic we just created

Console.WriteLine(GetResources(topicName + "/Subscriptions"));

// Get an Atom feed with all the rules for the topic and subscritpion we just created

Console.WriteLine(GetResources(topicName + "/Subscriptions/" + subscriptionName + "/Rules"));

// Delete the queue we created DeleteResource(queueName); // Delete the topic we created DeleteResource(topicName);

// Get an Atom feed with all the topics in the namespace, it shouldn't have the one we created now

Console.WriteLine(GetResources("$Resources/Topics"));

// Get an Atom feed with all the queues in the namespace, it shouldn't have the one we created now

Console.WriteLine(GetResources("$Resources/Queues")); }

catch (WebException we) {

using (HttpWebResponse response = we.Response as HttpWebResponse) { if (response != null) { Console.WriteLine(new StreamReader(response.GetResponseStream()).ReadToEnd()); } else { Console.WriteLine(we.ToString()); } } }

Console.WriteLine("\nPress ENTER to exit."); Console.ReadLine();

}

private static string GetToken(string issuerName, string issuerSecret) {

var acsEndpoint = "https://" + serviceNamespace + "-sb." + acsHostName + "/WRAPv0.9/";

// calls to the service are always issued over HTTPS

var realm = "http://" + serviceNamespace + "." + sbHostName + "/"; NameValueCollection values = new NameValueCollection();

values.Add("wrap_name", issuerName); values.Add("wrap_password", issuerSecret); values.Add("wrap_scope", realm);

WebClient webClient = new WebClient();

byte[] response = webClient.UploadValues(acsEndpoint, values); string responseString = Encoding.UTF8.GetString(response); var responseProperties = responseString.Split('&'); var tokenProperty = responseProperties[0].Split('='); var token = Uri.UnescapeDataString(tokenProperty[1]); return "WRAP access_token=\"" + token + "\"";

}

// Uses HTTP PUT to create the queue

private static string CreateQueue(string queueName, string token) {

// Create the URI of the new Queue, note that this uses the HTTPS scheme string queueAddress = baseAddress + queueName;

WebClient webClient = new WebClient();

webClient.Headers[HttpRequestHeader.Authorization] = token; Console.WriteLine("\nCreating queue {0}", queueAddress); // Prepare the body of the create queue request

var putData = @"<entry xmlns=""http://www.w3.org/2005/Atom""> <title type=""text"">" + queueName + @"</title>

<QueueDescription xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""

xmlns=""http://schemas.microsoft.com/netservices/2010/10/servicebus/connect"" /> </content>

</entry>";

byte[] response = webClient.UploadData(queueAddress, "PUT", Encoding.UTF8.GetBytes(putData));

return Encoding.UTF8.GetString(response); }

// Sends a message to the "queueName" queue, given the name, the value to enqueue, and the SWT token

// Uses an HTTP POST request.

private static void SendMessage(string queueName, string body) {

string fullAddress = baseAddress + queueName + "/messages" + "?timeout=60"; Console.WriteLine("\nSending message {0} - to address {1}", body,

fullAddress);

WebClient webClient = new WebClient();

webClient.Headers[HttpRequestHeader.Authorization] = token;

webClient.UploadData(fullAddress, "POST", Encoding.UTF8.GetBytes(body)); }

// Receives and deletes the next message from the given resource (Queue, Topic, or Subscription)

// using the resourceName, the SWT token, and an HTTP DELETE request. private static string ReceiveAndDeleteMessage(string resourceName) {

string fullAddress = baseAddress + resourceName + "/messages/head" + "?timeout=60";

Console.WriteLine("\nRetrieving message from {0}", fullAddress); WebClient webClient = new WebClient();

byte[] response = webClient.UploadData(fullAddress, "DELETE", new byte[0]); string responseStr = Encoding.UTF8.GetString(response);

Console.WriteLine(responseStr); return responseStr;

}

// Creates a Topic with the given topic name and the SWT token // Using an HTTP PUT request.

private static string CreateTopic(string topicName) {

var topicAddress = baseAddress + topicName; WebClient webClient = new WebClient();

webClient.Headers[HttpRequestHeader.Authorization] = token; Console.WriteLine("\nCreating topic {0}", topicAddress); // Prepare the body of the create queue request

var putData = @"<entry xmlns=""http://www.w3.org/2005/Atom""> <title type=""text"">" + topicName + @"</title>

<content type=""application/xml"">

<TopicDescription xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""

xmlns=""http://schemas.microsoft.com/netservices/2010/10/servicebus/connect"" /> </content>

</entry>";

byte[] response = webClient.UploadData(topicAddress, "PUT", Encoding.UTF8.GetBytes(putData));

return Encoding.UTF8.GetString(response); }

private static string CreateSubscription(string topicName, string subscriptionName)

var subscriptionAddress = baseAddress + topicName + "/Subscriptions/" + subscriptionName;

WebClient webClient = new WebClient();

webClient.Headers[HttpRequestHeader.Authorization] = token;

Console.WriteLine("\nCreating subscription {0}", subscriptionAddress); // Prepare the body of the create queue request

var putData = @"<entry xmlns=""http://www.w3.org/2005/Atom""> <title type=""text"">" + subscriptionName + @"</title>

<content type=""application/xml"">

<SubscriptionDescription xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.microsoft.com/netservices/2010/10/servicebus/connect"" /> </content>

</entry>";

byte[] response = webClient.UploadData(subscriptionAddress, "PUT", Encoding.UTF8.GetBytes(putData));

return Encoding.UTF8.GetString(response); }

private static string GetResources(string resourceAddress) {

string fullAddress = baseAddress + resourceAddress; WebClient webClient = new WebClient();

webClient.Headers[HttpRequestHeader.Authorization] = token; Console.WriteLine("\nGetting resources from {0}", fullAddress); return FormatXml(webClient.DownloadString(fullAddress)); }

private static string DeleteResource(string resourceName) {

string fullAddress = baseAddress + resourceName; WebClient webClient = new WebClient();

Console.WriteLine("\nDeleting resource at {0}", fullAddress);

byte[] response = webClient.UploadData(fullAddress, "DELETE", new byte[0]); return Encoding.UTF8.GetString(response);

}

// Formats the XML string to be more human-readable; intended for display purposes

private static string FormatXml(string inputXml) {

XmlDocument document = new XmlDocument(); document.Load(new StringReader(inputXml)); StringBuilder builder = new StringBuilder();

using (XmlTextWriter writer = new XmlTextWriter(new StringWriter(builder))) { writer.Formatting = Formatting.Indented; document.Save(writer); } return builder.ToString(); } } }