libbmq  a5f8a06ba1d16cb5a65643e1fa7f1a1d6aadef40
bmqt_uri.h
Go to the documentation of this file.
1 // Copyright 2014-2023 Bloomberg Finance L.P.
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 // bmqt_uri.h -*-C++-*-
17 #ifndef INCLUDED_BMQT_URI
18 #define INCLUDED_BMQT_URI
19 
128 
129 // BMQ
130 
131 // BDE
132 #include <ball_log.h>
133 #include <bsl_cstddef.h>
134 #include <bsl_iosfwd.h>
135 #include <bsl_string.h>
136 #include <bslh_hash.h>
137 #include <bslma_usesbslmaallocator.h>
138 #include <bslmf_nestedtraitdeclaration.h>
139 
140 namespace BloombergLP {
141 
142 // FORWARD DECLARATION
143 namespace bmqt {
144 class UriBuilder;
145 }
146 namespace bmqt {
147 struct UriParser;
148 }
149 
150 namespace bmqt {
151 
152 // =========
153 // class Uri
154 // =========
155 
157 class Uri {
158  public:
159  // PUBLIC CONSTANTS
160 
162  static const int k_QUEUENAME_MAX_LENGTH = 64;
163 
164  private:
165  // CLASS-SCOPE CATEGORY
166  BALL_LOG_SET_CLASS_CATEGORY("BMQT.URI");
167 
168  private:
169  // FRIENDS
170  friend struct UriParser;
171  friend class UriBuilder;
172  template <class HASH_ALGORITHM>
173  friend void hashAppend(HASH_ALGORITHM& hashAlgo, const Uri& uri);
174 
175  private:
176  // DATA
177 
179  bsl::string d_uri;
180 
182  bslstl::StringRef d_scheme;
183 
185  bslstl::StringRef d_authority;
186 
188  bslstl::StringRef d_domain;
189 
191  bslstl::StringRef d_tier;
192 
194  bslstl::StringRef d_path;
195 
197  bslstl::StringRef d_query_id;
198 
201  bool d_wasParserInitialized;
202 
203  private:
204  // PRIVATE MANIPULATORS
205 
207  void reset();
208 
211  void copyImpl(const Uri& src);
212 
213  public:
214  // TRAITS
215  BSLMF_NESTED_TRAIT_DECLARATION(Uri, bslma::UsesBslmaAllocator)
216 
217  // CREATORS
218 
219 
221  explicit Uri(bslma::Allocator* allocator = 0);
222 
226  Uri(const Uri& original,
227  bslma::Allocator* allocator = 0); // IMPLICIT
228 
233  Uri(const bsl::string& uri,
234  bslma::Allocator* allocator = 0); // IMPLICIT
235  Uri(const bslstl::StringRef& uri,
236  bslma::Allocator* allocator = 0); // IMPLICIT
237  Uri(const char* uri,
238  bslma::Allocator* allocator = 0); // IMPLICIT
239 
241  ~Uri();
242 
243  // MANIPULATORS
244 
246  Uri& operator=(const Uri& rhs);
247 
248  // ACCESSORS
249 
251  const bsl::string& asString() const;
252 
254  bool isValid() const;
255 
257  bool isCanonical() const;
258 
259  const bslstl::StringRef& scheme() const;
260  const bslstl::StringRef& authority() const;
261 
264  const bslstl::StringRef& path() const;
265 
266  const bslstl::StringRef& qualifiedDomain() const;
267  const bslstl::StringRef& domain() const;
268  const bslstl::StringRef& tier() const;
269  const bslstl::StringRef& queue() const;
270 
273  const bslstl::StringRef& id() const;
274 
277  bslstl::StringRef canonical() const;
278 
288  bsl::ostream&
289  print(bsl::ostream& stream, int level = 0, int spacesPerLevel = 4) const;
290 };
291 
292 // FREE OPERATORS
293 
297 bool operator==(const Uri& lhs, const Uri& rhs);
298 
302 bool operator!=(const Uri& lhs, const Uri& rhs);
303 
306 bool operator<(const Uri& lhs, const Uri& rhs);
307 
310 bsl::ostream& operator<<(bsl::ostream& stream, const Uri& rhs);
311 
312 // ================
313 // struct UriParser
314 // ================
315 
317 struct UriParser {
318  // CLASS METHODS
319 
329  static void initialize(bslma::Allocator* allocator = 0);
330 
339  static void shutdown();
340 
349  static int parse(Uri* result,
350  bsl::string* errorDescription,
351  const bslstl::StringRef& uriString);
352 
353  private:
354  // CLASS-SCOPE CATEGORY
355  BALL_LOG_SET_CLASS_CATEGORY("BMQT.URI");
356 };
357 
358 // ================
359 // class UriBuilder
360 // ================
361 
364 class UriBuilder {
365  private:
366  // DATA
367  Uri d_uri; // Placeholder object for URI object being built.
368 
369  private:
370  // NOT IMPLEMENTED
371 
373  UriBuilder(const UriBuilder&);
375 
376  public:
377  // TRAITS
378  BSLMF_NESTED_TRAIT_DECLARATION(UriBuilder, bslma::UsesBslmaAllocator)
379 
380  // CREATORS
381 
382 
383  explicit UriBuilder(bslma::Allocator* allocator = 0);
384 
387  explicit UriBuilder(const bmqt::Uri& uri, bslma::Allocator* allocator = 0);
388 
389  // MANIPULATORS
390  UriBuilder& setDomain(const bslstl::StringRef& value);
391  UriBuilder& setTier(const bslstl::StringRef& value);
392  UriBuilder& setQualifiedDomain(const bslstl::StringRef& value);
393  UriBuilder& setQueue(const bslstl::StringRef& value);
394 
399  UriBuilder& setId(const bslstl::StringRef& value);
400 
402  void reset();
403 
404  // ACCESSORS
405 
409  int uri(Uri* result, bsl::string* errorDescription = 0) const;
410 };
411 
412 // ============================================================================
413 // INLINE DEFINITIONS
414 // ============================================================================
415 
416 // ---------
417 // class Uri
418 // ---------
419 
420 inline Uri& Uri::operator=(const Uri& rhs)
421 {
422  copyImpl(rhs);
423 
424  return *this;
425 }
426 
427 inline const bsl::string& Uri::asString() const
428 {
429  return d_uri;
430 }
431 
432 inline bool Uri::isValid() const
433 {
434  // URI can only be set using either 'UriBuilder' or 'UriParser', which are
435  // resetting the d_uri on failure.
436  return !d_uri.empty();
437 }
438 
439 inline bool Uri::isCanonical() const
440 {
441  return d_query_id.isEmpty();
442 }
443 
444 inline const bslstl::StringRef& Uri::scheme() const
445 {
446  return d_scheme;
447 }
448 
449 inline const bslstl::StringRef& Uri::authority() const
450 {
451  return d_authority;
452 }
453 
454 inline const bslstl::StringRef& Uri::path() const
455 {
456  return d_path;
457 }
458 
459 inline const bslstl::StringRef& Uri::domain() const
460 {
461  return d_domain;
462 }
463 
464 inline const bslstl::StringRef& Uri::qualifiedDomain() const
465 {
466  return d_authority;
467 }
468 
469 inline const bslstl::StringRef& Uri::tier() const
470 {
471  return d_tier;
472 }
473 
474 inline const bslstl::StringRef& Uri::queue() const
475 {
476  return d_path;
477 }
478 
479 inline const bslstl::StringRef& Uri::id() const
480 {
481  return d_query_id;
482 }
483 
484 inline bslstl::StringRef Uri::canonical() const
485 {
486  size_t queryBeginPos = d_uri.find_first_of('?');
487  if (bsl::string::npos == queryBeginPos) {
488  return d_uri; // RETURN
489  }
490 
491  return bslstl::StringRef(d_uri.c_str(), queryBeginPos);
492 }
493 
494 // FREE FUNCTIONS
495 template <class HASH_ALGORITHM>
496 void hashAppend(HASH_ALGORITHM& hashAlgo, const Uri& uri)
497 {
498  using bslh::hashAppend; // for ADL
499  hashAppend(hashAlgo, uri.d_uri); // hashes full uri string
500 }
501 
502 // ----------------
503 // class UriBuilder
504 // ----------------
505 
506 inline UriBuilder& UriBuilder::setDomain(const bslstl::StringRef& value)
507 {
508  d_uri.d_domain = value;
509  return *this;
510 }
511 
512 inline UriBuilder& UriBuilder::setTier(const bslstl::StringRef& value)
513 {
514  d_uri.d_tier = value;
515  return *this;
516 }
517 
518 inline UriBuilder& UriBuilder::setQueue(const bslstl::StringRef& value)
519 {
520  d_uri.d_path = value;
521  return *this;
522 }
523 
524 inline UriBuilder& UriBuilder::setId(const bslstl::StringRef& value)
525 {
526  d_uri.d_query_id = value;
527  return *this;
528 }
529 
530 } // close package namespace
531 
532 // FREE OPERATORS
533 
534 inline bool bmqt::operator==(const bmqt::Uri& lhs, const bmqt::Uri& rhs)
535 {
536  return (lhs.asString() == rhs.asString());
537 }
538 
539 inline bool bmqt::operator!=(const bmqt::Uri& lhs, const bmqt::Uri& rhs)
540 {
541  return (lhs.asString() != rhs.asString());
542 }
543 
544 inline bool bmqt::operator<(const bmqt::Uri& lhs, const bmqt::Uri& rhs)
545 {
546  return (lhs.asString() < rhs.asString());
547 }
548 
549 inline bsl::ostream& bmqt::operator<<(bsl::ostream& stream,
550  const bmqt::Uri& rhs)
551 {
552  return rhs.print(stream, 0, -1);
553 }
554 
555 } // close enterprise namespace
556 
557 #endif
Definition: bmqt_uri.h:364
UriBuilder & setTier(const bslstl::StringRef &value)
Definition: bmqt_uri.h:512
UriBuilder & setId(const bslstl::StringRef &value)
Definition: bmqt_uri.h:524
UriBuilder & setDomain(const bslstl::StringRef &value)
Definition: bmqt_uri.h:506
UriBuilder & setQueue(const bslstl::StringRef &value)
Definition: bmqt_uri.h:518
Value semantic type representing a URI.
Definition: bmqt_uri.h:157
const bslstl::StringRef & domain() const
Definition: bmqt_uri.h:459
const bslstl::StringRef & queue() const
Definition: bmqt_uri.h:474
const bslstl::StringRef & path() const
Definition: bmqt_uri.h:454
const bslstl::StringRef & qualifiedDomain() const
Definition: bmqt_uri.h:464
bsl::ostream & print(bsl::ostream &stream, int level=0, int spacesPerLevel=4) const
const bslstl::StringRef & authority() const
Definition: bmqt_uri.h:449
bool isValid() const
Return true if this object represents a valid URI.
Definition: bmqt_uri.h:432
const bslstl::StringRef & tier() const
Definition: bmqt_uri.h:469
Uri & operator=(const Uri &rhs)
Set the value of this object to the specified rhs.
Definition: bmqt_uri.h:420
static const int k_QUEUENAME_MAX_LENGTH
The maximum authorized length for the queue name part of the URI.
Definition: bmqt_uri.h:162
friend class UriBuilder
Definition: bmqt_uri.h:171
bool isCanonical() const
Return true if this object represents a canonical URI.
Definition: bmqt_uri.h:439
const bsl::string & asString() const
Return the string representation of this URI.
Definition: bmqt_uri.h:427
const bslstl::StringRef & id() const
Definition: bmqt_uri.h:479
const bslstl::StringRef & scheme() const
Definition: bmqt_uri.h:444
bslstl::StringRef canonical() const
Definition: bmqt_uri.h:484
friend void hashAppend(HASH_ALGORITHM &hashAlgo, const Uri &uri)
Definition: bmqt_uri.h:496
void hashAppend(HASH_ALGORITHM &hashAlgo, const Uri &uri)
Definition: bmqt_uri.h:496
bsl::ostream & operator<<(bsl::ostream &stream, CompressionAlgorithmType::Enum value)
Definition: bmqt_compressionalgorithmtype.h:141
bool operator!=(const CorrelationId &lhs, const CorrelationId &rhs)
Definition: bmqt_correlationid.h:582
bool operator==(const CorrelationId &lhs, const CorrelationId &rhs)
Definition: bmqt_correlationid.h:576
bool operator<(const CorrelationId &lhs, const CorrelationId &rhs)
Definition: bmqt_correlationid.h:588
Definition: bmqa_abstractsession.h:42
Utility namespace of methods for parsing URI strings into Uri objects.
Definition: bmqt_uri.h:317
static void initialize(bslma::Allocator *allocator=0)
static int parse(Uri *result, bsl::string *errorDescription, const bslstl::StringRef &uriString)