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
2429namespace 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
0 commit comments