22#include "moc_qgsserverogcapi.cpp"
27QMap<QgsServerOgcApi::ContentType, QStringList> QgsServerOgcApi::sContentTypeMime = [ ]() -> QMap<QgsServerOgcApi::ContentType, QStringList>
29 QMap<QgsServerOgcApi::ContentType, QStringList> map;
32 QStringLiteral(
"application/geo+json" ),
33 QStringLiteral(
"application/vnd.geo+json" ),
34 QStringLiteral(
"application/geojson" )
42QHash<QgsServerOgcApi::ContentType, QList<QgsServerOgcApi::ContentType>> QgsServerOgcApi::sContentTypeAliases = [ ]() -> QHash<ContentType, QList<ContentType>>
44 QHash<QgsServerOgcApi::ContentType, QList<QgsServerOgcApi::ContentType>> map;
52 mRootPath( rootPath ),
54 mDescription( description ),
67 std::shared_ptr<QgsServerOgcApiHandler> hp( handler );
68 mHandlers.emplace_back( std::move( hp ) );
74 QUrl u { url.adjusted( QUrl::StripTrailingSlash | QUrl::NormalizePathSegments ) };
75 if ( u.path().contains( QLatin1String(
"//" ) ) )
77 u.setPath( u.path().replace( QLatin1String(
"//" ), QChar(
'/' ) ) );
80 if ( !u.path().startsWith(
'/' ) )
82 u.setPath( u.path().prepend(
'/' ) );
92 auto hasMatch {
false };
93 for (
const auto &handler : mHandlers )
96 if ( handler->path().match( path ).hasMatch() )
104 handler->handleRequest( context );
106 catch ( json::exception &ex )
122 return sContentTypeMime;
127 return sContentTypeAliases;
132 static const QMetaEnum metaEnum = QMetaEnum::fromType<QgsServerOgcApi::Rel>();
133 std::string val { metaEnum.valueToKey( rel ) };
134 std::replace( val.begin(), val.end(),
'_',
'-' );
140 static const QMetaEnum metaEnum = QMetaEnum::fromType<ContentType>();
141 QString result { metaEnum.valueToKey( ct ) };
142 return result.replace(
'_',
'-' );
147 static const QMetaEnum metaEnum = QMetaEnum::fromType<ContentType>();
148 return metaEnum.valueToKey( ct );
158 const QString exts = QString::fromStdString( extension );
160 for (
auto it = constMimeTypes.constBegin();
161 it != constMimeTypes.constEnd();
164 const auto constValues = it.value();
165 for (
const auto &value : constValues )
167 if ( value.contains( exts, Qt::CaseSensitivity::CaseInsensitive ) )
174 QgsMessageLog::logMessage( QStringLiteral(
"Content type for extension %1 not found! Returning default (JSON)" ).arg( exts ),
175 QStringLiteral(
"Server" ),
182 if ( ! sContentTypeMime.contains( contentType ) )
186 return sContentTypeMime.value( contentType ).first().toStdString();
@ Warning
Warning message.
@ Info
Information message.
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...
QString handlerPath() const
Returns the handler component of the URL path, i.e.
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 and the path URL component that always starts with...
virtual void executeRequest(const QgsServerApiContext &context) const override
Executes a request by passing the given context to the API handlers.
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).
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.
ContentType
Media types used for content negotiation, insert more specific first.
@ OPENAPI3
"application/openapi+json;version=3.0"
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.
static QgsServerOgcApi::ContentType contentTypeFromExtension(const std::string &extension)
Returns the Content-Type value corresponding to extension.