Added support for INIMAGE error handling.

This commit is contained in:
Jean-Francois Doyon 2006-04-16 04:17:39 +00:00
parent b8ac9b1984
commit f862ef425f
5 changed files with 82 additions and 47 deletions

View file

@ -89,8 +89,9 @@ class Handler(cgi.DebugHandler):
eh = ExceptionHandler130(self.debug) eh = ExceptionHandler130(self.debug)
else: else:
eh = ExceptionHandler111(self.debug) eh = ExceptionHandler111(self.debug)
req.set_header('Content-Type', eh.mimetype) response = eh.getresponse(reqparams)
req.write(eh.getcontent()) req.set_header('Content-Type', response.content_type)
req.write(response.content)
def lowerparams(params): def lowerparams(params):
reqparams = {} reqparams = {}

View file

@ -21,8 +21,13 @@
from exceptions import OGCException, ServerConfigurationError from exceptions import OGCException, ServerConfigurationError
from mapnik import Map, Color, Envelope, render, rawdata, Image, Projection from mapnik import Map, Color, Envelope, render, rawdata, Image, Projection
from PIL.Image import fromstring from PIL.Image import fromstring, new
from PIL.ImageDraw import Draw
from StringIO import StringIO from StringIO import StringIO
from copy import deepcopy
from traceback import print_tb
from sys import exc_info
from lxml import etree as ElementTree
import re import re
# from elementtree import ElementTree # from elementtree import ElementTree
# ElementTree._namespace_map.update({'http://www.opengis.net/wms': 'wms', # ElementTree._namespace_map.update({'http://www.opengis.net/wms': 'wms',
@ -208,7 +213,7 @@ class WMSBaseServiceHandler(BaseServiceHandler):
try: try:
layer = maplayers[layername] layer = maplayers[layername]
except KeyError: except KeyError:
raise OGCException('Layer not defined: %s.' % layername, 'LayerNotDefined') raise OGCException('Layer "%s" not defined.' % layername, 'LayerNotDefined')
for stylename in layer.styles: for stylename in layer.styles:
if stylename in mapstyles.keys(): if stylename in mapstyles.keys():
m.append_style(stylename, mapstyles[stylename]) m.append_style(stylename, mapstyles[stylename])
@ -222,5 +227,46 @@ class WMSBaseServiceHandler(BaseServiceHandler):
fh = StringIO() fh = StringIO()
im2.save(fh, PIL_TYPE_MAPPING[params['format']]) im2.save(fh, PIL_TYPE_MAPPING[params['format']])
fh.seek(0) fh.seek(0)
response = Response(params['format'], fh.read()) return Response(params['format'], fh.read())
return response
class BaseExceptionHandler:
def __init__(self, debug):
self.debug = debug
def getresponse(self, params):
code = ''
message = ''
excinfo = exc_info()
if self.debug:
fh = StringIO()
print_tb(excinfo[2], None, fh)
fh.seek(0)
message = '\n' + fh.read() + '\n' + str(excinfo[0]) + ': ' + ', '.join(excinfo[1].args) + '\n'
fh.close()
elif len(excinfo[1].args) > 0:
message = excinfo[1].args[0]
if isinstance(excinfo[1], OGCException) and len(excinfo[1].args) > 1:
code = excinfo[1].args[1]
exceptions = params.get('exceptions', None)
if not exceptions:
exceptions = self.defaulthandler
return self.handlers[exceptions](self, code, message, params)
def xmlhandler(self, code, message, params):
ogcexcetree = deepcopy(self.xmltemplate)
e = ogcexcetree.find(self.xpath)
e.text = message
if code:
e.set('code', code)
return Response(self.xmlmimetype, ElementTree.tostring(ogcexcetree))
def inimagehandler(self, code, message, params):
im = new('L', (int(params['width']), int(params['height'])))
draw = Draw(im)
for count, line in enumerate(message.strip().split('\n')):
draw.text((12,15*(count+1)), line, fill='#FFFFFF')
fh = StringIO()
im.save(fh, PIL_TYPE_MAPPING[params['format']])
fh.seek(0)
return Response(params['format'], fh.read())

View file

