20
20
***************************************************************************/
21
21
#include " qgswmsutils.h"
22
22
#include " qgswmsgetstyles.h"
23
+ #include " qgsserverprojectutils.h"
24
+
25
+ #include " qgsrenderer.h"
26
+ #include " qgsvectorlayer.h"
27
+ #include " qgsmaplayerstylemanager.h"
23
28
24
29
namespace QgsWms
25
30
{
26
31
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,
28
39
const QgsServerRequest &request, QgsServerResponse &response )
29
40
{
30
- QDomDocument doc = getStyles ( serverIface, version, request );
41
+ QDomDocument doc = getStyles ( serverIface, project, version, request );
31
42
response.setHeader ( QStringLiteral ( " Content-Type" ), QStringLiteral ( " text/xml; charset=utf-8" ) );
32
43
response.write ( doc.toByteArray () );
33
44
}
34
45
35
- QDomDocument getStyles ( QgsServerInterface *serverIface, const QString &version,
46
+ QDomDocument getStyles ( QgsServerInterface *serverIface, const QgsProject *project, const QString &version,
36
47
const QgsServerRequest &request )
37
48
{
38
49
Q_UNUSED ( version );
39
50
40
- QgsWmsConfigParser *configParser = getConfigParser ( serverIface );
41
51
QgsServerRequest::Parameters parameters = request.parameters ();
42
52
43
- QDomDocument doc;
44
-
45
53
QString layersName = parameters.value ( " LAYERS" );
46
54
47
55
if ( layersName.isEmpty () )
@@ -50,14 +58,135 @@ namespace QgsWms
50
58
QStringLiteral ( " Layers is mandatory for GetStyles operation" ) );
51
59
}
52
60
53
- QStringList layersList = layersName.split ( QStringLiteral ( " , " ) , QString::SkipEmptyParts );
54
- if ( layersList. size () < 1 )
61
+ QStringList layerList = layersName.split ( ' , ' , QString::SkipEmptyParts );
62
+ if ( layerList. isEmpty () )
55
63
{
56
64
throw QgsBadRequestException ( QStringLiteral ( " LayerNotSpecified" ),
57
65
QStringLiteral ( " Layers is mandatory for GetStyles operation" ) );
58
66
}
59
67
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
+ }
61
190
}
62
191
63
192
0 commit comments