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
|
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include <aws/s3/model/SelectObjectContentHandler.h>
#include <aws/s3/S3ErrorMarshaller.h>
#include <aws/core/client/CoreErrors.h>
#include <aws/core/utils/event/EventStreamErrors.h>
#include <aws/core/utils/logging/LogMacros.h>
#include <aws/core/utils/xml/XmlSerializer.h>
using namespace Aws::S3::Model;
using namespace Aws::Utils::Event;
using namespace Aws::Utils::Xml;
namespace Aws
{
namespace S3
{
namespace Model
{
using namespace Aws::Client;
static const char SELECTOBJECTCONTENT_HANDLER_CLASS_TAG[] = "SelectObjectContentHandler";
SelectObjectContentHandler::SelectObjectContentHandler() : EventStreamHandler()
{
m_onRecordsEvent = [&](const RecordsEvent&)
{
AWS_LOGSTREAM_TRACE(SELECTOBJECTCONTENT_HANDLER_CLASS_TAG, "RecordsEvent received.");
};
m_onStatsEvent = [&](const StatsEvent&)
{
AWS_LOGSTREAM_TRACE(SELECTOBJECTCONTENT_HANDLER_CLASS_TAG, "StatsEvent received.");
};
m_onProgressEvent = [&](const ProgressEvent&)
{
AWS_LOGSTREAM_TRACE(SELECTOBJECTCONTENT_HANDLER_CLASS_TAG, "ProgressEvent received.");
};
m_onContinuationEvent = [&]()
{
AWS_LOGSTREAM_TRACE(SELECTOBJECTCONTENT_HANDLER_CLASS_TAG, "ContinuationEvent received.");
};
m_onEndEvent = [&]()
{
AWS_LOGSTREAM_TRACE(SELECTOBJECTCONTENT_HANDLER_CLASS_TAG, "EndEvent received.");
};
m_onError = [&](const AWSError<S3Errors>& error)
{
AWS_LOGSTREAM_TRACE(SELECTOBJECTCONTENT_HANDLER_CLASS_TAG, "S3 Errors received, " << error);
};
}
void SelectObjectContentHandler::OnEvent()
{
// Handler internal error during event stream decoding.
if (!*this)
{
AWSError<CoreErrors> error = EventStreamErrorsMapper::GetAwsErrorForEventStreamError(GetInternalError());
error.SetMessage(GetEventPayloadAsString());
m_onError(AWSError<S3Errors>(error));
return;
}
const auto& headers = GetEventHeaders();
auto messageTypeHeaderIter = headers.find(MESSAGE_TYPE_HEADER);
if (messageTypeHeaderIter == headers.end())
{
AWS_LOGSTREAM_WARN(SELECTOBJECTCONTENT_HANDLER_CLASS_TAG, "Header: " << MESSAGE_TYPE_HEADER << " not found in the message.");
return;
}
switch (Aws::Utils::Event::Message::GetMessageTypeForName(messageTypeHeaderIter->second.GetEventHeaderValueAsString()))
{
case Aws::Utils::Event::Message::MessageType::EVENT:
HandleEventInMessage();
break;
case Aws::Utils::Event::Message::MessageType::REQUEST_LEVEL_ERROR:
case Aws::Utils::Event::Message::MessageType::REQUEST_LEVEL_EXCEPTION:
{
HandleErrorInMessage();
break;
}
default:
AWS_LOGSTREAM_WARN(SELECTOBJECTCONTENT_HANDLER_CLASS_TAG,
"Unexpected message type: " << messageTypeHeaderIter->second.GetEventHeaderValueAsString());
break;
}
}
void SelectObjectContentHandler::HandleEventInMessage()
{
const auto& headers = GetEventHeaders();
auto eventTypeHeaderIter = headers.find(EVENT_TYPE_HEADER);
if (eventTypeHeaderIter == headers.end())
{
AWS_LOGSTREAM_WARN(SELECTOBJECTCONTENT_HANDLER_CLASS_TAG, "Header: " << EVENT_TYPE_HEADER << " not found in the message.");
return;
}
switch (SelectObjectContentEventMapper::GetSelectObjectContentEventTypeForName(eventTypeHeaderIter->second.GetEventHeaderValueAsString()))
{
case SelectObjectContentEventType::RECORDS:
{
RecordsEvent event(GetEventPayloadWithOwnership());
m_onRecordsEvent(event);
break;
}
case SelectObjectContentEventType::STATS:
{
auto xmlDoc = XmlDocument::CreateFromXmlString(GetEventPayloadAsString());
if (!xmlDoc.WasParseSuccessful())
{
AWS_LOGSTREAM_WARN(SELECTOBJECTCONTENT_HANDLER_CLASS_TAG, "Unable to generate a proper StatsEvent object from the response in XML format.");
break;
}
m_onStatsEvent(StatsEvent(xmlDoc.GetRootElement()));
break;
}
case SelectObjectContentEventType::PROGRESS:
{
auto xmlDoc = XmlDocument::CreateFromXmlString(GetEventPayloadAsString());
if (!xmlDoc.WasParseSuccessful())
{
AWS_LOGSTREAM_WARN(SELECTOBJECTCONTENT_HANDLER_CLASS_TAG, "Unable to generate a proper ProgressEvent object from the response in XML format.");
break;
}
m_onProgressEvent(ProgressEvent(xmlDoc.GetRootElement()));
break;
}
case SelectObjectContentEventType::CONT:
{
m_onContinuationEvent();
break;
}
case SelectObjectContentEventType::END:
{
m_onEndEvent();
break;
}
default:
AWS_LOGSTREAM_WARN(SELECTOBJECTCONTENT_HANDLER_CLASS_TAG,
"Unexpected event type: " << eventTypeHeaderIter->second.GetEventHeaderValueAsString());
break;
}
}
void SelectObjectContentHandler::HandleErrorInMessage()
{
const auto& headers = GetEventHeaders();
Aws::String errorCode;
Aws::String errorMessage;
auto errorHeaderIter = headers.find(ERROR_CODE_HEADER);
if (errorHeaderIter == headers.end())
{
errorHeaderIter = headers.find(EXCEPTION_TYPE_HEADER);
if (errorHeaderIter == headers.end())
{
AWS_LOGSTREAM_WARN(SELECTOBJECTCONTENT_HANDLER_CLASS_TAG,
"Error type was not found in the event message.");
return;
}
}
errorCode = errorHeaderIter->second.GetEventHeaderValueAsString();
errorHeaderIter = headers.find(ERROR_MESSAGE_HEADER);
if (errorHeaderIter == headers.end())
{
errorHeaderIter = headers.find(EXCEPTION_TYPE_HEADER);
if (errorHeaderIter == headers.end())
{
AWS_LOGSTREAM_WARN(SELECTOBJECTCONTENT_HANDLER_CLASS_TAG,
"Error description was not found in the event message.");
return;
}
}
errorMessage = errorHeaderIter->second.GetEventHeaderValueAsString();
MarshallError(errorCode, errorMessage);
}
void SelectObjectContentHandler::MarshallError(const Aws::String& errorCode, const Aws::String& errorMessage)
{
S3ErrorMarshaller errorMarshaller;
AWSError<CoreErrors> error;
if (errorCode.empty())
{
error = AWSError<CoreErrors>(CoreErrors::UNKNOWN, "", errorMessage, false);
}
else
{
error = errorMarshaller.FindErrorByName(errorMessage.c_str());
if (error.GetErrorType() != CoreErrors::UNKNOWN)
{
AWS_LOGSTREAM_WARN(SELECTOBJECTCONTENT_HANDLER_CLASS_TAG, "Encountered AWSError '" << errorCode.c_str() << "': " << errorMessage.c_str());
error.SetExceptionName(errorCode);
error.SetMessage(errorMessage);
}
else
{
AWS_LOGSTREAM_WARN(SELECTOBJECTCONTENT_HANDLER_CLASS_TAG, "Encountered Unknown AWSError '" << errorCode.c_str() << "': " << errorMessage.c_str());
error = AWSError<CoreErrors>(CoreErrors::UNKNOWN, errorCode, "Unable to parse ExceptionName: " + errorCode + " Message: " + errorMessage, false);
}
}
m_onError(AWSError<S3Errors>(error));
}
namespace SelectObjectContentEventMapper
{
static const int RECORDS_HASH = Aws::Utils::HashingUtils::HashString("Records");
static const int STATS_HASH = Aws::Utils::HashingUtils::HashString("Stats");
static const int PROGRESS_HASH = Aws::Utils::HashingUtils::HashString("Progress");
static const int CONT_HASH = Aws::Utils::HashingUtils::HashString("Cont");
static const int END_HASH = Aws::Utils::HashingUtils::HashString("End");
SelectObjectContentEventType GetSelectObjectContentEventTypeForName(const Aws::String& name)
{
int hashCode = Aws::Utils::HashingUtils::HashString(name.c_str());
if (hashCode == RECORDS_HASH)
{
return SelectObjectContentEventType::RECORDS;
}
else if (hashCode == STATS_HASH)
{
return SelectObjectContentEventType::STATS;
}
else if (hashCode == PROGRESS_HASH)
{
return SelectObjectContentEventType::PROGRESS;
}
else if (hashCode == CONT_HASH)
{
return SelectObjectContentEventType::CONT;
}
else if (hashCode == END_HASH)
{
return SelectObjectContentEventType::END;
}
return SelectObjectContentEventType::UNKNOWN;
}
Aws::String GetNameForSelectObjectContentEventType(SelectObjectContentEventType value)
{
switch (value)
{
case SelectObjectContentEventType::RECORDS:
return "Records";
case SelectObjectContentEventType::STATS:
return "Stats";
case SelectObjectContentEventType::PROGRESS:
return "Progress";
case SelectObjectContentEventType::CONT:
return "Cont";
case SelectObjectContentEventType::END:
return "End";
default:
return "Unknown";
}
}
} // namespace SelectObjectContentEventMapper
} // namespace Model
} // namespace S3
} // namespace Aws
|