26 QMap<QgsServerOgcApi::ContentType, QStringList> QgsServerOgcApi::sContentTypeMime = [ ]() -> QMap<QgsServerOgcApi::ContentType, QStringList>
28 QMap<QgsServerOgcApi::ContentType, QStringList> map;
29 map[QgsServerOgcApi::ContentType::JSON] = QStringList { QStringLiteral(
"application/json" ) };
30 map[QgsServerOgcApi::ContentType::GEOJSON] = QStringList {
31 QStringLiteral(
"application/geo+json" ),
32 QStringLiteral(
"application/vnd.geo+json" ),
33 QStringLiteral(
"application/geojson" )
35 map[QgsServerOgcApi::ContentType::HTML] = QStringList { QStringLiteral(
"text/html" ) };
36 map[QgsServerOgcApi::ContentType::OPENAPI3] = QStringList { QStringLiteral(
"application/vnd.oai.openapi+json;version=3.0" ) };
37 map[QgsServerOgcApi::ContentType::XML] = QStringList { QStringLiteral(
"application/xml" ) };
41 QHash<QgsServerOgcApi::ContentType, QList<QgsServerOgcApi::ContentType>> QgsServerOgcApi::sContentTypeAliases = [ ]() -> QHash<ContentType, QList<ContentType>>
43 QHash<QgsServerOgcApi::ContentType, QList<QgsServerOgcApi::ContentType>> map;
44 map[ContentType::JSON] = { QgsServerOgcApi::ContentType::GEOJSON, QgsServerOgcApi::ContentType::OPENAPI3 };
51 mRootPath( rootPath ),
53 mDescription( description ),
66 std::shared_ptr<QgsServerOgcApiHandler> hp( handler );
67 mHandlers.emplace_back( std::move( hp ) );
73 QUrl u { url.adjusted( QUrl::StripTrailingSlash | QUrl::NormalizePathSegments ) };
74 if ( u.path().contains( QLatin1String(
"//" ) ) )
76 u.setPath( u.path().replace( QLatin1String(
"//" ), QChar(
'/' ) ) );
86 auto hasMatch {
false };
87 for (
const auto &handler : mHandlers )
89 QgsMessageLog::logMessage( QStringLiteral(
"Checking API path %1 for %2 " ).arg( path, handler->path().pattern() ), QStringLiteral(
"Server" ), Qgis::MessageLevel::Info );
90 if ( handler->path().match( path ).hasMatch() )
94 QgsMessageLog::logMessage( QStringLiteral(
"API %1: found handler %2" ).arg(
name(), QString::fromStdString( handler->operationId() ) ), QStringLiteral(
"Server" ), Qgis::MessageLevel::Info );
98 handler->handleRequest( context );
100 catch ( json::exception &ex )
116 return sContentTypeMime;
121 return sContentTypeAliases;
126 static QMetaEnum metaEnum = QMetaEnum::fromType<QgsServerOgcApi::Rel>();
127 std::string val { metaEnum.valueToKey( rel ) };
128 std::replace( val.begin(), val.end(),
'_',
'-' );
134 static QMetaEnum metaEnum = QMetaEnum::fromType<ContentType>();
135 QString result { metaEnum.valueToKey( ct ) };
136 return result.replace(
'_',
'-' );
141 static QMetaEnum metaEnum = QMetaEnum::fromType<ContentType>();
142 return metaEnum.valueToKey( ct );
152 const QString exts = QString::fromStdString( extension );
154 for (
auto it = constMimeTypes.constBegin();
155 it != constMimeTypes.constEnd();
158 const auto constValues = it.value();
159 for (
const auto &value : constValues )
161 if ( value.contains( exts, Qt::CaseSensitivity::CaseInsensitive ) )
168 QgsMessageLog::logMessage( QStringLiteral(
"Content type for extension %1 not found! Returning default (JSON)" ).arg( exts ),
169 QStringLiteral(
"Server" ),
170 Qgis::MessageLevel::Warning );
171 return QgsServerOgcApi::ContentType::JSON;
176 if ( ! sContentTypeMime.contains( contentType ) )
180 return sContentTypeMime.value( contentType ).first().toStdString();
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
Bad request error API exception.
The QgsServerApiContext class encapsulates the resources for a particular client request: the request...
const QgsServerRequest * request() const
Returns the server request object.
Internal server error API exception.
Server generic API endpoint abstract base class.
QgsServerInterface Class defining interfaces exposed by QGIS Server and made available to plugins.
The QgsServerOgcApiHandler abstract class represents a OGC API handler to be registered in QgsServerO...
static QUrl sanitizeUrl(const QUrl &url)
Returns a sanitized url with extra slashes removed.
void registerHandler(Args... args)
Registers an OGC API handler passing Args to the constructor.
QgsServerOgcApi(QgsServerInterface *serverIface, const QString &rootPath, const QString &name, const QString &description=QString(), const QString &version=QString())
QgsServerOgcApi constructor.
static QString contentTypeToExtension(const QgsServerOgcApi::ContentType &ct)
Returns the file extension for a ct (Content-Type).
virtual void executeRequest(const QgsServerApiContext &context) const override SIP_THROW(QgsServerApiBadRequestException)
Executes a request by passing the given context to the API handlers.
const std::vector< std::shared_ptr< QgsServerOgcApiHandler > > handlers() const
Returns registered handlers.
static const QMap< QgsServerOgcApi::ContentType, QStringList > contentTypeMimes()
Returns a map of contentType => list of mime types.
static QgsServerOgcApi::ContentType contenTypeFromExtension(const std::string &extension)
Returns the Content-Type value corresponding to extension.
ContentType
Media types used for content negotiation, insert more specific first.
static QString contentTypeToString(const QgsServerOgcApi::ContentType &ct)
Returns the string representation of a ct (Content-Type) attribute.
static std::string contentTypeToStdString(const QgsServerOgcApi::ContentType &ct)
Returns the string representation of a ct (Content-Type) attribute.
static std::string mimeType(const QgsServerOgcApi::ContentType &contentType)
Returns the mime-type for the contentType or an empty string if not found.
~QgsServerOgcApi() override
const QString name() const override
Returns the API name.
static std::string relToString(const QgsServerOgcApi::Rel &rel)
Returns the string representation of rel attribute.
static const QHash< QgsServerOgcApi::ContentType, QList< QgsServerOgcApi::ContentType > > contentTypeAliases()
Returns contentType specializations (e.g.