View of /trunk/AssetClient/AssetClient.cs
Parent Directory
|
Revision Log
Revision 41 -
(download)
(annotate)
Tue Dec 2 18:00:11 2008 UTC (4 years, 5 months ago) by jhurliman
File size: 7728 byte(s)
Tue Dec 2 18:00:11 2008 UTC (4 years, 5 months ago) by jhurliman
File size: 7728 byte(s)
* Better parsing of OpenSim asset requests (the ?texture parameter was not being stripped off) * Converted AssetClient to use the file extension to content-type map in AssetServer.Utils (AssetClient now has a reference to AssetServer for this function) * More file extension to content-type mappings
/*
* Copyright (c) 2008 Intel Corporation
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* -- Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* -- Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* -- Neither the name of the Intel Corporation nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR ITS
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Net;
using System.IO;
using System.Xml;
using OpenMetaverse;
using OpenMetaverse.StructuredData;
namespace AssetClient
{
public static class AssetClient
{
public const string ASSET_SERVER_URL = "http://localhost:9001";
public static bool TryGetAsset(UUID assetID, string assetURL, out byte[] assetData)
{
assetData = null;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(assetURL);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
int contentLength = (int)response.ContentLength;
assetData = new byte[contentLength];
using (Stream stream = response.GetResponseStream())
{
int pos = 0;
while (pos < contentLength)
pos += stream.Read(assetData, pos, contentLength - pos);
}
return true;
}
}
catch (WebException ex)
{
if (ex.Response == null || ((HttpWebResponse)ex.Response).StatusCode != HttpStatusCode.NotFound)
Console.WriteLine("[ERROR] " + ex.Message);
}
return false;
}
public static bool TryGetMetadata(UUID assetID, out byte[] metadata)
{
return TryGetMetadata(assetID, ASSET_SERVER_URL, out metadata);
}
public static bool TryGetMetadata(UUID assetID, string assetServerURL, out byte[] metadata)
{
metadata = null;
assetServerURL = assetServerURL.TrimEnd('/');
string requestURL = String.Format("{0}/{1}/metadata", assetServerURL, assetID);
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestURL);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
int contentLength = (int)response.ContentLength;
metadata = new byte[contentLength];
using (Stream stream = response.GetResponseStream())
{
int pos = 0;
while (pos < contentLength)
pos += stream.Read(metadata, pos, contentLength - pos);
}
return true;
}
}
catch (WebException ex)
{
if (ex.Response == null || ((HttpWebResponse)ex.Response).StatusCode != HttpStatusCode.NotFound)
Console.WriteLine("[ERROR] " + ex.Message);
}
return false;
}
public static UUID CreateAsset(string name, string description, string contentType, bool temporary, byte[] assetData)
{
return CreateAsset(name, description, contentType, temporary, assetData, UUID.Zero, ASSET_SERVER_URL);
}
public static UUID CreateAsset(string name, string description, string contentType, bool temporary, byte[] assetData,
UUID assetID)
{
return CreateAsset(name, description, contentType, temporary, assetData, assetID, ASSET_SERVER_URL);
}
public static UUID CreateAsset(string name, string description, string contentType, bool temporary, byte[] assetData,
UUID assetID, string assetServerURL)
{
assetServerURL = assetServerURL.TrimEnd('/');
string requestURL = assetServerURL + "/createasset";
OSDMap map = new OSDMap();
if (assetID != UUID.Zero) map["id"] = OSD.FromUUID(assetID);
map["name"] = OSD.FromString(name);
map["description"] = OSD.FromString(description);
map["type"] = OSD.FromString(contentType);
map["temporary"] = OSD.FromBoolean(temporary);
map["data"] = OSD.FromBinary(assetData);
LitJson.JsonData jsonData = OSDParser.SerializeJson(map);
string jsonString = jsonData.ToJson();
byte[] postData = System.Text.Encoding.UTF8.GetBytes(jsonString);
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestURL);
request.Method = "POST";
request.ContentLength = postData.Length;
using (Stream stream = request.GetRequestStream())
{
stream.Write(postData, 0, postData.Length);
stream.Flush();
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.Created)
{
assetID = UUID.Zero;
using (Stream stream = response.GetResponseStream())
{
OSD osdata = OSDParser.DeserializeJson(stream);
if (osdata.Type == OSDType.Map)
{
OSDMap responseMap = (OSDMap)osdata;
if (responseMap.ContainsKey("id"))
assetID = responseMap["id"].AsUUID();
}
}
response.Close();
return assetID;
}
else
{
response.Close();
return UUID.Zero;
}
}
catch (WebException ex)
{
Console.WriteLine("[ERROR] " + ex.Message);
}
return UUID.Zero;
}
}
}
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

