Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use self-reported operation URLs from capability documents #69

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/shared/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export interface FetchOptions {
integrity?: string;
}

export type OperationName = string;
export type HttpMethod = 'Get' | 'Post';
export type OperationUrl = Partial<Record<HttpMethod, string>>;

export interface LayerStyle {
name: string;
title: string;
Expand Down
117 changes: 117 additions & 0 deletions src/wfs/capabilities.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import capabilities200_noFormats from '../../fixtures/wfs/capabilities-geo2franc
import {
readFeatureTypesFromCapabilities,
readInfoFromCapabilities,
readOperationUrlsFromCapabilities,
readVersionFromCapabilities,
} from './capabilities.js';

Expand Down Expand Up @@ -250,4 +251,120 @@ describe('WFS capabilities', () => {
});
});
});

describe('readOperationUrlsFromCapabilities', () => {
it('reads the operation URLs (2.0.0)', () => {
const doc = parseXmlString(capabilities200);
const expectedUrls = {
GetCapabilities: {
Get: 'https://www.pigma.org/geoserver/wfs',
Post: 'https://www.pigma.org/geoserver/wfs',
},
DescribeFeatureType: {
Get: 'https://www.pigma.org/geoserver/wfs',
Post: 'https://www.pigma.org/geoserver/wfs',
},
GetFeature: {
Get: 'https://www.pigma.org/geoserver/wfs',
Post: 'https://www.pigma.org/geoserver/wfs',
},
GetPropertyValue: {
Get: 'https://www.pigma.org/geoserver/wfs',
Post: 'https://www.pigma.org/geoserver/wfs',
},
ListStoredQueries: {
Get: 'https://www.pigma.org/geoserver/wfs',
Post: 'https://www.pigma.org/geoserver/wfs',
},
DescribeStoredQueries: {
Get: 'https://www.pigma.org/geoserver/wfs',
Post: 'https://www.pigma.org/geoserver/wfs',
},
CreateStoredQuery: {
Post: 'https://www.pigma.org/geoserver/wfs',
},
DropStoredQuery: {
Get: 'https://www.pigma.org/geoserver/wfs',
Post: 'https://www.pigma.org/geoserver/wfs',
},
LockFeature: {
Get: 'https://www.pigma.org/geoserver/wfs',
Post: 'https://www.pigma.org/geoserver/wfs',
},
GetFeatureWithLock: {
Get: 'https://www.pigma.org/geoserver/wfs',
Post: 'https://www.pigma.org/geoserver/wfs',
},
Transaction: {
Get: 'https://www.pigma.org/geoserver/wfs',
Post: 'https://www.pigma.org/geoserver/wfs',
},
};
expect(readOperationUrlsFromCapabilities(doc)).toEqual(expectedUrls);
});
it('reads the operation URLs (1.1.0)', () => {
const doc = parseXmlString(capabilities110);
const expectedUrls = {
GetCapabilities: {
Get: 'https://www.pigma.org/geoserver/wfs',
Post: 'https://www.pigma.org/geoserver/wfs',
},
DescribeFeatureType: {
Get: 'https://www.pigma.org/geoserver/wfs',
Post: 'https://www.pigma.org/geoserver/wfs',
},
GetFeature: {
Get: 'https://www.pigma.org/geoserver/wfs',
Post: 'https://www.pigma.org/geoserver/wfs',
},
GetGmlObject: {
Get: 'https://www.pigma.org/geoserver/wfs',
Post: 'https://www.pigma.org/geoserver/wfs',
},
LockFeature: {
Get: 'https://www.pigma.org/geoserver/wfs',
Post: 'https://www.pigma.org/geoserver/wfs',
},
GetFeatureWithLock: {
Get: 'https://www.pigma.org/geoserver/wfs',
Post: 'https://www.pigma.org/geoserver/wfs',
},
Transaction: {
Get: 'https://www.pigma.org/geoserver/wfs',
Post: 'https://www.pigma.org/geoserver/wfs',
},
};
expect(readOperationUrlsFromCapabilities(doc)).toEqual(expectedUrls);
});
it('reads the operation URLs (1.0.0)', () => {
const doc = parseXmlString(capabilities100);
const expectedUrls = {
GetCapabilities: {
Get: 'https://www.pigma.org/geoserver/wfs?request=GetCapabilities',
Post: 'https://www.pigma.org/geoserver/wfs',
},
DescribeFeatureType: {
Get: 'https://www.pigma.org/geoserver/wfs?request=DescribeFeatureType',
Post: 'https://www.pigma.org/geoserver/wfs',
},
GetFeature: {
Get: 'https://www.pigma.org/geoserver/wfs?request=GetFeature',
Post: 'https://www.pigma.org/geoserver/wfs',
},
Transaction: {
Get: 'https://www.pigma.org/geoserver/wfs?request=Transaction',
Post: 'https://www.pigma.org/geoserver/wfs',
},
LockFeature: {
Get: 'https://www.pigma.org/geoserver/wfs?request=LockFeature',
Post: 'https://www.pigma.org/geoserver/wfs',
},
GetFeatureWithLock: {
Get: 'https://www.pigma.org/geoserver/wfs?request=GetFeatureWithLock',
Post: 'https://www.pigma.org/geoserver/wfs',
},
};
expect(readOperationUrlsFromCapabilities(doc)).toEqual(expectedUrls);
});
});
});
68 changes: 68 additions & 0 deletions src/wfs/capabilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,52 @@ import {
getElementName,
getElementText,
getRootElement,
stripNamespace,
} from '../shared/xml-utils.js';
import { simplifyEpsgUrn } from '../shared/crs-utils.js';
import { XmlDocument, XmlElement } from '@rgrove/parse-xml';
import {
BoundingBox,
GenericEndpointInfo,
MimeType,
type OperationName,
type OperationUrl,
} from '../shared/models.js';
import { WfsFeatureTypeInternal, WfsVersion } from './model.js';

