Skip to content

Commit 99a8612

Browse files
committed
Clean up code in qgis_map_serv.cpp
1 parent 0e113e1 commit 99a8612

File tree

1 file changed

+74
-80
lines changed

1 file changed

+74
-80
lines changed

src/mapserver/qgis_map_serv.cpp

+74-80
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/***************************************************************************
22
qgs_map_serv.cpp
3-
A server application supporting WMS/SLD syntax for HTTP GET and Orchestra
4-
map service syntax for SOAP/HTTP POST
3+
A server application supporting WMS / WFS / WCS
54
-------------------
65
begin : July 04, 2006
76
copyright : (C) 2006 by Marco Hugentobler & Ionut Iosifescu Enescu
@@ -50,8 +49,6 @@ map service syntax for SOAP/HTTP POST
5049

5150
void dummyMessageHandler( QtMsgType type, const char *msg )
5251
{
53-
Q_UNUSED( type );
54-
Q_UNUSED( msg );
5552
#ifdef QGSMSDEBUG
5653
QString output;
5754

@@ -76,6 +73,9 @@ void dummyMessageHandler( QtMsgType type, const char *msg )
7673

7774
if ( type == QtFatalMsg )
7875
abort();
76+
#else
77+
Q_UNUSED( type );
78+
Q_UNUSED( msg );
7979
#endif
8080
}
8181

@@ -133,12 +133,10 @@ void printRequestInfos()
133133
QFileInfo defaultProjectFile()
134134
{
135135
QDir currentDir;
136-
QgsDebugMsg( "current directory: " + currentDir.absolutePath() );
137136
fprintf( FCGI_stderr, "current directory: %s\n", currentDir.absolutePath().toUtf8().constData() );
138137
QStringList nameFilterList;
139138
nameFilterList << "*.qgs";
140139
QFileInfoList projectFiles = currentDir.entryInfoList( nameFilterList, QDir::Files, QDir::Name );
141-
QgsDebugMsg( "Project files found:" );
142140
for ( int x = 0; x < projectFiles.size(); x++ )
143141
{
144142
QgsDebugMsg( projectFiles.at( x ).absoluteFilePath() );
@@ -167,6 +165,69 @@ int fcgi_accept()
167165
#endif
168166
}
169167

168+
void setupNetworkAccessManager()
169+
{
170+
QSettings settings;
171+
QgsNetworkAccessManager *nam = QgsNetworkAccessManager::instance();
172+
QNetworkDiskCache *cache = new QNetworkDiskCache( 0 );
173+
QString cacheDirectory = settings.value( "cache/directory", QgsApplication::qgisSettingsDirPath() + "cache" ).toString();
174+
qint64 cacheSize = settings.value( "cache/size", 50 * 1024 * 1024 ).toULongLong();
175+
QgsDebugMsg( QString( "setCacheDirectory: %1" ).arg( cacheDirectory ) );
176+
QgsDebugMsg( QString( "setMaximumCacheSize: %1" ).arg( cacheSize ) );
177+
cache->setCacheDirectory( cacheDirectory );
178+
cache->setMaximumCacheSize( cacheSize );
179+
QgsDebugMsg( QString( "cacheDirectory: %1" ).arg( cache->cacheDirectory() ) );
180+
QgsDebugMsg( QString( "maximumCacheSize: %1" ).arg( cache->maximumCacheSize() ) );
181+
nam->setCache( cache );
182+
}
183+
184+
QgsRequestHandler* createRequestHandler()
185+
{
186+
QgsRequestHandler* requestHandler = 0;
187+
char* requestMethod = getenv( "REQUEST_METHOD" );
188+
if ( requestMethod != NULL )
189+
{
190+
if ( strcmp( requestMethod, "POST" ) == 0 )
191+
{
192+
//requestHandler = new QgsSOAPRequestHandler();
193+
requestHandler = new QgsPostRequestHandler();
194+
}
195+
else
196+
{
197+
requestHandler = new QgsGetRequestHandler();
198+
}
199+
}
200+
else
201+
{
202+
requestHandler = new QgsGetRequestHandler();
203+
}
204+
return requestHandler;
205+
}
206+
207+
QString configPath( const QString& defaultConfigPath, const QMap<QString, QString>& parameters )
208+
{
209+
QString cfPath( defaultConfigPath );
210+
QString projectFile = getenv( "QGIS_PROJECT_FILE" );
211+
if ( !projectFile.isEmpty() )
212+
{
213+
cfPath = projectFile;
214+
}
215+
else
216+
{
217+
QMap<QString, QString>::const_iterator paramIt = parameters.find( "MAP" );
218+
if ( paramIt == parameters.constEnd() )
219+
{
220+
QgsDebugMsg( QString( "Using default configuration file path: %1" ).arg( defaultConfigPath ) );
221+
}
222+
else
223+
{
224+
cfPath = paramIt.value();
225+
}
226+
}
227+
return cfPath;
228+
}
229+
230+
170231
int main( int argc, char * argv[] )
171232
{
172233
#ifndef _MSC_VER
@@ -188,22 +249,7 @@ int main( int argc, char * argv[] )
188249
QgsApplication::skipGdalDriver( "JP2ECW" );
189250
#endif
190251

191-
QSettings settings;
192-
193-
QgsNetworkAccessManager *nam = QgsNetworkAccessManager::instance();
194-
QNetworkDiskCache *cache = new QNetworkDiskCache( 0 );
195-
196-
QString cacheDirectory = settings.value( "cache/directory", QgsApplication::qgisSettingsDirPath() + "cache" ).toString();
197-
qint64 cacheSize = settings.value( "cache/size", 50 * 1024 * 1024 ).toULongLong();
198-
QgsDebugMsg( QString( "setCacheDirectory: %1" ).arg( cacheDirectory ) );
199-
QgsDebugMsg( QString( "setMaximumCacheSize: %1" ).arg( cacheSize ) );
200-
cache->setCacheDirectory( cacheDirectory );
201-
cache->setMaximumCacheSize( cacheSize );
202-
QgsDebugMsg( QString( "cacheDirectory: %1" ).arg( cache->cacheDirectory() ) );
203-
QgsDebugMsg( QString( "maximumCacheSize: %1" ).arg( cache->maximumCacheSize() ) );
204-
205-
nam->setCache( cache );
206-
252+
setupNetworkAccessManager();
207253
QDomImplementation::setInvalidDataPolicy( QDomImplementation::DropInvalidChars );
208254

209255
// Instantiate the plugin directory so that providers are loaded
@@ -216,8 +262,6 @@ int main( int argc, char * argv[] )
216262
QgsDebugMsg( qgsapp.applicationDirPath() + "/qgis_wms_server.log" );
217263
QgsApplication::createDB(); //init qgis.db (e.g. necessary for user crs)
218264

219-
//create config cache and search for config files in the current directory.
220-
//These configurations are used if no mapfile parameter is present in the request
221265
QString defaultConfigFilePath;
222266
QFileInfo projectFileInfo = defaultProjectFile(); //try to find a .qgs file in the server directory
223267
if ( projectFileInfo.exists() )
@@ -245,37 +289,13 @@ int main( int argc, char * argv[] )
245289
QgsFontUtils::loadStandardTestFonts( QStringList() << "Roman" << "Bold" );
246290
#endif
247291

248-
249-
//for( int i = 0; i < 2; ++i )
250292
while ( fcgi_accept() >= 0 )
251293
{
252294
printRequestInfos(); //print request infos if in debug mode
253295

254-
//use QgsGetRequestHandler in case of HTTP GET and QgsSOAPRequestHandler in case of HTTP POST
255-
QgsRequestHandler* theRequestHandler = 0;
256-
char* requestMethod = getenv( "REQUEST_METHOD" );
257-
if ( requestMethod != NULL )
258-
{
259-
if ( strcmp( requestMethod, "POST" ) == 0 )
260-
{
261-
//QgsDebugMsg( "Creating QgsSOAPRequestHandler" );
262-
//theRequestHandler = new QgsSOAPRequestHandler();
263-
theRequestHandler = new QgsPostRequestHandler();
264-
}
265-
else
266-
{
267-
QgsDebugMsg( "Creating QgsGetRequestHandler" );
268-
theRequestHandler = new QgsGetRequestHandler();
269-
}
270-
}
271-
else
272-
{
273-
QgsDebugMsg( "Creating QgsGetRequestHandler" );
274-
theRequestHandler = new QgsGetRequestHandler();
275-
}
276-
296+
//Request handler
297+
QgsRequestHandler* theRequestHandler = createRequestHandler();
277298
QMap<QString, QString> parameterMap;
278-
279299
try
280300
{
281301
parameterMap = theRequestHandler->parseInput();
@@ -289,51 +309,27 @@ int main( int argc, char * argv[] )
289309

290310
QMap<QString, QString>::const_iterator paramIt;
291311

292-
//set admin config file to wms server object
293-
QString configFilePath( defaultConfigFilePath );
294-
295-
QString projectFile = getenv( "QGIS_PROJECT_FILE" );
296-
if ( !projectFile.isEmpty() )
297-
{
298-
configFilePath = projectFile;
299-
}
300-
else
301-
{
302-
paramIt = parameterMap.find( "MAP" );
303-
if ( paramIt == parameterMap.constEnd() )
304-
{
305-
QgsDebugMsg( QString( "Using default configuration file path: %1" ).arg( defaultConfigFilePath ) );
306-
}
307-
else
308-
{
309-
configFilePath = paramIt.value();
310-
}
311-
}
312+
//Config file path
313+
QString configFilePath = configPath( defaultConfigFilePath, parameterMap );
312314

315+
//Admin config parser
313316
QgsConfigParser* adminConfigParser = QgsConfigCache::instance()->searchConfiguration( configFilePath );
314317
if ( !adminConfigParser )
315318
{
316319
QgsDebugMsg( "parse error on config file " + configFilePath );
317320
theRequestHandler->sendServiceException( QgsMapServiceException( "", "Configuration file problem : perhaps you left off the .qgs extension?" ) );
318321
continue;
319322
}
320-
321-
//sld parser might need information about request parameters
322323
adminConfigParser->setParameterMap( parameterMap );
323324

324-
//request to WMS?
325+
//Service parameter
325326
QString serviceString;
326327
paramIt = parameterMap.find( "SERVICE" );
327328
if ( paramIt == parameterMap.constEnd() )
328329
{
329-
#ifndef QGISDEBUG
330-
serviceString = parameterMap.value( "SERVICE", "WMS" );
331-
#else
332-
QgsDebugMsg( "unable to find 'SERVICE' parameter, exiting..." );
333330
theRequestHandler->sendServiceException( QgsMapServiceException( "ServiceNotSpecified", "Service not specified. The SERVICE parameter is mandatory" ) );
334331
delete theRequestHandler;
335332
continue;
336-
#endif
337333
}
338334
else
339335
{
@@ -344,7 +340,6 @@ int main( int argc, char * argv[] )
344340
{
345341
QgsWCSServer wcsServer( configFilePath, parameterMap, adminConfigParser, theRequestHandler );
346342
wcsServer.executeRequest();
347-
348343
}
349344
else if ( serviceString == "WFS" )
350345
{
@@ -360,7 +355,6 @@ int main( int argc, char * argv[] )
360355
}
361356

362357
delete theMapRenderer;
363-
QgsDebugMsg( "************* all done ***************" );
364358
return 0;
365359
}
366360

0 commit comments

Comments
 (0)