private JSONObj storeDetails(JSONObj json, StoreInfo store, HttpServletRequest req) throws IOException { store(json, store); JSONObj connection = new JSONObj(); Map<String, Serializable> params = store.getConnectionParameters(); for (Entry<String, Serializable> param : params.entrySet()) { String key = param.getKey(); Object value = param.getValue(); String text = value == null ? null : value.toString(); connection.put(key, text); } if (store instanceof CoverageStoreInfo) { CoverageStoreInfo info = (CoverageStoreInfo) store; connection.put("raster", info.getURL()); } if (store instanceof WMSStoreInfo) { WMSStoreInfo info = (WMSStoreInfo) store; json.put("wms", info.getCapabilitiesURL()); } json.put("connection", connection); json.put("error", IO.error(new JSONObj(), store.getError())); if (store.isEnabled()) { resources(store, json.putArray("resources"), req); } json.put("layer-count", layerCount(store)); return json; }
@SuppressWarnings("unchecked") private boolean define(StoreInfo store, JSONObj obj) { boolean reconnect = false; for (String prop : obj.keys()) { if ("description".equals(prop)) { store.setDescription(obj.str(prop)); } else if ("enabled".equals(prop)) { store.setEnabled(obj.bool(prop)); reconnect = true; } else if ("name".equals(prop)) { store.setName(obj.str(prop)); } else if ("workspace".equals(prop)) { WorkspaceInfo newWorkspace = findWorkspace(obj.str(prop)); store.setWorkspace(newWorkspace); } else if (store instanceof CoverageStoreInfo) { CoverageStoreInfo info = (CoverageStoreInfo) store; if ("connection".equals(prop)) { JSONObj connection = obj.object(prop); if (!connection.has("raster") && connection.str("raster") != null) { throw new IllegalArgumentException( "Property connection.raster required for coverage store"); } for (String param : connection.keys()) { if ("raster".equals(param)) { String url = connection.str(param); reconnect = reconnect || url == null || !url.equals(info.getURL()); info.setURL(url); } } } } else if (store instanceof WMSStoreInfo) { WMSStoreInfo info = (WMSStoreInfo) store; if ("connection".equals(prop)) { JSONObj connection = obj.object(prop); if (!connection.has("url") && connection.str("url") != null) { throw new IllegalArgumentException("Property connection.url required for wms store"); } for (String param : connection.keys()) { if ("url".equals(param)) { String url = connection.str(param); reconnect = reconnect || url == null || !url.equals(info.getCapabilitiesURL()); info.setCapabilitiesURL(url); } } } } if (store instanceof DataStoreInfo) { DataStoreInfo info = (DataStoreInfo) store; if ("connection".equals(prop)) { JSONObj connection = obj.object(prop); info.getConnectionParameters().clear(); info.getConnectionParameters().putAll(connection.raw()); reconnect = true; } } } return reconnect; }
private JSONObj store(JSONObj obj, StoreInfo store) { String name = store.getName(); obj.put("name", name) .put("workspace", store.getWorkspace().getName()) .put("enabled", store.isEnabled()) .put("description", store.getDescription()) .put("format", store.getType()); String source = source(store); obj.put("source", source) .put("type", IO.Kind.of(store).name()) .put("kind", IO.Type.of(store).name()); return IO.metadata(obj, store); }
private String source(StoreInfo store) { if (store instanceof CoverageStoreInfo) { CoverageStoreInfo coverage = (CoverageStoreInfo) store; return sourceURL(coverage.getURL()); } GeoServerResourceLoader resourceLoader = geoServer.getCatalog().getResourceLoader(); Map<String, Serializable> params = ResourcePool.getParams(store.getConnectionParameters(), resourceLoader); if (params.containsKey("dbtype")) { // See JDBCDataStoreFactory for details String host = Converters.convert(params.get("host"), String.class); String port = Converters.convert(params.get("port"), String.class); String dbtype = Converters.convert(params.get("dbtype"), String.class); String schema = Converters.convert(params.get("schema"), String.class); String database = Converters.convert(params.get("database"), String.class); StringBuilder source = new StringBuilder(); source.append(host); if (port != null) { source.append(':').append(port); } source.append('/').append(dbtype).append('/').append(database); if (schema != null) { source.append('/').append(schema); } return source.toString(); } else if (store instanceof WMSStoreInfo) { String url = ((WMSStoreInfo) store).getCapabilitiesURL(); return url; } else if (params.keySet().contains("directory")) { String directory = Converters.convert(params.get("directory"), String.class); return sourceFile(directory); } else if (params.keySet().contains("file")) { String file = Converters.convert(params.get("file"), String.class); return sourceFile(file); } if (params.containsKey("url")) { String url = Converters.convert(params.get("url"), String.class); return sourceURL(url); } for (Object value : params.values()) { if (value instanceof URL) { return source((URL) value); } if (value instanceof File) { return source((File) value); } if (value instanceof String) { String text = (String) value; if (text.startsWith("file:")) { return sourceURL(text); } else if (text.startsWith("http:") || text.startsWith("https:") || text.startsWith("ftp:")) { return text; } } } return "undertermined"; }
@SuppressWarnings("unchecked") private JSONArr resources(StoreInfo store, JSONArr list, HttpServletRequest req) throws IOException { Catalog cat = geoServer.getCatalog(); WorkspaceInfo ws = store.getWorkspace(); for (String resource : listResources(store)) { resource(list.addObject(), store, resource, req); } return list; }
/** * Returns the appropriate icon for the specified store. * * @param storeInfo * @return * @see #getStoreIcon(Class) */ public ResourceReference getStoreIcon(final StoreInfo storeInfo) { Class<?> factoryClass = null; Catalog catalog = storeInfo.getCatalog(); final ResourcePool resourcePool = catalog.getResourcePool(); if (storeInfo instanceof DataStoreInfo) { DataAccessFactory dataStoreFactory = null; try { dataStoreFactory = resourcePool.getDataStoreFactory((DataStoreInfo) storeInfo); } catch (IOException e) { LOGGER.log( Level.INFO, "factory class for storeInfo " + storeInfo.getName() + " not found", e); } if (dataStoreFactory != null) { return getStoreIcon(dataStoreFactory.getClass()); } } else if (storeInfo instanceof CoverageStoreInfo) { AbstractGridFormat format = resourcePool.getGridCoverageFormat((CoverageStoreInfo) storeInfo); if (format != null) { return getStoreIcon(format.getClass()); } } else if (storeInfo instanceof WMSStoreInfo) { return MAP_STORE_ICON; } else { throw new IllegalStateException(storeInfo.getClass().getName()); } LOGGER.info( "Could not determine icon for StoreInfo " + storeInfo.getName() + ". Using 'unknown' icon."); return UNKNOWN_ICON; }
private JSONArr layers(StoreInfo store, JSONArr list) throws IOException { Catalog cat = geoServer.getCatalog(); WorkspaceInfo ws = store.getWorkspace(); Filter filter = and(equal("store", store), equal("namespace.prefix", ws.getName())); try (CloseableIterator<ResourceInfo> layers = cat.list(ResourceInfo.class, filter); ) { while (layers.hasNext()) { ResourceInfo r = layers.next(); for (LayerInfo l : cat.getLayers(r)) { layer(list.addObject(), l, true); } } } return list; }
private static DataStorePanelInfo getDefaultPanelInfo( StoreInfo storeInfo, GeoServerApplication app) { final List<DataStorePanelInfo> providers = app.getBeansOfType(DataStorePanelInfo.class); DataStorePanelInfo panelInfo = null; for (DataStorePanelInfo provider : providers) { if (storeInfo instanceof DataStoreInfo && "defaultVector".equals(provider.getId())) { panelInfo = provider; break; } else if (storeInfo instanceof CoverageStoreInfo && "defaultRaster".equals(provider.getId())) { panelInfo = provider; break; } } if (panelInfo == null) { if (storeInfo instanceof DataStoreInfo) { throw new IllegalStateException( "Bean of type DataStorePanelInfo named " + "'defaultDataStorePanel' not provided by application context"); } else if (storeInfo instanceof CoverageStoreInfo) { throw new IllegalStateException( "Bean of type DataStorePanelInfo named " + "'defaultCoverageStorePanel' not provided by application context"); } else { throw new IllegalArgumentException("Unknown store type: " + storeInfo.getClass().getName()); } } if (panelInfo.getComponentClass() == null) { throw new IllegalStateException( "Default DataStorePanelInfo '" + panelInfo.getId() + "' does not define a componentClass property"); } if (panelInfo.getIconBase() == null || panelInfo.getIcon() == null) { throw new IllegalStateException( "Default DataStorePanelInfo '" + panelInfo.getId() + "' does not define default icon"); } return panelInfo; }
int layerCount(StoreInfo store) throws IOException { Catalog cat = geoServer.getCatalog(); WorkspaceInfo ws = store.getWorkspace(); Filter filter = and(equal("store", store), equal("namespace.prefix", ws.getName())); int count = 0; try (CloseableIterator<ResourceInfo> layers = cat.list(ResourceInfo.class, filter); ) { while (layers.hasNext()) { ResourceInfo r = layers.next(); for (LayerInfo l : cat.getLayers(r)) { if (l != null) { count++; } } } } return count; }
/** * @param storeInfo * @param app * @return the extension point descriptor for the given storeInfo, or {@code null} if there's no * contribution specific for the given storeInfo's type */ private static DataStorePanelInfo findPanelInfo( final StoreInfo storeInfo, final GeoServerApplication app) { final Catalog catalog = storeInfo.getCatalog(); final ResourcePool resourcePool = catalog.getResourcePool(); Class<?> factoryClass = null; if (storeInfo instanceof DataStoreInfo) { DataAccessFactory storeFactory; try { storeFactory = resourcePool.getDataStoreFactory((DataStoreInfo) storeInfo); } catch (IOException e) { throw new IllegalArgumentException("no factory found for StoreInfo " + storeInfo); } if (storeFactory != null) { factoryClass = storeFactory.getClass(); } } else if (storeInfo instanceof CoverageStoreInfo) { AbstractGridFormat gridFormat; gridFormat = resourcePool.getGridCoverageFormat((CoverageStoreInfo) storeInfo); if (gridFormat != null) { factoryClass = gridFormat.getClass(); } } else { throw new IllegalArgumentException("Unknown store type: " + storeInfo.getClass().getName()); } if (factoryClass == null) { throw new IllegalArgumentException("Can't locate the factory for the store"); } final List<DataStorePanelInfo> providers = app.getBeansOfType(DataStorePanelInfo.class); List<DataStorePanelInfo> fallbacks = new ArrayList<DataStorePanelInfo>(); for (DataStorePanelInfo provider : providers) { Class<?> providerFactoryClass = provider.getFactoryClass(); if (providerFactoryClass == null) { continue; } if (factoryClass.equals(providerFactoryClass)) { return provider; } else if (providerFactoryClass.isAssignableFrom(factoryClass)) { fallbacks.add(provider); } } if (fallbacks.size() == 1) { return fallbacks.get(0); } else if (fallbacks.size() > 1) { // sort by class hierarchy, pick the closest match Collections.sort( fallbacks, new Comparator<DataStorePanelInfo>() { public int compare(DataStorePanelInfo o1, DataStorePanelInfo o2) { Class c1 = o1.getFactoryClass(); Class c2 = o2.getFactoryClass(); if (c1.equals(c2)) { return 0; } if (c1.isAssignableFrom(c2)) { return 1; } if (c2.isAssignableFrom(c1)) {; } return -1; } }); // check first two and make sure bindings are not equal DataStorePanelInfo f1 = fallbacks.get(0); DataStorePanelInfo f2 = fallbacks.get(1); if (f1.getFactoryClass().equals(f2.getFactoryClass())) { String msg = "Multiple editor panels for : (" + f1.getFactoryClass() + "): " + f1 + ", " + f2; throw new RuntimeException(msg); } return f1; } // ok, we don't have a specific one return null; }
private JSONObj resource(JSONObj obj, StoreInfo store, String name, HttpServletRequest req) throws IOException { obj.put("name", name); if (store instanceof DataStoreInfo) { DataStoreInfo data = (DataStoreInfo) store; @SuppressWarnings("rawtypes") DataAccess dataStore = data.getDataStore(new NullProgressListener()); FeatureType schema; org.geotools.data.ResourceInfo info; if (dataStore instanceof DataStore) { schema = ((DataStore) dataStore).getSchema(name); info = ((DataStore) dataStore).getFeatureSource(name).getInfo(); } else { NameImpl qname = new NameImpl(name); schema = dataStore.getSchema(qname); info = dataStore.getFeatureSource(qname).getInfo(); } String title = info.getTitle() == null ? WordUtils.capitalize(name) : info.getTitle(); String description = info.getDescription() == null ? "" : info.getDescription(); obj.put("title", title); obj.put("description", description); JSONArr keywords = obj.putArray("keywords"); keywords.raw().addAll(info.getKeywords()); IO.bounds(obj.putObject("bounds"), info.getBounds()); IO.schema(obj.putObject("schema"), schema, false); } if (store instanceof CoverageStoreInfo) { CoverageStoreInfo data = (CoverageStoreInfo) store; GridCoverageReader r = data.getGridCoverageReader(null, null); obj.put("title", WordUtils.capitalize(name)); obj.put("description", ""); if (r instanceof GridCoverage2DReader) { GridCoverage2DReader reader = (GridCoverage2DReader) r; CoordinateReferenceSystem crs = reader.getCoordinateReferenceSystem(name); IO.schemaGrid(obj.putObject("schema"), crs, false); } else { IO.schemaGrid(obj.putObject("schema"), AbstractGridFormat.getDefaultCRS(), false); } } JSONArr layers = obj.putArray("layers"); Catalog cat = geoServer.getCatalog(); if (store instanceof CoverageStoreInfo) { // coverage store does not respect native name so we search by id for (CoverageInfo info : cat.getCoveragesByCoverageStore((CoverageStoreInfo) store)) { layers(info, layers); } } else { Filter filter = and(equal("namespace.prefix", store.getWorkspace().getName()), equal("nativeName", name)); try (CloseableIterator<ResourceInfo> published = cat.list(ResourceInfo.class, filter); ) { while (published.hasNext()) { ResourceInfo info = published.next(); if (!info.getStore().getId().equals(store.getId())) { continue; // native name is not enough, double check store id } layers(info, layers); } } } return obj; }