2009-09-04 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.ServiceModel.Web / System.ServiceModel.Web / OutgoingWebResponseContext.cs
1 //
2 // OutgoingWebResponseContext.cs
3 //
4 // Author:
5 //      Atsushi Enomoto  <atsushi@ximian.com>
6 //
7 // Copyright (C) 2008 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 using System;
29 using System.Globalization;
30 using System.Net;
31 using System.ServiceModel.Channels;
32
33 namespace System.ServiceModel.Web
34 {
35         public class OutgoingWebResponseContext
36         {
37                 WebHeaderCollection headers;
38                 long content_length;
39                 string content_type, etag, location, status_desc;
40                 DateTime last_modified;
41                 HttpStatusCode status_code;
42                 bool suppress_body;
43
44                 internal OutgoingWebResponseContext ()
45                 {
46                 }
47
48                 internal void Apply (HttpResponseMessageProperty hp)
49                 {
50                         if (headers != null)
51                                 hp.Headers.Add (headers);
52                         if (content_length != 0)
53                                 hp.Headers ["Content-Length"] = content_length.ToString (NumberFormatInfo.InvariantInfo);
54                         if (content_type != null)
55                                 hp.Headers ["Content-Type"] = content_type;
56                         if (etag != null)
57                                 hp.Headers ["ETag"] = etag;
58                         if (location != null)
59                                 hp.Headers ["Location"] = location;
60                         if (last_modified != default (DateTime))
61                                 hp.Headers ["Last-Modified"] = last_modified.ToString ("R");
62                         if (status_code != default (HttpStatusCode))
63                                 hp.StatusCode = status_code;
64                         if (status_desc != null)
65                                 hp.StatusDescription = status_desc;
66                         hp.SuppressEntityBody = suppress_body;
67                 }
68
69                 public long ContentLength {
70                         get { return content_length; }
71                         set { content_length = value; }
72                 }
73
74                 public string ContentType {
75                         get { return content_type; }
76                         set { content_type = value; }
77                 }
78
79                 public string ETag {
80                         get { return etag; }
81                         set { etag = value; }
82                 }
83
84                 public WebHeaderCollection Headers {
85                         get {
86                                 if (headers == null)
87                                         headers = new WebHeaderCollection ();
88                                 return headers;
89                         }
90                 }
91
92                 public DateTime LastModified {
93                         get { return last_modified; }
94                         set { last_modified = value; }
95                 }
96
97                 public string Location {
98                         get { return location; }
99                         set { location = value; }
100                 }
101
102                 public HttpStatusCode StatusCode {
103                         get { return status_code; }
104                         set { status_code = value; }
105                 }
106
107                 public string StatusDescription {
108                         get { return status_desc; }
109                         set { status_desc = value; }
110                 }
111
112                 public bool SuppressEntityBody {
113                         get { return suppress_body; }
114                         set { suppress_body = value; }
115                 }
116
117                 public void SetStatusAsCreated (Uri locationUri)
118                 {
119                         StatusCode = HttpStatusCode.Created;
120                         Location = locationUri.AbsoluteUri;
121                 }
122
123                 public void SetStatusAsNotFound ()
124                 {
125                         StatusCode = HttpStatusCode.NotFound;
126                 }
127
128                 public void SetStatusAsNotFound (string description)
129                 {
130                         StatusCode = HttpStatusCode.NotFound;
131                         StatusDescription = description;
132                 }
133         }
134 }