Skip to content

Commit

Permalink
Merge pull request #4637 from rldhont/server-wms-configparser-describ…
Browse files Browse the repository at this point in the history
…elayer

[Server] WMS DescribeLayer refactoring
  • Loading branch information
rldhont committed May 29, 2017
2 parents d996cf2 + 8a0261c commit 8356c76
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 8 deletions.
2 changes: 1 addition & 1 deletion python/server/qgsserverprojectutils.sip
Expand Up @@ -243,5 +243,5 @@ namespace QgsServerProjectUtils
* @param project the QGIS project * @param project the QGIS project
* @return the Layer ids list. * @return the Layer ids list.
*/ */
QStringList wcsLayers( const QgsProject &project ); QStringList wcsLayerIds( const QgsProject &project );
}; };
2 changes: 1 addition & 1 deletion src/server/qgsserverprojectutils.cpp
Expand Up @@ -263,7 +263,7 @@ QString QgsServerProjectUtils::wcsServiceUrl( const QgsProject &project )
return project.readEntry( QStringLiteral( "WCSUrl" ), QStringLiteral( "/" ), "" ); return project.readEntry( QStringLiteral( "WCSUrl" ), QStringLiteral( "/" ), "" );
} }


QStringList QgsServerProjectUtils::wcsLayers( const QgsProject &project ) QStringList QgsServerProjectUtils::wcsLayerIds( const QgsProject &project )
{ {
return project.readListEntry( QStringLiteral( "WCSLayers" ), QStringLiteral( "/" ) ); return project.readListEntry( QStringLiteral( "WCSLayers" ), QStringLiteral( "/" ) );
} }
2 changes: 1 addition & 1 deletion src/server/qgsserverprojectutils.h
Expand Up @@ -245,7 +245,7 @@ namespace QgsServerProjectUtils
* \param project the QGIS project * \param project the QGIS project
* \returns the Layer ids list. * \returns the Layer ids list.
*/ */
SERVER_EXPORT QStringList wcsLayers( const QgsProject &project ); SERVER_EXPORT QStringList wcsLayerIds( const QgsProject &project );
}; };


#endif #endif
2 changes: 1 addition & 1 deletion src/server/services/wcs/qgswcsdescribecoverage.cpp
Expand Up @@ -94,7 +94,7 @@ namespace QgsWcs
} }
} }


