1 /***************************************************************************
2                               qgswcs.cpp
3                               -------------------------
4   begin                : January 16 , 2017
5   copyright            : (C) 2013 by René-Luc D'Hont  ( parts from qgswcsserver )
6                          (C) 2017 by David Marteau
7   email                : rldhont at 3liz dot com
8                          david dot marteau at 3liz dot com
9  ***************************************************************************/
10 
11 /***************************************************************************
12  *                                                                         *
13  *   This program is free software; you can redistribute it and/or modify  *
14  *   it under the terms of the GNU General Public License as published by  *
15  *   the Free Software Foundation; either version 2 of the License, or     *
16  *   (at your option) any later version.                                   *
17  *                                                                         *
18  ***************************************************************************/
19 
20 #include "qgsmodule.h"
21 #include "qgswcsutils.h"
22 #include "qgswcsgetcapabilities.h"
23 #include "qgswcsdescribecoverage.h"
24 #include "qgswcsgetcoverage.h"
25 
26 #define QSTR_COMPARE( str, lit )\
27   (str.compare( QLatin1String( lit ), Qt::CaseInsensitive ) == 0)
28 
29 namespace QgsWcs
30 {
31 
32   /**
33    * \ingroup server
34    * \class QgsWcs::Service
35    * \brief OGC web service specialized for WCS
36    * \since QGIS 3.0
37    */
38   class Service: public QgsService
39   {
40     public:
41 
42       /**
43        * Constructor for WCS service.
44        * \param serverIface Interface for plugins.
45        */
Service(QgsServerInterface * serverIface)46       Service( QgsServerInterface *serverIface )
47         : mServerIface( serverIface )
48       {}
49 
name() const50       QString name()    const override { return QStringLiteral( "WCS" ); }
version() const51       QString version() const override { return implementationVersion(); }
52 
executeRequest(const QgsServerRequest & request,QgsServerResponse & response,const QgsProject * project)53       void executeRequest( const QgsServerRequest &request, QgsServerResponse &response,
54                            const QgsProject *project ) override
55       {
56         Q_UNUSED( project )
57 
58         QgsServerRequest::Parameters params = request.parameters();
59         QString versionString = params.value( "VERSION" );
60 
61         // Set the default version
62         if ( versionString.isEmpty() )
63         {
64           versionString = version();
65         }
66 
67         // Get the request
68         QString req = params.value( QStringLiteral( "REQUEST" ) );
69         if ( req.isEmpty() )
70         {
71           throw QgsServiceException( QStringLiteral( "OperationNotSupported" ),
72                                      QStringLiteral( "Please add or check the value of the REQUEST parameter" ), 501 );
73         }
74 
75         if ( QSTR_COMPARE( req, "GetCapabilities" ) )
76         {
77           writeGetCapabilities( mServerIface, project, versionString, request, response );
78         }
79         else if ( QSTR_COMPARE( req, "DescribeCoverage" ) )
80         {
81           writeDescribeCoverage( mServerIface, project, versionString, request, response );
82         }
83         else if ( QSTR_COMPARE( req, "GetCoverage" ) )
84         {
85           writeGetCoverage( mServerIface, project, versionString, request, response );
86         }
87         else
88         {
89           // Operation not supported
90           throw QgsServiceException( QStringLiteral( "OperationNotSupported" ),
91                                      QStringLiteral( "Request %1 is not supported" ).arg( req ), 501 );
92         }
93       }
94 
95     private:
96       QgsServerInterface *mServerIface = nullptr;
97   };
98 
99 
100 } // namespace QgsWcs
101 
102 /**
103  * \ingroup server
104  * \class QgsWcsModule
105  * \brief Service module specialized for WCS
106  * \since QGIS 3.0
107  */
108 class QgsWcsModule: public QgsServiceModule
109 {
110   public:
registerSelf(QgsServiceRegistry & registry,QgsServerInterface * serverIface)111     void registerSelf( QgsServiceRegistry &registry, QgsServerInterface *serverIface ) override
112     {
113       QgsDebugMsg( QStringLiteral( "WCSModule::registerSelf called" ) );
114       registry.registerService( new  QgsWcs::Service( serverIface ) );
115     }
116 };
117 
118 
119 // Entry points
QGS_ServiceModule_Init()120 QGISEXTERN QgsServiceModule *QGS_ServiceModule_Init()
121 {
122   static QgsWcsModule module;
123   return &module;
124 }
QGS_ServiceModule_Exit(QgsServiceModule *)125 QGISEXTERN void QGS_ServiceModule_Exit( QgsServiceModule * )
126 {
127   // Nothing to do
128 }
129