Skip to content

Commit 13c4f7d

Browse files
committed
Add PostGISCommands for easily create datastores and featuretypes from PostGIS
1 parent 83355f8 commit 13c4f7d

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

README.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,20 @@ Commands
194194

195195
* shapefile publish --workspace topp --datastore states_voronoi --layer states_voronoi --file states_voronoi.zip
196196

197+
* postgis
198+
199+
* postgis datastore create --workspace topp --datastore postgis --host localhost --port 5432 --database postgis --schema public --user uzer --password pass
200+
201+
* postgis featuretype publish --workspace topp --datastore postgis --table world_boundaries
202+
197203
* featuretype
198204

199205
* featuretype list topp --datastore taz_shapes
200206

201207
* featuretype get --workspace topp --datastore taz_shapes --featuretype tasmania_cities
202208

209+
* featuretype publish --workspace postgis --datastore tables --featuretype table
210+
203211
* featuretype create --workspace topp --datastore taz_shapes --featuretype taz_hydro --schema "the_geom:LineString:srid=4326,name:String,id:int"
204212

205213
* featuretype modify --workspace topp --datastore taz_shapes --featuretype taz_hydro --name "Tazmania Hydro Lines"
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package org.geoserver.shell;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.shell.core.CommandMarker;
5+
import org.springframework.shell.core.annotation.CliAvailabilityIndicator;
6+
import org.springframework.shell.core.annotation.CliCommand;
7+
import org.springframework.shell.core.annotation.CliOption;
8+
import org.springframework.stereotype.Component;
9+
10+
@Component
11+
public class PostGISCommands implements CommandMarker {
12+
13+
@Autowired
14+
private Geoserver geoserver;
15+
16+
public void setGeoserver(Geoserver gs) {
17+
this.geoserver = gs;
18+
}
19+
20+
@CliAvailabilityIndicator({"postgis datastore create", "postgis featuretype publish"})
21+
public boolean isCommandAvailable() {
22+
return geoserver.isSet();
23+
}
24+
25+
@CliCommand(value = "postgis datastore create", help = "Create a PostGIS DataStore.")
26+
public boolean createDataStore(
27+
@CliOption(key = "workspace", mandatory = true, help = "The workspace") String workspace,
28+
@CliOption(key = "datastore", mandatory = true, help = "The datastore") String datastore,
29+
@CliOption(key = "host", mandatory = true, help = "The host") String host,
30+
@CliOption(key = "port", mandatory = false, unspecifiedDefaultValue = "5432", help = "The port") String port,
31+
@CliOption(key = "database", mandatory = true, help = "The database name") String database,
32+
@CliOption(key = "schema", mandatory = false, unspecifiedDefaultValue = "public", help = "The schema") String schema,
33+
@CliOption(key = "user", mandatory = true, help = "The user name") String user,
34+
@CliOption(key = "password", mandatory = true, help = "The password") String password
35+
) throws Exception {
36+
StringBuilder connectionStringBuilder = new StringBuilder();
37+
connectionStringBuilder.append("dbtype=postgis").append(" ");
38+
connectionStringBuilder.append("host=").append(host).append(" ");
39+
connectionStringBuilder.append("port=").append(port).append(" ");
40+
connectionStringBuilder.append("database='").append(database).append("' ");
41+
connectionStringBuilder.append("schema='").append(schema).append("' ");
42+
connectionStringBuilder.append("user='").append(user).append("' ");
43+
connectionStringBuilder.append("password='").append(password).append("'");
44+
DataStoreCommands dataStoreCommands = new DataStoreCommands();
45+
dataStoreCommands.setGeoserver(geoserver);
46+
return dataStoreCommands.create(workspace, datastore, connectionStringBuilder.toString(), null, true);
47+
}
48+
49+
@CliCommand(value = "postgis featuretype publish", help = "Publish a PostGIS Table.")
50+
public boolean publishLayer(
51+
@CliOption(key = "workspace", mandatory = true, help = "The workspace") String workspace,
52+
@CliOption(key = "datastore", mandatory = true, help = "The datastore") String datastore,
53+
@CliOption(key = "table", mandatory = true, help = "The table") String table
54+
) throws Exception {
55+
FeatureTypeCommands featureTypeCommands = new FeatureTypeCommands();
56+
featureTypeCommands.setGeoserver(geoserver);
57+
return featureTypeCommands.publish(workspace, datastore, table);
58+
}
59+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package org.geoserver.shell;
2+
3+
import org.glassfish.grizzly.http.Method;
4+
import org.glassfish.grizzly.http.util.HttpStatus;
5+
import org.junit.Test;
6+
7+
import static com.xebialabs.restito.builder.stub.StubHttp.whenHttp;
8+
import static com.xebialabs.restito.builder.verify.VerifyHttp.verifyHttp;
9+
import static com.xebialabs.restito.semantics.Action.status;
10+
import static com.xebialabs.restito.semantics.Action.stringContent;
11+
import static com.xebialabs.restito.semantics.Condition.method;
12+
import static com.xebialabs.restito.semantics.Condition.post;
13+
import static com.xebialabs.restito.semantics.Condition.uri;
14+
import static junit.framework.Assert.assertEquals;
15+
import static junit.framework.Assert.assertTrue;
16+
17+
public class PostGISCommandsTest extends BaseTest {
18+
19+
@Test
20+
public void createDataStore() throws Exception {
21+
String url = "/geoserver/rest/workspaces/topp/datastores.xml";
22+
whenHttp(server).match(post(url)).then(stringContent("true"), status(HttpStatus.OK_200));
23+
Geoserver geoserver = new Geoserver("http://00.0.0.0:8888/geoserver", "admin", "geoserver");
24+
PostGISCommands commands = new PostGISCommands();
25+
commands.setGeoserver(geoserver);
26+
String workspace = "topp";
27+
String datastore = "postgis";
28+
String host = "localhost";
29+
String port = "5432";
30+
String database = "naturalearth";
31+
String schema = "ne";
32+
String user = "admin";
33+
String password = "s$cr$t";
34+
boolean result = commands.createDataStore(workspace, datastore, host, port, database, schema, user, password);
35+
assertTrue(result);
36+
String actual = server.getCalls().get(0).getPostBody();
37+
String expected = "<dataStore>" +
38+
"<name>postgis</name><enabled>true</enabled>" +
39+
"<connectionParameters><schema>ne</schema><port>5432</port><dbtype>postgis</dbtype>" +
40+
"<host>localhost</host><password>s$cr$t</password><user>admin</user><database>naturalearth</database>" +
41+
"</connectionParameters></dataStore>";
42+
assertEquals(expected, actual);
43+
verifyHttp(server).once(method(Method.POST), uri(url));
44+
}
45+
46+
@Test
47+
public void publishFeatureType() throws Exception {
48+
String url = "/geoserver/rest/workspaces/topp/datastores/postgis/featuretypes.xml";
49+
whenHttp(server).match(post(url)).then(stringContent(getResourceString("featuretypes.xml")), status(HttpStatus.OK_200));
50+
Geoserver geoserver = new Geoserver("http://00.0.0.0:8888/geoserver", "admin", "geoserver");
51+
PostGISCommands commands = new PostGISCommands();
52+
commands.setGeoserver(geoserver);
53+
String workspace = "topp";
54+
String dataStore = "postgis";
55+
String table = "world_boundaries";
56+
boolean result = commands.publishLayer(workspace, dataStore, table);
57+
assertTrue(result);
58+
String actual = server.getCalls().get(0).getPostBody();
59+
String expected = "<featureType><name>world_boundaries</name></featureType>";
60+
assertEquals(expected, actual);
61+
verifyHttp(server).once(method(Method.POST), uri(url));
62+
}
63+
}

0 commit comments

Comments
 (0)