QStringList wcsLayersId = QgsServerProjectUtils::wcsLayers( *project ); QStringList wcsLayersId = QgsServerProjectUtils::wcsLayerIds( *project );
for ( int i = 0; i < wcsLayersId.size(); ++i ) for ( int i = 0; i < wcsLayersId.size(); ++i )
{ {
QgsMapLayer *layer = project->mapLayer( wcsLayersId.at( i ) ); QgsMapLayer *layer = project->mapLayer( wcsLayersId.at( i ) );
Expand Down
2 changes: 1 addition & 1 deletion src/server/services/wcs/qgswcsgetcapabilities.cpp
Expand Up @@ -260,7 +260,7 @@ namespace QgsWcs
*/ */
QDomElement contentMetadataElement = doc.createElement( QStringLiteral( "ContentMetadata" )/*wcs:ContentMetadata*/ ); QDomElement contentMetadataElement = doc.createElement( QStringLiteral( "ContentMetadata" )/*wcs:ContentMetadata*/ );


QStringList wcsLayersId = QgsServerProjectUtils::wcsLayers( *project ); QStringList wcsLayersId = QgsServerProjectUtils::wcsLayerIds( *project );
for ( int i = 0; i < wcsLayersId.size(); ++i ) for ( int i = 0; i < wcsLayersId.size(); ++i )
{ {
QgsMapLayer *layer = project->mapLayer( wcsLayersId.at( i ) ); QgsMapLayer *layer = project->mapLayer( wcsLayersId.at( i ) );
Expand Down
2 changes: 1 addition & 1 deletion src/server/services/wcs/qgswcsgetcoverage.cpp
Expand Up @@ -73,7 +73,7 @@ namespace QgsWcs
} }


//get the raster layer //get the raster layer
QStringList wcsLayersId = QgsServerProjectUtils::wcsLayers( *project ); QStringList wcsLayersId = QgsServerProjectUtils::wcsLayerIds( *project );


QgsRasterLayer *rLayer = nullptr; QgsRasterLayer *rLayer = nullptr;
for ( int i = 0; i < wcsLayersId.size(); ++i ) for ( int i = 0; i < wcsLayersId.size(); ++i )
Expand Down
100 changes: 98 additions & 2 deletions src/server/services/wms/qgswmsdescribelayer.cpp
Expand Up @@ -40,7 +40,6 @@ namespace QgsWms
Q_UNUSED( version ); Q_UNUSED( version );


QgsServerRequest::Parameters parameters = request.parameters(); QgsServerRequest::Parameters parameters = request.parameters();
QgsWmsConfigParser *configParser = getConfigParser( serverIface );


if ( !parameters.contains( QStringLiteral( "SLD_VERSION" ) ) ) if ( !parameters.contains( QStringLiteral( "SLD_VERSION" ) ) )
{ {
Expand All @@ -64,6 +63,24 @@ namespace QgsWms
{ {
throw QgsServiceException( QStringLiteral( "InvalidParameterValue" ), QStringLiteral( "Layers is empty" ), 400 ); throw QgsServiceException( QStringLiteral( "InvalidParameterValue" ), QStringLiteral( "Layers is empty" ), 400 );
} }
QDomDocument myDocument = QDomDocument();

QDomNode header = myDocument.createProcessingInstruction( QStringLiteral( "xml" ), QStringLiteral( "version=\"1.0\" encoding=\"UTF-8\"" ) );
myDocument.appendChild( header );

// Create the root element
QDomElement root = myDocument.createElementNS( QStringLiteral( "http://www.opengis.net/sld" ), QStringLiteral( "DescribeLayerResponse" ) );
root.setAttribute( QStringLiteral( "xsi:schemaLocation" ), QStringLiteral( "http://www.opengis.net/sld http://schemas.opengis.net/sld/1.1.0/DescribeLayer.xsd" ) );
root.setAttribute( QStringLiteral( "xmlns:ows" ), QStringLiteral( "http://www.opengis.net/ows" ) );
root.setAttribute( QStringLiteral( "xmlns:se" ), QStringLiteral( "http://www.opengis.net/se" ) );
root.setAttribute( QStringLiteral( "xmlns:xlink" ), QStringLiteral( "http://www.w3.org/1999/xlink" ) );
root.setAttribute( QStringLiteral( "xmlns:xsi" ), QStringLiteral( "http://www.w3.org/2001/XMLSchema-instance" ) );
myDocument.appendChild( root );

// store the Version element
QDomElement versionNode = myDocument.createElement( QStringLiteral( "Version" ) );
versionNode.appendChild( myDocument.createTextNode( QStringLiteral( "1.1.0" ) ) );
root.appendChild( versionNode );


// get the wms service url defined in project or keep the one from the // get the wms service url defined in project or keep the one from the
// request url // request url
Expand All @@ -85,7 +102,86 @@ namespace QgsWms
wcsHrefString = wmsHrefString; wcsHrefString = wmsHrefString;
} }


return configParser->describeLayer( layersList, wfsHrefString, wcsHrefString ); // access control
QgsAccessControl *accessControl = serverIface->accessControls();
// Use layer ids
bool useLayerIds = QgsServerProjectUtils::wmsUseLayerIds( *project );
// WMS restricted layers
QStringList restrictedLayers = QgsServerProjectUtils::wmsRestrictedLayers( *project );
// WFS layers
QStringList wfsLayerIds = QgsServerProjectUtils::wfsLayerIds( *project );
// WCS layers
QStringList wcsLayerIds = QgsServerProjectUtils::wcsLayerIds( *project );

Q_FOREACH ( QgsMapLayer *layer, project->mapLayers() )
{
QString name = layer->name();
if ( useLayerIds )
name = layer->id();
else if ( !layer->shortName().isEmpty() )
name = layer->shortName();

if ( !layersList.contains( name ) )
{
continue;
}

//unpublished layer
if ( restrictedLayers.contains( layer->name() ) )
{
throw QgsSecurityException( QStringLiteral( "You are not allowed to access to this layer" ) );
}

if ( accessControl && !accessControl->layerReadPermission( layer ) )
{
throw QgsSecurityException( QStringLiteral( "You are not allowed to access to this layer" ) );
}

// Create the NamedLayer element
QDomElement layerNode = myDocument.createElement( QStringLiteral( "LayerDescription" ) );
root.appendChild( layerNode );

// store the owsType element
QDomElement typeNode = myDocument.createElement( QStringLiteral( "owsType" ) );
// store the se:OnlineResource element
QDomElement oResNode = myDocument.createElement( QStringLiteral( "se:OnlineResource" ) );
oResNode.setAttribute( QStringLiteral( "xlink:type" ), QStringLiteral( "simple" ) );
// store the TypeName element
QDomElement nameNode = myDocument.createElement( QStringLiteral( "TypeName" ) );
if ( layer->type() == QgsMapLayer::VectorLayer )
{
typeNode.appendChild( myDocument.createTextNode( QStringLiteral( "wfs" ) ) );

if ( wfsLayerIds.indexOf( layer->id() ) != -1 )
{
oResNode.setAttribute( QStringLiteral( "xlink:href" ), wfsHrefString );
}

// store the se:FeatureTypeName element
QDomElement typeNameNode = myDocument.createElement( QStringLiteral( "se:FeatureTypeName" ) );
typeNameNode.appendChild( myDocument.createTextNode( name ) );
nameNode.appendChild( typeNameNode );
}
else if ( layer->type() == QgsMapLayer::RasterLayer )
{
typeNode.appendChild( myDocument.createTextNode( QStringLiteral( "wcs" ) ) );

if ( wcsLayerIds.indexOf( layer->id() ) != -1 )
{
oResNode.setAttribute( QStringLiteral( "xlink:href" ), wcsHrefString );
}

// store the se:CoverageTypeName element
QDomElement typeNameNode = myDocument.createElement( QStringLiteral( "se:CoverageTypeName" ) );
typeNameNode.appendChild( myDocument.createTextNode( name ) );
nameNode.appendChild( typeNameNode );
}
layerNode.appendChild( typeNode );
layerNode.appendChild( oResNode );
layerNode.appendChild( nameNode );
}

return myDocument;
} }


} // samespace QgsWms } // samespace QgsWms
6 changes: 6 additions & 0 deletions tests/src/python/test_qgsserver_wms.py
Expand Up @@ -125,6 +125,12 @@ def test_project_wms(self):
'FEATURE_COUNT=10&FILTER=testlayer%20%C3%A8%C3%A9' + urllib.parse.quote(':"NAME" = \'two\' OR "utf8nameè" = \'three èé↓\''), 'FEATURE_COUNT=10&FILTER=testlayer%20%C3%A8%C3%A9' + urllib.parse.quote(':"NAME" = \'two\' OR "utf8nameè" = \'three èé↓\''),
'wms_getfeatureinfo_filter_or_utf8') 'wms_getfeatureinfo_filter_or_utf8')