/**
* Will read the operation URLS from the capabilities doc
* @param capabilitiesDoc Capabilities document
*/
export function readOperationUrlsFromCapabilities(
capabilitiesDoc: XmlDocument
): Record<OperationName, OperationUrl> {
const urls: Record<OperationName, OperationUrl> = {};
const capabilities = getRootElement(capabilitiesDoc);
const operationsMetadata = findChildElement(
capabilities,
'OperationsMetadata'
);
if (operationsMetadata) {
// WFS 1.1.0 or 2.0.0
findChildrenElement(operationsMetadata, 'Operation').forEach(
(operation) => {
const name = getElementAttribute(operation, 'name');
urls[name] = parseOperation110(operation);
}
);
} else {
// WFS 1.0.0
const capability = findChildElement(capabilities, 'Capability');
const request = findChildElement(capability, 'Request');
getChildrenElement(request).forEach((operation) => {
const name = stripNamespace(getElementName(operation));
urls[name] = parseOperation100(operation);
});
}
return urls;
}

/**
* Will read a WFS version from the capabilities doc
* @param capabilitiesDoc Capabilities document
Expand Down Expand Up @@ -120,6 +156,38 @@ export function readFeatureTypesFromCapabilities(
);
}

/**
* Parse an operation definition from a WFS 1.0.0 capabilities (e.g. GetFeature)
* @param operation Operation element
*/
function parseOperation100(operation: XmlElement): OperationUrl {
const urls: OperationUrl = {};
const dcpType = findChildrenElement(operation, 'DCPType');
const http = dcpType.flatMap((d) => findChildrenElement(d, 'HTTP'));
const methods = http.flatMap((h) => getChildrenElement(h));
methods.forEach((method) => {
const methodName = stripNamespace(getElementName(method));
urls[methodName] = getElementAttribute(method, 'onlineResource');
});
return urls;
}

/**
* Parse an operation definition from a WFS 1.1+ capabilities (e.g. GetFeature)
* @param operation Operation element
*/
function parseOperation110(operation: XmlElement): OperationUrl {
const urls: OperationUrl = {};
const dcpType = findChildrenElement(operation, 'DCP');
const http = dcpType.flatMap((d) => findChildElement(d, 'HTTP'));
const methods = http.flatMap((h) => getChildrenElement(h));
methods.forEach((method) => {
const methodName = stripNamespace(getElementName(method));
urls[methodName] = getElementAttribute(method, 'xlink:href');
});
return urls;
}

/**
* Parse a feature type in a capabilities doc
*/
Expand Down
41 changes: 38 additions & 3 deletions src/wfs/endpoint.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ describe('WfsEndpoint', () => {
outputFormat: 'application/gml+xml; version=3.2',
})
).toEqual(
'https://my.test.service/ogc/wfs?SERVICE=WFS&REQUEST=GetFeature&VERSION=2.0.0&TYPENAMES=cd16%3Ahierarchisation_l&OUTPUTFORMAT=application%2Fgml%2Bxml%3B+version%3D3.2&COUNT=200'
'https://www.pigma.org/geoserver/wfs?SERVICE=WFS&REQUEST=GetFeature&VERSION=2.0.0&TYPENAMES=cd16%3Ahierarchisation_l&OUTPUTFORMAT=application%2Fgml%2Bxml%3B+version%3D3.2&COUNT=200'
);
});
it('returns a GetFeature requesting geojson url for a given feature type', () => {
Expand All @@ -454,7 +454,7 @@ describe('WfsEndpoint', () => {
asJson: true,
})
).toEqual(
'https://my.test.service/ogc/wfs?SERVICE=WFS&REQUEST=GetFeature&VERSION=2.0.0&TYPENAMES=cd16%3Acomptages_routiers_l&OUTPUTFORMAT=application%2Fjson'
'https://www.pigma.org/geoserver/wfs?SERVICE=WFS&REQUEST=GetFeature&VERSION=2.0.0&TYPENAMES=cd16%3Acomptages_routiers_l&OUTPUTFORMAT=application%2Fjson'
);
});
it('returns a GetFeature with a bbox and output crs for a given feature type', () => {
Expand All @@ -464,7 +464,7 @@ describe('WfsEndpoint', () => {
outputCrs: 'EPSG:2154',
})
).toEqual(
'https://my.test.service/ogc/wfs?SERVICE=WFS&REQUEST=GetFeature&VERSION=2.0.0&TYPENAMES=cd16%3Ahierarchisation_l&SRSNAME=EPSG%3A2154&BBOX=1%2C2%2C3%2C4'
'https://www.pigma.org/geoserver/wfs?SERVICE=WFS&REQUEST=GetFeature&VERSION=2.0.0&TYPENAMES=cd16%3Ahierarchisation_l&SRSNAME=EPSG%3A2154&BBOX=1%2C2%2C3%2C4'
);
});
it('throws an error if the feature type was not found', () => {
Expand Down Expand Up @@ -513,4 +513,39 @@ describe('WfsEndpoint', () => {
expect(endpoint.supportsStartIndex()).toBeTruthy();
});
});

