Annotation of /trunk/AssetClient/AssetClient.cs
Parent Directory
|
Revision Log
Revision 47 - (view) (download)
| 1 : | jhurliman | 17 | /* |
| 2 : | * Copyright (c) 2008 Intel Corporation | ||
| 3 : | * All rights reserved. | ||
| 4 : | * Redistribution and use in source and binary forms, with or without | ||
| 5 : | * modification, are permitted provided that the following conditions | ||
| 6 : | * are met: | ||
| 7 : | * | ||
| 8 : | * -- Redistributions of source code must retain the above copyright | ||
| 9 : | * notice, this list of conditions and the following disclaimer. | ||
| 10 : | * -- Redistributions in binary form must reproduce the above copyright | ||
| 11 : | * notice, this list of conditions and the following disclaimer in the | ||
| 12 : | * documentation and/or other materials provided with the distribution. | ||
| 13 : | * -- Neither the name of the Intel Corporation nor the names of its | ||
| 14 : | * contributors may be used to endorse or promote products derived from | ||
| 15 : | * this software without specific prior written permission. | ||
| 16 : | * | ||
| 17 : | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| 18 : | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| 19 : | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | ||
| 20 : | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR ITS | ||
| 21 : | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
| 22 : | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
| 23 : | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
| 24 : | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
| 25 : | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
| 26 : | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
| 27 : | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 28 : | */ | ||
| 29 : | |||
| 30 : | jhurliman | 8 | using System; |
| 31 : | using System.Net; | ||
| 32 : | using System.IO; | ||
| 33 : | jhurliman | 47 | using System.Text; |
| 34 : | jhurliman | 20 | using System.Xml; |
| 35 : | jhurliman | 8 | using OpenMetaverse; |
| 36 : | jhurliman | 20 | using OpenMetaverse.StructuredData; |
| 37 : | jhurliman | 8 | |
| 38 : | namespace AssetClient | ||
| 39 : | { | ||
| 40 : | public static class AssetClient | ||
| 41 : | { | ||
| 42 : | jhurliman | 47 | public static bool TryGetAuthToken(Uri claimedIdentifier, string firstName, string lastName, string password, |
| 43 : | Uri serverUrl, out UUID authToken) | ||
| 44 : | jhurliman | 8 | { |
| 45 : | jhurliman | 47 | authToken = UUID.Zero; |
| 46 : | serverUrl = new Uri(serverUrl, "authenticate"); | ||
| 47 : | |||
| 48 : | OSDMap osd = new OSDMap(1); | ||
| 49 : | osd.Add("identifier", OSD.FromUri(claimedIdentifier)); | ||
| 50 : | byte[] postData = Encoding.UTF8.GetBytes(OSDParser.SerializeJson(osd).ToJson()); | ||
| 51 : | |||
| 52 : | try | ||
| 53 : | { | ||
| 54 : | HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serverUrl); | ||
| 55 : | request.Method = "POST"; | ||
| 56 : | request.ContentLength = postData.Length; | ||
| 57 : | request.ContentType = "application/json"; | ||
| 58 : | |||
| 59 : | using (Stream stream = request.GetRequestStream()) | ||
| 60 : | stream.Write(postData, 0, postData.Length); | ||
| 61 : | |||
| 62 : | HttpWebResponse response = (HttpWebResponse)request.GetResponse(); | ||
| 63 : | |||
| 64 : | if (response.ResponseUri.Authority == claimedIdentifier.Authority) | ||
| 65 : | { | ||
| 66 : | // This response came from the OpenID provider (user server). | ||
| 67 : | // Send back a form with our filled in data | ||
| 68 : | postData = Encoding.UTF8.GetBytes( | ||
| 69 : | String.Format("first={0}&last={1}&pass={2}", firstName, lastName, Utils.MD5(password))); | ||
| 70 : | |||
| 71 : | request = (HttpWebRequest)WebRequest.Create(response.ResponseUri); | ||
| 72 : | request.Method = "POST"; | ||
| 73 : | request.ContentLength = postData.Length; | ||
| 74 : | request.ContentType = "application/x-www-form-urlencoded"; | ||
| 75 : | |||
| 76 : | using (Stream stream = request.GetRequestStream()) | ||
| 77 : | stream.Write(postData, 0, postData.Length); | ||
| 78 : | |||
| 79 : | response = (HttpWebResponse)request.GetResponse(); | ||
| 80 : | |||
| 81 : | if (response.ResponseUri.Authority == serverUrl.Authority) | ||
| 82 : | { | ||
| 83 : | // This response came from the OpenID consumer (asset server) | ||
| 84 : | if (response.StatusCode == HttpStatusCode.OK) | ||
| 85 : | { | ||
| 86 : | using (Stream stream = response.GetResponseStream()) | ||
| 87 : | { | ||
| 88 : | OSDMap map = OSDParser.DeserializeJson(stream) as OSDMap; | ||
| 89 : | |||
| 90 : | if (map != null && map.ContainsKey("auth_token")) | ||
| 91 : | { | ||
| 92 : | authToken = map["auth_token"].AsUUID(); | ||
| 93 : | return true; | ||
| 94 : | } | ||
| 95 : | } | ||
| 96 : | } | ||
| 97 : | } | ||
| 98 : | else | ||
| 99 : | { | ||
| 100 : | } | ||
| 101 : | } | ||
| 102 : | } | ||
| 103 : | catch (WebException ex) | ||
| 104 : | { | ||
| 105 : | PrintWebException(ex); | ||
| 106 : | } | ||
| 107 : | |||
| 108 : | return false; | ||
| 109 : | } | ||
| 110 : | |||
| 111 : | public static bool TryGetAsset(UUID assetID, UUID authToken, Uri assetLocation, out byte[] assetData) | ||
| 112 : | { | ||
| 113 : | jhurliman | 20 | assetData = null; |
| 114 : | jhurliman | 8 | |
| 115 : | try | ||
| 116 : | { | ||
| 117 : | jhurliman | 47 | HttpWebRequest request = (HttpWebRequest)WebRequest.Create(assetLocation); |
| 118 : | AddAuthorizeHeader(request, authToken); | ||
| 119 : | jhurliman | 8 | HttpWebResponse response = (HttpWebResponse)request.GetResponse(); |
| 120 : | |||
| 121 : | if (response.StatusCode == HttpStatusCode.OK) | ||
| 122 : | { | ||
| 123 : | int contentLength = (int)response.ContentLength; | ||
| 124 : | jhurliman | 20 | assetData = new byte[contentLength]; |
| 125 : | |||
| 126 : | using (Stream stream = response.GetResponseStream()) | ||
| 127 : | jhurliman | 8 | { |
| 128 : | jhurliman | 20 | int pos = 0; |
| 129 : | while (pos < contentLength) | ||
| 130 : | pos += stream.Read(assetData, pos, contentLength - pos); | ||
| 131 : | jhurliman | 8 | } |
| 132 : | jhurliman | 20 | |
| 133 : | return true; | ||
| 134 : | jhurliman | 8 | } |
| 135 : | } | ||
| 136 : | jhurliman | 20 | catch (WebException ex) |
| 137 : | jhurliman | 8 | { |
| 138 : | jhurliman | 21 | if (ex.Response == null || ((HttpWebResponse)ex.Response).StatusCode != HttpStatusCode.NotFound) |
| 139 : | jhurliman | 47 | PrintWebException(ex); |
| 140 : | jhurliman | 8 | } |
| 141 : | |||
| 142 : | return false; | ||
| 143 : | } | ||
| 144 : | |||
| 145 : | jhurliman | 47 | public static bool TryGetMetadata(UUID assetID, UUID authToken, Uri serverUrl, out byte[] metadata) |
| 146 : | jhurliman | 8 | { |
| 147 : | metadata = null; | ||
| 148 : | jhurliman | 47 | serverUrl = new Uri(serverUrl, String.Format("{0}/metadata", assetID)); |
| 149 : | jhurliman | 8 | |
| 150 : | try | ||
| 151 : | { | ||
| 152 : | jhurliman | 47 | HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serverUrl); |
| 153 : | AddAuthorizeHeader(request, authToken); | ||
| 154 : | jhurliman | 8 | HttpWebResponse response = (HttpWebResponse)request.GetResponse(); |
| 155 : | |||
| 156 : | if (response.StatusCode == HttpStatusCode.OK) | ||
| 157 : | { | ||
| 158 : | int contentLength = (int)response.ContentLength; | ||
| 159 : | metadata = new byte[contentLength]; | ||
| 160 : | |||
| 161 : | jhurliman | 20 | using (Stream stream = response.GetResponseStream()) |
| 162 : | { | ||
| 163 : | int pos = 0; | ||
| 164 : | while (pos < contentLength) | ||
| 165 : | pos += stream.Read(metadata, pos, contentLength - pos); | ||
| 166 : | } | ||
| 167 : | jhurliman | 8 | |
| 168 : | return true; | ||
| 169 : | } | ||
| 170 : | } | ||
| 171 : | jhurliman | 21 | catch (WebException ex) |
| 172 : | jhurliman | 8 | { |
| 173 : | jhurliman | 21 | if (ex.Response == null || ((HttpWebResponse)ex.Response).StatusCode != HttpStatusCode.NotFound) |
| 174 : | jhurliman | 47 | PrintWebException(ex); |
| 175 : | jhurliman | 8 | } |
| 176 : | |||
| 177 : | return false; | ||
| 178 : | } | ||
| 179 : | jhurliman | 10 | |
| 180 : | jhurliman | 47 | public static UUID CreateAsset(string name, string description, string contentType, bool temporary, |
| 181 : | UUID authToken, Uri serverUrl, byte[] assetData, UUID assetID) | ||
| 182 : | jhurliman | 10 | { |
| 183 : | jhurliman | 47 | string requestURL = String.Format("{0}{1}", serverUrl, "createasset"); |
| 184 : | jhurliman | 20 | |
| 185 : | jhurliman | 47 | OSDMap osd = new OSDMap(); |
| 186 : | jhurliman | 20 | |
| 187 : | jhurliman | 47 | if (assetID != UUID.Zero) osd["id"] = OSD.FromUUID(assetID); |
| 188 : | osd["name"] = OSD.FromString(name); | ||
| 189 : | osd["description"] = OSD.FromString(description); | ||
| 190 : | osd["type"] = OSD.FromString(contentType); | ||
| 191 : | osd["temporary"] = OSD.FromBoolean(temporary); | ||
| 192 : | osd["data"] = OSD.FromBinary(assetData); | ||
| 193 : | jhurliman | 20 | |
| 194 : | jhurliman | 47 | byte[] postData = Encoding.UTF8.GetBytes(OSDParser.SerializeJson(osd).ToJson()); |
| 195 : | jhurliman | 20 | |
| 196 : | try | ||
| 197 : | { | ||
| 198 : | HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestURL); | ||
| 199 : | jhurliman | 47 | AddAuthorizeHeader(request, authToken); |
| 200 : | jhurliman | 20 | request.Method = "POST"; |
| 201 : | request.ContentLength = postData.Length; | ||
| 202 : | jhurliman | 47 | request.ContentType = "application/json"; |
| 203 : | jhurliman | 20 | |
| 204 : | using (Stream stream = request.GetRequestStream()) | ||
| 205 : | { | ||
| 206 : | stream.Write(postData, 0, postData.Length); | ||
| 207 : | stream.Flush(); | ||
| 208 : | } | ||
| 209 : | |||
| 210 : | HttpWebResponse response = (HttpWebResponse)request.GetResponse(); | ||
| 211 : | |||
| 212 : | if (response.StatusCode == HttpStatusCode.Created) | ||
| 213 : | { | ||
| 214 : | assetID = UUID.Zero; | ||
| 215 : | |||
| 216 : | using (Stream stream = response.GetResponseStream()) | ||
| 217 : | { | ||
| 218 : | jhurliman | 40 | OSD osdata = OSDParser.DeserializeJson(stream); |
| 219 : | jhurliman | 20 | if (osdata.Type == OSDType.Map) |
| 220 : | { | ||
| 221 : | OSDMap responseMap = (OSDMap)osdata; | ||
| 222 : | jhurliman | 23 | if (responseMap.ContainsKey("id")) |
| 223 : | assetID = responseMap["id"].AsUUID(); | ||
| 224 : | jhurliman | 20 | } |
| 225 : | } | ||
| 226 : | |||
| 227 : | response.Close(); | ||
| 228 : | return assetID; | ||
| 229 : | } | ||
| 230 : | else | ||
| 231 : | { | ||
| 232 : | response.Close(); | ||
| 233 : | return UUID.Zero; | ||
| 234 : | } | ||
| 235 : | } | ||
| 236 : | catch (WebException ex) | ||
| 237 : | { | ||
| 238 : | jhurliman | 47 | PrintWebException(ex); |
| 239 : | jhurliman | 20 | } |
| 240 : | |||
| 241 : | jhurliman | 10 | return UUID.Zero; |
| 242 : | } | ||
| 243 : | jhurliman | 47 | |
| 244 : | static void AddAuthorizeHeader(HttpWebRequest request, UUID authToken) | ||
| 245 : | { | ||
| 246 : | if (authToken != UUID.Zero) | ||
| 247 : | request.Headers.Add(HttpRequestHeader.Authorization, String.Format("OpenGrid {0}", authToken)); | ||
| 248 : | } | ||
| 249 : | |||
| 250 : | static void PrintWebException(WebException ex) | ||
| 251 : | { | ||
| 252 : | if (ex.Response != null) | ||
| 253 : | { | ||
| 254 : | HttpWebResponse response = (HttpWebResponse)ex.Response; | ||
| 255 : | |||
| 256 : | if (!String.IsNullOrEmpty(response.StatusDescription)) | ||
| 257 : | { | ||
| 258 : | Console.WriteLine("[ERROR] The remote server returned an error: " + response.StatusDescription); | ||
| 259 : | return; | ||
| 260 : | } | ||
| 261 : | } | ||
| 262 : | |||
| 263 : | Console.WriteLine("[ERROR] " + ex.Message); | ||
| 264 : | } | ||
| 265 : | jhurliman | 8 | } |
| 266 : | } |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

