1 /******************************************************************
\r
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
19 ******************************************************************/
\r
24 * @brief This file provides data Model for RAML file.
\r
33 #include "AbstractParam.h"
\r
34 #include "UriParameter.h"
\r
35 #include "QueryParameter.h"
\r
36 #include "FormParameter.h"
\r
39 #include "RequestResponseBody.h"
\r
40 #include "RamlResource.h"
\r
41 #include "ActionType.h"
\r
43 #include "Response.h"
\r
45 #include "IncludeResolver.h"
\r
47 #include "DocumentationItem.h"
\r
48 #include "yaml-cpp/yaml.h"
\r
49 #include "yaml-cpp/exceptions.h"
\r
50 #include "RamlExceptions.h"
\r
51 #include "RamlUtils.h"
\r
52 #if defined (__TIZENRT__)
\r
53 #include <apps/netutils/cJSON.h>
\r
63 * @brief This class provides data Model for RAML file.
\r
69 * This method is for getting Title from Raml.
\r
71 * @return Title as string.
\r
73 virtual std::string getTitle() const;
\r
76 * This method is for setting Title to Raml.
\r
78 * @param title - Title as string
\r
80 virtual void setTitle(const std::string &title);
\r
83 * This method is for getting Version from Raml.
\r
85 * @return Version as string.
\r
87 virtual std::string getVersion() const;
\r
90 * This method is for setting Version to Raml.
\r
92 * @param version - Version as string
\r
94 virtual void setVersion(const std::string &version);
\r
97 * This method is for getting BaseUri from Raml.
\r
99 * @return BaseUri as string.
\r
101 virtual std::string getBaseUri() const;
\r
104 * This method is for setting BaseUri to Raml.
\r
106 * @param baseUri - BaseUri as string
\r
108 virtual void setBaseUri(const std::string &baseUri);
\r
111 * This method is for getting Protocols from Raml.
\r
113 * @return list of Protocols as string.
\r
115 virtual std::list<std::string> const &getProtocols() const;
\r
118 * This method is for setting Protocols to Raml.
\r
120 * @param protocol - Protocol as string
\r
122 virtual void setProtocol(const std::string &protocol);
\r
126 * This method is for getting BaseUriParameter from Raml.
\r
128 * @return map of BaseUriParameter name and Pointer to UriParameter.
\r
130 virtual std::map<std::string, UriParameterPtr> const &getBaseUriParameters() const;
\r
133 * This method is for setting BaseUriParameter to Raml.
\r
135 * @param paramName - name of BaseUriParameter as string
\r
136 * @param uriParameter - pointer to UriParameter
\r
138 virtual void setBaseUriParameter(const std::string ¶mName,
\r
139 const UriParameterPtr &uriParameter);
\r
143 * This method is for getting MediaType from Raml.
\r
145 * @return Title as string.
\r
147 virtual std::string getMediaType() const;
\r
150 * This method is for setting MediaType to Raml.
\r
152 * @param mediaType - MediaType as string
\r
154 virtual void setMediaType(const std::string &mediaType);
\r
157 * This method is for getting Schemas from Raml.
\r
159 * @return list of Schemas name and Pointer to Schema as pair.
\r
161 virtual std::list<std::pair<std::string, SchemaPtr> > const &getSchemas() const;
\r
164 * This method is for setting Schemas to Raml.
\r
166 * @param schemaName - name of schema as string
\r
167 * @param schema - pointer to Schema
\r
169 virtual void setSchema(const std::string &schemaName, const SchemaPtr &schema);
\r
172 * This method is for getting ResourceTypes from Raml.
\r
174 * @return list of ResourceTypes name and Pointer to RamlResource as pair.
\r
176 virtual std::list<std::pair<std::string, RamlResourcePtr> > const &getResourceTypes() const;
\r
179 * This method is for setting ResourceTypes to Raml.
\r
181 * @param typeName - name of ResourceType as string
\r
182 * @param resourceType - pointer to RamlResource
\r
184 virtual void setResourceType(const std::string &typeName,
\r
185 const RamlResourcePtr &resourceType);
\r
188 * This method is for getting Traits from Raml.
\r
190 * @return list of Traits name and Pointer to Action as pair.
\r
192 virtual std::list<std::pair<std::string, ActionPtr> > const &getTraits() const;
\r
195 * This method is for setting Traits to Raml.
\r
197 * @param traitName - name of Trait as string
\r
198 * @param trait - pointer to Action
\r
200 virtual void setTrait(const std::string &traitName, const ActionPtr &trait);
\r
203 * This method is for getting Resource from Raml.
\r
205 * @param resourceName - name of Resource as string
\r
207 * @return Pointer to Resource
\r
209 virtual RamlResourcePtr getResource(const std::string &resourceName);
\r
212 * This method is for getting Resource from Raml.
\r
214 * @return map of Resource name and Pointer to Resource
\r
216 virtual std::map<std::string, RamlResourcePtr> const &getResources() const;
\r
219 * This method is for setting Resource to Raml.
\r
221 * @param resourceKey - name of Resource as string
\r
222 * @param resource - pointer to Resource
\r
224 virtual void setResource(const std::string &resourceKey,
\r
225 const RamlResourcePtr &resource);
\r
228 * This method is for setting DocumentationItem to Raml.
\r
230 * @param documentationItem - pointer to DocumentationItem
\r
232 virtual void setDocumentationItem(const std::shared_ptr<DocumentationItem> &documentationItem);
\r
235 * This method is for getting DocumentationItem from Raml.
\r
237 * @return list of DocumentationItem
\r
239 virtual std::list<std::shared_ptr<DocumentationItem> > const &getDocumentation() const;
\r
242 * This method is for setting Raml object reading from Yaml nodes
\r
244 * @param yamlNode - Reference to YamlNode for reading into Raml object
\r
246 void readRamlFromYaml(const YAML::Node &yamlNode);
\r
249 * Constructor of Raml.
\r
251 Raml() : m_includeResolver(std::make_shared<IncludeResolver>()) {}
\r
254 * Constructor of Raml.
\r
256 * @param fileLocation - RAML configuration file path.
\r
257 * @param ramlName - RAML configuration file Name
\r
259 * NOTE: Constructor would throw RamlException if any error occured.
\r
261 Raml(const std::string &fileLocation, const std::string &ramlName)
\r
262 : m_includeResolver(std::make_shared<IncludeResolver>(fileLocation))
\r
266 YAML::Node yamlRootNode = YAML::LoadFile(fileLocation + ramlName);
\r
267 readRamlFromYaml(yamlRootNode);
\r
269 catch (YAML::ParserException &e)
\r
271 throw RamlParserException(e.mark, e.msg);
\r
273 catch (YAML::RepresentationException &e)
\r
275 throw RamlRepresentationException(e.mark, e.msg);
\r
277 catch (YAML::BadFile &e)
\r
279 throw RamlBadFile(e.mark, e.msg);
\r
281 catch (JsonException &e)
\r
287 std::string m_title;
\r
288 std::string m_version;
\r
289 std::string m_baseUri;
\r
290 std::list<std::string> m_protocols;
\r
291 std::map<std::string, UriParameterPtr> m_baseUriParameters;
\r
292 std::string m_mediaType;
\r
293 std::list <std::pair<std::string, SchemaPtr> > m_schemas;
\r
294 std::list <std::pair<std::string, RamlResourcePtr> > m_resourceTypes;
\r
295 std::list <std::pair<std::string, ActionPtr> > m_traits;
\r
296 std::map<std::string, RamlResourcePtr> m_resources;
\r
297 std::list<std::shared_ptr<DocumentationItem> > m_documentation;
\r
298 IncludeResolverPtr m_includeResolver;
\r
301 /** RamlPtr - shared Ptr to Raml.*/
\r
302 typedef std::shared_ptr<Raml> RamlPtr;
\r