def killSeedingTasks(self): url = self.gwc.url + 'seed/' + self.name headers = {'Content-type': 'application/text'} response, content = self.gwc.http.request(url, 'POST', "kill_all=all", headers=headers) if response.status != 200: raise FailedRequestError(content)
def killSeedingTasks(self): url = self.gwc.url + 'seed/' + self.name headers = {'Content-type': 'application/text'} resp = self.gwc.catalog.http_request(url, "kill_all=all", 'POST', headers=headers) if resp.status_code != 200: raise FailedRequestError(resp.text)
def seed(self, operation, mimetype, gridset, minzoom, maxzoom, bbox): url = self.gwc.url + "seed/" + self.name + ".xml" root = ET.Element('seedRequest') name = ET.SubElement(root, 'name') name.text = self.name if bbox is not None: bounds = ET.SubElement(root, 'bounds') coords = ET.SubElement(bounds, 'coords') for coord in bbox: coordElement = ET.SubElement(coords, 'double') coordElement.text = str(coord) gridsetName = ET.SubElement(root, 'gridSetId') gridsetName.text = gridset zoomStart = ET.SubElement(root, 'zoomStart') zoomStart.text = str(minzoom) zoomStop = ET.SubElement(root, 'zoomStop') zoomStop.text = str(maxzoom) format = ET.SubElement(root, 'format') format.text = str(mimetype) type = ET.SubElement(root, 'type') type.text = str(operation) threads = ET.SubElement(root, 'threadCount') threads.text = "1" message = ET.tostring(root) response, content = self.gwc.http.request(url, "POST", message, headers=self.headers) if response.status != 200: raise FailedRequestError(content)
def create_geoserver_db_featurestore(store_type=None, store_name=None, author_name='admin', author_email='*****@*****.**'): cat = gs_catalog dsname = store_name or ogc_server_settings.DATASTORE # get or create datastore try: if store_type == 'geogig' and ogc_server_settings.GEOGIG_ENABLED: if store_name is not None: ds = cat.get_store(store_name) else: ds = cat.get_store(settings.GEOGIG_DATASTORE_NAME) elif dsname: ds = cat.get_store(dsname) else: return None if ds is None: raise FailedRequestError except FailedRequestError: if store_type == 'geogig': if store_name is None and hasattr(settings, 'GEOGIG_DATASTORE_NAME'): store_name = settings.GEOGIG_DATASTORE_NAME logger.debug('Creating target datastore %s' % store_name) payload = make_geogig_rest_payload(author_name, author_email) response = init_geogig_repo(payload, store_name) headers, body = response if 400 <= int(headers['status']) < 600: raise FailedRequestError( _("Error code (%s) from GeoServer: %s" % (headers['status'], body))) ds = cat.create_datastore(store_name) ds.type = "GeoGig" ds.connection_parameters.update( geogig_repository=("geoserver://%s" % store_name), branch="master", create="true") cat.save(ds) ds = cat.get_store(store_name) else: logging.info('Creating target datastore %s' % dsname) db = ogc_server_settings.datastore_db ds = cat.create_datastore(dsname) ds.connection_parameters.update( host=db['HOST'], port=db['PORT'] if isinstance( db['PORT'], basestring) else str(db['PORT']) or '5432', database=db['NAME'], user=db['USER'], passwd=db['PASSWORD'], dbtype='postgis') cat.save(ds) ds = cat.get_store(dsname) assert ds.enabled return ds
def delete(self): response, content = self.gwc.http.request(self.href, "DELETE", headers=self.headers) if response.status == 200: return (response, content) else: raise FailedRequestError(str(response) + content)
def addLayer(self, layer): headers = {"Content-type": "text/xml"} message = layer.xml() response = self.http.request(layer.href, "PUT", message, headers) headers, body = response if 400 <= int(headers['status']) < 600: raise FailedRequestError(body) return response
def addLayer(self, layer): headers = { "Content-type": "text/xml" } message = layer.xml() resp = self.catalog.http_request(layer.href, message, "PUT", headers) if 400 <= int(resp.status_code) < 600: raise FailedRequestError(resp.text) return resp.text
def truncate(self): url = self.gwc.url + "masstruncate" message = "<truncateLayer><layerName>" + self.name + "</layerName></truncateLayer>" resp = self.gwc.catalog.http_request(url, message, "POST", headers=self.headers) if resp.status_code == 200: return resp.text else: raise FailedRequestError(resp.text)
def get_layers(self, resource=None): """Prefix the layer name with ws name""" # Original code from gsconfig if isinstance(resource, basestring): resource = self.get_resource(resource) layers_url = url(self.service_url, ["layers.json"]) response, content = self.http.request(layers_url) if response.status == 200: lyrs = [] jsonlayers = json.loads(content) if "layer" not in jsonlayers["layers"]: #empty repo return [] for lyr in jsonlayers["layers"]["layer"]: lyrs.append(BaseLayer(self, lyr["name"])) else: raise FailedRequestError( "Tried to make a GET request to %s but got a %d status code: \n%s" % (layers_url, response.status, content)) if resource is not None: lyrs = [l for l in lyrs if l.resource.href == resource.href] # Start patch: layers = {} result = [] for l in lyrs: try: layers[l.name].append(l) except KeyError: layers[l.name] = [l] # Prefix all names noAscii = False for name, ls in layers.items(): try: if len(ls) == 1: l = ls[0] l.name = self.get_namespaced_name(l.name) result.append(l) else: i = 0 res = self._get_res(ls[0].name) for l in ls: l.name = "%s:%s" % (res[i].workspace.name, l.name) i += 1 result.append(l) except UnicodeDecodeError: noAscii = True if noAscii: config.iface.messageBar().pushMessage( "Warning", "Some layers contain non-ascii characters and could not be loaded", level=QgsMessageBar.WARNING, duration=10) return result
def truncate(self): url = self.gwc.url + "masstruncate" message = "<truncateLayer><layerName>" + self.name + "</layerName></truncateLayer>" response, content = self.gwc.http.request(url, "POST", message, headers=self.headers) if response.status == 200: return (response, content) else: raise FailedRequestError(str(response) + content)
def update(self, mimetypes = ['image/png'], gridsets = ['EPSG:4326', 'EPSG900913'], metaWidth = 4, metaHeight = 4): self.gridsets = gridsets self.mimetypes = mimetypes self.metaWidth = metaWidth self.metaHeight = metaHeight message = self.xml() resp = self.gwc.catalog.http_request(self.href, mesage, "POST", self.headers) if 400 <= int(resp.status_code) < 600: raise FailedRequestError(resp.text) return response
def update(self, mimetypes = ['image/png'], gridsets = ['EPSG:4326', 'EPSG900913'], metaWidth = 4, metaHeight = 4): self.gridsets = gridsets self.mimetypes = mimetypes self.metaWidth = metaWidth self.metaHeight = metaHeight message = self.xml() response = self.gwc.http.request(self.href, "POST", message, self.headers) headers, body = response if 400 <= int(headers['status']) < 600: raise FailedRequestError(body) return response
def fetch(self): response, content = self.gwc.http.request(self.href) if response.status == 200: xml = XML(content) self.mimetypes = [mimetype.text for mimetype in xml.iter('string')] self.gridsets = [gridset.text for gridset in xml.iter('gridSetName')] wh = xml.iter('metaWidthHeight') try: els = wh.next().iter('int') self.metaWidth, self.metaHeight = [int(el.text) for el in els] except: #in case this parameters are not in the layer description self.metaWidth, self.metaHeight = 1, 1 else: raise FailedRequestError(content)
def get_store(cat, name, workspace=None): # Make sure workspace is a workspace object and not a string. # If the workspace does not exist, continue as if no workspace had been defined. if isinstance(workspace, str) or isinstance(workspace, six.string_types): workspace = cat.get_workspace(workspace) if workspace is None: workspace = cat.get_default_workspace() if workspace: try: store = cat.get_xml('%s/%s.xml' % (workspace.datastore_url[:-4], name)) except FailedRequestError: try: store = cat.get_xml('%s/%s.xml' % (workspace.coveragestore_url[:-4], name)) except FailedRequestError: try: store = cat.get_xml('%s/%s.xml' % (workspace.wmsstore_url[:-4], name)) except FailedRequestError: raise FailedRequestError("No store found named: " + name) if store: if store.tag == 'dataStore': store = datastore_from_index(cat, workspace, store) elif store.tag == 'coverageStore': store = coveragestore_from_index(cat, workspace, store) elif store.tag == 'wmsStore': store = wmsstore_from_index(cat, workspace, store) return store else: raise FailedRequestError("No store found named: " + name) else: raise FailedRequestError("No store found named: " + name)
def getSeedingState(self): url = self.gwc.url + 'seed/' + self.name + '.xml' headers = {'Content-type': 'text/json'} resp = self.gwc.catalog.http_request(url, "", 'GET', headers=headers) if resp.status_code != 200: raise FailedRequestError(resp.text) else: try: array = json.loads(resp.text)['long-array-array'] if array: return array[0][0], array[0][1] else: return None except Exception as e: raise SeedingStatusParsingError() return resp.text
def getSeedingState(self): url = self.gwc.url + 'seed/' + self.name + '.xml' headers = {'Content-type': 'text/json'} response, content = self.gwc.http.request(url, 'GET', headers=headers) if response.status != 200: raise FailedRequestError(content) else: try: array = json.loads(content)['long-array-array'] if array: return array[0][0], array[0][1] else: return None except Exception, e: raise SeedingStatusParsingError() return content
class AuthCatalog(BaseCatalog): def __init__(self, service_url, authid, cache_time): # Do not call parent constructor, this is a patching class self.authid = authid self.cache_time = cache_time self.service_url = service_url self._cache = dict() self._version = None self.http = NetworkAccessManager(self.authid, exception_class=FailedRequestError) self.username = '' self.password = '' def setup_connection(self): pass def get_xml(self, rest_url): """Read cached time from settings""" logger.debug("GET %s", rest_url) cached_response = self._cache.get(rest_url) def is_valid(cached_response): return cached_response is not None and datetime.now( ) - cached_response[0] < timedelta(seconds=self.cache_time) def parse_or_raise(xml): try: return XML(xml) except (ExpatError, SyntaxError), e: msg = "GeoServer gave non-XML response for [GET %s]: %s" msg = msg % (rest_url, xml) raise Exception(msg, e) if is_valid(cached_response): raw_text = cached_response[1] return parse_or_raise(raw_text) else: response, content = self.http.request(rest_url) if response.status == 200: self._cache[rest_url] = (datetime.now(), content) return parse_or_raise(content) else: raise FailedRequestError( "Tried to make a GET request to %s but got a %d status code: \n%s" % (rest_url, response.status, content))
def set_default_style_to_open_in_visual_mode(instance, **kwargs): if isinstance(instance, Dataset): style = gs_catalog.get_style(instance.name, workspace=instance.workspace) or \ gs_catalog.get_style(isinstance.name) if style: headers = { "Content-type": "application/json", "Accept": "application/json" } data = {"style": {"metadata": {"msForceVisual": "true"}}} body_href = os.path.splitext(style.body_href)[0] + '.json' resp = gs_catalog.http_request(body_href, method='put', data=json.dumps(data), headers=headers) if resp.status_code not in (200, 201, 202): raise FailedRequestError( 'Failed to update style {} : {}, {}'.format( style.name, resp.status_code, resp.text))
class BaseCatalog(Catalog): def _get_res(self, name): return [r for r in self.get_resources() if r.name == name] def get_namespaced_name(self, layer_name): """ Prefix the layer name with the workspace by querying all the resources and finding the workspace from the one that matches the layer name. If the layer exists in several workspaces, the first match is returned. Return layer_name if the layer resource does not exists. """ if layer_name.find(':') != -1: return layer_name res = self._get_res(layer_name) try: return "%s:%s" % (res[0].workspace.name, layer_name) except IndexError: return layer_name def get_layers(self, resource=None): """Prefix the layer name with ws name""" # Original code from gsconfig if isinstance(resource, basestring): resource = self.get_resource(resource) layers_url = url(self.service_url, ["layers.json"]) response, content = self.http.request(layers_url) if response.status == 200: lyrs = [] jsonlayers = json.loads(content) if "layer" not in jsonlayers["layers"]: #empty repo return [] for lyr in jsonlayers["layers"]["layer"]: lyrs.append(BaseLayer(self, lyr["name"])) else: raise FailedRequestError( "Tried to make a GET request to %s but got a %d status code: \n%s" % (layers_url, response.status, content)) if resource is not None: lyrs = [l for l in lyrs if l.resource.href == resource.href] # Start patch: layers = {} result = [] for l in lyrs: try: layers[l.name].append(l) except KeyError: layers[l.name] = [l] # Prefix all names noAscii = False for name, ls in layers.items(): try: if len(ls) == 1: l = ls[0] l.name = self.get_namespaced_name(l.name) result.append(l) else: i = 0 res = self._get_res(ls[0].name) for l in ls: l.name = "%s:%s" % (res[i].workspace.name, l.name) i += 1 result.append(l) except UnicodeDecodeError: noAscii = True if noAscii: config.iface.messageBar().pushMessage( "Warning", "Some layers contain non-ascii characters and could not be loaded", level=QgsMessageBar.WARNING, duration=10) return result def get_xml(self, rest_url): cached_response = self._cache.get(rest_url) def is_valid(cached_response): return cached_response is not None and datetime.now( ) - cached_response[0] < timedelta(seconds=5) def parse_or_raise(xml): try: xml = unicode(xml, errors="ignore").decode("utf-8", errors="ignore") return XML(xml) except (ExpatError, SyntaxError), e: msg = "GeoServer gave non-XML response for [GET %s]: %s" msg = msg % (rest_url, xml) raise Exception(msg, e) if is_valid(cached_response): raw_text = cached_response[1] return parse_or_raise(raw_text) else: response, content = self.http.request(rest_url) if response.status == 200: self._cache[rest_url] = (datetime.now(), content) return parse_or_raise(content) else: raise FailedRequestError( "Tried to make a GET request to %s but got a %d status code: \n%s" % (rest_url, response.status, content))
def blowup(self): raise FailedRequestError()
def delete(self): resp = self.gwc.catalog.http_request(self.href, "", "DELETE", headers=self.headers) if resp.status_code == 200: return resp.text else: raise FailedRequestError(resp.text)