Skip to content

Commit 79c1074

Browse files
Now validate ETP URI before to get info from it in EtpHelpers
1 parent cc11886 commit 79c1074

4 files changed

Lines changed: 26 additions & 76 deletions

File tree

cmake/swigEtp1_2Include.i.in

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2129,19 +2129,14 @@ namespace ETP_NS
21292129
std::string getDataspaceUri(const std::string& uri);
21302130

21312131
/**
2132-
* @param session Provide this parameter if you want to send a protocol exception in case of non validation.
2132+
* Validate an ETP URI
21332133
*/
2134-
Energistics::Etp::v12::Datatypes::ErrorInfo validateUri(const std::string & uri, ETP_NS::AbstractSession* session = nullptr);
2134+
bool validateUri(const std::string& uri);
21352135

21362136
/**
2137-
* @param session Provide this parameter if you want to send a protocol exception in case of non validation.
2137+
* @Validate an ETP dataobject URI
21382138
*/
2139-
Energistics::Etp::v12::Datatypes::ErrorInfo validateDataObjectUri(const std::string & uri, ETP_NS::AbstractSession* session = nullptr);
2140-
2141-
/**
2142-
* Build a protocol exception message which only contains a single error message (not a messsage map).
2143-
*/
2144-
Energistics::Etp::v12::Protocol::Core::ProtocolException buildSingleMessageProtocolException(int32_t m_code, const std::string & m_message);
2139+
bool validateDataObjectUri(const std::string& uri);
21452140
}
21462141

21472142
/******************* CLIENT ***************************/

