Skip to content

Commit c8a4407

Browse files
committed
[Server][WFS] add QgsWfsParameters
1 parent c0117df commit c8a4407

File tree

4 files changed

+359
-6
lines changed

4 files changed

+359
-6
lines changed

src/server/services/wfs/CMakeLists.txt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,19 @@ SET (wfs_SRCS
1010
qgswfsdescribefeaturetype.cpp
1111
qgswfsgetfeature.cpp
1212
qgswfstransaction.cpp
13+
qgswfsparameters.cpp
14+
)
15+
16+
SET (wfs_MOC_HDRS
17+
qgswfsparameters.h
1318
)
1419

1520
########################################################
1621
# Build
1722

18-
ADD_LIBRARY (wfs MODULE ${wfs_SRCS})
23+
QT5_WRAP_CPP(wfs_MOC_SRCS ${wfs_MOC_HDRS})
24+
25+
ADD_LIBRARY (wfs MODULE ${wfs_SRCS} ${wfs_MOC_SRCS} ${wfs_MOC_HDRS})
1926

2027

2128
INCLUDE_DIRECTORIES(SYSTEM
@@ -31,10 +38,10 @@ INCLUDE_DIRECTORIES(
3138
${CMAKE_BINARY_DIR}/src/analysis
3239
${CMAKE_BINARY_DIR}/src/server
3340
${CMAKE_CURRENT_BINARY_DIR}
34-
../../../core
41+
../../../core
3542
../../../core/dxf
3643
../../../core/expression
37-
../../../core/geometry
44+
../../../core/geometry
3845
../../../core/metadata
3946
../../../core/raster
4047
../../../core/symbology
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
/***************************************************************************
2+
qgswfsparameters.cpp
3+
--------------------
4+
begin : Sept 14, 2017
5+
copyright : (C) 2017 by René-Luc Dhont
6+
email : rldhont at 3liz dot com
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
18+
#include "qgswfsparameters.h"
19+
#include "qgsmessagelog.h"
20+
#include <iostream>
21+
22+
namespace QgsWfs
23+
{
24+
QgsWfsParameters::QgsWfsParameters()
25+
{
26+
// Available version number
27+
mVersions.append( QgsProjectVersion( 1, 0, 0 ) );
28+
mVersions.append( QgsProjectVersion( 1, 1, 0 ) );
29+
30+
const Parameter pOutputFormat = { ParameterName::OUTPUTFORMAT,
31+
QVariant::String,
32+
QVariant( "" ),
33+
QVariant()
34+
};
35+
save( pOutputFormat );
36+
37+
const Parameter pSrsName = { ParameterName::SRSNAME,
38+
QVariant::String,
39+
QVariant( "" ),
40+
QVariant()
41+
};
42+
save( pSrsName );
43+
}
44+
45+
QgsWfsParameters::QgsWfsParameters( const QgsServerRequest::Parameters &parameters )
46+
{
47+
load( parameters );
48+
}
49+
50+
void QgsWfsParameters::load( const QgsServerRequest::Parameters &parameters )
51+
{
52+
mRequestParameters = parameters;
53+
54+
const QMetaEnum metaEnum( QMetaEnum::fromType<ParameterName>() );
55+
foreach ( QString key, parameters.keys() )
56+
{
57+
const ParameterName name = ( ParameterName ) metaEnum.keyToValue( key.toStdString().c_str() );
58+
if ( name >= 0 )
59+
{
60+
QVariant value( parameters[key] );
61+
if ( value.canConvert( mParameters[name].mType ) )
62+
{
63+
mParameters[name].mValue = value;
64+
}
65+
else
66+
{
67+
raiseError( name );
68+
}
69+
}
70+
}
71+
}
72+
73+
void QgsWfsParameters::dump() const
74+
{
75+
const QMetaEnum metaEnum( QMetaEnum::fromType<ParameterName>() );
76+
77+
log( "WFS Request parameters:" );
78+
for ( auto parameter : mParameters.toStdMap() )
79+
{
80+
const QString value = parameter.second.mValue.toString();
81+
82+
if ( ! value.isEmpty() )
83+
{
84+
const QString name = metaEnum.valueToKey( parameter.first );
85+
log( " - " + name + " : " + value );
86+
}
87+
}
88+
89+
if ( !version().isEmpty() )
90+
log( " - VERSION : " + version() );
91+
}
92+
93+
void QgsWfsParameters::save( const Parameter &parameter )
94+
{
95+
mParameters[ parameter.mName ] = parameter;
96+
}
97+
98+
QVariant QgsWfsParameters::value( ParameterName name ) const
99+
{
100+
return mParameters[name].mValue;
101+
}
102+
103+
QVariant QgsWfsParameters::defaultValue( ParameterName name ) const
104+
{
105+
return mParameters[name].mDefaultValue;
106+
}
107+
108+
QString QgsWfsParameters::outputFormatAsString() const
109+
{
110+
return value( ParameterName::OUTPUTFORMAT ).toString();
111+
}
112+
113+
QgsWfsParameters::Format QgsWfsParameters::outputFormat() const
114+
{
115+
QString fStr = outputFormatAsString();
116+
117+
if ( fStr.isEmpty() )
118+
{
119+
if ( versionAsNumber() >= QgsProjectVersion( 1, 1, 0 ) )
120+
return Format::GML3;
121+
else
122+
return Format::GML2;
123+
}
124+
125+
Format f = Format::NONE;
126+
if ( fStr.compare( QLatin1String( "text/xml; subtype=gml/2.1.2" ), Qt::CaseInsensitive ) == 0 )
127+
f = Format::GML2;
128+
else if ( fStr.compare( QLatin1String( "text/xml; subtype=gml/3.1.1" ), Qt::CaseInsensitive ) == 0 )
129+
f = Format::GML3;
130+
else if ( fStr.compare( QLatin1String( "application/vnd.geo+json" ), Qt::CaseInsensitive ) == 0 )
131+
f = Format::GeoJSON;
132+
else if ( fStr.compare( QLatin1String( "gml2" ), Qt::CaseInsensitive ) == 0 )
133+
f = Format::GML2;
134+
else if ( fStr.compare( QLatin1String( "gml3" ), Qt::CaseInsensitive ) == 0 )
135+
f = Format::GML3;
136+
else if ( fStr.compare( QLatin1String( "geojson" ), Qt::CaseInsensitive ) == 0 )
137+
f = Format::GeoJSON;
138+
139+
return f;
140+
}
141+
142+
QString QgsWfsParameters::srsName() const
143+
{
144+
return value( ParameterName::SRSNAME ).toString();
145+
}
146+
147+
QString QgsWfsParameters::version() const
148+
{
149+
// VERSION parameter is not managed with other parameters because
150+
// there's a conflict with qgis VERSION defined in qgsconfig.h
151+
if ( mRequestParameters.contains( "VERSION" ) )
152+
return mRequestParameters["VERSION"];
153+
else
154+
return QString();
155+
}
156+
157+
QgsProjectVersion QgsWfsParameters::versionAsNumber() const
158+
{
159+
QString vStr = version();
160+
QgsProjectVersion version;
161+
162+
if ( vStr.isEmpty() )
163+
version = QgsProjectVersion( 1, 1, 0 ); // default value
164+
else if ( mVersions.contains( QgsProjectVersion( vStr ) ) )
165+
version = QgsProjectVersion( vStr );
166+
167+
return version;
168+
}
169+
170+
QString QgsWfsParameters::name( ParameterName name ) const
171+
{
172+
const QMetaEnum metaEnum( QMetaEnum::fromType<ParameterName>() );
173+
return metaEnum.valueToKey( name );
174+
}
175+
176+
void QgsWfsParameters::log( const QString &msg ) const
177+
{
178+
QgsMessageLog::logMessage( msg, "Server", QgsMessageLog::INFO );
179+
}
180+
181+
void QgsWfsParameters::raiseError( ParameterName paramName ) const
182+
{
183+
const QString value = mParameters[paramName].mValue.toString();
184+
const QString param = name( paramName );
185+
const QString type = QVariant::typeToName( mParameters[paramName].mType );
186+
raiseError( param + " ('" + value + "') cannot be converted into " + type );
187+
}
188+
189+
void QgsWfsParameters::raiseError( const QString &msg ) const
190+
{
191+
throw QgsBadRequestException( QStringLiteral( "Invalid WFS Parameter" ), msg );
192+
}
193+
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/***************************************************************************
2+
qgswfsparameters.h
3+
------------------
4+
begin : Sept 14, 2017
5+
copyright : (C) 2017 by René-Luc Dhont
6+
email : rldhont at 3liz dot com
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
18+
#ifndef QGSWFSPARAMETERS_H
19+
#define QGSWFSPARAMETERS_H
20+
21+
#include <QMap>
22+
#include <QObject>
23+
#include <QMetaEnum>
24+
25+
#include "qgswfsserviceexception.h"
26+
#include "qgsserverrequest.h"
27+
#include "qgsprojectversion.h"
28+
29+
/**
30+
* QgsWfsParameters provides an interface to retrieve and manipulate WFS
31+
* parameters received from the client.
32+
* \since QGIS 3.0
33+
*/
34+
namespace QgsWfs
35+
{
36+
37+
class QgsWfsParameters
38+
{
39+
Q_GADGET
40+
41+
public:
42+
enum ParameterName
43+
{
44+
OUTPUTFORMAT,
45+
SRSNAME
46+
};
47+
Q_ENUM( ParameterName )
48+
49+
enum Format
50+
{
51+
NONE,
52+
GML2,
53+
GML3,
54+
GeoJSON,
55+
XSD
56+
};
57+
58+
struct Parameter
59+
{
60+
ParameterName mName;
61+
QVariant::Type mType;
62+
QVariant mDefaultValue;
63+
QVariant mValue;
64+
};
65+
66+
/**
67+
* Constructor.
68+
* \param map of parameters where keys are parameters' names.
69+
*/
70+
QgsWfsParameters( const QgsServerRequest::Parameters &parameters );
71+
72+
/**
73+
* Constructor.
74+
*/
75+
QgsWfsParameters();
76+
77+
/**
78+
* Loads new parameters.
79+
* \param map of parameters
80+
*/
81+
void load( const QgsServerRequest::Parameters &parameters );
82+
83+
/**
84+
* Dumps parameters.
85+
*/
86+
void dump() const;
87+
88+
<<<<<<< HEAD
89+
/** Returns VERSION parameter as a string or an empty string if not
90+
=======
91+
/**
92+
* Returns REQUEST parameter as a string or an empty string if not
93+
* defined.
94+
* \returns request
95+
*/
96+
QString request() const;
97+
98+
/**
99+
* Returns VERSION parameter as a string or an empty string if not
100+
>>>>>>> 747f00d... QgsWfsParameters
101+
* defined.
102+
* \returns version
103+
*/
104+
QString version() const;
105+
106+
/**
107+
* Returns VERSION parameter if defined or its default value.
108+
* \returns version
109+
*/
110+
QgsProjectVersion versionAsNumber() const;
111+
112+
/** Returns OUTPUTFORMAT parameter as a string.
113+
* \returns OUTPUTFORMAT parameter as string
114+
*/
115+
QString outputFormatAsString() const;
116+
117+
/**
118+
* Returns format. If the OUTPUTFORMAT parameter is not used, then the
119+
* default value is GML2 or GML3.
120+
* \returns format
121+
*/
122+
Format outputFormat() const;
123+
124+
/** Returns SRSNAME parameter as a string.
125+
* \returns SRSNAME parameter as string
126+
*/
127+
QString srsName() const;
128+
129+
130+
private:
131+
QString name( ParameterName name ) const;
132+
void raiseError( ParameterName name ) const;
133+
void raiseError( const QString &msg ) const;
134+
QVariant value( ParameterName name ) const;
135+
QVariant defaultValue( ParameterName name ) const;
136+
void log( const QString &msg ) const;
137+
void save( const Parameter &parameter );
138+
139+
QgsServerRequest::Parameters mRequestParameters;
140+
QMap<ParameterName, Parameter> mParameters;
141+
QList<QgsProjectVersion> mVersions;
142+
};
143+
}
144+
145+
#endif

src/server/services/wfs/qgswfsserviceexception.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,17 @@ namespace QgsWfs
7272
{}
7373
};
7474

75-
76-
77-
75+
/** \ingroup server
76+
* \class QgsBadRequestException
77+
* \brief Exception thrown in case of malformed request
78+
*/
79+
class QgsBadRequestException: public QgsServiceException
80+
{
81+
public:
82+
QgsBadRequestException( const QString &code, const QString &message, const QString &locator = QString() )
83+
: QgsServiceException( code, message, locator, 400 )
84+
{}
85+
};
7886

7987

8088
} // namespace QgsWfs

0 commit comments

Comments
 (0)