Skip to content

Commit

Permalink
With list commands, make parameters optional. By default show everyth…
Browse files Browse the repository at this point in the history
…ing.
  • Loading branch information
jericks committed Aug 10, 2014
1 parent a2ac66f commit b09960c
Show file tree
Hide file tree
Showing 7 changed files with 636 additions and 106 deletions.
34 changes: 27 additions & 7 deletions src/main/java/org/geoserver/shell/DataStoreCommands.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.geoserver.shell;

import com.google.common.base.Strings;
import it.geosolutions.geoserver.rest.GeoServerRESTReader;
import it.geosolutions.geoserver.rest.HTTPUtils;
import it.geosolutions.geoserver.rest.decoder.RESTDataStore;
Expand Down Expand Up @@ -35,15 +36,34 @@ public boolean isCommandAvailable() {

@CliCommand(value = "datastore list", help = "List data stores.")
public String list(
@CliOption(key = "workspace", mandatory = true, help = "The workspace") String workspace
@CliOption(key = "workspace", mandatory = false, help = "The workspace") String workspace
) throws Exception {
GeoServerRESTReader reader = new GeoServerRESTReader(geoserver.getUrl(), geoserver.getUser(), geoserver.getPassword());
RESTDataStoreList dataStores = reader.getDatastores(workspace);
List<String> names = dataStores.getNames();
Collections.sort(names);
StringBuilder builder = new StringBuilder();
for (String name : names) {
builder.append(name + OsUtils.LINE_SEPARATOR);
GeoServerRESTReader reader = new GeoServerRESTReader(geoserver.getUrl(), geoserver.getUser(), geoserver.getPassword());
List<String> workspaces = new ArrayList<String>();
if (workspace != null) {
workspaces.add(workspace);
} else {
List<String> names = reader.getWorkspaceNames();
Collections.sort(names);
workspaces.addAll(names);
}
int counter = 0;
for (String w : workspaces) {
if (workspaces.size() > 1) {
if (counter > 0) {
builder.append(OsUtils.LINE_SEPARATOR);
}
builder.append(w).append(OsUtils.LINE_SEPARATOR);
builder.append(Strings.repeat("-", w.length())).append(OsUtils.LINE_SEPARATOR);
}
RESTDataStoreList dataStores = reader.getDatastores(w);
List<String> names = dataStores.getNames();
Collections.sort(names);
for (String name : names) {
builder.append(name + OsUtils.LINE_SEPARATOR);
}
counter++;
}
return builder.toString();
}
Expand Down
123 changes: 84 additions & 39 deletions src/main/java/org/geoserver/shell/FeatureTypeCommands.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.geoserver.shell;

import com.google.common.base.Strings;
import it.geosolutions.geoserver.rest.GeoServerRESTReader;
import it.geosolutions.geoserver.rest.HTTPUtils;
import it.geosolutions.geoserver.rest.decoder.RESTDataStoreList;
import it.geosolutions.geoserver.rest.decoder.utils.JDOMBuilder;
import org.geotools.data.DataUtilities;
import org.geotools.referencing.CRS;
Expand Down Expand Up @@ -36,33 +39,75 @@ public boolean isCommandAvailable() {

@CliCommand(value = "featuretype list", help = "List feature types.")
public String list(
@CliOption(key = "workspace", mandatory = true, help = "The workspace") String workspace,
@CliOption(key = "datastore", mandatory = true, help = "The datastore") String datastore,
@CliOption(key = "workspace", mandatory = false, help = "The workspace") String workspace,
@CliOption(key = "datastore", mandatory = false, help = "The datastore") String datastore,
@CliOption(key = "list", mandatory = false, unspecifiedDefaultValue = "configured", help = "The list parameter (configured, available, available_with_geom, all)") String list
) throws Exception {
String url = geoserver.getUrl() + "/rest/workspaces/" + URLUtil.encode(workspace) + "/datastores/" + URLUtil.encode(datastore) + "/featuretypes.xml?list=" + list;
String xml = HTTPUtils.get(url, geoserver.getUser(), geoserver.getPassword());
Element element = JDOMBuilder.buildElement(xml);
List<String> names = new ArrayList<String>();
if (element.getName().equalsIgnoreCase("featureTypes")) {
List<Element> elements = element.getChildren("featureType");
for (Element elem : elements) {
names.add(elem.getChildText("name"));
}
} else {
List<Element> elements = element.getChildren("featureTypeName");
for (Element elem : elements) {
names.add(elem.getTextTrim());
}
}
Collections.sort(names);
StringBuilder builder = new StringBuilder();
for (String name : names) {
builder.append(name + OsUtils.LINE_SEPARATOR);
GeoServerRESTReader reader = new GeoServerRESTReader(geoserver.getUrl(), geoserver.getUser(), geoserver.getPassword());
// Build the List of Workspaces
List<String> workspaces = new ArrayList<String>();
if (workspace != null) {
workspaces.add(workspace);
} else {
List<String> names = reader.getWorkspaceNames();
Collections.sort(names);
workspaces.addAll(names);
}
if (geoserver.isVerbose()) {
System.out.println("URL: " + url);
System.out.println("Response: " + xml);
int workspaceCounter = 0;
for (String w : workspaces) {
// Build up the List of DataSstores
List<String> datastores = new ArrayList<String>();
if (datastore != null) {
datastores.add(datastore);
} else {
RESTDataStoreList dsList = reader.getDatastores(w);
List<String> names = dsList.getNames();
Collections.sort(names);
datastores.addAll(names);
}
// Display the Workspace (but only if we are showing more than one)
if (workspaces.size() > 1) {
if (workspaceCounter > 0) {
builder.append(OsUtils.LINE_SEPARATOR);
}
builder.append(w).append(OsUtils.LINE_SEPARATOR);
builder.append(Strings.repeat("-", w.length())).append(OsUtils.LINE_SEPARATOR);
}
for (String ds : datastores) {
String indent = Strings.repeat(" ", workspaces.size() > 1 ? 3 : 0);
// Display the Datastore (but only if we are showing more than one)
if (datastores.size() > 1) {
builder.append(OsUtils.LINE_SEPARATOR);
builder.append(indent).append(ds).append(OsUtils.LINE_SEPARATOR);
builder.append(indent).append(Strings.repeat("-", ds.length())).append(OsUtils.LINE_SEPARATOR);
}
// Get the List of FeatureTypes
String url = geoserver.getUrl() + "/rest/workspaces/" + URLUtil.encode(w) + "/datastores/" + URLUtil.encode(ds) + "/featuretypes.xml?list=" + list;
String xml = HTTPUtils.get(url, geoserver.getUser(), geoserver.getPassword());
Element element = JDOMBuilder.buildElement(xml);
List<String> names = new ArrayList<String>();
if (element.getName().equalsIgnoreCase("featureTypes")) {
List<Element> elements = element.getChildren("featureType");
for (Element elem : elements) {
names.add(elem.getChildText("name"));
}
} else {
List<Element> elements = element.getChildren("featureTypeName");
for (Element elem : elements) {
names.add(elem.getTextTrim());
}
}
Collections.sort(names);
for (String name : names) {
builder.append(indent).append(name + OsUtils.LINE_SEPARATOR);
}
if (geoserver.isVerbose()) {
System.out.println("URL: " + url);
System.out.println("Response: " + xml);
}
}
workspaceCounter++;
}
return builder.toString();
}
Expand Down Expand Up @@ -235,10 +280,10 @@ public String get(
builder.append(TAB).append("Keywords: ").append(OsUtils.LINE_SEPARATOR);
Element keywordElement = featureTypeElement.getChild("keywords");
if (keywordElement != null) {
List<Element> keywordElements = keywordElement.getChildren("string");
for (Element elem : keywordElements) {
builder.append(TAB).append(TAB).append(elem.getTextTrim()).append(OsUtils.LINE_SEPARATOR);
}
List<Element> keywordElements = keywordElement.getChildren("string");
for (Element elem : keywordElements) {
builder.append(TAB).append(TAB).append(elem.getTextTrim()).append(OsUtils.LINE_SEPARATOR);
}
}
builder.append(TAB).append("Native CRS: ").append(featureTypeElement.getChildText("nativeCRS")).append(OsUtils.LINE_SEPARATOR);
builder.append(TAB).append("SRS: ").append(featureTypeElement.getChildText("srs")).append(OsUtils.LINE_SEPARATOR);
Expand Down Expand Up @@ -277,18 +322,18 @@ public String get(

Element attributesElement = featureTypeElement.getChild("attributes");
if (attributesElement != null) {
List<Element> attributeElements = attributesElement.getChildren("attribute");
builder.append(TAB).append("Attributes: ").append(OsUtils.LINE_SEPARATOR);
for (Element elem : attributeElements) {
builder.append(TAB).append(TAB).append(elem.getChildText("name")).append(OsUtils.LINE_SEPARATOR);
builder.append(TAB).append(TAB).append(TAB).append("Binding: ").append(elem.getChildText("binding")).append(OsUtils.LINE_SEPARATOR);
builder.append(TAB).append(TAB).append(TAB).append("Min Occurs: ").append(elem.getChildText("minOccurs")).append(OsUtils.LINE_SEPARATOR);
builder.append(TAB).append(TAB).append(TAB).append("Max Occurs: ").append(elem.getChildText("maxOccurs")).append(OsUtils.LINE_SEPARATOR);
builder.append(TAB).append(TAB).append(TAB).append("Nillable: ").append(elem.getChildText("nillable")).append(OsUtils.LINE_SEPARATOR);
if (elem.getChild("length") != null) {
builder.append(TAB).append(TAB).append(TAB).append("Length: ").append(elem.getChildText("length")).append(OsUtils.LINE_SEPARATOR);
}
}
List<Element> attributeElements = attributesElement.getChildren("attribute");
builder.append(TAB).append("Attributes: ").append(OsUtils.LINE_SEPARATOR);
for (Element elem : attributeElements) {
builder.append(TAB).append(TAB).append(elem.getChildText("name")).append(OsUtils.LINE_SEPARATOR);
builder.append(TAB).append(TAB).append(TAB).append("Binding: ").append(elem.getChildText("binding")).append(OsUtils.LINE_SEPARATOR);
builder.append(TAB).append(TAB).append(TAB).append("Min Occurs: ").append(elem.getChildText("minOccurs")).append(OsUtils.LINE_SEPARATOR);
builder.append(TAB).append(TAB).append(TAB).append("Max Occurs: ").append(elem.getChildText("maxOccurs")).append(OsUtils.LINE_SEPARATOR);
builder.append(TAB).append(TAB).append(TAB).append("Nillable: ").append(elem.getChildText("nillable")).append(OsUtils.LINE_SEPARATOR);
if (elem.getChild("length") != null) {
builder.append(TAB).append(TAB).append(TAB).append("Length: ").append(elem.getChildText("length")).append(OsUtils.LINE_SEPARATOR);
}
}
}

if (geoserver.isVerbose()) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/geoserver/shell/LayerGroupCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ public boolean isCommandAvailable() {
public String list(
@CliOption(key = "workspace", mandatory = false, help = "The workspace") String workspace
) throws Exception {
StringBuilder builder = new StringBuilder();
GeoServerRESTReader reader = new GeoServerRESTReader(geoserver.getUrl(), geoserver.getUser(), geoserver.getPassword());
RESTLayerGroupList layerGroups = workspace == null ? reader.getLayerGroups() : reader.getLayerGroups(workspace);
List<String> names = layerGroups.getNames();
Collections.sort(names);
StringBuilder builder = new StringBuilder();
for (String name : names) {
builder.append(name + OsUtils.LINE_SEPARATOR);
}
Expand Down
Loading

0 comments on commit b09960c

Please sign in to comment.