-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathdatastore.js
541 lines (504 loc) · 16 KB
/
datastore.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
import fetch from 'node-fetch';
import fs from 'fs';
import { getGeoServerResponseText, GeoServerResponseError } from './util/geoserver.js';
import AboutClient from './about.js'
/**
* Client for GeoServer data stores
*
* @module DatastoreClient
*/
export default class DatastoreClient {
/**
* Creates a GeoServer REST DatastoreClient instance.
*
* @param {String} url The URL of the GeoServer REST API endpoint
* @param {String} auth The Basic Authentication string
*/
constructor (url, auth) {
this.url = url;
this.auth = auth;
}
/**
* Get all DataStores in a workspace.
*
* @param {String} workspace The workspace to get DataStores for
*
* @returns {Object} An object containing store details
*/
async getDataStores (workspace) {
return this.getStores(workspace, 'datastores');
}
/**
* Get all CoverageStores in a workspace.
*
* @param {String} workspace The workspace to get CoverageStores for
*
* @returns {Object} An object containing store details
*/
async getCoverageStores (workspace) {
return this.getStores(workspace, 'coveragestores');
}
/**
* Get all WmsStores in a workspace.
*
* @param {String} workspace The workspace to get WmsStores for
*
* @returns {Object} An object containing store details
*/
async getWmsStores (workspace) {
return this.getStores(workspace, 'wmsstores');
}
/**
* Get all WmtsStores in a workspace.
*
* @param {String} workspace The workspace to get WmtsStores for
*
* @returns {Object} An object containing store details
*/
async getWmtsStores (workspace) {
return this.getStores(workspace, 'wmtsstores');
}
/**
* @private
* Get information about various store types in a workspace.
*
* @param {String} workspace The workspace name
* @param {String} storeType The type of store
*
* @throws Error if request fails
*
* @returns {Object} An object containing store details or undefined if it cannot be found
*/
async getStores (workspace, storeType) {
const response = await fetch(this.url + 'workspaces/' + workspace + '/' + storeType + '.json', {
credentials: 'include',
method: 'GET',
headers: {
Authorization: this.auth
}
});
if (!response.ok) {
const geoServerResponse = await getGeoServerResponseText(response);
throw new GeoServerResponseError(null, geoServerResponse);
}
return response.json();
}
/**
* Get specific DataStore by name in a workspace.
*
* @param {String} workspace The workspace to search DataStore in
* @param {String} dataStore DataStore name
*
* @returns {Object} An object containing store details or undefined if it cannot be found
*/
async getDataStore (workspace, dataStore) {
return this.getStore(workspace, dataStore, 'datastores');
}
/**
* Get specific CoverageStore by name in a workspace.
*
* @param {String} workspace The workspace to search CoverageStore in
* @param {String} covStore CoverageStore name
*
* @returns {Object} An object containing store details or undefined if it cannot be found
*/
async getCoverageStore (workspace, covStore) {
return this.getStore(workspace, covStore, 'coveragestores');
}
/**
* Get specific WmsStore by name in a workspace.
*
* @param {String} workspace The workspace to search WmsStore in
* @param {String} wmsStore WmsStore name
*
* @returns {Object} An object containing store details or undefined if it cannot be found
*
*/
async getWmsStore (workspace, wmsStore) {
return this.getStore(workspace, wmsStore, 'wmsstores');
}
/**
* Get specific WmtsStore by name in a workspace.
*
* @param {String} workspace The workspace to search WmtsStore in
* @param {String} wmtsStore WmtsStore name
*
* @returns {Object} An object containing store details or undefined if it cannot be found
*/
async getWmtsStore (workspace, wmtsStore) {
return this.getStore(workspace, wmtsStore, 'wmtsstores');
}
/**
* @private
* Get GeoServer store by type
*
* @param {String} workspace The name of the workspace
* @param {String} storeName The name of the store
* @param {String} storeType The type of the store
*
* @throws Error if request fails
*
* @returns {Object} An object containing store details or undefined if it cannot be found
*/
async getStore (workspace, storeName, storeType) {
const url = this.url + 'workspaces/' + workspace + '/' + storeType + '/' + storeName + '.json';
const response = await fetch(url, {
credentials: 'include',
method: 'GET',
headers: {
Authorization: this.auth
}
});
if (!response.ok) {
const grc = new AboutClient(this.url, this.auth);
if (await grc.exists()) {
// GeoServer exists, but requested item does not exist, we return empty
return;
} else {
// There was a general problem with GeoServer
const geoServerResponse = await getGeoServerResponseText(response);
throw new GeoServerResponseError(null, geoServerResponse);
}
}
return response.json();
}
/**
* Creates a GeoTIFF store from a file by path and publishes it as layer.
* The GeoTIFF file has to be placed on the server, where your GeoServer
* is running.
*
* @param {String} workspace The workspace to create GeoTIFF store in
* @param {String} coverageStore The name of the new GeoTIFF store
* @param {String} layerName The published name of the new layer
* @param {String} layerTitle The published title of the new layer
* @param {String} filePath The path to the GeoTIFF file on the server
*
* @throws Error if request fails
*
* @returns {String} The successful response text
*/
async createGeotiffFromFile (workspace, coverageStore, layerName, layerTitle, filePath) {
const lyrTitle = layerTitle || layerName;
const stats = fs.statSync(filePath);
const fileSizeInBytes = stats.size;
const readStream = fs.createReadStream(filePath);
let url = this.url + 'workspaces/' + workspace + '/coveragestores/' +
coverageStore + '/file.geotiff';
url += '?filename=' + lyrTitle + '&coverageName=' + layerName;
const response = await fetch(url, {
credentials: 'include',
method: 'PUT',
headers: {
Authorization: this.auth,
'Content-Type': 'image/tiff',
'Content-length': fileSizeInBytes
},
body: readStream
});
if (!response.ok) {
const geoServerResponse = await getGeoServerResponseText(response);
throw new GeoServerResponseError(null, geoServerResponse);
}
// TODO: enforce JSON response or parse XML
return response.text();
}
/**
* Creates a PostGIS based data store.
*
* @param {String} workspace The WS to create the data store in
* @param {String} namespaceUri The namespace URI of the workspace
* @param {String} dataStore The data store name to be created
* @param {String} pgHost The PostGIS DB host
* @param {String} pgPort The PostGIS DB port
* @param {String} pgUser The PostGIS DB user
* @param {String} pgPassword The PostGIS DB password
* @param {String} pgSchema The PostGIS DB schema
* @param {String} pgDb The PostGIS DB name
* @param {String} [exposePk] expose primary key, defaults to false
*
* @throws Error if request fails
*/
async createPostgisStore (workspace, namespaceUri, dataStore, pgHost, pgPort, pgUser, pgPassword, pgSchema, pgDb, exposePk) {
const body = {
dataStore: {
name: dataStore,
type: 'PostGIS',
enabled: true,
workspace: {
name: workspace
},
connectionParameters: {
entry: [
{
'@key': 'dbtype',
$: 'postgis'
},
{
'@key': 'schema',
$: pgSchema
},
{
'@key': 'database',
$: pgDb
},
{
'@key': 'host',
$: pgHost
},
{
'@key': 'port',
$: pgPort
},
{
'@key': 'passwd',
$: pgPassword
},
{
'@key': 'namespace',
$: namespaceUri
},
{
'@key': 'user',
$: pgUser
},
{
'@key': 'Expose primary keys',
$: exposePk || false
}
]
}
}
};
const url = this.url + 'workspaces/' + workspace + '/datastores';
const response = await fetch(url, {
credentials: 'include',
method: 'POST',
headers: {
Authorization: this.auth,
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
});
// TODO: not tested yet
if (!response.ok) {
const geoServerResponse = await getGeoServerResponseText(response);
throw new GeoServerResponseError(null, geoServerResponse);
}
}
/**
* Creates an ImageMosaic store from a zip archive with the 3 necessary files
* - datastore.properties
* - indexer.properties
* - timeregex.properties
*
* The zip archive has to be given as absolute path, so before it has to be
* placed on the server, where your GeoServer is running.
*
* @param {String} workspace The WS to create the data store in
* @param {String} dataStore The data store name
* @param {String} zipArchivePath Absolute path to zip archive with the 3 properties files
*
* @throws Error if request fails
*
* @returns {String} The response text
*/
async createImageMosaicStore (workspace, coverageStore, zipArchivePath) {
const readStream = fs.createReadStream(zipArchivePath);
const url = this.url + 'workspaces/' + workspace + '/coveragestores/' + coverageStore + '/file.imagemosaic';
const response = await fetch(url, {
credentials: 'include',
method: 'PUT',
headers: {
Authorization: this.auth,
'Content-Type': 'application/zip'
},
body: readStream
});
if (!response.ok) {
const geoServerResponse = await getGeoServerResponseText(response);
throw new GeoServerResponseError(null, geoServerResponse);
}
return response.text();
};
/**
* Creates a WMS based data store.
*
* @param {String} workspace The WS to create the data store in
* @param {String} dataStore The data store name
* @param {String} wmsCapabilitiesUrl Base WMS capabilities URL
*
* @throws Error if request fails
*/
async createWmsStore (workspace, dataStore, wmsCapabilitiesUrl) {
const body = {
wmsStore: {
name: dataStore,
type: 'WMS',
capabilitiesURL: wmsCapabilitiesUrl
}
};
const url = this.url + 'workspaces/' + workspace + '/wmsstores';
const response = await fetch(url, {
credentials: 'include',
method: 'POST',
headers: {
Authorization: this.auth,
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
});
if (!response.ok) {
const geoServerResponse = await getGeoServerResponseText(response);
throw new GeoServerResponseError(null, geoServerResponse);
}
}
/**
* Creates a WFS based data store.
*
* @param {String} workspace The WS to create the data store in
* @param {String} dataStore The data store name
* @param {String} wfsCapabilitiesUrl WFS capabilities URL
* @param {String} namespaceUrl URL of the GeoServer namespace
* @param {Boolean} [useHttpConnectionPooling=true] use HTTP connection pooling for WFS connection
*
* @throws Error if request fails
*/
async createWfsStore (workspace, dataStore, wfsCapabilitiesUrl, namespaceUrl, useHttpConnectionPooling) {
const body = {
dataStore: {
name: dataStore,
type: 'Web Feature Server (NG)',
connectionParameters: {
entry: [
{
'@key': 'WFSDataStoreFactory:GET_CAPABILITIES_URL',
$: wfsCapabilitiesUrl
},
{
'@key': 'namespace',
$: namespaceUrl
},
{
'@key': 'WFSDataStoreFactory:USE_HTTP_CONNECTION_POOLING',
$: useHttpConnectionPooling !== false ? 'true' : 'false'
}
]
}
}
};
const url = this.url + 'workspaces/' + workspace + '/datastores';
const response = await fetch(url, {
credentials: 'include',
method: 'POST',
headers: {
Authorization: this.auth,
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
});
if (!response.ok) {
const geoServerResponse = await getGeoServerResponseText(response);
throw new GeoServerResponseError(null, geoServerResponse);
}
}
/**
* Deletes a data store.
*
* @param {String} workspace The workspace where the data store is in
* @param {String} coverageStore Name of data store to delete
* @param {String} recurse Flag to enable recursive deletion
*
* @throws Error if request fails
*/
async deleteDataStore (workspace, dataStore, recurse) {
let url = this.url + 'workspaces/' + workspace + '/datastores/' + dataStore;
url += '?recurse=' + recurse;
const response = await fetch(url, {
credentials: 'include',
method: 'DELETE',
headers: {
Authorization: this.auth
}
});
if (!response.ok) {
// TODO: could not find status codes in the docs or via testing
// https://docs.geoserver.org/latest/en/api/#1.0.0/datastores.yaml
const geoServerResponse = await getGeoServerResponseText(response);
throw new GeoServerResponseError(null, geoServerResponse);
}
}
/**
* Deletes a CoverageStore.
*
* @param {String} workspace The workspace where the CoverageStore is in
* @param {String} coverageStore Name of CoverageStore to delete
* @param {String} recurse Flag to enable recursive deletion
*
* @throws Error if request fails
*/
async deleteCoverageStore (workspace, coverageStore, recurse) {
let url = this.url + 'workspaces/' + workspace + '/coveragestores/' + coverageStore;
url += '?recurse=' + recurse;
const response = await fetch(url, {
credentials: 'include',
method: 'DELETE',
headers: {
Authorization: this.auth
}
});
// TODO: could not test it
if (!response.ok) {
const geoServerResponse = await getGeoServerResponseText(response);
switch (response.status) {
case 401:
throw new GeoServerResponseError('Deletion failed. There might be dependant objects to ' +
'this store. Delete them first or call this with "recurse=false"', geoServerResponse);
default:
throw new GeoServerResponseError(null, geoServerResponse);
}
}
}
/**
* Creates a GeoPackage store from a file placed in the geoserver_data dir.
*
* @param {String} workspace The WS to create the data store in
* @param {String} dataStore The data store name
* @param {String} gpkgPath Relative path to GeoPackage file within geoserver_data dir
*
* @throws Error if request fails
*/
async createGpkgStore (workspace, dataStore, gpkgPath) {
const body = {
dataStore: {
name: dataStore,
type: 'GeoPackage',
connectionParameters: {
entry: [
{
'@key': 'database',
$: `file:${gpkgPath}`
},
{
'@key': 'dbtype',
$: 'geopkg'
}
]
}
}
};
const url = this.url + 'workspaces/' + workspace + '/datastores';
const response = await fetch(url, {
credentials: 'include',
method: 'POST',
headers: {
Authorization: this.auth,
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
});
if (!response.ok) {
const geoServerResponse = await getGeoServerResponseText(response);
throw new GeoServerResponseError(null, geoServerResponse);
}
}
}