geoserver-node-client 0.0.7 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/namespace.js CHANGED
@@ -1,4 +1,6 @@
1
1
  import fetch from 'node-fetch';
2
+ import { getGeoServerResponseText, GeoServerResponseError } from './util/geoserver.js';
3
+ import AboutClient from './about.js'
2
4
 
3
5
  /**
4
6
  * Client for GeoServer namespace
@@ -10,36 +12,33 @@ export default class NamespaceClient {
10
12
  * Creates a GeoServer REST NamespaceClient instance.
11
13
  *
12
14
  * @param {String} url The URL of the GeoServer REST API endpoint
13
- * @param {String} user The user for the GeoServer REST API
14
- * @param {String} password The password for the GeoServer REST API
15
+ * @param {String} auth The Basic Authentication string
15
16
  */
16
- constructor (url, user, password) {
17
- this.url = url.endsWith('/') ? url : url + '/';
18
- this.user = user;
19
- this.password = password;
17
+ constructor (url, auth) {
18
+ this.url = url;
19
+ this.auth = auth;
20
20
  }
21
21
 
22
22
  /**
23
23
  * Returns all namespaces.
24
24
  *
25
- * @returns {Object|Boolean} An object describing the namespace or 'false'
25
+ * @throws Error if request fails
26
+ *
27
+ * @returns {Object} An object describing the namespace
26
28
  */
27
29
  async getAll () {
28
- try {
29
- const auth =
30
- Buffer.from(this.user + ':' + this.password).toString('base64');
31
- const response = await fetch(this.url + 'namespaces.json', {
32
- credentials: 'include',
33
- method: 'GET',
34
- headers: {
35
- Authorization: 'Basic ' + auth
36
- }
37
- });
38
- const json = await response.json();
39
- return json;
40
- } catch (error) {
41
- return false;
30
+ const response = await fetch(this.url + 'namespaces.json', {
31
+ credentials: 'include',
32
+ method: 'GET',
33
+ headers: {
34
+ Authorization: this.auth
35
+ }
36
+ });
37
+ if (!response.ok) {
38
+ const geoServerResponse = await getGeoServerResponseText(response);
39
+ throw new GeoServerResponseError(null, geoServerResponse);
42
40
  }
41
+ return response.json();
43
42
  }
44
43
 
45
44
  /**
@@ -48,66 +47,65 @@ export default class NamespaceClient {
48
47
  * @param {String} prefix Prefix of the new namespace
49
48
  * @param {String} uri Uri of the new namespace
50
49
  *
51
- * @returns {String|Boolean} The name of the created namespace or 'false'
50
+ * @throws Error if request fails
51
+ *
52
+ * @returns {String} The name of the created namespace
52
53
  */
53
54
  async create (prefix, uri) {
54
- try {
55
- const body = {
56
- namespace: {
57
- prefix: prefix,
58
- uri: uri
59
- }
60
- };
61
-
62
- const auth =
63
- Buffer.from(this.user + ':' + this.password).toString('base64');
55
+ const body = {
56
+ namespace: {
57
+ prefix: prefix,
58
+ uri: uri
59
+ }
60
+ };
64
61
 
65
- const response = await fetch(this.url + 'namespaces', {
66
- credentials: 'include',
67
- method: 'POST',
68
- headers: {
69
- Authorization: 'Basic ' + auth,
70
- 'Content-Type': 'application/json'
71
- },
72
- body: JSON.stringify(body)
73
- });
62
+ const response = await fetch(this.url + 'namespaces', {
63
+ credentials: 'include',
64
+ method: 'POST',
65
+ headers: {
66
+ Authorization: this.auth,
67
+ 'Content-Type': 'application/json'
68
+ },
69
+ body: JSON.stringify(body)
70
+ });
74
71
 
75
- if (response.status === 201) {
76
- const responseText = await response.text();
77
- return responseText;
78
- } else {
79
- return false;
80
- }
81
- } catch (error) {
82
- return false;
72
+ if (!response.ok) {
73
+ const geoServerResponse = await getGeoServerResponseText(response);
74
+ throw new GeoServerResponseError(null, geoServerResponse);
83
75
  }
76
+
77
+ return response.text();
84
78
  }
85
79
 
86
80
  /**
87
81
  * Returns a namespace.
88
82
  *
89
83
  * @param {String} name Name of the namespace
90
- * @returns {Object|Boolean} An object describing the namespace or 'false'
84
+ *
85
+ * @throws Error if request fails
86
+ *
87
+ * @returns {Object} An object describing the namespace or undefined if it cannot be found
91
88
  */
92
89
  async get (name) {
93
- try {
94
- const auth =
95
- Buffer.from(this.user + ':' + this.password).toString('base64');
96
- const response = await fetch(this.url + 'namespaces/' + name + '.json', {
97
- credentials: 'include',
98
- method: 'GET',
99
- headers: {
100
- Authorization: 'Basic ' + auth
101
- }
102
- });
103
- if (response.status === 200) {
104
- return await response.json();
90
+ const response = await fetch(this.url + 'namespaces/' + name + '.json', {
91
+ credentials: 'include',
92
+ method: 'GET',
93
+ headers: {
94
+ Authorization: this.auth
95
+ }
96
+ });
97
+ if (!response.ok) {
98
+ const grc = new AboutClient(this.url, this.auth);
99
+ if (await grc.exists()) {
100
+ // GeoServer exists, but requested item does not exist, we return empty
101
+ return;
105
102
  } else {
106
- return false;
103
+ // There was a general problem with GeoServer
104
+ const geoServerResponse = await getGeoServerResponseText(response);
105
+ throw new GeoServerResponseError(null, geoServerResponse);
107
106
  }
108
- } catch (error) {
109
- return false;
110
107
  }
108
+ return response.json();
111
109
  }
112
110
 
113
111
  /**
@@ -115,28 +113,31 @@ export default class NamespaceClient {
115
113
  *
116
114
  * @param {String} name Name of the namespace to delete
117
115
  *
118
- * @returns {Boolean} If deletion was successful
116
+ * @throws Error if request fails
119
117
  */
120
118
  async delete (name) {
121
- try {
122
- const auth =
123
- Buffer.from(this.user + ':' + this.password).toString('base64');
124
- const response = await fetch(this.url + 'namespaces/' + name, {
125
- credentials: 'include',
126
- method: 'DELETE',
127
- headers: {
128
- Authorization: 'Basic ' + auth
129
- }
130
- });
119
+ const response = await fetch(this.url + 'namespaces/' + name, {
120
+ credentials: 'include',
121
+ method: 'DELETE',
122
+ headers: {
123
+ Authorization: this.auth
124
+ }
125
+ });
131
126
 
132
- // TODO map other HTTP status
133
- if (response.status === 200) {
134
- return true;
135
- } else {
136
- return false;
127
+ if (!response.ok) {
128
+ const geoServerResponse = await getGeoServerResponseText(response);
129
+ switch (response.status) {
130
+ case 403:
131
+ throw new GeoServerResponseError(
132
+ 'Namespace or related Workspace is not empty (and recurse not true)',
133
+ geoServerResponse);
134
+ case 404:
135
+ throw new GeoServerResponseError('Namespace doesn\'t exist', geoServerResponse);
136
+ case 405:
137
+ throw new GeoServerResponseError('Can\'t delete default namespace', geoServerResponse);
138
+ default:
139
+ throw new GeoServerResponseError('Response not recognized', geoServerResponse)
137
140
  }
138
- } catch (error) {
139
- return false;
140
141
  }
141
142
  }
142
143
  }
@@ -0,0 +1,70 @@
1
+ import fetch from 'node-fetch';
2
+ import { GeoServerResponseError, getGeoServerResponseText } from './util/geoserver.js';
3
+
4
+ /**
5
+ * Client for GeoServer "Reset/Reload" to clear internal caches and reload
6
+ * configuration from disk endpoint.
7
+ *
8
+ * @module ResetReloadClient
9
+ */
10
+ export default class ResetReloadClient {
11
+ /**
12
+ * Creates a GeoServer REST ResetReloadClient instance.
13
+ *
14
+ * @param {String} url The URL of the GeoServer REST API endpoint
15
+ * @param {String} auth The Basic Authentication string
16
+ */
17
+ constructor (url, auth) {
18
+ this.url = url;
19
+ this.auth = auth;
20
+ }
21
+
22
+ /**
23
+ * Resets all store, raster, and schema caches. This operation is used to
24
+ * force GeoServer to drop all caches and store connections and reconnect to
25
+ * each of them the next time they are needed by a request.
26
+ * This is useful in case the stores themselves cache some information about
27
+ * the data structures they manage that may have changed in the meantime.
28
+ *
29
+ * @throws Error if request fails
30
+ */
31
+ async reset () {
32
+ const url = this.url + 'reset';
33
+ const response = await fetch(url, {
34
+ credentials: 'include',
35
+ method: 'POST',
36
+ headers: {
37
+ Authorization: this.auth
38
+ }
39
+ });
40
+
41
+ if (!response.ok) {
42
+ const geoServerResponse = await getGeoServerResponseText(response);
43
+ throw new GeoServerResponseError(null, geoServerResponse);
44
+ }
45
+ }
46
+
47
+ /**
48
+ * Reloads the GeoServer catalog and configuration from disk. This operation
49
+ * is used in cases where an external tool has modified the on-disk
50
+ * configuration. This operation will also force GeoServer to drop any
51
+ * internal caches and reconnect to all data stores.
52
+ *
53
+ * @throws Error if request fails
54
+ */
55
+ async reload () {
56
+ const url = this.url + 'reload';
57
+ const response = await fetch(url, {
58
+ credentials: 'include',
59
+ method: 'POST',
60
+ headers: {
61
+ Authorization: this.auth
62
+ }
63
+ });
64
+
65
+ if (!response.ok) {
66
+ const geoServerResponse = await getGeoServerResponseText(response);
67
+ throw new GeoServerResponseError(null, geoServerResponse);
68
+ }
69
+ }
70
+ }
package/src/security.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import fetch from 'node-fetch';
2
+ import { getGeoServerResponseText, GeoServerResponseError } from './util/geoserver.js';
2
3
 
3
4
  /**
4
5
  * Client for GeoServer security.
@@ -10,39 +11,34 @@ export default class SecurityClient {
10
11
  * Creates a GeoServer REST SecurityClient instance.
11
12
  *
12
13
  * @param {String} url The URL of the GeoServer REST API endpoint
13
- * @param {String} user The user for the GeoServer REST API
14
- * @param {String} password The password for the GeoServer REST API
14
+ * @param {String} auth The Basic Authentication string
15
15
  */
16
- constructor (url, user, password) {
17
- this.url = url.endsWith('/') ? url : url + '/';
18
- this.user = user;
19
- this.password = password;
16
+ constructor (url, auth) {
17
+ this.url = url;
18
+ this.auth = auth;
20
19
  }
21
20
 
22
- // TODO: I could not get it working, got Code '406'
23
21
  /**
24
22
  * Returns all users registered in GeoServer.
23
+ *
24
+ * @throws Error if request fails
25
+ *
26
+ * @returns {Object} An object with all users
25
27
  */
26
28
  async getAllUsers () {
27
- try {
28
- const auth = Buffer.from(this.user + ':' + this.password).toString('base64');
29
- const response = await fetch(this.url + 'security/usergroup/users.json', {
30
- credentials: 'include',
31
- method: 'GET',
32
- headers: {
33
- Authorization: 'Basic ' + auth
34
- }
35
- });
36
-
37
- if (response.status === 200) {
38
- return await response.json();
39
- } else {
40
- console.warn(await response.text());
41
- return false;
29
+ const response = await fetch(this.url + 'security/usergroup/users.json', {
30
+ credentials: 'include',
31
+ method: 'GET',
32
+ headers: {
33
+ Authorization: this.auth
42
34
  }
43
- } catch (error) {
44
- return false;
35
+ });
36
+
37
+ if (!response.ok) {
38
+ const geoServerResponse = await getGeoServerResponseText(response);
39
+ throw new GeoServerResponseError(null, geoServerResponse);
45
40
  }
41
+ return response.json();
46
42
  }
47
43
 
48
44
  /**
@@ -51,7 +47,7 @@ export default class SecurityClient {
51
47
  * @param {String} username The name of the user to be created
52
48
  * @param {String} password The password of the user to be created
53
49
  *
54
- * @returns {Boolean} If the user could be created
50
+ * @throws Error if request fails
55
51
  */
56
52
  async createUser (username, password) {
57
53
  const body = {
@@ -62,28 +58,24 @@ export default class SecurityClient {
62
58
  }
63
59
  };
64
60
 
65
- try {
66
- const auth = Buffer.from(this.user + ':' + this.password).toString('base64');
67
- const response = await fetch(this.url + 'security/usergroup/users.json', {
68
- credentials: 'include',
69
- method: 'POST',
70
- headers: {
71
- Authorization: 'Basic ' + auth,
72
- 'Content-Type': 'application/json'
73
- },
74
- body: JSON.stringify(body)
75
- });
61
+ const response = await fetch(this.url + 'security/usergroup/users.json', {
62
+ credentials: 'include',
63
+ method: 'POST',
64
+ headers: {
65
+ Authorization: this.auth,
66
+ 'Content-Type': 'application/json'
67
+ },
68
+ body: JSON.stringify(body)
69
+ });
76
70
 
77
- if (response.status === 201) {
78
- return true;
79
- } else if (response.status === 404) {
80
- console.warn(`Received HTTP 404 - the user ${username} might already exist.`);
81
- } else {
82
- console.warn(await response.text());
83
- return false;
71
+ if (!response.ok) {
72
+ const geoServerResponse = await getGeoServerResponseText(response);
73
+ switch (response.status) {
74
+ case 404:
75
+ throw new GeoServerResponseError(`User ${username} might already exists.`, geoServerResponse);
76
+ default:
77
+ throw new GeoServerResponseError(null, geoServerResponse);
84
78
  }
85
- } catch (error) {
86
- return false;
87
79
  }
88
80
  }
89
81
 
@@ -95,7 +87,7 @@ export default class SecurityClient {
95
87
  * @param {String} password The password of the user to be created
96
88
  * @param {Boolean} enabled Enable / disable the user
97
89
  *
98
- * @returns {Boolean} If user could be updated
90
+ * @throws Error if request fails
99
91
  */
100
92
  async updateUser (username, password, enabled) {
101
93
  const body = {
@@ -105,26 +97,19 @@ export default class SecurityClient {
105
97
  }
106
98
  };
107
99
 
108
- try {
109
- const auth = Buffer.from(this.user + ':' + this.password).toString('base64');
110
- const response = await fetch(this.url + 'security/usergroup/user/' + username, {
111
- credentials: 'include',
112
- method: 'POST',
113
- headers: {
114
- Authorization: 'Basic ' + auth,
115
- 'Content-Type': 'application/json'
116
- },
117
- body: JSON.stringify(body)
118
- });
100
+ const response = await fetch(this.url + 'security/usergroup/user/' + username, {
101
+ credentials: 'include',
102
+ method: 'POST',
103
+ headers: {
104
+ Authorization: this.auth,
105
+ 'Content-Type': 'application/json'
106
+ },
107
+ body: JSON.stringify(body)
108
+ });
119
109
 
120
- if (response.status === 200) {
121
- return true;
122
- } else {
123
- console.warn(await response.text());
124
- return false;
125
- }
126
- } catch (error) {
127
- return false;
110
+ if (!response.ok) {
111
+ const geoServerResponse = await getGeoServerResponseText(response);
112
+ throw new GeoServerResponseError(null, geoServerResponse);
128
113
  }
129
114
  }
130
115
 
@@ -134,28 +119,20 @@ export default class SecurityClient {
134
119
  * @param {String} username The name of the user to add the role to
135
120
  * @param {String} role The role to associate
136
121
  *
137
- * @returns {Boolean} If the role could be associated
122
+ * @throws Error if request fails
138
123
  */
139
124
  async associateUserRole (username, role) {
140
- try {
141
- const auth = Buffer.from(this.user + ':' + this.password).toString('base64');
142
- console.log(`${this.url}security/roles/role/${role}/user/${username}`);
143
- const response = await fetch(`${this.url}security/roles/role/${role}/user/${username}`, {
144
- credentials: 'include',
145
- method: 'POST',
146
- headers: {
147
- Authorization: 'Basic ' + auth
148
- }
149
- });
150
-
151
- if (response.status === 200) {
152
- return true;
153
- } else {
154
- console.warn(await response.text());
155
- return false;
125
+ const response = await fetch(`${this.url}security/roles/role/${role}/user/${username}`, {
126
+ credentials: 'include',
127
+ method: 'POST',
128
+ headers: {
129
+ Authorization: this.auth
156
130
  }
157
- } catch (error) {
158
- return false;
131
+ });
132
+
133
+ if (!response.ok) {
134
+ const geoServerResponse = await getGeoServerResponseText(response);
135
+ throw new GeoServerResponseError(null, geoServerResponse);
159
136
  }
160
137
  }
161
138
  }