espsecurecontext.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2016 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #ifndef ESPSECURECONTEXT_HPP
  14. #define ESPSECURECONTEXT_HPP
  15. #include "jiface.hpp"
  16. #include "tokenserialization.hpp"
  17. class CTxSummary;
  18. // Declares a protocol-independent interface to give security managers read
  19. // access to protocol-dependent data values. Subclasses determine the types
  20. // of data to which they will provide access. Callers are assumed to know
  21. // which data types they may request.
  22. //
  23. // For example, consider an HTTP request. A caller may need access to HTTP
  24. // cookies. In such a case, the caller may call getProtocol() to confirm that
  25. // the supported protocol is "http" and then may assume that a property type
  26. // of "cookie" will be supported.
  27. interface IEspSecureContext : extends IInterface
  28. {
  29. // Return a protocol-specific identifier. Callers may use this value to
  30. // confirm availability of required data types.
  31. virtual const char* getProtocol() const = 0;
  32. // Returns the TxSummary object to be used for a request.
  33. virtual CTxSummary* queryTxSummary() const = 0;
  34. // Fetches a data value based on a given type and name. If the requested
  35. // value exists it is stored in the supplied value buffer and true is
  36. // returned. If the requested value does not exist, the value buffer is
  37. // unchanged and false is returned.
  38. //
  39. // Acceptable values of the 'type' parameter are defined by protocol-
  40. // specific subclasses. E.g., an HTTP specific subclass might support
  41. // cookie data.
  42. //
  43. // Acceptable values of the 'name' parameter are dependent on the caller.
  44. virtual bool getProp(int type, const char* name, StringBuffer& value) = 0;
  45. // Implementation-independent wrapper of the abstract getProp method
  46. // providing convenient access to non-string values. Non-string means
  47. // numeric (including Boolean) values by default, but callers do have
  48. // the option to replace the default deserializer with one capable of
  49. // supporting more complex types.
  50. //
  51. // True is returned if the requested property exists and its value is
  52. // convertible to TValue. False is returned in all other cases. The
  53. // caller may request the conversion result code to understand why a
  54. // request failed.
  55. //
  56. // Template Parameters
  57. // - TValue: a type supported by of TDeserializer
  58. // - TDefault[TValue]: a type from which TValue can be initialized
  59. // - TDeserializer[TokenDeserializer]: a callback function, lambda, or
  60. // functor with signature:
  61. // DeserializationResult (*pfn)(const char*, TValue&)
  62. template <typename TValue, typename TDefault = TValue, class TDeserializer = TokenDeserializer>
  63. bool getProp(int type, const char* name, TValue& value, const TDefault dflt = TDefault(), DeserializationResult* deserializationResult = NULL, TDeserializer& deserializer = TDeserializer());
  64. };
  65. template <typename TValue, typename TDefault, class TDeserializer>
  66. inline bool IEspSecureContext::getProp(int type, const char* name, TValue& value, const TDefault dflt, DeserializationResult* deserializationResult, TDeserializer& deserializer)
  67. {
  68. DeserializationResult result = Deserialization_UNKNOWN;
  69. StringBuffer prop;
  70. bool found = getProp(type, name, prop);
  71. if (found)
  72. {
  73. result = deserializer(prop, value);
  74. found = (Deserialization_SUCCESS == result);
  75. }
  76. if (!found)
  77. {
  78. value = TValue(dflt);
  79. }
  80. if (deserializationResult)
  81. {
  82. *deserializationResult = result;
  83. }
  84. return found;
  85. }
  86. #endif // ESPSECURECONTEXT_HPP