example/withFesapi/etpClient.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ void askUser(std::shared_ptr<ETP_NS::AbstractSession> session, COMMON_NS::DataOb
290290
ctxInfo.includeSecondaryTargets = false;
291291
ctxInfo.includeSecondarySources = false;
292292
const auto resources = session->getResources(ctxInfo, Energistics::Etp::v12::Datatypes::Object::ContextScopeKind::targets);
293-
std::cout << "************ GET ALL DATAOBJECTS ************" << std::endl;
293+
std::cout << "************ GET ALL " << resources.size() << " DATAOBJECTS ************" << std::endl;
294294
if (!resources.empty()) {
295295
std::map< std::string, std::string > query;
296296
size_t index = 0;
@@ -737,7 +737,7 @@ int main(int argc, char **argv)
737737

738738
std::cout << "Creating a client session..." << std::endl;
739739
auto clientSession = ETP_NS::ClientSessionLaunchers::createClientSession(&initializationParams, authorization);
740-
clientSession->setVerbose(true);
740+
clientSession->setVerbose(false);
741741

742742
repo.setHdfProxyFactory(new ETP_NS::FesapiHdfProxyFactory(clientSession.get()));
743743

src/etp/EtpHelpers.cpp

Lines changed: 16 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,16 @@ under the License.
1818
-----------------------------------------------------------------------*/
1919
#include "EtpHelpers.h"
2020

21-
#if (defined(_WIN32) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9))))
2221
#include <regex>
23-
#endif
2422

25-
#include "AbstractSession.h"
2623
#include "EtpException.h"
2724

2825
std::string ETP_NS::EtpHelpers::getDataObjectType(const std::string& uri)
2926
{
27+
if (!ETP_NS::EtpHelpers::validateDataObjectUri(uri)) {
28+
throw ETP_NS::EtpException(9, "The dataobject ETP URI \"" + uri + "\" is invalid.");
29+
}
30+
3031
const std::size_t lastSlash = uri.find_last_of("/");
3132
const std::size_t lastOpenParenthesis = uri.find_last_of("(");
3233
return lastSlash != std::string::npos && lastOpenParenthesis != std::string::npos
@@ -36,9 +37,13 @@ std::string ETP_NS::EtpHelpers::getDataObjectType(const std::string& uri)
3637

3738
std::string ETP_NS::EtpHelpers::getDataspaceUri(const std::string& uri)
3839
{
40+
if (!ETP_NS::EtpHelpers::validateUri(uri)) {
41+
throw ETP_NS::EtpException(9, "The ETP URI \"" + uri + "\" is invalid.");
42+
}
43+
3944
const size_t dataspacePos = uri.find("dataspace('");
4045
if (dataspacePos == std::string::npos) {
41-
return "";
46+
return "eml:///";
4247
}
4348

4449
const size_t closingParenthesisPos = uri.find(')', dataspacePos);
@@ -48,72 +53,23 @@ std::string ETP_NS::EtpHelpers::getDataspaceUri(const std::string& uri)
4853
return uri.substr(0, closingParenthesisPos+1);
4954
}
5055

51-
Energistics::Etp::v12::Datatypes::ErrorInfo ETP_NS::EtpHelpers::validateUri(const std::string & uri, ETP_NS::AbstractSession* session)
56+
bool ETP_NS::EtpHelpers::validateUri(const std::string & uri)
5257
{
53-
Energistics::Etp::v12::Datatypes::ErrorInfo errorInfo;
54-
errorInfo.code = -1;
5558
// Regular expressions are not handled before GCC 4.9
5659
// https://stackoverflow.com/questions/12530406/is-gcc-4-8-or-earlier-buggy-about-regular-expressions
57-
#if (defined(_WIN32) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9))))
58-
const bool result =
60+
return
5961
std::regex_match(uri, std::regex("^eml:///(dataspace[(]'.*'[)])?", std::regex::ECMAScript)) ||
6062
std::regex_match(uri, std::regex("^eml:///(dataspace[(]'.*'[)]/)?(resqml20|eml20)\.obj_[a-zA-Z0-9]+[(][a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}(,.*)?[)]", std::regex::ECMAScript)) ||
6163
std::regex_match(uri, std::regex("^eml:///(dataspace[(]'.*'[)]/)?(witsml|resqml|prodml|eml)([0-9]{2})\[a-zA-Z0-9]+[(][a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}(,.*)?[)]", std::regex::ECMAScript));
62-
if (!result) {
63-
std::cerr << "The URI \"" + uri + "\" is invalid." << std::endl;
64-
}
65-
#else
66-
const bool result = uri.find("eml:///") == 0;
67-
#endif
68-
69-
if (!result) {
70-
errorInfo.code = 9;
71-
errorInfo.message = "The URI " + uri + " is invalid.";
72-
73-
if (session != nullptr) {
74-
Energistics::Etp::v12::Protocol::Core::ProtocolException error;
75-
error.error.emplace(errorInfo);
76-
session->send(error);
77-
}
78-
}
79-
80-
return errorInfo;
81-
8264
}
8365

84-
Energistics::Etp::v12::Datatypes::ErrorInfo ETP_NS::EtpHelpers::validateDataObjectUri(const std::string & uri, AbstractSession* session)
66+
bool ETP_NS::EtpHelpers::validateDataObjectUri(const std::string & uri)
8567
{
86-
Energistics::Etp::v12::Datatypes::ErrorInfo errorInfo;
87-
errorInfo.code = -1;
8868
// Regular expressions are not handled before GCC 4.9
8969
// https://stackoverflow.com/questions/12530406/is-gcc-4-8-or-earlier-buggy-about-regular-expressions
90-
#if (defined(_WIN32) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9))))
91-
const bool result = (uri.find("resqml20") != std::string::npos || uri.find("eml20") != std::string::npos)
70+
return (uri.find("resqml20") != std::string::npos || uri.find("eml20") != std::string::npos)
9271
? std::regex_match(uri, std::regex("^eml:///(dataspace[(]'.*'[)]/)?(resqml20|eml20)\.obj_[a-zA-Z0-9]+[(][a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}(,.*)?[)]", std::regex::ECMAScript))
9372
: std::regex_match(uri, std::regex("^eml:///(dataspace[(]'.*'[)]/)?(witsml|resqml|prodml|eml)([0-9]{2})\.[a-zA-Z0-9]+[(][a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}(,.*)?[)]", std::regex::ECMAScript));
94-
if (!result) {
95-
std::cerr << "The data object URI \"" + uri + "\" is invalid." << std::endl;
96-
}
97-
#else
98-
const bool result = uri.find("eml:///") == 0 &&
99-
(uri.find("resqml20.obj_") != std::string::npos || uri.find("eml20.obj_") != std::string::npos ||
100-
uri.find("witsml20.") != std::string::npos || uri.find("eml21.") != std::string::npos ||
101-
uri.find("prodml21.") != std::string::npos || uri.find("eml22.") != std::string::npos ||
102-
uri.find("witsml21.") != std::string::npos);
103-
#endif
104-
105-
if (!result) {
106-
errorInfo.code = 9;
107-
errorInfo.message = "The data object URI " + uri + " is invalid.";
108-
109-
if (session != nullptr) {
110-
Energistics::Etp::v12::Protocol::Core::ProtocolException error;
111-
error.error.emplace(errorInfo);
112-
session->send(error);
113-
}
114-
}
115-
116-
return errorInfo;
11773
}
11874

11975
Energistics::Etp::v12::Protocol::Core::ProtocolException ETP_NS::EtpHelpers::buildSingleMessageProtocolException(int32_t m_code, const std::string & m_message)
@@ -131,11 +87,11 @@ std::pair<std::string, std::string> ETP_NS::EtpHelpers::getUuidAndVersionFromUri
13187
{
13288
std::pair<std::string, std::string> result;
13389

134-
Energistics::Etp::v12::Datatypes::ErrorInfo error = ETP_NS::EtpHelpers::validateDataObjectUri(uri);
135-
if (error.code > -1) {
136-
throw ETP_NS::EtpException(error.code, error.message);
90+
if (!ETP_NS::EtpHelpers::validateDataObjectUri(uri)) {
91+
throw ETP_NS::EtpException(9, "Invalid URI");
13792
}
13893

94+
// old code to be updated
13995
if (uri[6] != '/') {
14096
throw ETP_NS::EtpException(2, "The URI " + uri + " uses some dataspaces. This agent does not support dataspace.");
14197
}

src/etp/EtpHelpers.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ namespace COMMON_NS
4040

4141
namespace ETP_NS
4242
{
43-
class AbstractSession;
4443

4544
namespace EtpHelpers {
4645

@@ -61,14 +60,14 @@ namespace ETP_NS
6160
FETPAPI_DLL_IMPORT_OR_EXPORT std::string getDataspaceUri(const std::string& uri);
6261

6362
/**
64-
* @param session Provide this parameter if you want to send a protocol exception in case of non validation.
63+
* Validate an ETP URI
6564
*/
66-
FETPAPI_DLL_IMPORT_OR_EXPORT Energistics::Etp::v12::Datatypes::ErrorInfo validateUri(const std::string & uri, AbstractSession* session = nullptr);
65+
FETPAPI_DLL_IMPORT_OR_EXPORT bool validateUri(const std::string& uri);
6766

6867
/**
69-
* @param session Provide this parameter if you want to send a protocol exception in case of non validation.
68+
* @Validate an ETP dataobject URI
7069
*/
71-
FETPAPI_DLL_IMPORT_OR_EXPORT Energistics::Etp::v12::Datatypes::ErrorInfo validateDataObjectUri(const std::string & uri, AbstractSession* session = nullptr);
70+
FETPAPI_DLL_IMPORT_OR_EXPORT bool validateDataObjectUri(const std::string& uri);
7271

7372
/**
7473
* Build a protocol exception message which only contains a single error message (not a messsage map).

0 commit comments

Comments
 (0)