@ -19,34 +19,8 @@
# #
# $Id$ # $Id$
from copy import deepcopy
from lxml import etree as ElementTree
from StringIO import StringIO
from traceback import print_tb
from sys import exc_info
class OGCException(Exception): class OGCException(Exception):
pass pass
class ServerConfigurationError(Exception): class ServerConfigurationError(Exception):
pass pass
class BaseExceptionHandler:
def __init__(self, debug):
self.debug = debug
def getcontent(self):
excinfo = exc_info()
ogcexcetree = deepcopy(self.xmltemplate)
e = ogcexcetree.find(self.xpath)
if self.debug:
fh = StringIO()
print_tb(excinfo[2], None, fh)
fh.seek(0)
e.text = '\n' + fh.read() + '\n' + str(excinfo[0]) + ': ' + ', '.join(excinfo[1].args) + '\n'
elif len(excinfo[1].args) > 0:
e.text = excinfo[1].args[0]
if isinstance(excinfo[1], OGCException) and len(excinfo[1].args) > 1:
e.set('code', excinfo[1].args[1])
return ElementTree.tostring(ogcexcetree)

View file

@ -20,8 +20,9 @@
# $Id$ # $Id$
from common import ParameterDefinition, Response, Version, ListFactory, \ from common import ParameterDefinition, Response, Version, ListFactory, \
ColorFactory, CRSFactory, WMSBaseServiceHandler, CRS ColorFactory, CRSFactory, WMSBaseServiceHandler, CRS, \
from exceptions import OGCException, ServerConfigurationError, BaseExceptionHandler BaseExceptionHandler
from exceptions import OGCException, ServerConfigurationError
from lxml import etree as ElementTree from lxml import etree as ElementTree
class ServiceHandler(WMSBaseServiceHandler): class ServiceHandler(WMSBaseServiceHandler):
@ -38,10 +39,10 @@ class ServiceHandler(WMSBaseServiceHandler):
'bbox': ParameterDefinition(True, ListFactory(float)), 'bbox': ParameterDefinition(True, ListFactory(float)),
'width': ParameterDefinition(True, int), 'width': ParameterDefinition(True, int),
'height': ParameterDefinition(True, int), 'height': ParameterDefinition(True, int),
'format': ParameterDefinition(True, str, allowedvalues=('image/png','image/jpeg','image/gif')), 'format': ParameterDefinition(True, str, allowedvalues=('image/png', 'image/jpeg', 'image/gif')),
'transparent': ParameterDefinition(False, str, 'FALSE', ('TRUE','FALSE')), 'transparent': ParameterDefinition(False, str, 'FALSE', ('TRUE', 'FALSE')),
'bgcolor': ParameterDefinition(False, ColorFactory, ColorFactory('0xFFFFFF')), 'bgcolor': ParameterDefinition(False, ColorFactory, ColorFactory('0xFFFFFF')),
'exceptions': ParameterDefinition(False, str, 'application/vnd.ogc.se_xml', ('application/vnd.ogc.se_xml',)) 'exceptions': ParameterDefinition(False, str, 'application/vnd.ogc.se_xml', ('application/vnd.ogc.se_xml', 'application/vnd.ogc.se_inimage'))
} }
} }
@ -92,6 +93,7 @@ class ServiceHandler(WMSBaseServiceHandler):
</Request> </Request>
<Exception> <Exception>
<Format>application/vnd.ogc.se_xml</Format> <Format>application/vnd.ogc.se_xml</Format>
<Format>application/vnd.ogc.se_inimage</Format>
</Exception> </Exception>
<Layer> <Layer>
<Title>A Mapnik WMS Server</Title> <Title>A Mapnik WMS Server</Title>
@ -179,7 +181,7 @@ class ServiceHandler(WMSBaseServiceHandler):
class ExceptionHandler(BaseExceptionHandler): class ExceptionHandler(BaseExceptionHandler):
mimetype = "application/vnd.ogc.se_xml" xmlmimetype = "application/vnd.ogc.se_xml"
xmltemplate = ElementTree.fromstring("""<?xml version='1.0' encoding="UTF-8" standalone="no"?> xmltemplate = ElementTree.fromstring("""<?xml version='1.0' encoding="UTF-8" standalone="no"?>
<!DOCTYPE ServiceExceptionReport SYSTEM "http://www.digitalearth.gov/wmt/xml/exception_1_1_1.dtd"> <!DOCTYPE ServiceExceptionReport SYSTEM "http://www.digitalearth.gov/wmt/xml/exception_1_1_1.dtd">
@ -189,3 +191,8 @@ class ExceptionHandler(BaseExceptionHandler):
""") """)
xpath = 'ServiceException' xpath = 'ServiceException'
handlers = {'application/vnd.ogc.se_xml': BaseExceptionHandler.xmlhandler,
'application/vnd.ogc.se_inimage': BaseExceptionHandler.inimagehandler}
defaulthandler = 'application/vnd.ogc.se_xml'

