forked from cloudevents/sdk-csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCloudEventContent.cs
171 lines (153 loc) · 6.37 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
// 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.Text;
using System.Threading.Tasks;
/// <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));
Headers.ContentType = new MediaTypeHeaderValue(contentType.MediaType);
MapHeaders(cloudEvent);
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));
}
Headers.ContentType = new MediaTypeHeaderValue(cloudEvent.DataContentType?.MediaType);
MapHeaders(cloudEvent);
}
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)
{
foreach (var attribute in cloudEvent.GetAttributes())
{
if (!(attribute.Key.Equals(CloudEventAttributes.DataAttributeName(cloudEvent.SpecVersion)) ||
attribute.Key.Equals(CloudEventAttributes.DataContentTypeAttributeName(cloudEvent.SpecVersion))))
{
if (attribute.Value is string)
{
Headers.Add("ce-" + attribute.Key, WebUtility.UrlEncode(attribute.Value.ToString()));
}
else if (attribute.Value is DateTime)
{
Headers.Add("ce-" + attribute.Key, ((DateTime)attribute.Value).ToString("u"));
}
else if (attribute.Value is Uri || attribute.Value is int)
{
Headers.Add("ce-" + attribute.Key, attribute.Value.ToString());
}
else
{
Headers.Add("ce-" + attribute.Key,
WebUtility.UrlEncode(
Encoding.UTF8.GetString(jsonFormatter.EncodeAttribute(cloudEvent.SpecVersion, attribute.Key, attribute.Value,
cloudEvent.Extensions.Values))));
}
}
}
}
/// <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);
}
}
}
}