#NEWLINE# def dispatch( self , writer = sys . stdout , write_headers = True ) : #NEWLINE# #INDENT# ' Handle incoming HTTP request ' #NEWLINE# if ( self . requesttype == 'GET' ) : #NEWLINE# #INDENT# self . kvp = self . normalize_kvp ( self . kvp ) #NEWLINE# version_202 = ( ( 'version' in self . kvp ) and ( self . kvp [ 'version' ] == '2.0.2' ) ) #NEWLINE# accept_version_202 = ( ( 'acceptversions' in self . kvp ) and ( '2.0.2' in self . kvp [ 'acceptversions' ] ) ) #NEWLINE# if ( version_202 or accept_version_202 ) : #NEWLINE# #INDENT# self . request_version = '2.0.2' #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( self . requesttype == 'POST' ) : #NEWLINE# #INDENT# if ( self . request . find ( b'2.0.2' ) != ( - 1 ) ) : #NEWLINE# #INDENT# self . request_version = '2.0.2' #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if ( ( not isinstance ( self . kvp , str ) ) and ( 'mode' in self . kvp ) and ( self . kvp [ 'mode' ] == 'sru' ) ) : #NEWLINE# #INDENT# self . mode = 'sru' #NEWLINE# self . request_version = '2.0.2' #NEWLINE# LOGGER . debug ( 'SRU mode detected; processing request.' ) #NEWLINE# self . kvp = self . sru ( ) . request_sru2csw ( self . kvp ) #NEWLINE# #UNINDENT# if ( ( not isinstance ( self . kvp , str ) ) and ( 'mode' in self . kvp ) and ( self . kvp [ 'mode' ] == 'oaipmh' ) ) : #NEWLINE# #INDENT# self . mode = 'oaipmh' #NEWLINE# self . request_version = '2.0.2' #NEWLINE# LOGGER . debug ( 'OAI-PMH mode detected; processing request.' ) #NEWLINE# self . oaiargs = dict ( ( ( k , v ) for ( k , v ) in self . kvp . items ( ) if k ) ) #NEWLINE# self . kvp = self . oaipmh ( ) . request ( self . kvp ) #NEWLINE# #UNINDENT# if ( self . request_version == '2.0.2' ) : #NEWLINE# #INDENT# self . iface = csw2 . Csw2 ( server_csw = self ) #NEWLINE# self . context . set_model ( 'csw' ) #NEWLINE# #UNINDENT# self . _gen_manager ( ) #NEWLINE# namespaces = self . context . namespaces #NEWLINE# ops = self . context . model [ 'operations' ] #NEWLINE# constraints = self . context . model [ 'constraints' ] #NEWLINE# if ( 'GetDomain' not in ops ) : #NEWLINE# #INDENT# ops [ 'GetDomain' ] = self . context . gen_domains ( ) #NEWLINE# #UNINDENT# if self . config . has_option ( 'server' , 'federatedcatalogues' ) : #NEWLINE# #INDENT# LOGGER . debug ( 'Configuring distributed search.' ) #NEWLINE# constraints [ 'FederatedCatalogues' ] = { #NEWLINE# #INDENT# 'values' : [ ] , #NEWLINE# #UNINDENT# } #NEWLINE# for fedcat in self . config . get ( 'server' , 'federatedcatalogues' ) . split ( ',' ) : #NEWLINE# #INDENT# constraints [ 'FederatedCatalogues' ] [ 'values' ] . append ( fedcat ) #NEWLINE# #UNINDENT# #UNINDENT# for ( key , value ) in self . outputschemas . items ( ) : #NEWLINE# #INDENT# get_records_params = ops [ 'GetRecords' ] [ 'parameters' ] #NEWLINE# get_records_params [ 'outputSchema' ] [ 'values' ] . append ( value . NAMESPACE ) #NEWLINE# get_records_by_id_params = ops [ 'GetRecordById' ] [ 'parameters' ] #NEWLINE# get_records_by_id_params [ 'outputSchema' ] [ 'values' ] . append ( value . NAMESPACE ) #NEWLINE# if ( 'Harvest' in ops ) : #NEWLINE# #INDENT# harvest_params = ops [ 'Harvest' ] [ 'parameters' ] #NEWLINE# harvest_params [ 'ResourceType' ] [ 'values' ] . append ( value . NAMESPACE ) #NEWLINE# #UNINDENT# #UNINDENT# LOGGER . debug ( 'Setting MaxRecordDefault' ) #NEWLINE# if self . config . has_option ( 'server' , 'maxrecords' ) : #NEWLINE# #INDENT# constraints [ 'MaxRecordDefault' ] [ 'values' ] = [ self . config . get ( 'server' , 'maxrecords' ) ] #NEWLINE# #UNINDENT# if self . config . has_option ( 'server' , 'profiles' ) : #NEWLINE# #INDENT# self . profiles = pprofile . load_profiles ( os . path . join ( 'pycsw' , 'plugins' , 'profiles' ) , pprofile . Profile , self . config . get ( 'server' , 'profiles' ) ) #NEWLINE# for prof in self . profiles [ 'plugins' ] . keys ( ) : #NEWLINE# #INDENT# tmp = self . profiles [ 'plugins' ] [ prof ] ( self . context . model , namespaces , self . context ) #NEWLINE# key = tmp . outputschema #NEWLINE# self . profiles [ 'loaded' ] [ key ] = tmp #NEWLINE# self . profiles [ 'loaded' ] [ key ] . extend_core ( self . context . model , namespaces , self . config ) #NEWLINE# #UNINDENT# LOGGER . debug ( ( 'Profiles loaded: %s.' % list ( self . profiles [ 'loaded' ] . keys ( ) ) ) ) #NEWLINE# #UNINDENT# if ( not self . config . has_option ( 'repository' , 'table' ) ) : #NEWLINE# #INDENT# self . config . set ( 'repository' , 'table' , 'records' ) #NEWLINE# #UNINDENT# repo_filter = None #NEWLINE# if self . config . has_option ( 'repository' , 'filter' ) : #NEWLINE# #INDENT# repo_filter = self . config . get ( 'repository' , 'filter' ) #NEWLINE# #UNINDENT# if ( self . config . has_option ( 'repository' , 'source' ) and ( self . config . get ( 'repository' , 'source' ) == 'geonode' ) ) : #NEWLINE# #INDENT# from pycsw.plugins.repository.geonode import geonode_ #NEWLINE# try : #NEWLINE# #INDENT# self . repository = geonode_ . GeoNodeRepository ( self . context , repo_filter ) #NEWLINE# LOGGER . debug ( ( 'GeoNode repository loaded (geonode): %s.' % self . repository . dbtype ) ) #NEWLINE# #UNINDENT# except Exception as err : #NEWLINE# #INDENT# self . response = self . iface . exceptionreport ( 'NoApplicableCode' , 'service' , ( 'Could not load repository (geonode): %s' % str ( err ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( self . config . has_option ( 'repository' , 'source' ) and ( self . config . get ( 'repository' , 'source' ) == 'odc' ) ) : #NEWLINE# #INDENT# from pycsw.plugins.repository.odc import odc #NEWLINE# try : #NEWLINE# #INDENT# self . repository = odc . OpenDataCatalogRepository ( self . context , repo_filter ) #NEWLINE# LOGGER . debug ( ( 'OpenDataCatalog repository loaded (geonode): %s.' % self . repository . dbtype ) ) #NEWLINE# #UNINDENT# except Exception as err : #NEWLINE# #INDENT# self . response = self . iface . exceptionreport ( 'NoApplicableCode' , 'service' , ( 'Could not load repository (odc): %s' % str ( err ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# self . orm = 'sqlalchemy' #NEWLINE# from pycsw.core import repository #NEWLINE# try : #NEWLINE# #INDENT# self . repository = repository . Repository ( self . config . get ( 'repository' , 'database' ) , self . context , self . environ . get ( 'local.app_root' , None ) , self . config . get ( 'repository' , 'table' ) , repo_filter ) #NEWLINE# LOGGER . debug ( ( 'Repository loaded (local): %s.' % self . repository . dbtype ) ) #NEWLINE# #UNINDENT# except Exception as err : #NEWLINE# #INDENT# self . response = self . iface . exceptionreport ( 'NoApplicableCode' , 'service' , ( 'Could not load repository (local): %s' % str ( err ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if ( self . requesttype == 'POST' ) : #NEWLINE# #INDENT# LOGGER . debug ( self . iface . version ) #NEWLINE# self . kvp = self . iface . parse_postdata ( self . request ) #NEWLINE# #UNINDENT# error = 0 #NEWLINE# if isinstance ( self . kvp , str ) : #NEWLINE# #INDENT# error = 1 #NEWLINE# locator = 'service' #NEWLINE# text = self . kvp #NEWLINE# if ( ( self . kvp . find ( 'the document is not valid' ) != ( - 1 ) ) or ( self . kvp . find ( 'document not well-formed' ) != ( - 1 ) ) ) : #NEWLINE# #INDENT# code = 'NoApplicableCode' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# code = 'InvalidParameterValue' #NEWLINE# #UNINDENT# #UNINDENT# LOGGER . debug ( ( 'HTTP Headers:\n%s.' % self . environ ) ) #NEWLINE# LOGGER . debug ( ( 'Parsed request parameters: %s' % self . kvp ) ) #NEWLINE# if ( ( not isinstance ( self . kvp , str ) ) and ( 'mode' in self . kvp ) and ( self . kvp [ 'mode' ] == 'opensearch' ) ) : #NEWLINE# #INDENT# self . mode = 'opensearch' #NEWLINE# LOGGER . debug ( 'OpenSearch mode detected; processing request.' ) #NEWLINE# self . kvp [ 'outputschema' ] = 'http://www.w3.org/2005/Atom' #NEWLINE# #UNINDENT# if ( ( ( self . kvp == { #NEWLINE# #INDENT# '' : '' , #NEWLINE# #UNINDENT# } ) and ( self . request_version == '3.0.0' ) ) or ( ( len ( self . kvp ) == 1 ) and ( 'config' in self . kvp ) ) ) : #NEWLINE# #INDENT# LOGGER . debug ( 'Turning on default csw30:Capabilities for base URL' ) #NEWLINE# self . kvp = { #NEWLINE# #INDENT# 'service' : 'CSW' , #NEWLINE# 'acceptversions' : '3.0.0' , #NEWLINE# 'request' : 'GetCapabilities' , #NEWLINE# #UNINDENT# } #NEWLINE# http_accept = self . environ . get ( 'HTTP_ACCEPT' , '' ) #NEWLINE# if ( 'application/opensearchdescription+xml' in http_accept ) : #NEWLINE# #INDENT# self . mode = 'opensearch' #NEWLINE# self . kvp [ 'outputschema' ] = 'http://www.w3.org/2005/Atom' #NEWLINE# #UNINDENT# #UNINDENT# if ( error == 0 ) : #NEWLINE# #INDENT# basic_options = [ 'service' , 'request' ] #NEWLINE# request = self . kvp . get ( 'request' , '' ) #NEWLINE# own_version_integer = util . get_version_integer ( self . request_version ) #NEWLINE# if ( self . request_version == '2.0.2' ) : #NEWLINE# #INDENT# basic_options . append ( 'version' ) #NEWLINE# #UNINDENT# for k in basic_options : #NEWLINE# #INDENT# if ( k not in self . kvp ) : #NEWLINE# #INDENT# if ( ( k in [ 'version' , 'acceptversions' ] ) and ( request == 'GetCapabilities' ) ) : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# error = 1 #NEWLINE# locator = k #NEWLINE# code = 'MissingParameterValue' #NEWLINE# text = ( 'Missing keyword: %s' % k ) #NEWLINE# break #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if ( error == 0 ) : #NEWLINE# #INDENT# if ( self . kvp [ 'service' ] != 'CSW' ) : #NEWLINE# #INDENT# error = 1 #NEWLINE# locator = 'service' #NEWLINE# code = 'InvalidParameterValue' #NEWLINE# text = ( 'Invalid value for service: %s.                    Value MUST be CSW' % self . kvp [ 'service' ] ) #NEWLINE# #UNINDENT# kvp_version = self . kvp . get ( 'version' , '' ) #NEWLINE# kvp_version_integer = util . get_version_integer ( kvp_version ) #NEWLINE# if ( ( request != 'GetCapabilities' ) and ( kvp_version_integer != own_version_integer ) ) : #NEWLINE# #INDENT# error = 1 #NEWLINE# locator = 'version' #NEWLINE# code = 'InvalidParameterValue' #NEWLINE# text = ( 'Invalid value for version: %s. Value MUST be 2.0.2 or 3.0.0' % kvp_version ) #NEWLINE# #UNINDENT# if ( 'acceptversions' in self . kvp ) : #NEWLINE# #INDENT# for vers in self . kvp [ 'acceptversions' ] . split ( ',' ) : #NEWLINE# #INDENT# vers_integer = util . get_version_integer ( vers ) #NEWLINE# if ( vers_integer == own_version_integer ) : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# error = 1 #NEWLINE# locator = 'acceptversions' #NEWLINE# code = 'VersionNegotiationFailed' #NEWLINE# text = ( 'Invalid parameter value in acceptversions: %s. Value MUST be 2.0.2 or 3.0.0' % self . kvp [ 'acceptversions' ] ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if ( self . kvp [ 'request' ] not in self . context . model [ 'operations' ] ) : #NEWLINE# #INDENT# error = 1 #NEWLINE# locator = 'request' #NEWLINE# if ( request in [ 'Transaction' , 'Harvest' ] ) : #NEWLINE# #INDENT# code = 'OperationNotSupported' #NEWLINE# text = ( '%s operations are not supported' % request ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# code = 'InvalidParameterValue' #NEWLINE# text = ( 'Invalid value for request: %s' % request ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# if ( error == 1 ) : #NEWLINE# #INDENT# self . response = self . iface . exceptionreport ( code , locator , text ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( 'responsehandler' in self . kvp ) : #NEWLINE# #INDENT# import threading #NEWLINE# self . async = True #NEWLINE# request_id = self . kvp . get ( 'requestid' , None ) #NEWLINE# if ( request_id is None ) : #NEWLINE# #INDENT# import uuid #NEWLINE# self . kvp [ 'requestid' ] = str ( uuid . uuid4 ( ) ) #NEWLINE# #UNINDENT# #UNINDENT# if ( self . kvp [ 'request' ] == 'GetCapabilities' ) : #NEWLINE# #INDENT# self . response = self . iface . getcapabilities ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( self . kvp [ 'request' ] == 'DescribeRecord' ) : #NEWLINE# #INDENT# self . response = self . iface . describerecord ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( self . kvp [ 'request' ] == 'GetDomain' ) : #NEWLINE# #INDENT# self . response = self . iface . getdomain ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( self . kvp [ 'request' ] == 'GetRecords' ) : #NEWLINE# #INDENT# if self . async : #NEWLINE# #INDENT# threading . Thread ( target = self . iface . getrecords ) . start ( ) #NEWLINE# self . response = self . iface . _write_acknowledgement ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# self . response = self . iface . getrecords ( ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( self . kvp [ 'request' ] == 'GetRecordById' ) : #NEWLINE# #INDENT# self . response = self . iface . getrecordbyid ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( self . kvp [ 'request' ] == 'GetRepositoryItem' ) : #NEWLINE# #INDENT# self . response = self . iface . getrepositoryitem ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( self . kvp [ 'request' ] == 'Transaction' ) : #NEWLINE# #INDENT# self . response = self . iface . transaction ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( self . kvp [ 'request' ] == 'Harvest' ) : #NEWLINE# #INDENT# if self . async : #NEWLINE# #INDENT# threading . Thread ( target = self . iface . harvest ) . start ( ) #NEWLINE# self . response = self . iface . _write_acknowledgement ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# self . response = self . iface . harvest ( ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# self . response = self . iface . exceptionreport ( 'InvalidParameterValue' , 'request' , ( 'Invalid request parameter: %s' % self . kvp [ 'request' ] ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# if ( self . mode == 'sru' ) : #NEWLINE# #INDENT# LOGGER . debug ( 'SRU mode detected; processing response.' ) #NEWLINE# self . response = self . sru ( ) . response_csw2sru ( self . response , self . environ ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( self . mode == 'opensearch' ) : #NEWLINE# #INDENT# LOGGER . debug ( 'OpenSearch mode detected; processing response.' ) #NEWLINE# self . response = self . opensearch ( ) . response_csw2opensearch ( self . response , self . config ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( self . mode == 'oaipmh' ) : #NEWLINE# #INDENT# LOGGER . debug ( 'OAI-PMH mode detected; processing response.' ) #NEWLINE# self . response = self . oaipmh ( ) . response ( self . response , self . oaiargs , self . repository , self . config . get ( 'server' , 'url' ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# return self . _write_response ( )