package com.sigem.gis.service; import org.springframework.http.*; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; @Service public class GeoServerService { private final String GS_URL = "http://proyecto-geoserver-1:8080/geoserver/rest"; private final String GS_USER = "admin"; private final String GS_PASS = "geoserver"; private final RestTemplate restTemplate; public GeoServerService() { org.springframework.http.client.SimpleClientHttpRequestFactory factory = new org.springframework.http.client.SimpleClientHttpRequestFactory(); factory.setConnectTimeout(2000); factory.setReadTimeout(2000); this.restTemplate = new RestTemplate(factory); } public void ensureStyleExists(String styleName) { String url = GS_URL + "/styles/" + styleName; try { restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(createHeaders()), String.class); } catch (Exception e) { String sldBody = "" + "" + "morosidad_style_wms" + "Verdeultimo_pago2026#71de75#0000000.2" + "Ambarultimo_pago2024#ffaa00#0000000.2" + "Rojoultimo_pago2023#f87171#0000000.2" + "#94a3b80.4" + ""; try { // Crear el estilo String createJson = String.format("{\"style\": {\"name\": \"%s\", \"filename\": \"%s.sld\"}}", styleName, styleName); HttpHeaders h = createHeaders(); h.setContentType(MediaType.APPLICATION_JSON); restTemplate.postForEntity(GS_URL + "/styles", new HttpEntity<>(createJson, h), String.class); // Subir el SLD HttpHeaders sldHeaders = createHeaders(); sldHeaders.setContentType(MediaType.parseMediaType("application/vnd.ogc.sld+xml")); restTemplate.exchange(url, HttpMethod.PUT, new HttpEntity<>(sldBody, sldHeaders), String.class); } catch (Exception ex) { System.err.println("Error creando estilo: " + ex.getMessage()); } } } public void publishLayer(String layerName, String viewName, String styleName, String boundNo, String boundSe) { ensureInfrastructureExists(); if (styleName != null) ensureStyleExists(styleName); String workspace = "sigem"; String datastore = "sigem_db"; String url = String.format("%s/workspaces/%s/datastores/%s/featuretypes/%s", GS_URL, workspace, datastore, layerName); String jsonBody = String.format("{\"featureType\": {\"name\": \"%s\", \"nativeName\": \"%s\", \"srs\": \"EPSG:4326\"}}", layerName, viewName); HttpHeaders headers = createHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); try { restTemplate.exchange(url, HttpMethod.PUT, new HttpEntity<>(jsonBody, headers), String.class); } catch (Exception e) { String postUrl = String.format("%s/workspaces/%s/datastores/%s/featuretypes", GS_URL, workspace, datastore); try { restTemplate.postForEntity(postUrl, new HttpEntity<>(jsonBody, headers), String.class); } catch (Exception ex) {} } if (styleName != null) { String layerUrl = String.format("%s/layers/sigem:%s", GS_URL, layerName); String layerJson = String.format("{\"layer\": {\"defaultStyle\": {\"name\": \"%s\"}}}", styleName); try { restTemplate.exchange(layerUrl, HttpMethod.PUT, new HttpEntity<>(layerJson, headers), String.class); } catch (Exception e) {} } } private void ensureInfrastructureExists() { String workspace = "sigem"; String datastore = "sigem_db"; try { restTemplate.exchange(GS_URL + "/workspaces/" + workspace, HttpMethod.GET, new HttpEntity<>(createHeaders()), String.class); } catch (Exception e) { String wsJson = String.format("{\"workspace\": {\"name\": \"%s\"}}", workspace); HttpHeaders h = createHeaders(); h.setContentType(MediaType.APPLICATION_JSON); try { restTemplate.postForEntity(GS_URL + "/workspaces", new HttpEntity<>(wsJson, h), String.class); } catch (Exception ex) {} } try { restTemplate.exchange(GS_URL + "/workspaces/" + workspace + "/datastores/" + datastore, HttpMethod.GET, new HttpEntity<>(createHeaders()), String.class); } catch (Exception e) { String dsJson = String.format("{\"dataStore\": {\"name\": \"%s\", \"connectionParameters\": {\"host\": \"proyecto-postgres-1\", \"port\": \"5432\", \"database\": \"sigem\", \"user\": \"sigem_user\", \"passwd\": \"sigem_pass\", \"dbtype\": \"postgis\"}}}", datastore); HttpHeaders h = createHeaders(); h.setContentType(MediaType.APPLICATION_JSON); try { restTemplate.postForEntity(GS_URL + "/workspaces/" + workspace + "/datastores", new HttpEntity<>(dsJson, h), String.class); } catch (Exception ex) {} } } public void truncateCache(String layerName) { String url = "http://proyecto-geoserver-1:8080/geoserver/gwc/rest/masstruncate"; String xmlBody = String.format("sigem:%s", layerName); HttpHeaders h = createHeaders(); h.setContentType(MediaType.TEXT_XML); try { restTemplate.postForEntity(url, new HttpEntity<>(xmlBody, h), String.class); } catch (Exception e) {} } private HttpHeaders createHeaders() { HttpHeaders headers = new HttpHeaders(); headers.setBasicAuth(GS_USER, GS_PASS); return headers; } }