describe('#getCapabilitiesUrl', () => {
it.skip('returns the URL used for the request before the capabilities are retrieved', async () => {
expect(endpoint.getCapabilitiesUrl()).toBe(
'https://my.test.service/ogc/wms?aa=bb&SERVICE=WMS&REQUEST=GetCapabilities'
);
await endpoint.isReady();
});

it('returns the self-reported URL after the capabilities are retrieved', async () => {
await endpoint.isReady();
expect(endpoint.getCapabilitiesUrl()).toBe(
'https://www.pigma.org/geoserver/wfs?SERVICE=WMS&REQUEST=GetCapabilities'
);
});
});

describe('#getOperationUrl', () => {
it.skip('returns NULL before the document is loaded', async () => {
expect(endpoint.getOperationUrl('GetMap')).toBeNull();
await endpoint.isReady();
});

it('returns undefined for a non-existant operation', async () => {
await endpoint.isReady();
expect(endpoint.getOperationUrl('foo')).toBeUndefined();
});

it('returns the correct URL for an existant operation', async () => {
await endpoint.isReady();
expect(endpoint.getOperationUrl('GetFeature')).toBe(
'https://www.pigma.org/geoserver/wfs'
);
});
});
});
42 changes: 38 additions & 4 deletions src/wfs/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import {
BoundingBox,
CrsCode,
GenericEndpointInfo,
type HttpMethod,
MimeType,
type OperationName,
type OperationUrl,
} from '../shared/models.js';
import {
WfsFeatureTypeBrief,
Expand All @@ -32,6 +35,7 @@ export default class WfsEndpoint {
private _capabilitiesPromise: Promise<void>;
private _info: GenericEndpointInfo | null;
private _featureTypes: WfsFeatureTypeInternal[] | null;
private _url: Record<OperationName, OperationUrl>;
private _version: WfsVersion | null;

/**
Expand All @@ -53,9 +57,10 @@ export default class WfsEndpoint {
'WFS',
'CAPABILITIES',
this._capabilitiesUrl
).then(({ info, featureTypes, version }) => {
).then(({ info, featureTypes, url, version }) => {
this._info = info;
this._featureTypes = featureTypes;
this._url = url;
this._version = version;
});
}
Expand Down Expand Up @@ -141,12 +146,12 @@ export default class WfsEndpoint {
return useCache(
() => {
const describeUrl = generateDescribeFeatureTypeUrl(
this._capabilitiesUrl,
this.getOperationUrl('DescribeFeatureType'),
this._version,
name
);
const getFeatureUrl = generateGetFeatureUrl(
this._capabilitiesUrl,
this.getOperationUrl('GetFeature'),
this._version,
name,
undefined,
Expand Down Expand Up @@ -305,7 +310,7 @@ export default class WfsEndpoint {
);
}
return generateGetFeatureUrl(
this._capabilitiesUrl,
this.getOperationUrl('GetFeature'),
this._version,
internalFeatureType.name,
format,
Expand All @@ -318,4 +323,33 @@ export default class WfsEndpoint {
startIndex
);
}

/**
* Returns the Capabilities URL of the WMS
*
* This is the URL reported by the service if available, otherwise the URL
* passed to the constructor
*/
getCapabilitiesUrl() {
const baseUrl = this.getOperationUrl('GetCapabilities');
if (!baseUrl) {
return this._capabilitiesUrl;
}
return setQueryParams(baseUrl, {
SERVICE: 'WMS',
REQUEST: 'GetCapabilities',
});
}

/**
* Returns the URL reported by the WFS for the given operation
* @param operationName e.g. GetFeature, GetCapabilities, etc.
* @param method HTTP method
*/
getOperationUrl(operationName: OperationName, method: HttpMethod = 'Get') {
if (!this._url) {
return null;
}
return this._url[operationName]?.[method];
}
}
Loading