View file

@ -20,8 +20,9 @@
# $Id$ # $Id$
from common import ParameterDefinition, Response, Version, ListFactory, \ from common import ParameterDefinition, Response, Version, ListFactory, \
ColorFactory, CRSFactory, CRS, WMSBaseServiceHandler ColorFactory, CRSFactory, CRS, WMSBaseServiceHandler, \
from exceptions import OGCException, ServerConfigurationError, BaseExceptionHandler BaseExceptionHandler
from exceptions import OGCException, ServerConfigurationError
from lxml import etree as ElementTree from lxml import etree as ElementTree
class ServiceHandler(WMSBaseServiceHandler): class ServiceHandler(WMSBaseServiceHandler):
@ -39,10 +40,10 @@ class ServiceHandler(WMSBaseServiceHandler):
'bbox': ParameterDefinition(True, ListFactory(float)), 'bbox': ParameterDefinition(True, ListFactory(float)),
'width': ParameterDefinition(True, int), 'width': ParameterDefinition(True, int),
'height': ParameterDefinition(True, int), 'height': ParameterDefinition(True, int),
'format': ParameterDefinition(True, str, allowedvalues=('image/gif','image/png','image/jpeg')), 'format': ParameterDefinition(True, str, allowedvalues=('image/gif','image/png', 'image/jpeg')),
'transparent': ParameterDefinition(False, str, 'FALSE', ('TRUE','FALSE')), 'transparent': ParameterDefinition(False, str, 'FALSE', ('TRUE', 'FALSE')),
'bgcolor': ParameterDefinition(False, ColorFactory, ColorFactory('0xFFFFFF')), 'bgcolor': ParameterDefinition(False, ColorFactory, ColorFactory('0xFFFFFF')),
'exceptions': ParameterDefinition(False, str, 'XML', ('XML',)), 'exceptions': ParameterDefinition(False, str, 'XML', ('XML', 'INIMAGE')),
} }
} }
@ -98,6 +99,7 @@ class ServiceHandler(WMSBaseServiceHandler):
</Request> </Request>
<Exception> <Exception>
<Format>XML</Format> <Format>XML</Format>
<Format>INIMAGE</Format>
</Exception> </Exception>
<Layer> <Layer>
<Title>A Mapnik WMS Server</Title> <Title>A Mapnik WMS Server</Title>
@ -196,7 +198,7 @@ class ServiceHandler(WMSBaseServiceHandler):
class ExceptionHandler(BaseExceptionHandler): class ExceptionHandler(BaseExceptionHandler):
mimetype = "text/xml" xmlmimetype = "text/xml"
xmltemplate = ElementTree.fromstring("""<?xml version='1.0' encoding="UTF-8"?> xmltemplate = ElementTree.fromstring("""<?xml version='1.0' encoding="UTF-8"?>
<ServiceExceptionReport version="1.3.0" <ServiceExceptionReport version="1.3.0"
@ -208,3 +210,8 @@ class ExceptionHandler(BaseExceptionHandler):
""") """)
xpath = '{http://www.opengis.net/ogc}ServiceException' xpath = '{http://www.opengis.net/ogc}ServiceException'
handlers = {'XML': BaseExceptionHandler.xmlhandler,
'INIMAGE': BaseExceptionHandler.inimagehandler}
defaulthandler = 'XML'