1 /***************************************************************************
2                               qgswms.cpp
3                               -------------------------
4   begin                : December 20 , 2016
5   copyright            : (C) 2007 by Marco Hugentobler  ( parts from qgswmshandler)
6                          (C) 2014 by Alessandro Pasotti ( parts from qgswmshandler)
7                          (C) 2016 by David Marteau
8   email                : marco dot hugentobler at karto dot baug dot ethz dot ch
9                          a dot pasotti at itopen dot it
10                          david dot marteau at 3liz dot com
11  ***************************************************************************/
12 
13 /***************************************************************************
14  *                                                                         *
15  *   This program is free software; you can redistribute it and/or modify  *
16  *   it under the terms of the GNU General Public License as published by  *
17  *   the Free Software Foundation; either version 2 of the License, or     *
18  *   (at your option) any later version.                                   *
19  *                                                                         *
20  ***************************************************************************/
21 
22 #include "qgsmodule.h"
23 #include "qgsdxfwriter.h"
24 #include "qgswmsserviceexception.h"
25 #include "qgswmsgetcapabilities.h"
26 #include "qgswmsgetmap.h"
27 #include "qgswmsgetstyles.h"
28 #include "qgswmsgetcontext.h"
29 #include "qgswmsgetschemaextension.h"
30 #include "qgswmsgetprint.h"
31 #include "qgswmsgetfeatureinfo.h"
32 #include "qgswmsdescribelayer.h"
33 #include "qgswmsgetlegendgraphics.h"
34 #include "qgswmsparameters.h"
35 #include "qgswmsrequest.h"
36 
37 #define QSTR_COMPARE( str, lit )\
38   (str.compare( QLatin1String( lit ), Qt::CaseInsensitive ) == 0)
39 
40 namespace QgsWms
41 {
42 
43   /**
44    * \ingroup server
45    * \class QgsWms::Service
46    * \brief OGC web service specialized for WMS
47    * \since QGIS 3.0
48    */
49   class Service: public QgsService
50   {
51     public:
52 
53       /**
54        * Constructor for WMS service.
55        * \param version Version of the WMS service.
56        * \param serverIface Interface for plugins.
57        */
Service(const QString & version,QgsServerInterface * serverIface)58       Service( const QString &version, QgsServerInterface *serverIface )
59         : mVersion( version )
60         , mServerIface( serverIface )
61       {}
62 
name() const63       QString name()    const override { return QStringLiteral( "WMS" ); }
version() const64       QString version() const override { return mVersion; }
65 
executeRequest(const QgsServerRequest & request,QgsServerResponse & response,const QgsProject * project)66       void executeRequest( const QgsServerRequest &request, QgsServerResponse &response,
67                            const QgsProject *project ) override
68       {
69         // Get the request
70         const QgsWmsRequest wmsRequest( request );
71         const QString req = wmsRequest.wmsParameters().request();
72 
73         if ( req.isEmpty() )
74         {
75           throw QgsServiceException( QgsServiceException::OGC_OperationNotSupported,
76                                      QStringLiteral( "Please add or check the value of the REQUEST parameter" ), 501 );
77         }
78 
79         if ( QSTR_COMPARE( req, "GetCapabilities" ) )
80         {
81           writeGetCapabilities( mServerIface, project, wmsRequest, response );
82         }
83         else if ( QSTR_COMPARE( req, "GetProjectSettings" ) )
84         {
85           writeGetCapabilities( mServerIface, project, request, response, true );
86         }
87         else if ( QSTR_COMPARE( req, "GetMap" ) )
88         {
89           if QSTR_COMPARE( wmsRequest.wmsParameters().formatAsString(), "application/dxf" )
90           {
91             writeAsDxf( mServerIface, project, request, response );
92           }
93           else
94           {
95             writeGetMap( mServerIface, project, request, response );
96           }
97         }
98         else if ( QSTR_COMPARE( req, "GetFeatureInfo" ) )
99         {
100           writeGetFeatureInfo( mServerIface, project, request, response );
101         }
102         else if ( QSTR_COMPARE( req, "GetContext" ) )
103         {
104           writeGetContext( mServerIface, project, request, response );
105         }
106         else if ( QSTR_COMPARE( req, "GetSchemaExtension" ) )
107         {
108           writeGetSchemaExtension( response );
109         }
110         else if ( QSTR_COMPARE( req, "GetStyle" ) || QSTR_COMPARE( req, "GetStyles" ) )
111         {
112           writeGetStyles( mServerIface, project, request, response );
113         }
114         else if ( QSTR_COMPARE( req, "DescribeLayer" ) )
115         {
116           writeDescribeLayer( mServerIface, project, request, response );
117         }
118         else if ( QSTR_COMPARE( req, "GetLegendGraphic" ) || QSTR_COMPARE( req, "GetLegendGraphics" ) )
119         {
120           writeGetLegendGraphics( mServerIface, project, request, response );
121         }
122         else if ( QSTR_COMPARE( req, "GetPrint" ) )
123         {
124           if ( mServerIface->serverSettings() && mServerIface->serverSettings()->getPrintDisabled() )
125           {
126             // GetPrint has been disabled
127             QgsDebugMsg( QStringLiteral( "WMS GetPrint request called, but it has been disabled." ) );
128             throw QgsServiceException( QgsServiceException::OGC_OperationNotSupported,
129                                        QStringLiteral( "Request %1 is not supported" ).arg( req ), 501 );
130           }
131           writeGetPrint( mServerIface, project, request, response );
132         }
133         else
134         {
135           // Operation not supported
136           throw QgsServiceException( QgsServiceException::OGC_OperationNotSupported,
137                                      QStringLiteral( "Request %1 is not supported" ).arg( req ), 501 );
138         }
139       }
140 
141     private:
142       QString mVersion;
143       QgsServerInterface *mServerIface = nullptr;
144   };
145 } // namespace QgsWms
146 
147 /**
148  * \ingroup server
149  * \class QgsWmsModule
150  * \brief Module specialized for WMS service
151  * \since QGIS 3.0
152  */
153 class QgsWmsModule: public QgsServiceModule
154 {
155   public:
registerSelf(QgsServiceRegistry & registry,QgsServerInterface * serverIface)156     void registerSelf( QgsServiceRegistry &registry, QgsServerInterface *serverIface ) override
157     {
158       QgsDebugMsg( QStringLiteral( "WMSModule::registerSelf called" ) );
159       registry.registerService( new  QgsWms::Service( "1.3.0", serverIface ) );
160     }
161 };
162 
163 
164 // Entry points
QGS_ServiceModule_Init()165 QGISEXTERN QgsServiceModule *QGS_ServiceModule_Init()
166 {
167   static QgsWmsModule sModule;
168   return &sModule;
169 }
QGS_ServiceModule_Exit(QgsServiceModule *)170 QGISEXTERN void QGS_ServiceModule_Exit( QgsServiceModule * )
171 {
172   // Nothing to do
173 }
174