diff options
Diffstat (limited to 'OpenSim/Grid/AssetInventoryServer/Plugins/BrowseFrontendPlugin.cs')
-rw-r--r-- | OpenSim/Grid/AssetInventoryServer/Plugins/BrowseFrontendPlugin.cs | 151 |
1 files changed, 151 insertions, 0 deletions
diff --git a/OpenSim/Grid/AssetInventoryServer/Plugins/BrowseFrontendPlugin.cs b/OpenSim/Grid/AssetInventoryServer/Plugins/BrowseFrontendPlugin.cs new file mode 100644 index 0000000..f445b97 --- /dev/null +++ b/OpenSim/Grid/AssetInventoryServer/Plugins/BrowseFrontendPlugin.cs | |||
@@ -0,0 +1,151 @@ | |||
1 | /* | ||
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 | using System; | ||
31 | using System.Collections.Generic; | ||
32 | using System.Collections.Specialized; | ||
33 | using System.Net; | ||
34 | using System.Text; | ||
35 | using System.Web; | ||
36 | using OpenMetaverse; | ||
37 | using HttpServer; | ||
38 | using OpenSim.Framework; | ||
39 | |||
40 | namespace OpenSim.Grid.AssetInventoryServer.Plugins | ||
41 | { | ||
42 | public class BrowseFrontendPlugin : IAssetInventoryServerPlugin | ||
43 | { | ||
44 | AssetInventoryServer server; | ||
45 | |||
46 | public BrowseFrontendPlugin() | ||
47 | { | ||
48 | } | ||
49 | |||
50 | #region IPlugin implementation | ||
51 | |||
52 | public void Initialise(AssetInventoryServer server) | ||
53 | { | ||
54 | this.server = server; | ||
55 | |||
56 | // Request for / or /?... | ||
57 | server.HttpServer.AddHandler("get", null, @"(^/$)|(^/\?.*)", BrowseRequestHandler); | ||
58 | |||
59 | Logger.Log.Info("[ASSET] Browser Frontend loaded."); | ||
60 | } | ||
61 | |||
62 | /// <summary> | ||
63 | /// <para>Initialises asset interface</para> | ||
64 | /// </summary> | ||
65 | public void Initialise() | ||
66 | { | ||
67 | Logger.Log.InfoFormat("[ASSET]: {0} cannot be default-initialized!", Name); | ||
68 | throw new PluginNotInitialisedException(Name); | ||
69 | } | ||
70 | |||
71 | public void Dispose() | ||
72 | { | ||
73 | } | ||
74 | |||
75 | public string Version | ||
76 | { | ||
77 | // TODO: this should be something meaningful and not hardcoded? | ||
78 | get { return "0.1"; } | ||
79 | } | ||
80 | |||
81 | public string Name | ||
82 | { | ||
83 | get { return "AssetInventoryServer Browse asset frontend"; } | ||
84 | } | ||
85 | |||
86 | #endregion IPlugin implementation | ||
87 | |||
88 | bool BrowseRequestHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response) | ||
89 | { | ||
90 | const int ASSETS_PER_PAGE = 25; | ||
91 | const string HEADER = "<html><head><title>Asset Server</title></head><body>"; | ||
92 | const string TABLE_HEADER = | ||
93 | "<table><tr><th>Name</th><th>Description</th><th>Type</th><th>ID</th><th>Temporary</th><th>SHA-1</th></tr>"; | ||
94 | const string TABLE_FOOTER = "</table>"; | ||
95 | const string FOOTER = "</body></html>"; | ||
96 | |||
97 | UUID authToken = Utils.GetAuthToken(request); | ||
98 | |||
99 | StringBuilder html = new StringBuilder(); | ||
100 | int start = 0; | ||
101 | uint page = 0; | ||
102 | |||
103 | if (!String.IsNullOrEmpty(request.Uri.Query)) | ||
104 | { | ||
105 | NameValueCollection query = HttpUtility.ParseQueryString(request.Uri.Query); | ||
106 | if (!String.IsNullOrEmpty(query["page"]) && UInt32.TryParse(query["page"], out page)) | ||
107 | start = (int)page * ASSETS_PER_PAGE; | ||
108 | } | ||
109 | |||
110 | html.AppendLine(HEADER); | ||
111 | |||
112 | html.AppendLine("<p>"); | ||
113 | if (page > 0) | ||
114 | html.AppendFormat("<a href=\"{0}?page={1}\">< Previous Page</a> | ", request.Uri.AbsolutePath, page - 1); | ||
115 | html.AppendFormat("<a href=\"{0}?page={1}\">Next Page ></a>", request.Uri.AbsolutePath, page + 1); | ||
116 | html.AppendLine("</p>"); | ||
117 | |||
118 | html.AppendLine(TABLE_HEADER); | ||
119 | |||
120 | server.StorageProvider.ForEach( | ||
121 | delegate(Metadata data) | ||
122 | { | ||
123 | if (server.AuthorizationProvider.IsMetadataAuthorized(authToken, data.ID)) | ||
124 | { | ||
125 | html.AppendLine(String.Format( | ||
126 | "<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td></tr>", | ||
127 | data.Name, data.Description, data.ContentType, data.ID, data.Temporary, | ||
128 | BitConverter.ToString(data.SHA1).Replace("-", String.Empty))); | ||
129 | } | ||
130 | else | ||
131 | { | ||
132 | html.AppendLine(String.Format( | ||
133 | "<tr><td>[Protected Asset]</td><td> </td><td> </td><td>{0}</td><td>{1}</td><td> </td></tr>", | ||
134 | data.ID, data.Temporary)); | ||
135 | } | ||
136 | }, start, ASSETS_PER_PAGE | ||
137 | ); | ||
138 | |||
139 | html.AppendLine(TABLE_FOOTER); | ||
140 | |||
141 | html.AppendLine(FOOTER); | ||
142 | |||
143 | byte[] responseData = System.Text.Encoding.UTF8.GetBytes(html.ToString()); | ||
144 | |||
145 | response.Status = HttpStatusCode.OK; | ||
146 | response.Body.Write(responseData, 0, responseData.Length); | ||
147 | response.Body.Flush(); | ||
148 | return true; | ||
149 | } | ||
150 | } | ||
151 | } | ||