import { getGeoServerResponseText, GeoServerResponseError, httpPut } from './util/geoserver.js';
/**
* Client for GeoServer settings.
*
* @module SettingsClient
*/
export default class SettingsClient {
/**
* Creates a GeoServer REST SettingsClient 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 the complete GeoServer settings object.
*
* @throws Error if request fails
*
* @returns {Object} Settings object
*/
async getSettings() {
const response = await fetch(this.url + 'settings.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();
}
/**
* Update the global GeoServer settings.
*
* @param {Object} settings The adapted GeoServer settings object
*/
async updateSettings(settings) {
const response = await httpPut(this.url + 'settings', {
headers: {
Authorization: this.auth,
'Content-Type': 'application/json'
},
body: JSON.stringify(settings)
});
if (!response.ok) {
const geoServerResponse = await getGeoServerResponseText(response);
throw new GeoServerResponseError(null, geoServerResponse);
}
}
/**
* Update the global proxyBaseUrl setting.
*
* @param {String} proxyBaseUrl The proxy base URL
*/
async updateProxyBaseUrl(proxyBaseUrl) {
const settingsJson = await this.getSettings();
// check if settings are correctly formatted
if (!settingsJson?.global?.settings) {
return false;
}
// add proxyBaseUrl to settings
settingsJson.global.settings.proxyBaseUrl = proxyBaseUrl;
await this.updateSettings(settingsJson);
}
/**
* Get the contact information of the GeoServer.
*
* @throws Error if request fails
*
* @returns {Object} An object with contact information
*/
async getContactInformation() {
const response = await fetch(this.url + 'settings/contact', {
credentials: 'include',
method: 'GET',
headers: {
Authorization: this.auth
}
});
if (!response.ok) {
const geoServerResponse = await getGeoServerResponseText(response);
throw new GeoServerResponseError(null, geoServerResponse);
}
return response.json();
}
/**
* Update the contact information.
*
* Deleting is not supported.
*
* @param {String} [address] The contact's address
* @param {String} [city] The contact's city
* @param {String} [country] The contact's country
* @param {String} [postalCode] The contact's postCode
* @param {String} [state] The contact's state
* @param {String} [email] The contact's email
* @param {String} [organization] The contact's organization
* @param {String} [contactPerson] The contact person
* @param {String} [phoneNumber] The contact's phone number
*
* @throws Error if request fails
*/
async updateContactInformation(
address,
city,
country,
postalCode,
state,
email,
organization,
contactPerson,
phoneNumber
) {
const contact = {
address: address,
addressCity: city,
addressCountry: country,
addressPostalCode: postalCode,
addressState: state,
contactEmail: email,
contactOrganization: organization,
contactPerson: contactPerson,
contactVoice: phoneNumber
};
const body = {
contact: contact
};
const url = this.url + 'settings/contact';
const response = await httpPut(url, {
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);
}
}
// ==== WMS Settings ====
/**
* Get the complete settings object for the GeoServer WMS.
*
* @returns {Promise<object>} The WMS settings object
*/
async getWmsSettings() {
const response = await fetch(this.url + 'services/wms/settings.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();
}
/**
* Updates the WMS settings with a given settings object.
*
* @param {object} wmsSettings The adapted WMS settings object
*/
async updateWmsSettings(wmsSettings) {
if (!wmsSettings || !wmsSettings.wms) {
throw new GeoServerResponseError('Invalid WMS settings object');
}
const response = await httpPut(this.url + 'services/wms/settings.json', {
headers: {
Authorization: this.auth,
'Content-Type': 'application/json'
},
body: JSON.stringify(wmsSettings)
});
if (!response.ok) {
const geoServerResponse = await getGeoServerResponseText(response);
throw new GeoServerResponseError(null, geoServerResponse);
}
}
/**
* Updates the SRS limitation to a given list of supported SRS.
*
* @param {string[]} supportedSrs The list of supported SRS, e.g. ['EPSG:4326', 'EPSG:3857']
*/
async updateWmsSrsLimitation(supportedSrs) {
const wmsSettings = await this.getWmsSettings();
// apply the new SRS limitations
wmsSettings.wms.srs ??= {};
wmsSettings.wms.srs.string = supportedSrs;
await this.updateWmsSettings(wmsSettings);
}
}