StreamBase C++ API  7.7.2.3
StreamBaseURI.hpp
1 // Copyright (c) 2004-2017 Cloud Software Group, Inc. All rights reserved.
2 
3 #ifndef STREAMBASE_URI_H
4 #define STREAMBASE_URI_H
5 
6 #include "StreamBase.hpp"
7 
8 #include "Exceptions.hpp"
9 NMSTL_NAMESPACE_BEGIN;
10 class Address;
11 NMSTL_NAMESPACE_END;
12 
13 SB_INTERNAL_FWD(Errors);
14 SB_INTERNAL_FWD(StreamBaseURIUtil);
15 
16 SB_NAMESPACE_BEGIN;
17 
18 /// An invalid URI was specified in a StreamBaseURI constructor.
19 STREAMBASE_EXCEPTION_TYPE(MalformedURIException, sb_internal::Errors::NON_FATAL_ERROR);
20 
21 /// A URI for a StreamBase client connection. These URIs
22 /// are of the form
23 ///
24 /// \verbatim
25 /// sb://hostname:port/;param1=val1;param2=val2
26 /// \endverbatim
27 ///
28 /// e.g.,
29 ///
30 /// \verbatim
31 /// sb://somehost.mycompany.com:10000/;user=me;password=foobar
32 /// \endverbatim
33 ///
34 /// Parameters are optional. The port number is optional; it defaults
35 /// to 10000.
37  public:
38 
39  /// The default StreamBase port (10000).
40  static const unsigned int DEFAULT_PORT = 10000;
41 
42  /// The default container name
43  static std::string const DEFAULT_HOST;
44 
45  /// URI parameter used for username.
46  static const char * const USER_PARAM;
47 
48  /// URI parameter used for username.
49  static const char * const USERNAME_PARAM;
50 
51  /// URI parameter used for password.
52  static const char * const PASSWORD_PARAM;
53 
54  /// The default container name
55  static std::string const DEFAULT_CONTAINER;
56 
57  /// The system container name
58  static std::string const SYSTEM_CONTAINER;
59 
60  /// No container specification
61  static std::string const NO_CONTAINER;
62 
63  /// The default URI (pointing to localhost, port 10000, default container).
64  static const StreamBaseURI DEFAULT;
65 
66  /// The default URI
67  static StreamBaseURI const DEFAULT_URI;
68 
69 #ifndef DOXYGEN_SKIP
70  static const unsigned int DEFAULT_NODE_MANAGER_PORT = 10002;
71  static const char *const ENVIRONMENT_VARIABLE;
72 
73  static const char *const URI_ENVIRONMENT_VARIABLE;
74 
75  static const char *const URI_PROPERTY_NAME;
76 
77 #endif
78 
79  typedef std::map<std::string, std::string> ParamMap;
80  private:
81  std::string _host;
82  unsigned int _port;
83  std::string _container;
84 
85  ParamMap _params;
86 
87  public:
88  /// Default-constructed object; no operations are allowed.
90 
91  /// Parse URI from a string.
92  StreamBaseURI(const std::string &uri);
93 
94  /// Create explicitly from host/port.
95  StreamBaseURI(const std::string &host, unsigned int port, const std::string &user = "", const std::string &password = "");
96 
97  /// Create explicitly from host/port with container
98  StreamBaseURI(const std::string &host, const std::string &container, unsigned int port, const std::string &user = "", const std::string &password = "");
99 
100  /// Create explicitly from host/port, including a map of parameters
101  StreamBaseURI(const std::string &host, unsigned int port, const ParamMap &paramMap)
102  : _host(host), _port(port), _container(NO_CONTAINER)
103  {
104  copy(paramMap.begin(), paramMap.end(), inserter(_params, _params.begin()));
105  check();
106  }
107 
108  /// Create explicitly from host/port with container, including a map of parameters
109  StreamBaseURI(const std::string &host, const std::string &container, unsigned int port, const ParamMap &paramMap)
110  : _host(host), _port(port), _container(container)
111  {
112  copy(paramMap.begin(), paramMap.end(), inserter(_params, _params.begin()));
113  check();
114  }
115 
116  /// Return a StreamBaseURI vector from a string of comma sepearted URIs.
117  /// The string of URIs supplied must conform to a valid HA URI list, for example
118  /// they must all reference the same container.
119  static std::vector<StreamBaseURI> vectorFromString(const std::string& uris);
120 
121  /// Return a string representation of a list of uris
122  static std::string as_string(const std::vector<StreamBaseURI> &uris);
123 
124  /// Return the URI as a string.
125  std::string as_string() const;
126 
127  /// Return the host component of the URI.
128  std::string getHost() const { return _host; }
129 
130  /// Returns the container name component of the URI. May return an empty string
131  /// if no container was specified.
132  std::string getContainer() const { return _container; }
133 
134  /// Return the port component of the URI.
135  unsigned int getPort() const { return _port; }
136 
137  /// Return the user name component of the URI.
138  std::string getUser() const { return getParameter(USER_PARAM); }
139 
140  /// Return the password component of the URI.
141  std::string getPassword() const { return getParameter(PASSWORD_PARAM); }
142 
143  /// Return a parameter, or empty string if undefined.
144  std::string getParameter(const std::string &key) const { return NMSTL::lookup(_params, key); }
145 
146  /// Return a parameter, or empty string if undefined.
147  std::string getParam(const std::string &key) const { return NMSTL::lookup(_params, key); }
148 
149  /// Return a parameter into value (returning true if one was found).
150  /// Useful to distinguish an undefined parameter from an empty
151  /// one.
152  bool getParameter(const std::string &key, std::string& value) const;
153 
154  /// Add a parameter.
155  void addParameter(const std::string &key, const std::string &value) { _params[key] = value; }
156 
157  /// Add a parameter.
158  void removeParameter(std::string key) { _params.erase(key); }
159 
160  /// return the number of parameters
161  size_t getNumParameters() { return _params.size(); }
162 
163  /// Returns true if this object was not default-constructed.
164  operator const void *() const { return _host.empty() ? 0 : this; }
165 
166  /// Create from the environment variable
167  static StreamBaseURI fromEnvironment();
168 
169  /// Create from the environment variable and return a vector of StreamBaseURIs
170  static std::vector < StreamBaseURI > vectorFromEnvironment();
171 
172  /// return a StreamBaseURI without Parameters
173  const StreamBaseURI getBaseURI() const { return StreamBaseURI(_host, _container, _port); }
174 
175  private:
176  void init(NMSTL::Address address, const std::string &container);
177 
178  void processParams(const std::string &uri, std::string::size_type paramsPos);
179  // Make sure the URI is valid (currently, just check host name)
180  void check();
181 
182  // setup authentication
183  int auth(const std::string& user, const std::string& password);
184 
185  friend class sb_internal::StreamBaseURIUtil;
186 };
187 
188 SB_NAMESPACE_END;
189 
190 inline std::ostream& operator << (std::ostream& os, const sb::StreamBaseURI& uri) {
191  os << uri.as_string();
192  return os;
193 }
194 
195 #endif