forked from cloudevents/sdk-csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCloudEventContent.cs
185 lines (164 loc) · 7.03 KB
/
CloudEventContent.cs
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
// Copyright (c) Cloud Native Foundation.
// Licensed under the Apache 2.0 license.
// See LICENSE file in the project root for full license information.
namespace CloudNative.CloudEvents
{
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Mime;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
/// <summary>
/// This class is for use with `HttpClient` and constructs content and headers for
/// a HTTP request from a CloudEvent.
/// </summary>
public class CloudEventContent : HttpContent
{
IInnerContent inner;
static JsonEventFormatter jsonFormatter = new JsonEventFormatter();
/// <summary>
/// Constructor
/// </summary>
/// <param name="cloudEvent">CloudEvent</param>
/// <param name="contentMode">Content mode. Structured or binary.</param>
/// <param name="formatter">Event formatter</param>
public CloudEventContent(CloudEvent cloudEvent, ContentMode contentMode, ICloudEventFormatter formatter)
{
if (contentMode == ContentMode.Structured)
{
inner = new InnerByteArrayContent(formatter.EncodeStructuredEvent(cloudEvent, out var contentType));
// This is optional in the specification, but can be useful.
MapHeaders(cloudEvent, includeDataContentType: true);
Headers.ContentType = new MediaTypeHeaderValue(contentType.MediaType);
return;
}
if (cloudEvent.Data is byte[])
{
inner = new InnerByteArrayContent((byte[])cloudEvent.Data);
}
else if (cloudEvent.Data is string)
{
inner = new InnerStringContent((string)cloudEvent.Data);
}
else if (cloudEvent.Data is Stream)
{
inner = new InnerStreamContent((Stream)cloudEvent.Data);
}
else
{
inner = new InnerByteArrayContent(formatter.EncodeAttribute(cloudEvent.SpecVersion, CloudEventAttributes.DataAttributeName(cloudEvent.SpecVersion),
cloudEvent.Data, cloudEvent.Extensions.Values));
}
var mediaType = cloudEvent.DataContentType?.MediaType
?? throw new ArgumentException(Strings.ErrorContentTypeUnspecified, nameof(cloudEvent));
Headers.ContentType = new MediaTypeHeaderValue(mediaType);
MapHeaders(cloudEvent, includeDataContentType: false);
}
interface IInnerContent
{
Task InnerSerializeToStreamAsync(Stream stream, TransportContext context);
bool InnerTryComputeLength(out long length);
}
protected override Task SerializeToStreamAsync(Stream stream, TransportContext context)
{
return inner.InnerSerializeToStreamAsync(stream, context);
}
protected override bool TryComputeLength(out long length)
{
return inner.InnerTryComputeLength(out length);
}
void MapHeaders(CloudEvent cloudEvent, bool includeDataContentType)
{
string specVersionAttributeName = CloudEventAttributes.DataAttributeName(cloudEvent.SpecVersion);
string dataContentTypeAttributeName = CloudEventAttributes.DataContentTypeAttributeName(cloudEvent.SpecVersion);
foreach (var attribute in cloudEvent.GetAttributes())
{
string key = attribute.Key;
string headerName = "ce-" + key;
object value = attribute.Value;
// Never map the spec attribute to a header
if (key == specVersionAttributeName)
{
continue;
}
// Only map the data content type attribute to a header if we've been asked to
else if (key == dataContentTypeAttributeName && !includeDataContentType)
{
continue;
}
else
{
string headerValue = attribute.Value switch
{
string text => WebUtility.UrlEncode(text),
ContentType contentType => contentType.ToString(),
DateTime dt => XmlConvert.ToString(dt, XmlDateTimeSerializationMode.Utc),
Uri uri => uri.ToString(),
int integer => integer.ToString(),
_ => WebUtility.UrlEncode(Encoding.UTF8.GetString(
jsonFormatter.EncodeAttribute(cloudEvent.SpecVersion, key, value, cloudEvent.Extensions.Values)))
};
Headers.Add(headerName, headerValue);
}
}
}
/// <summary>
/// This inner class is required to get around the 'protected'-ness of the
/// override functions of HttpContent for enabling containment/delegation
/// </summary>
class InnerByteArrayContent : ByteArrayContent, IInnerContent
{
public InnerByteArrayContent(byte[] content) : base(content)
{
}
public Task InnerSerializeToStreamAsync(Stream stream, TransportContext context)
{
return base.SerializeToStreamAsync(stream, context);
}
public bool InnerTryComputeLength(out long length)
{
return base.TryComputeLength(out length);
}
}
/// <summary>
/// This inner class is required to get around the 'protected'-ness of the
/// override functions of HttpContent for enabling containment/delegation
/// </summary>
class InnerStreamContent : StreamContent, IInnerContent
{
public InnerStreamContent(Stream content) : base(content)
{
}
public Task InnerSerializeToStreamAsync(Stream stream, TransportContext context)
{
return base.SerializeToStreamAsync(stream, context);
}
public bool InnerTryComputeLength(out long length)
{
return base.TryComputeLength(out length);
}
}
/// <summary>
/// This inner class is required to get around the 'protected'-ness of the
/// override functions of HttpContent for enabling containment/delegation
/// </summary>
class InnerStringContent : StringContent, IInnerContent
{
public InnerStringContent(string content) : base(content)
{
}
public Task InnerSerializeToStreamAsync(Stream stream, TransportContext context)
{
return base.SerializeToStreamAsync(stream, context);
}
public bool InnerTryComputeLength(out long length)
{
return base.TryComputeLength(out length);
}
}
}
}