Annotation of /trunk/AssetClient/AssetClient.cs
Parent Directory
|
Revision Log
Revision 41 - (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 | 20 | using System.Xml; |
| 34 : | jhurliman | 8 | using OpenMetaverse; |
| 35 : | jhurliman | 20 | using OpenMetaverse.StructuredData; |
| 36 : | jhurliman | 8 | |
| 37 : | namespace AssetClient | ||
| 38 : | { | ||
| 39 : | public static class AssetClient | ||
| 40 : | { | ||
| 41 : | jhurliman | 20 | public const string ASSET_SERVER_URL = "http://localhost:9001"; |
| 42 : | jhurliman | 8 | |
| 43 : | jhurliman | 20 | public static bool TryGetAsset(UUID assetID, string assetURL, out byte[] assetData) |
| 44 : | jhurliman | 8 | { |
| 45 : | jhurliman | 20 | assetData = null; |
| 46 : | jhurliman | 8 | |
| 47 : | try | ||
| 48 : | { | ||
| 49 : | jhurliman | 11 | HttpWebRequest request = (HttpWebRequest)WebRequest.Create(assetURL); |
| 50 : | jhurliman | 8 | HttpWebResponse response = (HttpWebResponse)request.GetResponse(); |
| 51 : | |||
| 52 : | if (response.StatusCode == HttpStatusCode.OK) | ||
| 53 : | { | ||
| 54 : | int contentLength = (int)response.ContentLength; | ||
| 55 : | jhurliman | 20 | assetData = new byte[contentLength]; |
| 56 : | |||
| 57 : | using (Stream stream = response.GetResponseStream()) | ||
| 58 : | jhurliman | 8 | { |
| 59 : | jhurliman | 20 | int pos = 0; |
| 60 : | while (pos < contentLength) | ||
| 61 : | pos += stream.Read(assetData, pos, contentLength - pos); | ||
| 62 : | jhurliman | 8 | } |
| 63 : | jhurliman | 20 | |
| 64 : | return true; | ||
| 65 : | jhurliman | 8 | } |
| 66 : | } | ||
| 67 : | jhurliman | 20 | catch (WebException ex) |
| 68 : | jhurliman | 8 | { |
| 69 : | jhurliman | 21 | if (ex.Response == null || ((HttpWebResponse)ex.Response).StatusCode != HttpStatusCode.NotFound) |
| 70 : | jhurliman | 29 | Console.WriteLine("[ERROR] " + ex.Message); |
| 71 : | jhurliman | 8 | } |
| 72 : | |||
| 73 : | return false; | ||
| 74 : | } | ||
| 75 : | |||
| 76 : | public static bool TryGetMetadata(UUID assetID, out byte[] metadata) | ||
| 77 : | { | ||
| 78 : | return TryGetMetadata(assetID, ASSET_SERVER_URL, out metadata); | ||
| 79 : | } | ||
| 80 : | |||
| 81 : | public static bool TryGetMetadata(UUID assetID, string assetServerURL, out byte[] metadata) | ||
| 82 : | { | ||
| 83 : | metadata = null; | ||
| 84 : | assetServerURL = assetServerURL.TrimEnd('/'); | ||
| 85 : | string requestURL = String.Format("{0}/{1}/metadata", assetServerURL, assetID); | ||
| 86 : | |||
| 87 : | try | ||
| 88 : | { | ||
| 89 : | HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestURL); | ||
| 90 : | HttpWebResponse response = (HttpWebResponse)request.GetResponse(); | ||
| 91 : | |||
| 92 : | if (response.StatusCode == HttpStatusCode.OK) | ||
| 93 : | { | ||
| 94 : | int contentLength = (int)response.ContentLength; | ||
| 95 : | metadata = new byte[contentLength]; | ||
| 96 : | |||
| 97 : | jhurliman | 20 | using (Stream stream = response.GetResponseStream()) |
| 98 : | { | ||
| 99 : | int pos = 0; | ||
| 100 : | while (pos < contentLength) | ||
| 101 : | pos += stream.Read(metadata, pos, contentLength - pos); | ||
| 102 : | } | ||
| 103 : | jhurliman | 8 | |
| 104 : | return true; | ||
| 105 : | } | ||
| 106 : | } | ||
| 107 : | jhurliman | 21 | catch (WebException ex) |
| 108 : | jhurliman | 8 | { |
| 109 : | jhurliman | 21 | if (ex.Response == null || ((HttpWebResponse)ex.Response).StatusCode != HttpStatusCode.NotFound) |
| 110 : | jhurliman | 29 | Console.WriteLine("[ERROR] " + ex.Message); |
| 111 : | jhurliman | 8 | } |
| 112 : | |||
| 113 : | return false; | ||
| 114 : | } | ||
| 115 : | jhurliman | 10 | |
| 116 : | jhurliman | 29 | public static UUID CreateAsset(string name, string description, string contentType, bool temporary, byte[] assetData) |
| 117 : | jhurliman | 10 | { |
| 118 : | jhurliman | 29 | return CreateAsset(name, description, contentType, temporary, assetData, UUID.Zero, ASSET_SERVER_URL); |
| 119 : | jhurliman | 20 | } |
| 120 : | |||
| 121 : | jhurliman | 29 | public static UUID CreateAsset(string name, string description, string contentType, bool temporary, byte[] assetData, |
| 122 : | jhurliman | 20 | UUID assetID) |
| 123 : | { | ||
| 124 : | jhurliman | 29 | return CreateAsset(name, description, contentType, temporary, assetData, assetID, ASSET_SERVER_URL); |
| 125 : | jhurliman | 20 | } |
| 126 : | |||
| 127 : | jhurliman | 29 | public static UUID CreateAsset(string name, string description, string contentType, bool temporary, byte[] assetData, |
| 128 : | jhurliman | 20 | UUID assetID, string assetServerURL) |
| 129 : | { | ||
| 130 : | assetServerURL = assetServerURL.TrimEnd('/'); | ||
| 131 : | string requestURL = assetServerURL + "/createasset"; | ||
| 132 : | |||
| 133 : | OSDMap map = new OSDMap(); | ||
| 134 : | |||
| 135 : | jhurliman | 25 | if (assetID != UUID.Zero) map["id"] = OSD.FromUUID(assetID); |
| 136 : | jhurliman | 20 | map["name"] = OSD.FromString(name); |
| 137 : | map["description"] = OSD.FromString(description); | ||
| 138 : | jhurliman | 29 | map["type"] = OSD.FromString(contentType); |
| 139 : | jhurliman | 20 | map["temporary"] = OSD.FromBoolean(temporary); |
| 140 : | map["data"] = OSD.FromBinary(assetData); | ||
| 141 : | |||
| 142 : | jhurliman | 40 | LitJson.JsonData jsonData = OSDParser.SerializeJson(map); |
| 143 : | jhurliman | 41 | string jsonString = jsonData.ToJson(); |
| 144 : | byte[] postData = System.Text.Encoding.UTF8.GetBytes(jsonString); | ||
| 145 : | jhurliman | 20 | |
| 146 : | try | ||
| 147 : | { | ||
| 148 : | HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestURL); | ||
| 149 : | request.Method = "POST"; | ||
| 150 : | request.ContentLength = postData.Length; | ||
| 151 : | |||
| 152 : | using (Stream stream = request.GetRequestStream()) | ||
| 153 : | { | ||
| 154 : | stream.Write(postData, 0, postData.Length); | ||
| 155 : | stream.Flush(); | ||
| 156 : | } | ||
| 157 : | |||
| 158 : | HttpWebResponse response = (HttpWebResponse)request.GetResponse(); | ||
| 159 : | |||
| 160 : | if (response.StatusCode == HttpStatusCode.Created) | ||
| 161 : | { | ||
| 162 : | assetID = UUID.Zero; | ||
| 163 : | |||
| 164 : | using (Stream stream = response.GetResponseStream()) | ||
| 165 : | { | ||
| 166 : | jhurliman | 40 | OSD osdata = OSDParser.DeserializeJson(stream); |
| 167 : | jhurliman | 20 | if (osdata.Type == OSDType.Map) |
| 168 : | { | ||
| 169 : | OSDMap responseMap = (OSDMap)osdata; | ||
| 170 : | jhurliman | 23 | if (responseMap.ContainsKey("id")) |
| 171 : | assetID = responseMap["id"].AsUUID(); | ||
| 172 : | jhurliman | 20 | } |
| 173 : | } | ||
| 174 : | |||
| 175 : | response.Close(); | ||
| 176 : | return assetID; | ||
| 177 : | } | ||
| 178 : | else | ||
| 179 : | { | ||
| 180 : | response.Close(); | ||
| 181 : | return UUID.Zero; | ||
| 182 : | } | ||
| 183 : | } | ||
| 184 : | catch (WebException ex) | ||
| 185 : | { | ||
| 186 : | jhurliman | 29 | Console.WriteLine("[ERROR] " + ex.Message); |
| 187 : | jhurliman | 20 | } |
| 188 : | |||
| 189 : | jhurliman | 10 | return UUID.Zero; |
| 190 : | } | ||
| 191 : | jhurliman | 8 | } |
| 192 : | } |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

