Changeset 4727

Show
Ignore:
Timestamp:
06/18/08 10:11:16 (6 months ago)
Author:
xmlhacker
Message:

a bunch of cleanup work

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/nuxleus/Source/Nuxleus.Extension.Aws/Nuxleus.Extension.Aws.csproj

    r4723 r4727  
    7070      <DependentUpon>Settings.settings</DependentUpon> 
    7171    </Compile> 
     72    <Compile Include="Request\QueryRequest.cs" /> 
     73    <Compile Include="Request\ListDomainsRequest.cs" /> 
     74    <Compile Include="Request\DeleteDomainRequest.cs" /> 
     75    <Compile Include="Request\CreateDomainRequest.cs" /> 
     76    <Compile Include="Request\GetAttributesRequest.cs" /> 
    7277    <Compile Include="Request\IRequest.cs" /> 
    7378    <Compile Include="Request\PutAttributesRequest.cs" /> 
     
    9095    <Compile Include="SimpleDB\Task\Task.cs" /> 
    9196    <Compile Include="Utils\HMACSigner.cs" /> 
    92     <Compile Include="Utils\PropertyCaller.cs" /> 
    93     <Compile Include="Utils\Serializer.cs" /> 
    94     <Compile Include="Utils\Util.cs" /> 
    9597  </ItemGroup> 
    9698  <ItemGroup> 
  • trunk/nuxleus/Source/Nuxleus.Extension.Aws/Request/PutAttributesRequest.cs

    r4725 r4727  
    11using System; 
    22using System.Collections.Generic; 
    3 using System.Linq; 
    4 using System.Text; 
    53using Nuxleus.Extension.AWS.SimpleDB; 
    6 using System.Xml.Linq; 
    74using Nuxleus.MetaData; 
    85 
  • trunk/nuxleus/Source/Nuxleus.Extension.Aws/Response/IResponse.cs

    r4715 r4727  
    66 
    77namespace Nuxleus.Extension.AWS.SimpleDB { 
    8     interface IResponse<T>
     8    public interface IResponse
    99        KeyValuePair<string,string>[] Headers { get; set;} 
    10         T Response { get; set; } 
     10        String Response { get; set; } 
    1111    } 
    1212} 
  • trunk/nuxleus/Source/Nuxleus.Extension.Aws/SimpleDB/SdbAction.cs

    r4723 r4727  
    1010 
    1111namespace Nuxleus.Extension.AWS.SimpleDB { 
    12  
    13     public enum RequestType { 
    14         [Label("Query")] 
    15         Query, 
    16         [Label("CreateDomain")] 
    17         CreateDomain, 
    18         [Label("DeleteDomain")] 
    19         DeleteDomain, 
    20         [Label("ListDomains")] 
    21         ListDomains, 
    22         [Label("PutAttributes")] 
    23         PutAttributes, 
    24         [Label("DeleteAttributes")] 
    25         DeleteAttributes, 
    26         [Label("GetAttributes")] 
    27         GetAttributes 
    28     } 
    2912 
    3013    public struct SdbAction { 
  • trunk/nuxleus/Source/Nuxleus.Extension.Aws/SimpleDB/SimpleDBService.cs

    r4726 r4727  
    2020namespace Nuxleus.Extension.AWS.SimpleDB { 
    2121 
     22    public enum RequestType { 
     23        [Label("Query")] 
     24        Query, 
     25        [Label("CreateDomain")] 
     26        CreateDomain, 
     27        [Label("DeleteDomain")] 
     28        DeleteDomain, 
     29        [Label("ListDomains")] 
     30        ListDomains, 
     31        [Label("PutAttributes")] 
     32        PutAttributes, 
     33        [Label("DeleteAttributes")] 
     34        DeleteAttributes, 
     35        [Label("GetAttributes")] 
     36        GetAttributes 
     37    } 
     38 
    2239    public struct SimpleDBService<TRequestType> { 
    2340 
     41        static string AWS_PUBLIC_KEY = System.Environment.GetEnvironmentVariable("AWS_PUBLIC_KEY"); 
     42        static string AWS_PRIVATE_KEY = System.Environment.GetEnvironmentVariable("AWS_PRIVATE_KEY"); 
    2443        static XNamespace s = "http://schemas.xmlsoap.org/soap/envelope/"; 
    25         static XmlSerializer m_xSerializer = new System.Xml.Serialization.XmlSerializer(typeof(TRequestType)); 
     44        static XNamespace aws = "http://sdb.amazonaws.com/doc/2007-11-07/"; 
     45        static XNamespace i = "http://www.w3.org/2001/XMLSchema-instance"; 
     46        static XmlSerializer m_xSerializer = new XmlSerializer(typeof(TRequestType)); 
    2647 
    27         //static XElement GetRequestXElement(RequestType requestType, params string[] paramArray) { 
    28         //    switch (requestType) { 
    29         //        case RequestType.Query: 
    30         //            return SdbAction.Query(paramArray[0], paramArray[1], paramArray[2], paramArray[3]); 
    31         //        case RequestType.CreateDomain: 
    32         //            return SdbAction.CreateDomain(paramArray[0]); 
    33         //        case RequestType.DeleteDomain: 
    34         //            return SdbAction.DeleteDomain(paramArray[0]); 
    35         //        case RequestType.ListDomains: 
    36         //            return SdbAction.ListDomains(paramArray[0], paramArray[1]); 
    37         //        case RequestType.GetAttributes: 
    38         //            String[] getattributes = new String[paramArray.Length - 2]; 
    39         //            int pa = 0; 
    40         //            int ia = 0; 
    41         //            foreach (String attribute in paramArray) { 
    42         //                if (pa > 1) { 
    43         //                    getattributes[ia] = attribute; 
    44         //                    ia++; 
    45         //                } 
    46         //                pa++; 
    47         //            } 
    48         //            return SdbAction.GetAttributes(paramArray[0], paramArray[1], getattributes); 
    49         //        case RequestType.PutAttributes: 
    50         //            SdbAttribute[] attributes = new SdbAttribute[paramArray.Length - 2]; 
    51         //            int p = 0; 
    52         //            int i = 0; 
    53         //            foreach (String attribute in paramArray) { 
    54         //                if (p > 1) { 
    55         //                    attributes[i] = new SdbAttribute { Name = attribute.SubstringBefore("="), Value = attribute.SubstringAfter("=") }; 
    56         //                    i++; 
    57         //                } 
    58         //                p++; 
    59         //            } 
    60         //            return SdbAction.PutAttributes(paramArray[0], paramArray[1], attributes); 
    61         //        case RequestType.DeleteAttributes: 
    62         //            SdbAttribute[] delAttributes = new SdbAttribute[paramArray.Length - 2]; 
    63         //            int p1 = 0; 
    64         //            int i1 = 0; 
    65         //            foreach (String attribute in paramArray) { 
    66         //                if (p1 > 1) { 
    67         //                    delAttributes[i1] = new SdbAttribute { Name = attribute.SubstringBefore("="), Value = attribute.SubstringAfter("=") }; 
    68         //                    i1++; 
    69         //                } 
    70         //                p1++; 
    71         //            } 
    72         //            return SdbAction.DeleteAttributes(paramArray[0], paramArray[1], delAttributes); 
    73         //        default: 
    74         //            return new XElement("null", null); 
    75         //    } 
    76         //} 
    77  
    78         public StreamReader MakeRequest(RequestType requestType, XElement message) { 
    79  
    80             string rType = LabelAttribute.FromMember(requestType); 
    81  
    82             Encoding encoding = new UTF8Encoding(); 
    83  
    84             HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://sdb.amazonaws.com/"); 
    85  
    86             request.Timeout = 90000 /*TODO: This should be set dynamically*/; 
    87             request.KeepAlive = true; 
    88             request.Pipelined = true; 
    89  
    90             XmlReader xreader = message.CreateReader(); 
    91             StringBuilder output = new StringBuilder("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); 
    92  
    93             byte[] buffer = null; 
    94  
    95             do { 
    96                 if (xreader.IsStartElement()) { 
    97                     output.Append(xreader.ReadOuterXml()); 
    98                     Console.WriteLine("Output: {0} :/Output", output.ToString()); 
    99                     buffer = encoding.GetBytes(output.ToString()); 
    100                 } 
    101             } while (xreader.Read()); 
    102  
    103             int contentLength = buffer.Length; 
    104             request.ContentLength = contentLength; 
    105             request.Method = "POST"; 
    106             request.ContentType = "application/soap+xml"; 
    107             request.Headers.Add("SOAPAction", rType); 
    108  
    109             try { 
    110                 using (Stream newStream = request.GetRequestStream()) { 
    111                     newStream.Write(buffer, 0, contentLength); 
    112                     return new StreamReader(request.GetResponse().GetResponseStream()); 
    113                 } 
    114             } catch (System.Net.WebException e) { 
    115                 Console.WriteLine("Failed! Reason: {0}, Message: {1}", e.Response, e.Message); 
    116                 return new StreamReader(e.Response.GetResponseStream()); 
    117             } 
    118         } 
    119  
    120         public static IEnumerable<IAsync> CallWebService<TRequestType, TResponseType>(ITask task, IRequest sdbRequest, Dictionary<IRequest, TResponseType> responseList) { 
     48        public static IEnumerable<IAsync> CallWebService<TResultType>(ITask task, IRequest sdbRequest, Dictionary<IRequest, TResultType> responseList) { 
    12149 
    12250            Encoding encoding = new UTF8Encoding(); 
     
    15179            } 
    15280 
    153             System.Console.WriteLine("Start Request: Thread is background: {0}, Thread ID: {1}, Thread is managed: {2}", Thread.CurrentThread.IsBackground, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread); 
     81            //System.Console.WriteLine("Start Request: Thread is background: {0}, Thread ID: {1}, Thread is managed: {2}", Thread.CurrentThread.IsBackground, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread); 
    15482 
    15583            using (Stream newStream = request.GetRequestStream()) { 
     
    15785                Async<WebResponse> response = request.GetResponseAsync(); 
    15886                yield return response; 
    159                 System.Console.WriteLine("[] got response on thread: {0}", Thread.CurrentThread.ManagedThreadId); 
     87                //System.Console.WriteLine("[] got response on thread: {0}", Thread.CurrentThread.ManagedThreadId); 
    16088                Stream stream = response.Result.GetResponseStream(); 
    161                 Async<TResponseType> responseObject = stream.ReadToEndAsync<TResponseType>().ExecuteAsync<TResponseType>(); 
     89                Async<TResultType> responseObject = stream.ReadToEndAsync<TResultType>().ExecuteAsync<TResultType>(); 
    16290                yield return responseObject; 
    16391                responseList.Add(sdbRequest, responseObject.Result); 
     
    16694 
    16795        private static XElement CreateSoapMessage(ITask task, String action) { 
    168              
     96 
    16997            StringBuilder builder = new StringBuilder(); 
    17098            using (TextWriter writer = new StringWriter(builder)) { 
     
    172100            } 
    173101            XElement body = XElement.Parse(builder.ToString()); 
    174             body.Add(SdbAction.GetAuthorizationElements(action)); 
     102            body.Add(GetAuthorizationElements(action)); 
    175103 
    176104            return new XElement(s + "Envelope", 
     
    180108                ); 
    181109        } 
     110 
     111        private static XElement[] GetAuthorizationElements(string action) { 
     112            string timestamp = GetFormattedTimestamp(); 
     113            return 
     114                new XElement[] {  
     115                    new XElement(aws + "AWSAccessKeyId", AWS_PUBLIC_KEY), 
     116                    new XElement(aws + "Timestamp", timestamp), 
     117                    GetSignature(action, timestamp) 
     118                }; 
     119 
     120        } 
     121 
     122        private static XElement GetSignature(string action, string timestamp) { 
     123            return new XElement(aws + "Signature", Sign(System.String.Format("{0}{1}", action, timestamp), AWS_PRIVATE_KEY)); 
     124        } 
     125 
     126        private static string Sign(string data, string key) { 
     127            Encoding encoding = new UTF8Encoding(); 
     128            HMACSHA1 signature = new HMACSHA1(encoding.GetBytes(key)); 
     129            return System.Convert.ToBase64String(signature.ComputeHash( 
     130                encoding.GetBytes(data.ToCharArray()))); 
     131        } 
     132 
     133        private static string GetFormattedTimestamp() { 
     134            System.DateTime dateTime = System.DateTime.Now; 
     135            return 
     136                new System.DateTime(dateTime.Year, dateTime.Month, dateTime.Day, 
     137                             dateTime.Hour, dateTime.Minute, dateTime.Second, 
     138                             dateTime.Millisecond, System.DateTimeKind.Local) 
     139                                .ToUniversalTime().ToString("yyyy-MM-dd\\THH:mm:ss.fff\\Z", CultureInfo.InvariantCulture); 
     140        } 
    182141    } 
    183142} 
  • trunk/nuxleus/Source/Nuxleus.Extension.Aws/SimpleDB/Task/CreateDomain.cs

    r4715 r4727  
    11using System; 
    22using System.Collections.Generic; 
    3 using System.Linq; 
    4 using System.Text; 
     3using System.Xml.Serialization; 
     4using EeekSoft.Asynchronous; 
     5using Nuxleus.Extension.AWS.SimpleDB.Model; 
    56 
    67namespace Nuxleus.Extension.AWS.SimpleDB { 
    7     class CreateDomain { 
     8 
     9    [XmlTypeAttribute(Namespace = "http://sdb.amazonaws.com/doc/2007-11-07/")] 
     10    [XmlRootAttribute(Namespace = "http://sdb.amazonaws.com/doc/2007-11-07/", IsNullable = false)] 
     11    public struct CreateDomain : ITask { 
     12 
     13        string m_domainName; 
     14        List<String> m_attributeNameArray; 
     15        static Guid m_taskID = new Guid(); 
     16        static IRequest m_request = new GetAttributesRequest(); 
     17 
     18        [XmlElementAttribute(ElementName = "DomainName")] 
     19        public string DomainName { 
     20            get { return m_domainName; } 
     21            set { m_domainName = value; } 
     22        } 
     23 
     24        public Guid TaskID { 
     25            get { return m_taskID; } 
     26        } 
     27 
     28        public IRequest Request { 
     29            get { 
     30                return m_request; 
     31            } 
     32        } 
     33 
     34        public IResponse Response { get; set; } 
     35 
     36        public IEnumerable<IAsync> Invoke<T>(Dictionary<IRequest, T> responseList) { 
     37            return SimpleDBService<CreateDomain>.CallWebService<T>(this, Request, responseList); 
     38        } 
    839    } 
    940} 
  • trunk/nuxleus/Source/Nuxleus.Extension.Aws/SimpleDB/Task/GetAttributes.cs

    r4715 r4727  
    11using System; 
    22using System.Collections.Generic; 
    3 using System.Linq; 
    4 using System.Text; 
     3using System.Xml.Serialization; 
     4using EeekSoft.Asynchronous; 
     5using Nuxleus.Extension.AWS.SimpleDB.Model; 
    56 
    67namespace Nuxleus.Extension.AWS.SimpleDB { 
    7     class GetAttributes { 
     8 
     9    [XmlTypeAttribute(Namespace = "http://sdb.amazonaws.com/doc/2007-11-07/")] 
     10    [XmlRootAttribute(Namespace = "http://sdb.amazonaws.com/doc/2007-11-07/", IsNullable = false)] 
     11    public struct GetAttributes : ITask { 
     12 
     13        string m_domainName; 
     14        string m_itemName; 
     15        List<String> m_attributeNameArray; 
     16        static Guid m_taskID = new Guid(); 
     17        static IRequest m_request = new GetAttributesRequest(); 
     18 
     19        [XmlElementAttribute(ElementName = "DomainName")] 
     20        public string DomainName { 
     21            get { return m_domainName; } 
     22            set { m_domainName = value; } 
     23        } 
     24 
     25        [XmlElementAttribute(ElementName = "ItemName")] 
     26        public string ItemName { 
     27            get { return m_itemName; } 
     28            set { m_itemName = value; } 
     29        } 
     30 
     31        [XmlElementAttribute(ElementName = "AttributeName")] 
     32        public List<String> AttributeName { 
     33            get { return m_attributeNameArray; } 
     34            set { m_attributeNameArray = value; } 
     35        } 
     36 
     37        public Guid TaskID { 
     38            get { return m_taskID; } 
     39        } 
     40 
     41        public IRequest Request { 
     42            get { 
     43                return m_request; 
     44            } 
     45        } 
     46 
     47        public IResponse Response { get; set; } 
     48 
     49        public IEnumerable<IAsync> Invoke<T>(Dictionary<IRequest, T> responseList) { 
     50            return SimpleDBService<GetAttributes>.CallWebService<T>(this, Request, responseList); 
     51        } 
    852    } 
    953} 
  • trunk/nuxleus/Source/Nuxleus.Extension.Aws/SimpleDB/Task/ITask.cs

    r4723 r4727  
    1010        Guid TaskID { get; } 
    1111        IRequest Request { get; } 
    12         //IResponse<T> Response { get; set; } 
     12        IResponse Response { get; set; } 
    1313        IEnumerable<IAsync> Invoke<T>(Dictionary<IRequest, T> responseList); 
    1414    } 
  • trunk/nuxleus/Source/Nuxleus.Extension.Aws/SimpleDB/Task/PutAttributes.cs

    r4723 r4727  
    11using System.Collections.Generic; 
    2 using System.Linq; 
    3 using System.Text; 
    42using System.Xml.Serialization; 
    53using EeekSoft.Asynchronous; 
    6 using System.Xml.Linq; 
    74using Nuxleus.Extension.AWS.SimpleDB.Model; 
    8 using System.Collections; 
    9 using Nuxleus.MetaData; 
    10 using System.Net; 
    11 using System.Xml; 
    12 using System.IO; 
    13 using System.Security.Cryptography; 
    14 using System.Globalization; 
    15 using System.Threading; 
     5 
    166 
    177namespace Nuxleus.Extension.AWS.SimpleDB { 
     
    2111    public struct PutAttributes : ITask { 
    2212 
    23         static XNamespace s = "http://schemas.xmlsoap.org/soap/envelope/"; 
    24  
    2513        string m_domainName; 
    2614        string m_itemName; 
     
    2816        static System.Guid m_taskID = new System.Guid(); 
    2917        static IRequest m_request = new PutAttributesRequest(); 
    30  
    31         static XNamespace aws = "http://sdb.amazonaws.com/doc/2007-11-07/"; 
    32         static XNamespace i = "http://www.w3.org/2001/XMLSchema-instance"; 
    3318 
    3419        [XmlElementAttribute(ElementName = "DomainName")] 
     
    5944            } 
    6045        } 
     46 
     47        public IResponse Response { get; set; } 
     48 
    6149        public IEnumerable<IAsync> Invoke<T>(Dictionary<IRequest, T> responseList) { 
    62             return SimpleDBService<PutAttributes>.CallWebService<PutAttributes,T>(this, Request, responseList); 
     50            return SimpleDBService<PutAttributes>.CallWebService<T>(this, Request, responseList); 
    6351        } 
    6452    } 
  • trunk/nuxleus/Source/Nuxleus.Extension.Aws/SimpleDB/Task/Query.cs

    r4723 r4727  
    11using System; 
    22using System.Collections.Generic; 
    3 using System.Linq; 
    4 using System.Text; 
    53using System.Xml.Serialization; 
    64using EeekSoft.Asynchronous; 
    7 using System.Xml.Linq; 
    85 
    96namespace Nuxleus.Extension.AWS.SimpleDB { 
     
    1714        string m_maxNumberOfItems; 
    1815        string m_nextToken; 
    19         static System.Guid m_taskID = new System.Guid(); 
     16        static Guid m_taskID = new Guid(); 
    2017 
    2118        [XmlElementAttribute(ElementName = "DomainName")] 
     
    3734        } 
    3835 
     36        [XmlElementAttribute(ElementName = "NextToken")] 
     37        public String NextToken { 
     38            get { return m_nextToken; } 
     39            set { m_nextToken = value; } 
     40        } 
     41 
    3942 
    4043        #region ITask Members 
     
    4952        } 
    5053 
    51         public System.Xml.Linq.XElement[] GetXMLBody { 
    52             get { throw new NotImplementedException(); } 
    53         } 
     54        public IResponse Response { get; set; } 
    5455 
    55         public System.Guid TaskID { 
     56        public Guid TaskID { 
    5657            get { return m_taskID; } 
    5758        }