| 1 | // |
| 2 | // URIStreamOpener.h |
| 3 | // |
| 4 | // Library: Foundation |
| 5 | // Package: URI |
| 6 | // Module: URIStreamOpener |
| 7 | // |
| 8 | // Definition of the URIStreamOpener class. |
| 9 | // |
| 10 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
| 11 | // and Contributors. |
| 12 | // |
| 13 | // SPDX-License-Identifier: BSL-1.0 |
| 14 | // |
| 15 | |
| 16 | |
| 17 | #ifndef Foundation_URIStreamOpener_INCLUDED |
| 18 | #define Foundation_URIStreamOpener_INCLUDED |
| 19 | |
| 20 | |
| 21 | #include "Poco/Foundation.h" |
| 22 | #include "Poco/Mutex.h" |
| 23 | #include <istream> |
| 24 | #include <map> |
| 25 | |
| 26 | |
| 27 | namespace Poco { |
| 28 | |
| 29 | |
| 30 | class URI; |
| 31 | class URIStreamFactory; |
| 32 | class Path; |
| 33 | |
| 34 | |
| 35 | class Foundation_API URIStreamOpener |
| 36 | /// The URIStreamOpener class is used to create and open input streams |
| 37 | /// for resourced identified by Uniform Resource Identifiers. |
| 38 | /// |
| 39 | /// For every URI scheme used, a URIStreamFactory must be registered. |
| 40 | /// A FileStreamFactory is automatically registered for file URIs. |
| 41 | { |
| 42 | public: |
| 43 | enum |
| 44 | { |
| 45 | MAX_REDIRECTS = 10 |
| 46 | }; |
| 47 | |
| 48 | URIStreamOpener(); |
| 49 | /// Creates the URIStreamOpener and registers a FileStreamFactory |
| 50 | /// for file URIs. |
| 51 | |
| 52 | ~URIStreamOpener(); |
| 53 | /// Destroys the URIStreamOpener and deletes all registered |
| 54 | /// URI stream factories. |
| 55 | |
| 56 | std::istream* open(const URI& uri) const; |
| 57 | /// Tries to create and open an input stream for the resource specified |
| 58 | /// by the given uniform resource identifier. |
| 59 | /// |
| 60 | /// If no URIStreamFactory has been registered for the URI's |
| 61 | /// scheme, a UnknownURIScheme exception is thrown. |
| 62 | /// If the stream cannot be opened for any reason, an |
| 63 | /// IOException is thrown. |
| 64 | /// |
| 65 | /// The given URI must be a valid one. This excludes file system paths. |
| 66 | /// |
| 67 | /// Whoever calls the method is responsible for deleting |
| 68 | /// the returned stream. |
| 69 | |
| 70 | std::istream* open(const std::string& pathOrURI) const; |
| 71 | /// Tries to create and open an input stream for the resource specified |
| 72 | /// by the given path or uniform resource identifier. |
| 73 | /// |
| 74 | /// If the stream cannot be opened for any reason, an |
| 75 | /// Exception is thrown. |
| 76 | /// |
| 77 | /// The method first tries to interpret the given pathOrURI as an URI. |
| 78 | /// If this fails, the pathOrURI is treated as local filesystem path. |
| 79 | /// If this also fails, an exception is thrown. |
| 80 | /// |
| 81 | /// Whoever calls the method is responsible for deleting |
| 82 | /// the returned stream. |
| 83 | |
| 84 | std::istream* open(const std::string& basePathOrURI, const std::string& pathOrURI) const; |
| 85 | /// Tries to create and open an input stream for the resource specified |
| 86 | /// by the given path or uniform resource identifier. |
| 87 | /// |
| 88 | /// pathOrURI is resolved against basePathOrURI (see URI::resolve() and |
| 89 | /// Path::resolve() for more information). |
| 90 | /// |
| 91 | /// If the stream cannot be opened for any reason, an |
| 92 | /// Exception is thrown. |
| 93 | /// |
| 94 | /// Whoever calls the method is responsible for deleting |
| 95 | /// the returned stream. |
| 96 | |
| 97 | void registerStreamFactory(const std::string& scheme, URIStreamFactory* pFactory); |
| 98 | /// Registers a URIStreamFactory for the given scheme. If another factory |
| 99 | /// has already been registered for the scheme, an ExistsException is thrown. |
| 100 | /// |
| 101 | /// The URIStreamOpener takes ownership of the factory and deletes it when it is |
| 102 | /// no longer needed (in other words, when the URIStreamOpener is deleted). |
| 103 | |
| 104 | void unregisterStreamFactory(const std::string& scheme); |
| 105 | /// Unregisters and deletes the URIStreamFactory for the given scheme. |
| 106 | /// |
| 107 | /// Throws a NotFoundException if no URIStreamFactory has been registered |
| 108 | /// for the given scheme. |
| 109 | |
| 110 | bool supportsScheme(const std::string& scheme); |
| 111 | /// Returns true iff a URIStreamFactory for the given scheme |
| 112 | /// has been registered. |
| 113 | |
| 114 | static URIStreamOpener& defaultOpener(); |
| 115 | /// Returns a reference to the default URIStreamOpener. |
| 116 | |
| 117 | protected: |
| 118 | std::istream* openFile(const Path& path) const; |
| 119 | std::istream* openURI(const std::string& scheme, const URI& uri) const; |
| 120 | |
| 121 | private: |
| 122 | URIStreamOpener(const URIStreamOpener&); |
| 123 | URIStreamOpener& operator = (const URIStreamOpener&); |
| 124 | |
| 125 | typedef std::map<std::string, URIStreamFactory*> FactoryMap; |
| 126 | |
| 127 | FactoryMap _map; |
| 128 | mutable FastMutex _mutex; |
| 129 | }; |
| 130 | |
| 131 | |
| 132 | } // namespace Poco |
| 133 | |
| 134 | |
| 135 | #endif // Foundation_URIStreamOpener_INCLUDED |
| 136 | |