-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathqgsserverogcapi.cpp
More file actions
258 lines (233 loc) · 8.62 KB
/
Copy pathqgsserverogcapi.cpp
File metadata and controls
258 lines (233 loc) · 8.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/***************************************************************************
qgsserverogcapi.cpp - QgsServerOgcApi
---------------------
begin : 10.7.2019
copyright : (C) 2019 by Alessandro Pasotti
email : elpaso at itopen dot it
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsserverogcapi.h"
#include "qgsapplication.h"
#include "qgsmessagelog.h"
#include "qgsserverogcapihandler.h"
#include <QDebug>
#include <QDir>
#include <QString>
#include <QtGlobal>
#include "moc_qgsserverogcapi.cpp"
using namespace Qt::StringLiterals;
QMap<QgsServerOgcApi::ContentType, QStringList> QgsServerOgcApi::sContentTypeMime = []() -> QMap<QgsServerOgcApi::ContentType, QStringList> {
QMap<QgsServerOgcApi::ContentType, QStringList> map;
map[QgsServerOgcApi::ContentType::JSON] = QStringList { u"application/json"_s };
map[QgsServerOgcApi::ContentType::SCHEMA_JSON] = QStringList { u"application/schema+json"_s };
map[QgsServerOgcApi::ContentType::GEOJSON] = QStringList { u"application/geo+json"_s, u"application/vnd.geo+json"_s, u"application/geojson"_s };
map[QgsServerOgcApi::ContentType::HTML] = QStringList { u"text/html"_s };
map[QgsServerOgcApi::ContentType::OPENAPI3] = QStringList { u"application/vnd.oai.openapi+json;version=3.0"_s };
map[QgsServerOgcApi::ContentType::XML] = QStringList { u"application/xml"_s };
map[QgsServerOgcApi::ContentType::FLATGEOBUF] = QStringList { u"application/flatgeobuf"_s, u"application/vnd.fgb"_s };
return map;
}();
QHash<QgsServerOgcApi::ContentType, QList<QgsServerOgcApi::ContentType>> QgsServerOgcApi::sContentTypeAliases = []() -> QHash<ContentType, QList<ContentType>> {
QHash<QgsServerOgcApi::ContentType, QList<QgsServerOgcApi::ContentType>> map;
map[ContentType::JSON] = { QgsServerOgcApi::ContentType::GEOJSON, QgsServerOgcApi::ContentType::OPENAPI3, QgsServerOgcApi::ContentType::SCHEMA_JSON };
return map;
}();
QgsServerOgcApi::QgsServerOgcApi( QgsServerInterface *serverIface, const QString &rootPath, const QString &name, const QString &description, const QString &version )
: QgsServerApi( serverIface )
, mRootPath( rootPath )
, mName( name )
, mDescription( description )
, mVersion( version )
{}
QgsServerOgcApi::~QgsServerOgcApi()
{
//qDebug() << "API destroyed: " << name();
}
void QgsServerOgcApi::registerHandler( QgsServerOgcApiHandler *handler )
{
std::shared_ptr<QgsServerOgcApiHandler> hp( handler );
mHandlers.emplace_back( std::move( hp ) );
}
QUrl QgsServerOgcApi::sanitizeUrl( const QUrl &url )
{
// Since QT 5.12 NormalizePathSegments does not collapse double slashes
QUrl u { url.adjusted( QUrl::StripTrailingSlash | QUrl::NormalizePathSegments ) };
if ( u.path().contains( "//"_L1 ) )
{
u.setPath( u.path().replace( "//"_L1, QChar( '/' ) ) );
}
// Make sure the path starts with '/'
if ( !u.path().startsWith( '/' ) )
{
u.setPath( u.path().prepend( '/' ) );
}
return u;
}
void QgsServerOgcApi::executeRequest( const QgsServerApiContext &context ) const
{
// Get url
const auto path { sanitizeUrl( context.handlerPath() ).path() };
// Find matching handler
auto hasMatch { false };
for ( const auto &handler : mHandlers )
{
QgsMessageLog::logMessage( u"Checking API path %1 for %2 "_s.arg( path, handler->path().pattern() ), u"Server"_s, Qgis::MessageLevel::Info );
if ( handler->path().match( path ).hasMatch() )
{
hasMatch = true;
// Execute handler
QgsMessageLog::logMessage( u"API %1: found handler %2"_s.arg( name(), QString::fromStdString( handler->operationId() ) ), u"Server"_s, Qgis::MessageLevel::Info );
// May throw QgsServerApiBadRequestException or JSON exceptions on serializing
try
{
handler->handleRequest( context );
}
catch ( json::exception &ex )
{
throw QgsServerApiInternalServerError( u"The API handler returned an error: %1"_s.arg( ex.what() ) );
}
break;
}
}
// Throw
if ( !hasMatch )
{
throw QgsServerApiBadRequestException( u"Requested URI does not match any registered API handler"_s );
}
}
const QMap<QgsServerOgcApi::ContentType, QStringList> QgsServerOgcApi::contentTypeMimes()
{
return sContentTypeMime;
}
const QHash<QgsServerOgcApi::ContentType, QList<QgsServerOgcApi::ContentType>> QgsServerOgcApi::contentTypeAliases()
{
return sContentTypeAliases;
}
QString QgsServerOgcApi::profileToString( const Profile &profile )
{
switch ( profile )
{
case Profile::RFC7946:
return u"json"_s;
#if 0
// This not supported yet but I am leaving it here because
// I am very optimistic that it will be supported soon!
case Profile::JSONFG:
return u"jsonfg"_s;
case Profile::JSONFG_PLUS:
return u"jsonfg-plus"_s;
#endif
case Profile::NONE:
return QString();
case Profile::REL_AS_KEY:
return u"rel-as-key"_s;
case Profile::REL_AS_URI:
return u"rel-as-uri"_s;
case Profile::REL_AS_LINK:
return u"rel-as-link"_s;
}
Q_UNREACHABLE();
return QString();
}
QString QgsServerOgcApi::profileToUri( const Profile &profile )
{
switch ( profile )
{
case Profile::RFC7946:
return u"http://www.opengis.net/def/profile/OGC/0/rfc7946"_s;
#if 0
// This not supported yet but I am leaving it here because
// I am very optimistic that it will be supported soon!
case Profile::JSONFG:
return u"http://www.opengis.net/def/profile/OGC/0/jsonfg"_s;
case Profile::JSONFG_PLUS:
return u"http://www.opengis.net/def/profile/OGC/0/jsonfg-plus"_s;
#endif
case Profile::REL_AS_KEY:
return u"http://www.opengis.net/def/profile/ogc/0/rel-as-key"_s;
case Profile::REL_AS_URI:
return u"http://www.opengis.net/def/profile/ogc/0/rel-as-uri"_s;
case Profile::REL_AS_LINK:
return u"http://www.opengis.net/def/profile/ogc/0/rel-as-link"_s;
case Profile::NONE:
return QString();
}
Q_UNREACHABLE();
return QString();
}
std::string QgsServerOgcApi::relToString( const Rel &rel )
{
if ( rel == Rel::schema )
{
return "http://www.opengis.net/def/rel/ogc/1.0/schema";
}
static const QMetaEnum metaEnum = QMetaEnum::fromType<QgsServerOgcApi::Rel>();
std::string val { metaEnum.valueToKey( rel ) };
std::replace( val.begin(), val.end(), '_', '-' );
return val;
}
QString QgsServerOgcApi::contentTypeToString( const ContentType &ct )
{
static const QMetaEnum metaEnum = QMetaEnum::fromType<ContentType>();
QString result { metaEnum.valueToKey( ct ) };
return result.replace( '_', '-' );
}
std::string QgsServerOgcApi::contentTypeToStdString( const ContentType &ct )
{
static const QMetaEnum metaEnum = QMetaEnum::fromType<ContentType>();
return metaEnum.valueToKey( ct );
}
QString QgsServerOgcApi::contentTypeToExtension( const ContentType &ct )
{
switch ( ct )
{
case ContentType::SCHEMA_JSON:
{
return u"json"_s;
}
case ContentType::FLATGEOBUF:
{
return u"fgb"_s;
}
default:
return contentTypeToString( ct ).toLower();
}
// UNREACHABLE CODE
}
QgsServerOgcApi::ContentType QgsServerOgcApi::contentTypeFromExtension( const std::string &extension )
{
const QString exts = QString::fromStdString( extension );
const auto constMimeTypes( QgsServerOgcApi::contentTypeMimes() );
for ( auto it = constMimeTypes.constBegin(); it != constMimeTypes.constEnd(); ++it )
{
const auto constValues = it.value();
for ( const auto &value : constValues )
{
if ( value.contains( exts, Qt::CaseSensitivity::CaseInsensitive ) )
{
return it.key();
}
}
}
// Default to JSON, but log a warning!
QgsMessageLog::logMessage( u"Content type for extension %1 not found! Returning default (JSON)"_s.arg( exts ), u"Server"_s, Qgis::MessageLevel::Warning );
return QgsServerOgcApi::ContentType::JSON;
}
std::string QgsServerOgcApi::mimeType( const QgsServerOgcApi::ContentType &contentType )
{
if ( !sContentTypeMime.contains( contentType ) )
{
return "";
}
return sContentTypeMime.value( contentType ).first().toStdString();
}
const std::vector<std::shared_ptr<QgsServerOgcApiHandler>> QgsServerOgcApi::handlers() const
{
return mHandlers;
}