Skip to content

Commit 451bda0

Browse files
authoredMay 31, 2017
Merge pull request #4644 from rldhont/server-wms-configparser-getstyles
[Server] WMS GetStyles (and GetStyle) refactoring
2 parents 2148fa9 + bb789e9 commit 451bda0

File tree

8 files changed

+189
-122
lines changed

8 files changed

+189
-122
lines changed
 

‎src/server/services/wms/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ SET (wms_SRCS
1414
qgswmsgetmap.cpp
1515
qgswmsgetprint.cpp
1616
qgswmsgetschemaextension.cpp
17-
qgswmsgetstyle.cpp
1817
qgswmsgetstyles.cpp
1918
qgsmaprendererjobproxy.cpp
2019
qgsmediancut.cpp

‎src/server/services/wms/qgswms.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include "qgsdxfwriter.h"
2525
#include "qgswmsgetcapabilities.h"
2626
#include "qgswmsgetmap.h"
27-
#include "qgswmsgetstyle.h"
2827
#include "qgswmsgetstyles.h"
2928
#include "qgswmsgetcontext.h"
3029
#include "qgswmsgetschemaextension.h"
@@ -118,11 +117,11 @@ namespace QgsWms
118117
}
119118
else if ( QSTR_COMPARE( req, "GetStyle" ) )
120119
{
121-
writeGetStyle( mServerIface, versionString, request, response );
120+
writeGetStyle( mServerIface, project, versionString, request, response );
122121
}
123122
else if ( QSTR_COMPARE( req, "GetStyles" ) )
124123
{
125-
writeGetStyles( mServerIface, versionString, request, response );
124+
writeGetStyles( mServerIface, project, versionString, request, response );
126125
}
127126
else if ( QSTR_COMPARE( req, "DescribeLayer" ) )
128127
{

‎src/server/services/wms/qgswmsgetstyle.cpp

Lines changed: 0 additions & 68 deletions
This file was deleted.

‎src/server/services/wms/qgswmsgetstyle.h

Lines changed: 0 additions & 39 deletions
This file was deleted.

‎src/server/services/wms/qgswmsgetstyles.cpp

Lines changed: 138 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,36 @@
2020
***************************************************************************/
2121
#include "qgswmsutils.h"
2222
#include "qgswmsgetstyles.h"
23+
#include "qgsserverprojectutils.h"
24+
25+
#include "qgsrenderer.h"
26+
#include "qgsvectorlayer.h"
27+
#include "qgsmaplayerstylemanager.h"
2328

2429
namespace QgsWms
2530
{
2631

27-
void writeGetStyles( QgsServerInterface *serverIface, const QString &version,
32+
namespace
33+
{
34+
QDomDocument getStyledLayerDescriptorDocument( QgsServerInterface *serverIface, const QgsProject *project,
35+
QStringList &layerList );
36+
}
37+
38+
void writeGetStyles( QgsServerInterface *serverIface, const QgsProject *project, const QString &version,
2839
const QgsServerRequest &request, QgsServerResponse &response )
2940
{
30-
QDomDocument doc = getStyles( serverIface, version, request );
41+
QDomDocument doc = getStyles( serverIface, project, version, request );
3142
response.setHeader( QStringLiteral( "Content-Type" ), QStringLiteral( "text/xml; charset=utf-8" ) );
3243
response.write( doc.toByteArray() );
3344
}
3445

35-
QDomDocument getStyles( QgsServerInterface *serverIface, const QString &version,
46+
QDomDocument getStyles( QgsServerInterface *serverIface, const QgsProject *project, const QString &version,
3647
const QgsServerRequest &request )
3748
{
3849
Q_UNUSED( version );
3950

40-
QgsWmsConfigParser *configParser = getConfigParser( serverIface );
4151
QgsServerRequest::Parameters parameters = request.parameters();
4252

43-
QDomDocument doc;
44-
4553
QString layersName = parameters.value( "LAYERS" );
4654

4755
if ( layersName.isEmpty() )
@@ -50,14 +58,135 @@ namespace QgsWms
5058
QStringLiteral( "Layers is mandatory for GetStyles operation" ) );
5159
}
5260

53-
QStringList layersList = layersName.split( QStringLiteral( "," ), QString::SkipEmptyParts );
54-
if ( layersList.size() < 1 )
61+
QStringList layerList = layersName.split( ',', QString::SkipEmptyParts );
62+
if ( layerList.isEmpty() )
5563
{
5664
throw QgsBadRequestException( QStringLiteral( "LayerNotSpecified" ),
5765
QStringLiteral( "Layers is mandatory for GetStyles operation" ) );
5866
}
5967

60-
return configParser->getStyles( layersList );
68+
return getStyledLayerDescriptorDocument( serverIface, project, layerList );
69+
}
70+
71+
//GetStyle for compatibility with earlier QGIS versions
72+
void writeGetStyle( QgsServerInterface *serverIface, const QgsProject *project, const QString &version,
73+
const QgsServerRequest &request, QgsServerResponse &response )
74+
{
75+
QDomDocument doc = getStyle( serverIface, project, version, request );
76+
response.setHeader( QStringLiteral( "Content-Type" ), QStringLiteral( "text/xml; charset=utf-8" ) );
77+
response.write( doc.toByteArray() );
78+
}
79+
80+
QDomDocument getStyle( QgsServerInterface *serverIface, const QgsProject *project, const QString &version,
81+
const QgsServerRequest &request )
82+
{
83+
Q_UNUSED( version );
84+
85+
QgsServerRequest::Parameters parameters = request.parameters();
86+
87+
QDomDocument doc;
88+
89+
QString styleName = parameters.value( QStringLiteral( "STYLE" ) );
90+
QString layerName = parameters.value( QStringLiteral( "LAYER" ) );
91+
92+
if ( styleName.isEmpty() )
93+
{
94+
throw QgsServiceException( QStringLiteral( "StyleNotSpecified" ),
95+
QStringLiteral( "Style is mandatory for GetStyle operation" ), 400 );
96+
}
97+
98+
if ( layerName.isEmpty() )
99+
{
100+
throw QgsServiceException( QStringLiteral( "LayerNotSpecified" ),
101+
QStringLiteral( "Layer is mandatory for GetStyle operation" ), 400 );
102+
}
103+
104+
QStringList layerList;
105+
layerList.append( layerName );
106+
return getStyledLayerDescriptorDocument( serverIface, project, layerList );
107+
}
108+
109+
namespace
110+
{
111+
QDomDocument getStyledLayerDescriptorDocument( QgsServerInterface *serverIface, const QgsProject *project,
112+
QStringList &layerList )
113+
{
114+
QDomDocument myDocument = QDomDocument();
115+
116+
QDomNode header = myDocument.createProcessingInstruction( QStringLiteral( "xml" ), QStringLiteral( "version=\"1.0\" encoding=\"UTF-8\"" ) );
117+
myDocument.appendChild( header );
118+
119+
// Create the root element
120+
QDomElement root = myDocument.createElementNS( QStringLiteral( "http://www.opengis.net/sld" ), QStringLiteral( "StyledLayerDescriptor" ) );
121+
root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.1.0" ) );
122+
root.setAttribute( QStringLiteral( "xsi:schemaLocation" ), QStringLiteral( "http://www.opengis.net/sld http://schemas.opengis.net/sld/1.1.0/StyledLayerDescriptor.xsd" ) );
123+
root.setAttribute( QStringLiteral( "xmlns:ogc" ), QStringLiteral( "http://www.opengis.net/ogc" ) );
124+
root.setAttribute( QStringLiteral( "xmlns:se" ), QStringLiteral( "http://www.opengis.net/se" ) );
125+
root.setAttribute( QStringLiteral( "xmlns:xlink" ), QStringLiteral( "http://www.w3.org/1999/xlink" ) );
126+
root.setAttribute( QStringLiteral( "xmlns:xsi" ), QStringLiteral( "http://www.w3.org/2001/XMLSchema-instance" ) );
127+
myDocument.appendChild( root );
128+
129+
// access control
130+
QgsAccessControl *accessControl = serverIface->accessControls();
131+
// Use layer ids
132+
bool useLayerIds = QgsServerProjectUtils::wmsUseLayerIds( *project );
133+
// WMS restricted layers
134+
QStringList restrictedLayers = QgsServerProjectUtils::wmsRestrictedLayers( *project );
135+
136+
Q_FOREACH ( QgsMapLayer *layer, project->mapLayers() )
137+
{
138+
QString name = layer->name();
139+
if ( useLayerIds )
140+
name = layer->id();
141+
else if ( !layer->shortName().isEmpty() )
142+
name = layer->shortName();
143+
144+
if ( !layerList.contains( name ) )
145+
{
146+
continue;
147+
}
148+
149+
//unpublished layer
150+
if ( restrictedLayers.contains( layer->name() ) )
151+
{
152+
throw QgsSecurityException( QStringLiteral( "You are not allowed to access to this layer" ) );
153+
}
154+
155+
if ( accessControl && !accessControl->layerReadPermission( layer ) )
156+
{
157+
throw QgsSecurityException( QStringLiteral( "You are not allowed to access to this layer" ) );
158+
}
159+
160+
// Create the NamedLayer element
161+
QDomElement namedLayerNode = myDocument.createElement( QStringLiteral( "NamedLayer" ) );
162+
root.appendChild( namedLayerNode );
163+
164+
// store the Name element
165+
QDomElement nameNode = myDocument.createElement( QStringLiteral( "se:Name" ) );
166+
nameNode.appendChild( myDocument.createTextNode( name ) );
167+
namedLayerNode.appendChild( nameNode );
168+
169+
if ( layer->type() == QgsMapLayer::VectorLayer )
170+
{
171+
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
172+
if ( vlayer->hasGeometryType() )
173+
{
174+
QString currentStyle = vlayer->styleManager()->currentStyle();
175+
Q_FOREACH ( QString styleName, vlayer->styleManager()->styles() )
176+
{
177+
vlayer->styleManager()->setCurrentStyle( styleName );
178+
if ( styleName.isEmpty() )
179+
styleName = EMPTY_STYLE_NAME;
180+
QDomElement styleElem = vlayer->renderer()->writeSld( myDocument, styleName );
181+
namedLayerNode.appendChild( styleElem );
182+
}
183+
vlayer->styleManager()->setCurrentStyle( currentStyle );
184+
}
185+
}
186+
}
187+
188+
return myDocument;
189+
}
61190
}
62191

63192

‎src/server/services/wms/qgswmsgetstyles.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,28 @@ namespace QgsWms
2424

2525
/** Output GetStyles response
2626
*/
27-
void writeGetStyles( QgsServerInterface *serverIface, const QString &version,
27+
void writeGetStyles( QgsServerInterface *serverIface, const QgsProject *project, const QString &version,
2828
const QgsServerRequest &request, QgsServerResponse &response );
2929

3030

3131
/**
3232
* Returns an SLD file with the styles of the requested layers. Exception is raised in case of troubles :-)
3333
*/
34-
QDomDocument getStyles( QgsServerInterface *serverIface, const QString &version,
34+
QDomDocument getStyles( QgsServerInterface *serverIface, const QgsProject *project, const QString &version,
3535
const QgsServerRequest &request );
3636

37+
//GetStyle for compatibility with earlier QGIS versions
38+
39+
/** Output GetStyle response
40+
*/
41+
void writeGetStyle( QgsServerInterface *serverIface, const QgsProject *project, const QString &version,
42+
const QgsServerRequest &request, QgsServerResponse &response );
43+
44+
/** Returns an SLD file with the style of the requested layer
45+
*/
46+
QDomDocument getStyle( QgsServerInterface *serverIface, const QgsProject *project, const QString &version,
47+
const QgsServerRequest &request );
48+
3749

3850

3951
} // samespace QgsWms

‎tests/src/python/test_qgsserver_wms.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ def test_project_wms(self):
130130
'&layers=testlayer%20%C3%A8%C3%A9&' +
131131
'SLD_VERSION=1.1.0',
132132
'describelayer')
133+
# Test GetStyles
134+
self.wms_request_compare('GetStyles',
135+
'&layers=testlayer%20%C3%A8%C3%A9&',
136+
'getstyles')
133137

134138
def wms_inspire_request_compare(self, request):
135139
"""WMS INSPIRE tests"""
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
*****
2+
Content-Type: text/xml; charset=utf-8
3+
4+
<?xml version="1.0" encoding="utf-8"?>
5+
<StyledLayerDescriptor xmlns="http://www.opengis.net/sld" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/sld http://schemas.opengis.net/sld/1.1.0/StyledLayerDescriptor.xsd" xmlns:ogc="http://www.opengis.net/ogc" xmlns:se="http://www.opengis.net/se" version="1.1.0" xmlns:xlink="http://www.w3.org/1999/xlink">
6+
<NamedLayer>
7+
<se:Name>testlayer èé</se:Name>
8+
<UserStyle>
9+
<se:Name>default</se:Name>
10+
<se:FeatureTypeStyle>
11+
<se:Rule>
12+
<se:Name>Single symbol</se:Name>
13+
<se:PointSymbolizer>
14+
<se:Graphic>
15+
<se:Mark>
16+
<se:WellKnownName>circle</se:WellKnownName>
17+
<se:Fill>
18+
<se:SvgParameter name="fill">#66a443</se:SvgParameter>
19+
</se:Fill>
20+
<se:Stroke>
21+
<se:SvgParameter name="stroke">#000000</se:SvgParameter>
22+
</se:Stroke>
23+
</se:Mark>
24+
<se:Size>7</se:Size>
25+
</se:Graphic>
26+
</se:PointSymbolizer>
27+
</se:Rule>
28+
</se:FeatureTypeStyle>
29+
</UserStyle>
30+
</NamedLayer>
31+
</StyledLayerDescriptor>

0 commit comments

Comments
 (0)
Please sign in to comment.