1/*
2 * Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16#include <aws/core/utils/event/EventMessage.h>
17#include <aws/core/utils/HashingUtils.h>
18#include <algorithm>
19#include <iterator>
20
21namespace Aws
22{
23 namespace Utils
24 {
25 namespace Event
26 {
27 const char EVENT_TYPE_HEADER[] = ":event-type";
28 const char CONTENT_TYPE_HEADER[] = ":content-type";
29 const char MESSAGE_TYPE_HEADER[] = ":message-type";
30 const char ERROR_CODE_HEADER[] = ":error-code";
31 const char ERROR_MESSAGE_HEADER[] = ":error-message";
32 const char EXCEPTION_TYPE_HEADER[] = ":exception-type";
33
34 static const int EVENT_HASH = HashingUtils::HashString("event");
35 static const int ERROR_HASH = HashingUtils::HashString("error");
36 static const int EXCEPTION_HASH = HashingUtils::HashString("exception");
37
38 static const int CONTENT_TYPE_APPLICATION_OCTET_STREAM_HASH = HashingUtils::HashString("application/octet-stream");
39 static const int CONTENT_TYPE_APPLICATION_JSON_HASH = HashingUtils::HashString("application/json");
40 static const int CONTENT_TYPE_TEXT_PLAIN_HASH = HashingUtils::HashString("text/plain");
41
42 Message::MessageType Message::GetMessageTypeForName(const Aws::String& name)
43 {
44 int hashCode = Aws::Utils::HashingUtils::HashString(name.c_str());
45 if (hashCode == EVENT_HASH)
46 {
47 return MessageType::EVENT;
48 }
49 else if (hashCode == ERROR_HASH)
50 {
51 return MessageType::REQUEST_LEVEL_ERROR;
52 }
53 else if (hashCode == EXCEPTION_HASH)
54 {
55 return MessageType::REQUEST_LEVEL_EXCEPTION;
56 }
57 else
58 {
59 return MessageType::UNKNOWN;
60 }
61 }
62
63 Aws::String Message::GetNameForMessageType(MessageType value)
64 {
65 switch (value)
66 {
67 case MessageType::EVENT:
68 return "event";
69 case MessageType::REQUEST_LEVEL_ERROR:
70 return "error";
71 case MessageType::REQUEST_LEVEL_EXCEPTION:
72 return "exception";
73 default:
74 return "unknown";
75 }
76 }
77
78 Message::ContentType Message::GetContentTypeForName(const Aws::String& name)
79 {
80 int hashCode = Aws::Utils::HashingUtils::HashString(name.c_str());
81 if (hashCode == CONTENT_TYPE_APPLICATION_OCTET_STREAM_HASH)
82 {
83 return ContentType::APPLICATION_OCTET_STREAM;
84 }
85 else if (hashCode == CONTENT_TYPE_APPLICATION_JSON_HASH)
86 {
87 return ContentType::APPLICATION_JSON;
88 }
89 else if (hashCode == CONTENT_TYPE_TEXT_PLAIN_HASH)
90 {
91 return ContentType::TEXT_PLAIN;
92 }
93 else
94 {
95 return ContentType::UNKNOWN;
96 }
97 }
98
99 Aws::String Message::GetNameForContentType(ContentType value)
100 {
101 switch (value)
102 {
103 case ContentType::APPLICATION_OCTET_STREAM:
104 return "application/octet-stream";
105 case ContentType::APPLICATION_JSON:
106 return "application/json";
107 case ContentType::TEXT_PLAIN:
108 return "text/plain";
109 default:
110 return "unknown";
111 }
112 }
113
114 void Message::Reset()
115 {
116 m_totalLength = 0;
117 m_headersLength = 0;
118 m_payloadLength = 0;
119
120 m_eventHeaders.clear();
121 m_eventPayload.clear();
122 }
123
124 void Message::WriteEventPayload(const unsigned char* data, size_t length)
125 {
126 std::copy(data, data + length, std::back_inserter(m_eventPayload));
127 }
128
129 void Message::WriteEventPayload(const Aws::Vector<unsigned char>& bits)
130 {
131 std::copy(bits.cbegin(), bits.cend(), std::back_inserter(m_eventPayload));
132 }
133
134 void Message::WriteEventPayload(const Aws::String& bits)
135 {
136 std::copy(bits.cbegin(), bits.cend(), std::back_inserter(m_eventPayload));
137 }
138
139 } // namespace Event
140 } // namespace Utils
141} // namespace Aws
142
143