コード例 #1
0
RAML::RequestResponseBodyPtr SimulatorResourceFactory::getRAMLResponseBody(
    std::shared_ptr<RAML::RamlResource> ramlResource, RAML::ActionType type, std::string responseCode)
{
    // Get the resource representation schema from response body
    RAML::ActionPtr action = ramlResource->getAction(type);
    if (!action)
    {
        OIC_LOG(ERROR, TAG, "Resource does not possess the request!");
        return nullptr;
    }

    RAML::ResponsePtr response = action->getResponse(responseCode);
    if (!response)
    {
        OIC_LOG(ERROR, TAG, "Resource does not provide valid GET response!");
        return nullptr;
    }

    RAML::RequestResponseBodyPtr responseBody = response->getResponseBody("application/json");
    if (!responseBody)
    {
        OIC_LOG(ERROR, TAG, "GET response is not of type \"application/json\" ");
        return nullptr;
    }

    return responseBody;
}
コード例 #2
0
ResponseModelSP RequestModelBuilder::createResponseModel(int code,
        const RAML::ResponsePtr &response)
{
    ResponseModelSP responseModel(new ResponseModel(code));
    RAML::RequestResponseBodyPtr responseBody = response->getResponseBody("application/json");
    SimulatorResourceModelSP repSchema = createRepSchema(responseBody);
    responseModel->setRepSchema(repSchema);
    return responseModel;
}
コード例 #3
0
SimulatorResourceServerImplSP SimulatorResourceCreator::createResource(
    const std::string &configPath)
{
    RAML::RamlPtr raml;

    try
    {
        std::shared_ptr<RAML::RamlParser> ramlParser = std::make_shared<RAML::RamlParser>(configPath);
        raml = ramlParser->getRamlPtr();
    }
    catch (RAML::RamlException &e)
    {
        OC_LOG_V(ERROR, TAG, "RAML Exception occured! [%s]", e.what());
        throw;
    }

    std::map<std::string, RAML::RamlResourcePtr> ramlResources = raml->getResources();
    RAML::RamlResourcePtr ramlResource;
    if (0 == ramlResources.size() || (ramlResource = ramlResources.begin()->second) == nullptr)
    {
        OC_LOG(ERROR, TAG, "Zero resources detected from RAML!");
        return nullptr;
    }

    if (ramlResource)
    {
        SimulatorResourceServerImplSP simResource(new SimulatorResourceServerImpl());
        simResource->setName(ramlResource->getDisplayName());
        simResource->setURI(ramlResource->getResourceUri());

        // Get the resource representation schema from GET response body
        RAML::ActionPtr action = ramlResource->getAction(RAML::ActionType::GET);
        if (!action)
        {
            OC_LOG(ERROR, TAG, "Failed to create resource representation schema as it does not"
                   "posess the GET request!");
            return nullptr;
        }

        RAML::ResponsePtr getResponse = action->getResponse("200");
        if (!getResponse)
        {
            OC_LOG(ERROR, TAG, "Resource does not provide valid GET response!");
            return nullptr;
        }

        RAML::RequestResponseBodyPtr responseBody = getResponse->getResponseBody("application/json");
        if (responseBody)
        {
            RAML::JsonSchemaPtr resourceProperties = responseBody->getSchema()->getProperties();
            for ( auto & propertyElement : resourceProperties->getProperties())
            {
                if (!propertyElement.second)
                    continue;

                std::string propName = propertyElement.second->getName();
                if ("rt" == propName || "resourceType" == propName)
                {
                    simResource->setResourceType(propertyElement.second->getValueString());
                    continue;
                }
                else if ("if" == propName)
                {
                    simResource->setInterfaceType(propertyElement.second->getValueString());
                    continue;
                }
                else if ("p" == propName || "n" == propName || "id" == propName)
                {
                    continue;
                }

                // Build representation attribute
                SimulatorResourceModel::Attribute attribute(propName);
                switch (propertyElement.second->getValueType())
                {
                    case 0: // Integer
                        attribute.setValue(propertyElement.second->getValue<int>());
                        break;

                    case 1: // Double
                        attribute.setValue(propertyElement.second->getValue<double>());
                        break;

                    case 2: // Boolean
                        attribute.setValue(propertyElement.second->getValue<bool>());
                        break;

                    case 3: // String
                        attribute.setValue(propertyElement.second->getValue<std::string>());
                        break;
                }

                // Set range/supported values set
                int min = 0, max = 0, multipleof = 0;
                propertyElement.second->getRange(min, max, multipleof);
                attribute.setRange(min, max);

                if (propertyElement.second->getAllowedValuesSize() > 0)
                    attribute.setAllowedValues(propertyElement.second->getAllowedValues());

                simResource->addAttribute(attribute);
            }
        }

        simResource->setURI(constructURI(simResource->getURI()));
        return simResource;
    }

    return nullptr;
}