forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicRequestContext.cs
More file actions
122 lines (99 loc) · 3.58 KB
/
Copy pathBasicRequestContext.cs
File metadata and controls
122 lines (99 loc) · 3.58 KB
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
using System.Collections.Generic;
using System.Globalization;
using System.Net;
using ServiceStack.Configuration;
using ServiceStack.Messaging;
using ServiceStack.Text;
using ServiceStack.Web;
namespace ServiceStack.Host
{
public class BasicRequestContext : IRequestContext
{
public IMessage Message { get; set; }
public BasicRequest Request { get; set; }
public BasicResponse Response { get; set; }
private IResolver resolver;
public IResolver Resolver
{
get { return resolver ?? Service.GlobalResolver; }
set { resolver = value; }
}
public BasicRequestContext(IMessage message=null)
{
Message = message ?? new Message();
ContentType = this.ResponseContentType = MimeTypes.Json;
if (Message.Body != null)
PathInfo = "/json/oneway/" + OperationName;
Request = new BasicRequest(this);
Response = new BasicResponse(this);
}
private string operationName;
public string OperationName
{
get { return operationName ?? (operationName = Message.Body != null ? Message.Body.GetType().Name : null); }
set { operationName = value; }
}
public T Get<T>() where T : class
{
if (typeof(T) == typeof(IHttpRequest))
return (T)(object)Request;
if (typeof(T) == typeof(IHttpResponse))
return (T)(object)Response;
return Resolver.TryResolve<T>();
}
public string IpAddress { get; set; }
public string GetHeader(string headerName)
{
string headerValue;
Headers.TryGetValue(headerName, out headerValue);
return headerValue;
}
private Dictionary<string, string> headers;
public Dictionary<string, string> Headers
{
get
{
if (headers != null)
{
headers = Message.ToHeaders();
}
return headers;
}
}
public IDictionary<string, Cookie> Cookies
{
get { return new Dictionary<string, Cookie>(); }
}
public RequestAttributes RequestAttributes
{
get { return RequestAttributes.LocalSubnet | RequestAttributes.MessageQueue; }
}
public Web.IRequestPreferences RequestPreferences { get; set; }
public string ContentType { get; set; }
public string ResponseContentType { get; set; }
public string CompressionType { get; set; }
public string AbsoluteUri { get; set; }
public string PathInfo { get; set; }
public IHttpFile[] Files { get; set; }
public void Dispose()
{
}
}
public static class MqExtensions
{
public static Dictionary<string,string> ToHeaders(this IMessage message)
{
var map = new Dictionary<string, string>
{
{"CreatedDate",message.CreatedDate.ToLongDateString()},
{"Priority",message.Priority.ToString(CultureInfo.InvariantCulture)},
{"RetryAttempts",message.RetryAttempts.ToString(CultureInfo.InvariantCulture)},
{"ReplyId",message.ReplyId.HasValue ? message.ReplyId.Value.ToString() : null},
{"ReplyTo",message.ReplyTo},
{"Options",message.Options.ToString(CultureInfo.InvariantCulture)},
{"Error",message.Error.Dump()},
};
return map;
}
}
}