aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/ApplicationPlugins/Rest/Inventory/RestAppearanceServices.cs
blob: c375ade5bf35203e9fe3de5f8a4307df49c7c12a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
/*
 * Copyright (c) Contributors, http://opensimulator.org/
 * See CONTRIBUTORS.TXT for a full list of copyright holders.
 *
 * 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 OpenSim Project 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 DEVELOPERS ``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 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.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Xml;
using System.Drawing;
using OpenSim.Framework;
using OpenSim.Framework.Servers;
using OpenSim.Framework.Communications;
using OpenSim.Framework.Communications.Cache;
using OpenMetaverse;
using OpenMetaverse.Imaging;
using Nini.Config;

namespace OpenSim.ApplicationPlugins.Rest.Inventory
{

    public class RestAppearanceServices : IRest
    {

        private static readonly int PARM_USERID = 0;

        // private static readonly int PARM_PATH   = 1;

        private bool       enabled = false;
        private string     qPrefix = "appearance";

        /// <summary>
        /// The constructor makes sure that the service prefix is absolute
        /// and the registers the service handler and the allocator.
        /// </summary>

        public RestAppearanceServices()
        {
            Rest.Log.InfoFormat("{0} User appearance services initializing", MsgId);
            Rest.Log.InfoFormat("{0} Using REST Implementation Version {1}", MsgId, Rest.Version);

            // If a relative path was specified for the handler's domain,
            // add the standard prefix to make it absolute, e.g. /admin

            if (!qPrefix.StartsWith(Rest.UrlPathSeparator))
            {
                Rest.Log.InfoFormat("{0} Domain is relative, adding absolute prefix", MsgId);
                qPrefix = String.Format("{0}{1}{2}", Rest.Prefix, Rest.UrlPathSeparator, qPrefix);
                Rest.Log.InfoFormat("{0} Domain is now <{1}>", MsgId, qPrefix);
            }

            // Register interface using the absolute URI.

            Rest.Plugin.AddPathHandler(DoAppearance,qPrefix,Allocate);

            // Activate if everything went OK

            enabled = true;

            Rest.Log.InfoFormat("{0} User appearance services initialization complete", MsgId);
        }

        /// <summary>
        /// Post-construction, pre-enabled initialization opportunity
        /// Not currently exploited.
        /// </summary>

        public void Initialize()
        {
        }

        /// <summary>
        /// Called by the plug-in to halt service processing. Local processing is
        /// disabled.
        /// </summary>

        public void Close()
        {
            enabled = false;
            Rest.Log.InfoFormat("{0} User appearance services closing down", MsgId);
        }

        /// <summary>
        /// This property is declared locally because it is used a lot and
        /// brevity is nice.
        /// </summary>

        internal string MsgId
        {
            get { return Rest.MsgId; }
        }

        #region Interface

        /// <summary>
        /// The plugin (RestHandler) calls this method to allocate the request
        /// state carrier for a new request. It is destroyed when the request
        /// completes. All request-instance specific state is kept here. This
        /// is registered when this service provider is registered.
        /// </summary>
        /// <param name=request>Inbound HTTP request information</param>
        /// <param name=response>Outbound HTTP request information</param>
        /// <param name=qPrefix>REST service domain prefix</param>
        /// <returns>A RequestData instance suitable for this service</returns>

        private RequestData Allocate(OSHttpRequest request, OSHttpResponse response, string prefix)
        {
            return (RequestData) new AppearanceRequestData(request, response, prefix);
        }

        /// <summary>
        /// This method is registered with the handler when this service provider
        /// is initialized. It is called whenever the plug-in identifies this service
        /// provider as the best match for a given request.
        /// It handles all aspects of inventory REST processing, i.e. /admin/inventory
        /// </summary>
        /// <param name=hdata>A consolidated HTTP request work area</param>

        private void DoAppearance(RequestData hdata)
        {

            AppearanceRequestData rdata = (AppearanceRequestData) hdata;

            Rest.Log.DebugFormat("{0} DoAppearance ENTRY", MsgId);

            // If we're disabled, do nothing.

            if (!enabled)
            {
                return;
            }

            // Now that we know this is a serious attempt to
            // access inventory data, we should find out who
            // is asking, and make sure they are authorized
            // to do so. We need to validate the caller's
            // identity before revealing anything about the
            // status quo. Authenticate throws an exception
            // via Fail if no identity information is present.
            //
            // With the present HTTP server we can't use the
            // builtin authentication mechanisms because they
            // would be enforced for all in-bound requests.
            // Instead we look at the headers ourselves and
            // handle authentication directly.

            try
            {
                // digest scheme seems borked: disable it for the time
                // being
                rdata.scheme = Rest.AS_BASIC;
                if (!rdata.IsAuthenticated)
                {
                    rdata.Fail(Rest.HttpStatusCodeNotAuthorized,String.Format("user \"{0}\" could not be authenticated", rdata.userName));
                }
            }
            catch (RestException e)
            {
                if (e.statusCode == Rest.HttpStatusCodeNotAuthorized)
                {
                    Rest.Log.WarnFormat("{0} User not authenticated", MsgId);
                    Rest.Log.DebugFormat("{0} Authorization header: {1}", MsgId, rdata.request.Headers.Get("Authorization"));
                }
                else
                {
                    Rest.Log.ErrorFormat("{0} User authentication failed", MsgId);
                    Rest.Log.DebugFormat("{0} Authorization header: {1}", MsgId, rdata.request.Headers.Get("Authorization"));
                }
                throw (e);
            }

            Rest.Log.DebugFormat("{0} Authenticated {1}", MsgId, rdata.userName);

            // We can only get here if we are authorized
            //
            // The requestor may have specified an UUID or
            // a conjoined FirstName LastName string. We'll
            // try both. If we fail with the first, UUID,
            // attempt, we try the other. As an example, the
            // URI for a valid inventory request might be:
            //
            // http://<host>:<port>/admin/inventory/Arthur Dent
            //
            // Indicating that this is an inventory request for
            // an avatar named Arthur Dent. This is ALL that is
            // required to designate a GET for an entire
            // inventory.
            //

            // Do we have at least a user agent name?

            if (rdata.Parameters.Length < 1)
            {
                Rest.Log.WarnFormat("{0} Appearance: No user agent identifier specified", MsgId);
                rdata.Fail(Rest.HttpStatusCodeBadRequest, "no user identity specified");
            }

            // The first parameter MUST be the agent identification, either an UUID
            // or a space-separated First-name Last-Name specification. We check for
            // an UUID first, if anyone names their character using a valid UUID
            // that identifies another existing avatar will cause this a problem...

            try
            {
                rdata.uuid = new UUID(rdata.Parameters[PARM_USERID]);
                Rest.Log.DebugFormat("{0} UUID supplied", MsgId);
                rdata.userProfile = Rest.UserServices.GetUserProfile(rdata.uuid);
            }
            catch
            {
                string[] names = rdata.Parameters[PARM_USERID].Split(Rest.CA_SPACE);
                if (names.Length == 2)
                {
                    Rest.Log.DebugFormat("{0} Agent Name supplied [2]", MsgId);
                    rdata.userProfile = Rest.UserServices.GetUserProfile(names[0],names[1]);
                }
                else
                {
                    Rest.Log.WarnFormat("{0} A Valid UUID or both first and last names must be specified", MsgId);
                    rdata.Fail(Rest.HttpStatusCodeBadRequest, "invalid user identity");
                }
            }

            // If the user profile is null then either the server is broken, or the
            // user is not known. We always assume the latter case.

            if (rdata.userProfile != null)
            {
                Rest.Log.DebugFormat("{0} User profile obtained for agent {1} {2}",
                                     MsgId, rdata.userProfile.FirstName, rdata.userProfile.SurName);
            }
            else
            {
                Rest.Log.WarnFormat("{0} No user profile for {1}", MsgId, rdata.path);
                rdata.Fail(Rest.HttpStatusCodeNotFound, "unrecognized user identity");
            }

            // If we get to here, then we have effectively validated the user's

            switch (rdata.method)
            {
                case Rest.HEAD   : // Do the processing, set the status code, suppress entity
                    DoGet(rdata);
                    rdata.buffer = null;
                    break;

                case Rest.GET    : // Do the processing, set the status code, return entity
                    DoGet(rdata);
                    break;

                case Rest.PUT    : // Update named element
                    DoUpdate(rdata);
                    break;

                case Rest.POST   : // Add new information to identified context.
                    DoExtend(rdata);
                    break;

                case Rest.DELETE : // Delete information
                    DoDelete(rdata);
                    break;

                default :
                    Rest.Log.WarnFormat("{0} Method {1} not supported for {2}",
                                         MsgId, rdata.method, rdata.path);
                    rdata.Fail(Rest.HttpStatusCodeMethodNotAllowed, 
                               String.Format("{0} not supported", rdata.method));
                    break;
            }
        }

        #endregion Interface

        #region method-specific processing

        /// <summary>
        /// This method implements GET processing for user's appearance.
        /// </summary>
        /// <param name=rdata>HTTP service request work area</param>

        private void DoGet(AppearanceRequestData rdata)
        {

            rdata.userAppearance = Rest.AvatarServices.GetUserAppearance(rdata.userProfile.ID);

            if (rdata.userAppearance == null)
            {
                rdata.Fail(Rest.HttpStatusCodeNoContent,
                    String.Format("appearance data not found for user {0} {1}", 
                      rdata.userProfile.FirstName, rdata.userProfile.SurName));
            }

            rdata.initXmlWriter();

            FormatUserAppearance(rdata);

            // Indicate a successful request

            rdata.Complete();

            // Send the response to the user. The body will be implicitly
            // constructed from the result of the XML writer.

            rdata.Respond(String.Format("Appearance {0} Normal completion", rdata.method));
        }

        /// <summary>
        /// POST adds NEW information to the user profile database.
        /// This effectively resets the appearance before applying those
        /// characteristics supplied in the request.
        /// </summary>

        private void DoExtend(AppearanceRequestData rdata)
        {

            bool  created  = false;
            bool  modified = false;
            string newnode = String.Empty;

            Rest.Log.DebugFormat("{0} POST ENTRY", MsgId);

            AvatarAppearance old = Rest.AvatarServices.GetUserAppearance(rdata.userProfile.ID);

            rdata.userAppearance = new AvatarAppearance();

            //  Although the following behavior is admitted by HTTP I am becoming 
            //  increasingly doubtful that it is appropriate for REST. If I attempt to
            //  add a new record, and it already exists, then it seems to me that the
            //  attempt should fail, rather than update the existing record.

            if (GetUserAppearance(rdata))
            {
                modified = rdata.userAppearance != null;
                created  = !modified;
                Rest.AvatarServices.UpdateUserAppearance(rdata.userProfile.ID, rdata.userAppearance);
            //    Rest.UserServices.UpdateUserProfile(rdata.userProfile);
            }
            else
            {
                created  = true;
                Rest.AvatarServices.UpdateUserAppearance(rdata.userProfile.ID, rdata.userAppearance);
             //   Rest.UserServices.UpdateUserProfile(rdata.userProfile);
            }

            if (created)
            {
                newnode = String.Format("{0} {1}", rdata.userProfile.FirstName,
                                   rdata.userProfile.SurName);
                // Must include a location header with a URI that identifies the new resource.

                rdata.AddHeader(Rest.HttpHeaderLocation,String.Format("http://{0}{1}:{2}{3}{4}",
                         rdata.hostname,rdata.port,rdata.path,Rest.UrlPathSeparator, newnode));
                rdata.Complete(Rest.HttpStatusCodeCreated);

            }
            else
            {
                if (modified)
                {
                    rdata.Complete(Rest.HttpStatusCodeOK);
                }
                else
                {
                    rdata.Complete(Rest.HttpStatusCodeNoContent);
                }
            }

            rdata.Respond(String.Format("Appearance {0} : Normal completion", rdata.method));
            
        }

        /// <summary>
        /// This updates the user's appearance. not all aspects need to be provided,
        /// only those supplied will be changed.
        /// </summary>

        private void DoUpdate(AppearanceRequestData rdata)
        {

            bool  created  = false;
            bool  modified = false;


            rdata.userAppearance = Rest.AvatarServices.GetUserAppearance(rdata.userProfile.ID);

            // If the user exists then this is considered a modification regardless
            // of what may, or may not be, specified in the payload.

            if (rdata.userAppearance != null)
            {
                modified = true;
                Rest.AvatarServices.UpdateUserAppearance(rdata.userProfile.ID, rdata.userAppearance);
                Rest.UserServices.UpdateUserProfile(rdata.userProfile);
            }

            if (created)
            {
                rdata.Complete(Rest.HttpStatusCodeCreated);
            }
            else
            {
                if (modified)
                {
                    rdata.Complete(Rest.HttpStatusCodeOK);
                }
                else
                {
                    rdata.Complete(Rest.HttpStatusCodeNoContent);
                }
            }

            rdata.Respond(String.Format("Appearance {0} : Normal completion", rdata.method));

        }

        /// <summary>
        /// Delete the specified user's appearance. This actually performs a reset
        /// to the default avatar appearance, if the info is already there. 
        /// Existing ownership is preserved. All prior updates are lost and can not
        /// be recovered.
        /// </summary>

        private void DoDelete(AppearanceRequestData rdata)
        {

            AvatarAppearance old = Rest.AvatarServices.GetUserAppearance(rdata.userProfile.ID);

            if (old != null)
            {
                rdata.userAppearance = new AvatarAppearance();

                rdata.userAppearance.Owner = old.Owner;
                
                Rest.AvatarServices.UpdateUserAppearance(rdata.userProfile.ID, rdata.userAppearance);

                rdata.Complete();
            }
            else
            {
                rdata.Complete(Rest.HttpStatusCodeNoContent);
            }

            rdata.Respond(String.Format("Appearance {0} : Normal completion", rdata.method));

        }

#endregion method-specific processing

        private bool GetUserAppearance(AppearanceRequestData rdata)
        {

            XmlReader xml;
            bool indata = false;

            rdata.initXmlReader();
            xml     = rdata.reader;

            while (xml.Read())
            {
                switch (xml.NodeType)
                {
                    case XmlNodeType.Element :
                        switch (xml.Name)
                        {
                            case "Appearance" :
                                if (xml.MoveToAttribute("Height"))
                                {
                                    rdata.userAppearance.AvatarHeight = (float) Convert.ToDouble(xml.Value);
                                    indata = true;
                                }
                                if (xml.MoveToAttribute("Owner"))
                                {
                                    rdata.userAppearance.Owner = xml.Value;
                                    indata = true;
                                }
                                if (xml.MoveToAttribute("Serial"))
                                {
                                    rdata.userAppearance.Serial = Convert.ToInt32(xml.Value);
                                    indata = true;
                                }
                            break;
                            case "Body" :
                                if (xml.MoveToAttribute("Item"))
                                {
                                    rdata.userAppearance.BodyItem = xml.Value;
                                    indata = true;
                                }
                                if (xml.MoveToAttribute("Asset"))
                                {
                                    rdata.userAppearance.BodyAsset = xml.Value;
                                    indata = true;
                                }
                            break;
                            case "Skin" :
                                if (xml.MoveToAttribute("Item"))
                                {
                                    rdata.userAppearance.SkinItem = xml.Value;
                                    indata = true;
                                }
                                if (xml.MoveToAttribute("Asset"))
                                {
                                    rdata.userAppearance.SkinAsset = xml.Value;
                                    indata = true;
                                }
                            break;
                            case "Hair" :
                                if (xml.MoveToAttribute("Item"))
                                {
                                    rdata.userAppearance.HairItem = xml.Value;
                                    indata = true;
                                }
                                if (xml.MoveToAttribute("Asset"))
                                {
                                    rdata.userAppearance.HairAsset = xml.Value;
                                    indata = true;
                                }
                            break;
                            case "Eyes" :
                                if (xml.MoveToAttribute("Item"))
                                {
                                    rdata.userAppearance.EyesItem = xml.Value;
                                    indata = true;
                                }
                                if (xml.MoveToAttribute("Asset"))
                                {
                                    rdata.userAppearance.EyesAsset = xml.Value;
                                    indata = true;
                                }
                            break;
                            case "Shirt" :
                                if (xml.MoveToAttribute("Item"))
                                {
                                    rdata.userAppearance.ShirtItem = xml.Value;
                                    indata = true;
                                }
                                if (xml.MoveToAttribute("Asset"))
                                {
                                    rdata.userAppearance.ShirtAsset = xml.Value;
                                    indata = true;
                                }
                            break;
                            case "Pants" :
                                if (xml.MoveToAttribute("Item"))
                                {
                                    rdata.userAppearance.PantsItem = xml.Value;
                                    indata = true;
                                }
                                if (xml.MoveToAttribute("Asset"))
                                {
                                    rdata.userAppearance.PantsAsset = xml.Value;
                                    indata = true;
                                }
                            break;
                            case "Shoes" :
                                if (xml.MoveToAttribute("Item"))
                                {
                                    rdata.userAppearance.ShoesItem = xml.Value;
                                    indata = true;
                                }
                                if (xml.MoveToAttribute("Asset"))
                                {
                                    rdata.userAppearance.ShoesAsset = xml.Value;
                                    indata = true;
                                }
                            break;
                            case "Socks" :
                                if (xml.MoveToAttribute("Item"))
                                {
                                    rdata.userAppearance.SocksItem = xml.Value;
                                    indata = true;
                                }
                                if (xml.MoveToAttribute("Asset"))
                                {
                                    rdata.userAppearance.SocksAsset = xml.Value;
                                    indata = true;
                                }
                            break;
                            case "Jacket" :
                                if (xml.MoveToAttribute("Item"))
                                {
                                    rdata.userAppearance.JacketItem = xml.Value;
                                    indata = true;
                                }
                                if (xml.MoveToAttribute("Asset"))
                                {
                                    rdata.userAppearance.JacketAsset = xml.Value;
                                    indata = true;
                                }
                            break;
                            case "Gloves" :
                                if (xml.MoveToAttribute("Item"))
                                {
                                    rdata.userAppearance.GlovesItem = xml.Value;
                                    indata = true;
                                }
                                if (xml.MoveToAttribute("Asset"))
                                {
                                    rdata.userAppearance.GlovesAsset = xml.Value;
                                    indata = true;
                                }
                            break;
                            case "UnderShirt" :
                                if (xml.MoveToAttribute("Item"))
                                {
                                    rdata.userAppearance.UnderShirtItem = xml.Value;
                                    indata = true;
                                }
                                if (xml.MoveToAttribute("Asset"))
                                {
                                    rdata.userAppearance.UnderShirtAsset = xml.Value;
                                    indata = true;
                                }
                            break;
                            case "UnderPants" :
                                if (xml.MoveToAttribute("Item"))
                                {
                                    rdata.userAppearance.UnderPantsItem = xml.Value;
                                    indata = true;
                                }
                                if (xml.MoveToAttribute("Asset"))
                                {
                                    rdata.userAppearance.UnderPantsAsset = xml.Value;
                                    indata = true;
                                }
                            break;
                            case "Skirt" :
                                if (xml.MoveToAttribute("Item"))
                                {
                                    rdata.userAppearance.SkirtItem = xml.Value;
                                    indata = true;
                                }
                                if (xml.MoveToAttribute("Asset"))
                                {
                                    rdata.userAppearance.SkirtAsset = xml.Value;
                                    indata = true;
                                }
                            break;
                            case "Attachment" :
                                {

                                    int  ap;
                                    UUID asset;
                                    UUID item;

                                    if (xml.MoveToAttribute("AtPoint"))
                                    {
                                        ap = Convert.ToInt32(xml.Value);
                                        if (xml.MoveToAttribute("Asset"))
                                        {
                                            asset = new UUID(xml.Value);
                                            if (xml.MoveToAttribute("Asset"))
                                            {
                                                item = new UUID(xml.Value);
                                                rdata.userAppearance.SetAttachment(ap, item, asset);
                                                indata = true;
                                            }
                                        }
                                    }
                                }
                            break;
                            case "Texture" :
                                if (xml.MoveToAttribute("Default"))
                                {
                                    rdata.userAppearance.Texture = new Primitive.TextureEntry(new UUID(xml.Value));
                                    indata = true;
                                }
                            break;
                            case "Face" :
                                {
                                    uint index;
                                    if (xml.MoveToAttribute("Index"))
                                    {
                                        index = Convert.ToUInt32(xml.Value);
                                        if (xml.MoveToAttribute("Id"))
                                        {
                                            rdata.userAppearance.Texture.CreateFace(index).TextureID = new UUID(xml.Value);
                                            indata = true;
                                        }
                                    }
                                }
                            break;
                            case "VisualParameters" :
                                {
                                    xml.ReadContentAsBase64(rdata.userAppearance.VisualParams,
                                                            0, rdata.userAppearance.VisualParams.Length);
                                    indata = true;
                                }
                            break;
                        } 
                    break;
                }
            }

            return indata;

        }

        private void FormatPart(AppearanceRequestData rdata, string part, UUID item, UUID asset)
        {
            if(item != UUID.Zero || asset != UUID.Zero)
            {
				rdata.writer.WriteStartElement(part);
                if(item  != UUID.Zero)
                {
					rdata.writer.WriteAttributeString("Item",item.ToString());
                }
                if(asset != UUID.Zero)
                {
					rdata.writer.WriteAttributeString("Item",asset.ToString());
                }
				rdata.writer.WriteEndElement();
            }
        }

        private void FormatUserAppearance(AppearanceRequestData rdata)
        {

            Rest.Log.DebugFormat("{0} FormatUserAppearance", MsgId);

            if(rdata.userAppearance != null)
            {

				Rest.Log.DebugFormat("{0} FormatUserAppearance: appearance object exists", MsgId);
				rdata.writer.WriteStartElement("Appearance");

				rdata.writer.WriteAttributeString("Height", rdata.userAppearance.AvatarHeight.ToString());
                if(rdata.userAppearance.Owner != null)
					rdata.writer.WriteAttributeString("Owner", rdata.userAppearance.Owner.ToString());
				rdata.writer.WriteAttributeString("Serial", rdata.userAppearance.Serial.ToString());

                FormatPart(rdata, "Body", rdata.userAppearance.BodyItem, rdata.userAppearance.BodyAsset);
                FormatPart(rdata, "Skin", rdata.userAppearance.SkinItem, rdata.userAppearance.SkinAsset);
                FormatPart(rdata, "Hair", rdata.userAppearance.HairItem, rdata.userAppearance.HairAsset);
                FormatPart(rdata, "Eyes", rdata.userAppearance.EyesItem, rdata.userAppearance.EyesAsset);

                FormatPart(rdata, "Shirt", rdata.userAppearance.ShirtItem, rdata.userAppearance.ShirtAsset);
                FormatPart(rdata, "Pants", rdata.userAppearance.PantsItem, rdata.userAppearance.PantsAsset);
                FormatPart(rdata, "Skirt", rdata.userAppearance.SkirtItem, rdata.userAppearance.SkirtAsset);
                FormatPart(rdata, "Shoes", rdata.userAppearance.ShoesItem, rdata.userAppearance.ShoesAsset);
                FormatPart(rdata, "Socks", rdata.userAppearance.SocksItem, rdata.userAppearance.SocksAsset);

                FormatPart(rdata, "Jacket", rdata.userAppearance.JacketItem, rdata.userAppearance.JacketAsset);
                FormatPart(rdata, "Gloves", rdata.userAppearance.GlovesItem, rdata.userAppearance.GlovesAsset);

                FormatPart(rdata, "UnderShirt", rdata.userAppearance.UnderShirtItem, rdata.userAppearance.UnderShirtAsset);
                FormatPart(rdata, "UnderPants", rdata.userAppearance.UnderPantsItem, rdata.userAppearance.UnderPantsAsset);

				Hashtable attachments = rdata.userAppearance.GetAttachments();

				if(attachments != null)
				{

				    Rest.Log.DebugFormat("{0} FormatUserAppearance: Formatting attachments", MsgId);

					rdata.writer.WriteStartElement("Attachments");
					for (int i=0; i<attachments.Count;i++)
					{
						Hashtable attachment = attachments[i] as Hashtable;
						rdata.writer.WriteStartElement("Attachment");
						rdata.writer.WriteAttributeString("AtPoint", i.ToString());
						rdata.writer.WriteAttributeString("Item", (string) attachment["item"]);
						rdata.writer.WriteAttributeString("Asset", (string) attachment["asset"]);
						rdata.writer.WriteEndElement();
					}
					rdata.writer.WriteEndElement();
				}

				Primitive.TextureEntry texture = rdata.userAppearance.Texture;

				if (texture != null && (texture.DefaultTexture != null || texture.FaceTextures != null))
				{

				    Rest.Log.DebugFormat("{0} FormatUserAppearance: Formatting textures", MsgId);

					rdata.writer.WriteStartElement("Texture");

                    if(texture.DefaultTexture != null)
                    {
						Rest.Log.DebugFormat("{0} FormatUserAppearance: Formatting default texture", MsgId);
						rdata.writer.WriteAttributeString("Default",
							texture.DefaultTexture.TextureID.ToString());
                    }

                    if(texture.FaceTextures != null)
                    {

						Rest.Log.DebugFormat("{0} FormatUserAppearance: Formatting face textures", MsgId);

						for (int i=0; i<texture.FaceTextures.Length;i++)
						{
							if (texture.FaceTextures[i] != null)
                            {
								rdata.writer.WriteStartElement("Face");
								rdata.writer.WriteAttributeString("Index", i.ToString());
								rdata.writer.WriteAttributeString("Id",
										texture.FaceTextures[i].TextureID.ToString());
								rdata.writer.WriteEndElement();
                            }
						}
                    }

					rdata.writer.WriteEndElement();
				}

				Rest.Log.DebugFormat("{0} FormatUserAppearance: Formatting visual parameters", MsgId);

				rdata.writer.WriteStartElement("VisualParameters");
				rdata.writer.WriteBase64(rdata.userAppearance.VisualParams,0,
							rdata.userAppearance.VisualParams.Length);
				rdata.writer.WriteEndElement();
				rdata.writer.WriteFullEndElement();
            }

			Rest.Log.DebugFormat("{0} FormatUserAppearance: completed", MsgId);

            return;
        }

        #region appearance RequestData extension

        internal class AppearanceRequestData : RequestData
        {

            /// <summary>
            /// These are the inventory specific request/response state
            /// extensions.
            /// </summary>

            internal UUID                       uuid = UUID.Zero;
            internal UserProfileData     userProfile = null;
            internal AvatarAppearance userAppearance = null;

            internal AppearanceRequestData(OSHttpRequest request, OSHttpResponse response, string prefix)
                : base(request, response, prefix)
            {
            }

        }

        #endregion Appearance RequestData extension

    }
}