QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
qgsserverogcapihandler.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsserverogcapihandler.cpp - QgsServerOgcApiHandler
3
4 ---------------------
5 begin : 10.7.2019
6 copyright : (C) 2019 by Alessandro Pasotti
7 email : elpaso at itopen dot it
8 ***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
17#include <QDateTime>
18#include <QFileInfo>
19#include <QDir>
20#include <QDebug>
21
22#include "qgsmessagelog.h"
23#include "qgsproject.h"
24#include "qgsjsonutils.h"
25#include "qgsvectorlayer.h"
26
28#include "qgsserverapiutils.h"
29#include "qgsserverresponse.h"
30#include "qgsserverinterface.h"
31
32#include "nlohmann/json.hpp"
33#include "inja/inja.hpp"
34
35using namespace nlohmann;
36using namespace inja;
37
38
39
40QVariantMap QgsServerOgcApiHandler::values( const QgsServerApiContext &context ) const
41{
42 QVariantMap result ;
43 const auto constParameters { parameters( context ) };
44 for ( const auto &p : constParameters )
45 {
46 // value() calls the validators and throws an exception if validation fails
47 result[p.name()] = p.value( context );
48 }
49 const auto sanitizedPath { QgsServerOgcApi::sanitizeUrl( context.handlerPath( ) ).path() };
50 const auto match { path().match( sanitizedPath ) };
51 if ( match.hasMatch() )
52 {
53 const auto constNamed { path().namedCaptureGroups() };
54 // Get named path parameters
55 for ( const auto &name : constNamed )
56 {
57 if ( ! name.isEmpty() )
58 result[name] = QUrlQuery( match.captured( name ) ).toString() ;
59 }
60 }
61 return result;
62}
63
65{
66 //qDebug() << "handler destroyed";
67}
68
70{
71 const auto constContentTypes( contentTypes() );
72 return constContentTypes.size() > 0 ? constContentTypes.first() : QgsServerOgcApi::ContentType::JSON;
73}
74
75QList<QgsServerOgcApi::ContentType> QgsServerOgcApiHandler::contentTypes() const
76{
77 return mContentTypes;
78}
79
81{
82 Q_UNUSED( context )
83 throw QgsServerApiNotImplementedException( QStringLiteral( "Subclasses must implement handleRequest" ) );
84}
85
86QString QgsServerOgcApiHandler::contentTypeForAccept( const QString &accept ) const
87{
88 const auto constContentTypes( QgsServerOgcApi::contentTypeMimes() );
89 for ( auto it = constContentTypes.constBegin();
90 it != constContentTypes.constEnd(); ++it )
91 {
92 const auto constValues = it.value();
93 for ( const auto &value : constValues )
94 {
95 if ( accept.contains( value, Qt::CaseSensitivity::CaseInsensitive ) )
96 {
97 return value;
98 }
99 }
100 }
101 // Log level info because this is not completely unexpected
102 QgsMessageLog::logMessage( QStringLiteral( "Content type for accept %1 not found!" ).arg( accept ),
103 QStringLiteral( "Server" ),
105
106 return QString();
107}
108
109void QgsServerOgcApiHandler::write( json &data, const QgsServerApiContext &context, const json &htmlMetadata ) const
110{
111 const QgsServerOgcApi::ContentType contentType { contentTypeFromRequest( context.request() ) };
112 switch ( contentType )
113 {
115 data["handler"] = schema( context );
116 if ( ! htmlMetadata.is_null() )
117 {
118 data["metadata"] = htmlMetadata;
119 }
120 htmlDump( data, context );
121 break;
125 jsonDump( data, context, QgsServerOgcApi::contentTypeMimes().value( contentType ).first() );
126 break;
128 // Not handled yet
129 break;
130 }
131}
132
133void QgsServerOgcApiHandler::write( QVariant &data, const QgsServerApiContext &context, const QVariantMap &htmlMetadata ) const
134{
135 json j = QgsJsonUtils::jsonFromVariant( data );
136 json jm = QgsJsonUtils::jsonFromVariant( htmlMetadata );
137 QgsServerOgcApiHandler::write( j, context, jm );
138}
139
140std::string QgsServerOgcApiHandler::href( const QgsServerApiContext &context, const QString &extraPath, const QString &extension ) const
141{
142 QUrl url { context.request()->url() };
143 QString urlBasePath { context.matchedPath() };
144 const auto match { path().match( QgsServerOgcApi::sanitizeUrl( context.handlerPath( ) ).path( ) ) };
145 if ( match.captured().count() > 0 )
146 {
147 url.setPath( urlBasePath + match.captured( 0 ) );
148 }
149 else
150 {
151 url.setPath( urlBasePath );
152 }
153
154 // Remove any existing extension
155 const auto suffixLength { QFileInfo( url.path() ).suffix().length() };
156 if ( suffixLength > 0 )
157 {
158 auto path {url.path()};
159 path.truncate( path.length() - ( suffixLength + 1 ) );
160 url.setPath( path );
161 }
162
163 // Add extra path
164 url.setPath( url.path() + extraPath );
165
166 // (re-)add extension
167 // JSON is the default anyway so we don't need to add it
168 if ( ! extension.isEmpty() )
169 {
170 // Remove trailing slashes if any.
171 QString path { url.path() };
172 while ( path.endsWith( '/' ) )
173 {
174 path.chop( 1 );
175 }
176 url.setPath( path + '.' + extension );
177 }
178 return QgsServerOgcApi::sanitizeUrl( url ).toString( QUrl::FullyEncoded ).toStdString();
179
180}
181
182void QgsServerOgcApiHandler::jsonDump( json &data, const QgsServerApiContext &context, const QString &contentType ) const
183{
184 // Do not append timestamp to openapi
185 if ( ! QgsServerOgcApi::contentTypeMimes().value( QgsServerOgcApi::ContentType::OPENAPI3 ).contains( contentType, Qt::CaseSensitivity::CaseInsensitive ) )
186 {
187 QDateTime time { QDateTime::currentDateTime() };
188 time.setTimeSpec( Qt::TimeSpec::UTC );
189 data["timeStamp"] = time.toString( Qt::DateFormat::ISODate ).toStdString() ;
190 }
191 context.response()->setStatusCode( 200 );
192 context.response()->setHeader( QStringLiteral( "Content-Type" ), contentType );
193#ifdef QGISDEBUG
194 context.response()->write( data.dump( 2 ) );
195#else
196 context.response()->write( data.dump( ) );
197#endif
198}
199
201{
202 Q_UNUSED( context )
203 return nullptr;
204}
205
206json QgsServerOgcApiHandler::link( const QgsServerApiContext &context, const QgsServerOgcApi::Rel &linkType, const QgsServerOgcApi::ContentType contentType, const std::string &title ) const
207{
208 json l
209 {
210 {
211 "href", href( context, "/",
213 },
215 { "type", QgsServerOgcApi::mimeType( contentType ) },
216 { "title", title != "" ? title : linkTitle() },
217 };
218 return l;
219}
220
222{
223 const QgsServerOgcApi::ContentType currentCt { contentTypeFromRequest( context.request() ) };
224 json links = json::array();
225 const QList<QgsServerOgcApi::ContentType> constCts { contentTypes() };
226 for ( const auto &ct : constCts )
227 {
228 links.push_back( link( context, ( ct == currentCt ? QgsServerOgcApi::Rel::self :
231 }
232 return links;
233}
234
236{
237 if ( ! context.project() )
238 {
239 throw QgsServerApiImproperlyConfiguredException( QStringLiteral( "Project is invalid or undefined" ) );
240 }
241 // Check collectionId
242 const QRegularExpressionMatch match { path().match( context.request()->url().path( ) ) };
243 if ( ! match.hasMatch() )
244 {
245 throw QgsServerApiNotFoundError( QStringLiteral( "Collection was not found" ) );
246 }
247 const QString collectionId { match.captured( QStringLiteral( "collectionId" ) ) };
248 // May throw if not found
249 return layerFromCollectionId( context, collectionId );
250
251}
252
253const QString QgsServerOgcApiHandler::staticPath( const QgsServerApiContext &context ) const
254{
255 // resources/server/api + /static
256 return context.serverInterface()->serverSettings()->apiResourcesDirectory() + QStringLiteral( "/ogc/static" );
257}
258
260{
261 // resources/server/api + /ogc/templates/ + operationId + .html
262 QString path { context.serverInterface()->serverSettings()->apiResourcesDirectory() };
263 path += QLatin1String( "/ogc/templates" );
264 path += context.apiRootPath();
265 path += '/';
266 path += QString::fromStdString( operationId() );
267 path += QLatin1String( ".html" );
268 return path;
269}
270
271
272void QgsServerOgcApiHandler::htmlDump( const json &data, const QgsServerApiContext &context ) const
273{
274 context.response()->setHeader( QStringLiteral( "Content-Type" ), QStringLiteral( "text/html" ) );
275 auto path { templatePath( context ) };
276 if ( ! QFile::exists( path ) )
277 {
278 QgsMessageLog::logMessage( QStringLiteral( "Template not found error: %1" ).arg( path ), QStringLiteral( "Server" ), Qgis::MessageLevel::Critical );
279 throw QgsServerApiBadRequestException( QStringLiteral( "Template not found: %1" ).arg( QFileInfo( path ).fileName() ) );
280 }
281
282 QFile f( path );
283 if ( ! f.open( QFile::ReadOnly | QFile::Text ) )
284 {
285 QgsMessageLog::logMessage( QStringLiteral( "Could not open template file: %1" ).arg( path ), QStringLiteral( "Server" ), Qgis::MessageLevel::Critical );
286 throw QgsServerApiInternalServerError( QStringLiteral( "Could not open template file: %1" ).arg( QFileInfo( path ).fileName() ) );
287 }
288
289 try
290 {
291 // Get the template directory and the file name
292 QFileInfo pathInfo { path };
293 Environment env { QString( pathInfo.dir().path() + QDir::separator() ).toStdString() };
294
295 // For template debugging:
296 env.add_callback( "json_dump", 0, [ = ]( Arguments & )
297 {
298 return data.dump();
299 } );
300
301 // Path manipulation: appends a directory path to the current url
302 env.add_callback( "path_append", 1, [ = ]( Arguments & args )
303 {
304 auto url { context.request()->url() };
305 QFileInfo fi{ url.path() };
306 auto suffix { fi.suffix() };
307 auto fName { fi.filePath()};
308 if ( !suffix.isEmpty() )
309 {
310 fName.chop( suffix.length() + 1 );
311 }
312 // Chop any ending slashes
313 while ( fName.endsWith( '/' ) )
314 {
315 fName.chop( 1 );
316 }
317 fName += '/' + QString::fromStdString( args.at( 0 )->get<std::string>( ) );
318 if ( !suffix.isEmpty() )
319 {
320 fName += '.' + suffix;
321 }
322 fi.setFile( fName );
323 url.setPath( fi.filePath() );
324 return url.toString().toStdString();
325 } );
326
327 // Path manipulation: removes the specified number of directory components from the current url path
328 env.add_callback( "path_chomp", 1, [ = ]( Arguments & args )
329 {
330 QUrl url { QString::fromStdString( args.at( 0 )->get<std::string>( ) ) };
331 QFileInfo fi{ url.path() };
332 auto suffix { fi.suffix() };
333 auto fName { fi.filePath()};
334 fName.chop( suffix.length() + 1 );
335 // Chomp last segment
336 const thread_local QRegularExpression segmentRx( R"raw(\/[^/]+$)raw" );
337 fName = fName.replace( segmentRx, QString() );
338 if ( !suffix.isEmpty() )
339 {
340 fName += '.' + suffix;
341 }
342 fi.setFile( fName );
343 url.setPath( fi.filePath() );
344 return url.toString().toStdString();
345 } );
346
347 // Returns filtered links from a link list
348 // links_filter( <links>, <key>, <value> )
349 env.add_callback( "links_filter", 3, [ = ]( Arguments & args )
350 {
351 json links = args.at( 0 )->get<json>( );
352 if ( ! links.is_array() )
353 {
354 links = json::array();
355 }
356 std::string key { args.at( 1 )->get<std::string>( ) };
357 std::string value { args.at( 2 )->get<std::string>( ) };
358 json result = json::array();
359 for ( const auto &l : links )
360 {
361 if ( l[key] == value )
362 {
363 result.push_back( l );
364 }
365 }
366 return result;
367 } );
368
369 // Returns a short name from content types
370 env.add_callback( "content_type_name", 1, [ = ]( Arguments & args )
371 {
372 const QgsServerOgcApi::ContentType ct { QgsServerOgcApi::contentTypeFromExtension( args.at( 0 )->get<std::string>( ) ) };
374 } );
375
376 // Replace newlines with <br>
377 env.add_callback( "nl2br", 1, [ = ]( Arguments & args )
378 {
379 QString text { QString::fromStdString( args.at( 0 )->get<std::string>( ) ) };
380 return text.replace( '\n', QLatin1String( "<br>" ) ).toStdString();
381 } );
382
383
384 // Returns a list of parameter component data from components -> parameters by ref name
385 // parameter( <ref object> )
386 env.add_callback( "component_parameter", 1, [ = ]( Arguments & args )
387 {
388 json ret = json::array();
389 json ref = args.at( 0 )->get<json>( );
390 if ( ! ref.is_object() )
391 {
392 return ret;
393 }
394 try
395 {
396 QString name = QString::fromStdString( ref["$ref"] );
397 name = name.split( '/' ).last();
398 ret.push_back( data["components"]["parameters"][name.toStdString()] );
399 }
400 catch ( std::exception & )
401 {
402 // Do nothing
403 }
404 return ret;
405 } );
406
407
408 // Static: returns the full URL to the specified static <path>
409 env.add_callback( "static", 1, [ = ]( Arguments & args )
410 {
411 auto asset( args.at( 0 )->get<std::string>( ) );
412 QString matchedPath { context.matchedPath() };
413 // If its the root path '/' strip it!
414 if ( matchedPath == '/' )
415 {
416 matchedPath.clear();
417 }
418 return matchedPath.toStdString() + "/static/" + asset;
419 } );
420
421
422 // Returns true if a string begins with the provided string prefix, false otherwise
423 env.add_callback( "starts_with", 2, [ ]( Arguments & args )
424 {
425 return string_view::starts_with( args.at( 0 )->get<std::string_view>( ), args.at( 1 )->get<std::string_view>( ) );
426 } );
427
428 context.response()->write( env.render_file( pathInfo.fileName().toStdString(), data ) );
429 }
430 catch ( std::exception &e )
431 {
432 QgsMessageLog::logMessage( QStringLiteral( "Error parsing template file: %1 - %2" ).arg( path, e.what() ), QStringLiteral( "Server" ), Qgis::MessageLevel::Critical );
433 throw QgsServerApiInternalServerError( QStringLiteral( "Error parsing template file: %1" ).arg( e.what() ) );
434 }
435}
436
438{
439 // Fallback to default
441 bool found { false };
442 // First file extension ...
443 const QString extension { QFileInfo( request->url().path() ).suffix().toUpper() };
444 if ( ! extension.isEmpty() )
445 {
446 static QMetaEnum metaEnum { QMetaEnum::fromType<QgsServerOgcApi::ContentType>() };
447 bool ok { false };
448 const int ct { metaEnum.keyToValue( extension.toLocal8Bit().constData(), &ok ) };
449 if ( ok )
450 {
451 result = static_cast<QgsServerOgcApi::ContentType>( ct );
452 found = true;
453 }
454 else
455 {
456 QgsMessageLog::logMessage( QStringLiteral( "The client requested an unsupported extension: %1" ).arg( extension ), QStringLiteral( "Server" ), Qgis::MessageLevel::Warning );
457 }
458 }
459 // ... then "Accept"
460 const QString accept { request->header( QStringLiteral( "Accept" ) ) };
461 if ( ! found && ! accept.isEmpty() )
462 {
463 const QString ctFromAccept { contentTypeForAccept( accept ) };
464 if ( ! ctFromAccept.isEmpty() )
465 {
466 const auto constContentTypes( QgsServerOgcApi::contentTypeMimes() );
467 auto it = constContentTypes.constBegin();
468 while ( ! found && it != constContentTypes.constEnd() )
469 {
470 int idx = it.value().indexOf( ctFromAccept );
471 if ( idx >= 0 )
472 {
473 found = true;
474 result = it.key();
475 }
476 it++;
477 }
478 }
479 else
480 {
481 QgsMessageLog::logMessage( QStringLiteral( "The client requested an unsupported content type in Accept header: %1" ).arg( accept ), QStringLiteral( "Server" ), Qgis::MessageLevel::Warning );
482 }
483 }
484 // Validation: check if the requested content type (or an alias) is supported by the handler
485 if ( ! contentTypes().contains( result ) )
486 {
487 // Check aliases
488 bool found { false };
489 if ( QgsServerOgcApi::contentTypeAliases().contains( result ) )
490 {
491 const QList<QgsServerOgcApi::ContentType> constCt { contentTypes() };
492 for ( const auto &ct : constCt )
493 {
494 if ( QgsServerOgcApi::contentTypeAliases()[result].contains( ct ) )
495 {
496 result = ct;
497 found = true;
498 break;
499 }
500 }
501 }
502
503 if ( ! found )
504 {
505 QgsMessageLog::logMessage( QStringLiteral( "Unsupported Content-Type: %1" ).arg( QgsServerOgcApi::contentTypeToString( result ) ), QStringLiteral( "Server" ), Qgis::MessageLevel::Info );
506 throw QgsServerApiBadRequestException( QStringLiteral( "Unsupported Content-Type: %1" ).arg( QgsServerOgcApi::contentTypeToString( result ) ) );
507 }
508 }
509 return result;
510}
511
512QString QgsServerOgcApiHandler::parentLink( const QUrl &url, int levels )
513{
514 QString path { url.path() };
515 const QFileInfo fi { path };
516 const QString suffix { fi.suffix() };
517 if ( ! suffix.isEmpty() )
518 {
519 path.chop( suffix.length() + 1 );
520 }
521 while ( path.endsWith( '/' ) )
522 {
523 path.chop( 1 );
524 }
525 const thread_local QRegularExpression re( R"raw(\/[^/]+$)raw" );
526 for ( int i = 0; i < levels ; i++ )
527 {
528 path = path.replace( re, QString() );
529 }
530 QUrl result( url );
531 QUrlQuery query( result );
532 QList<QPair<QString, QString> > qi;
533 const auto constItems { query.queryItems( ) };
534 for ( const auto &i : constItems )
535 {
536 if ( i.first.compare( QStringLiteral( "MAP" ), Qt::CaseSensitivity::CaseInsensitive ) == 0 )
537 {
538 qi.push_back( i );
539 }
540 }
541 // Make sure the parent link ends with a slash
542 if ( ! path.endsWith( '/' ) )
543 {
544 path.append( '/' );
545 }
546 QUrlQuery resultQuery;
547 resultQuery.setQueryItems( qi );
548 result.setQuery( resultQuery );
549 result.setPath( path );
550 return result.toString();
551}
552
554{
555 const auto mapLayers { context.project()->mapLayersByShortName<QgsVectorLayer *>( collectionId ) };
556 if ( mapLayers.count() != 1 )
557 {
558 throw QgsServerApiNotFoundError( QStringLiteral( "Collection with given id (%1) was not found or multiple matches were found" ).arg( collectionId ) );
559 }
560 return mapLayers.first();
561}
562
564{
565 static json defRes =
566 {
567 { "description", "An error occurred." },
568 {
569 "content", {
570 {
571 "application/json", {
572 {
573 "schema", {
574 { "$ref", "#/components/schemas/exception" }
575 }
576 }
577 }
578 },
579 {
580 "text/html", {
581 {
582 "schema", {
583 { "type", "string" }
584 }
585 }
586 }
587 }
588 }
589 }
590 };
591 return defRes;
592}
593
598
599void QgsServerOgcApiHandler::setContentTypesInt( const QList<int> &contentTypes )
600{
601 mContentTypes.clear();
602 for ( const int &i : std::as_const( contentTypes ) )
603 {
604 mContentTypes.push_back( static_cast<QgsServerOgcApi::ContentType>( i ) );
605 }
606}
607
608void QgsServerOgcApiHandler::setContentTypes( const QList<QgsServerOgcApi::ContentType> &contentTypes )
609{
610 mContentTypes = contentTypes;
611}
@ Warning
Warning message.
Definition qgis.h:156
@ Critical
Critical/error message.
Definition qgis.h:157
@ Info
Information message.
Definition qgis.h:155
static json jsonFromVariant(const QVariant &v)
Converts a QVariant v to a json object.
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).
QList< QgsMapLayer * > mapLayersByShortName(const QString &shortName) const
Retrieves a list of matching registered layers by layer shortName.
Bad request error API exception.
The QgsServerApiContext class encapsulates the resources for a particular client request: the request...
const QgsProject * project() const
Returns the (possibly NULL) project.
QgsServerResponse * response() const
Returns the server response object.
QString handlerPath() const
Returns the handler component of the URL path, i.e.
const QgsServerRequest * request() const
Returns the server request object.
QgsServerInterface * serverInterface() const
Returns the server interface.
QString apiRootPath() const
Returns the API root path.
const QString matchedPath() const
Returns the initial part of the incoming request URL path that matches the API root path.
configuration error on the server prevents to serve the request, which would be valid otherwise.
Internal server error API exception.
Not found error API exception.
this method is not yet implemented
virtual QgsServerSettings * serverSettings()=0
Returns the server settings.
std::string href(const QgsServerApiContext &context, const QString &extraPath=QString(), const QString &extension=QString()) const
Returns an URL to self, to be used for links to the current resources and as a base for constructing ...
virtual const QString templatePath(const QgsServerApiContext &context) const
Returns the HTML template path for the handler in the given context.
virtual const QString staticPath(const QgsServerApiContext &context) const
Returns the absolute path to the base directory where static resources for this handler are stored in...
virtual QVariantMap values(const QgsServerApiContext &context) const
Analyzes the incoming request context and returns the validated parameter map, throws QgsServerApiBad...
json jsonTags() const
Returns tags as JSON.
void htmlDump(const json &data, const QgsServerApiContext &context) const
Writes data as HTML to the response stream in context using a template.
virtual QgsServerOgcApi::ContentType defaultContentType() const
Returns the default response content type in case the client did not specifically ask for any particu...
QgsServerOgcApi::ContentType contentTypeFromRequest(const QgsServerRequest *request) const
Returns the content type from the request.
virtual QgsServerOgcApi::Rel linkType() const =0
Main role for the resource link.
virtual json schema(const QgsServerApiContext &context) const
Returns handler information from the context for the OPENAPI description (id, description and other m...
virtual QStringList tags() const
Tags.
void setContentTypes(const QList< QgsServerOgcApi::ContentType > &contentTypes)
Set the content types to contentTypes.
void write(json &data, const QgsServerApiContext &context, const json &htmlMetadata=nullptr) const
Writes data to the context response stream, content-type is calculated from the context request,...
QList< QgsServerOgcApi::ContentType > contentTypes() const
Returns the list of content types this handler can serve, default to JSON and HTML.
virtual std::string linkTitle() const =0
Title for the handler link.
virtual QList< QgsServerQueryStringParameter > parameters(const QgsServerApiContext &context) const
Returns a list of query string parameters.
virtual std::string operationId() const =0
Returns the operation id for template file names and other internal references.
static json defaultResponse()
Returns the defaultResponse as JSON.
void jsonDump(json &data, const QgsServerApiContext &context, const QString &contentType=QStringLiteral("application/json")) const
Writes data to the context response stream as JSON (indented if debug is active), an optional content...
QgsVectorLayer * layerFromContext(const QgsServerApiContext &context) const
Returns a vector layer instance from the "collectionId" parameter of the path in the given context,...
QString contentTypeForAccept(const QString &accept) const
Looks for the first ContentType match in the accept header and returns its mime type,...
virtual void handleRequest(const QgsServerApiContext &context) const
Handles the request within its context.
json link(const QgsServerApiContext &context, const QgsServerOgcApi::Rel &linkType=QgsServerOgcApi::Rel::self, const QgsServerOgcApi::ContentType contentType=QgsServerOgcApi::ContentType::JSON, const std::string &title="") const
Builds and returns a link to the resource.
json links(const QgsServerApiContext &context) const
Returns all the links for the given request context.
static QString parentLink(const QUrl &url, int levels=1)
Returns a link to the parent page up to levels in the HTML hierarchy from the given url,...
virtual QRegularExpression path() const =0
URL pattern for this handler, named capture group are automatically extracted and returned by values(...
void setContentTypesInt(const QList< int > &contentTypes)
Set the content types to contentTypes.
static QgsVectorLayer * layerFromCollectionId(const QgsServerApiContext &context, const QString &collectionId)
Returns a vector layer from the collectionId in the given context.
static QUrl sanitizeUrl(const QUrl &url)
Returns a sanitized url with extra slashes removed and the path URL component that always starts with...
static QString contentTypeToExtension(const QgsServerOgcApi::ContentType &ct)
Returns the file extension for a ct (Content-Type).
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.
Rel
Rel link types.
@ alternate
Refers to a substitute for this context.
@ self
Conveys an identifier for the link’s context.
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.
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.
QgsServerRequest Class defining request interface passed to services QgsService::executeRequest() met...
virtual QString header(const QString &name) const
Returns the header value.
virtual void write(const QString &data)
Write string This is a convenient method that will write directly to the underlying I/O device.
virtual void setHeader(const QString &key, const QString &value)=0
Set Header entry Add Header entry to the response Note that it is usually an error to set Header afte...
virtual void setStatusCode(int code)=0
Set the http status code.
QString apiResourcesDirectory() const
Returns the server-wide base directory where HTML templates and static assets (e.g.
Represents a vector layer which manages a vector based data sets.