# Test DescribeLayer
self.wms_request_compare('DescribeLayer',
'&layers=testlayer%20%C3%A8%C3%A9&' +
'SLD_VERSION=1.1.0',
'describelayer')

def wms_inspire_request_compare(self, request): def wms_inspire_request_compare(self, request):
"""WMS INSPIRE tests""" """WMS INSPIRE tests"""
project = self.testdata_path + "test_project_inspire.qgs" project = self.testdata_path + "test_project_inspire.qgs"
Expand Down
14 changes: 14 additions & 0 deletions tests/testdata/qgis_server/describelayer.txt
@@ -0,0 +1,14 @@
*****
Content-Type: text/xml; charset=utf-8

<?xml version="1.0" encoding="utf-8"?>
<DescribeLayerResponse xmlns="http://www.opengis.net/sld" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ows="http://www.opengis.net/ows" xsi:schemaLocation="http://www.opengis.net/sld http://schemas.opengis.net/sld/1.1.0/DescribeLayer.xsd" xmlns:se="http://www.opengis.net/se" xmlns:xlink="http://www.w3.org/1999/xlink">
<Version>1.1.0</Version>
<LayerDescription>
<owsType>wfs</owsType>
<se:OnlineResource xlink:type="simple" xlink:href="https://www.qgis.org/?*****"/>
<TypeName>
<se:FeatureTypeName>testlayer èé</se:FeatureTypeName>
</TypeName>
</LayerDescription>
</DescribeLayerResponse>

0 comments on commit 8356c76

Please sign in to comment.