Skip to content

Commit

Permalink
Add options.query to CZML in order to append tokens to URL/URI
Browse files Browse the repository at this point in the history
  • Loading branch information
ottaviohartman committed Jun 5, 2017
1 parent 04396a9 commit c2bb133
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 5 deletions.
27 changes: 25 additions & 2 deletions Source/DataSources/CzmlDataSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ define([
'../Core/HermitePolynomialApproximation',
'../Core/isArray',
'../Core/Iso8601',
'../Core/joinUrls',
'../Core/JulianDate',
'../Core/LagrangePolynomialApproximation',
'../Core/LinearApproximation',
'../Core/loadJson',
'../Core/Math',
'../Core/NearFarScalar',
'../Core/objectToQuery',
'../Core/Quaternion',
'../Core/Rectangle',
'../Core/ReferenceFrame',
Expand Down Expand Up @@ -107,12 +109,14 @@ define([
HermitePolynomialApproximation,
isArray,
Iso8601,
joinUrls,
JulianDate,
LagrangePolynomialApproximation,
LinearApproximation,
loadJson,
CesiumMath,
NearFarScalar,
objectToQuery,
Quaternion,
Rectangle,
ReferenceFrame,
Expand Down Expand Up @@ -217,7 +221,16 @@ define([
function unwrapUriInterval(czmlInterval, sourceUri) {
var result = defaultValue(czmlInterval.uri, czmlInterval);
if (defined(sourceUri)) {
result = getAbsoluteUri(result, getAbsoluteUri(sourceUri));
var uriComponents = sourceUri.split('?');
var sourceUriPath = uriComponents[0];
var query = uriComponents[1];
result = getAbsoluteUri(result, getAbsoluteUri(sourceUriPath));

if (defined(query)) {
// Add back the question mark and append to the result
query = '?' + query;
result = joinUrls(result, query, false);
}
}
return result;
}
Expand Down Expand Up @@ -1892,9 +1905,18 @@ define([

var promise = czml;
var sourceUri = options.sourceUri;
var query = options.query;
var queryBlob = defined(query) ? '?' + objectToQuery(query) : '';

// If the czml is a URL
if (typeof czml === 'string') {
promise = loadJson(czml);
sourceUri = defaultValue(sourceUri, czml);
czml = joinUrls(czml, queryBlob, false);
promise = loadJson(czml);
}

if (defined(sourceUri)) {
sourceUri = joinUrls(sourceUri, queryBlob, false);
}

DataSource.setLoading(dataSource, true);
Expand Down Expand Up @@ -1973,6 +1995,7 @@ define([
* @param {String|Object} czml A url or CZML object to be processed.
* @param {Object} [options] An object with the following properties:
* @param {String} [options.sourceUri] Overrides the url to use for resolving relative links.
* @param {Object} [options.query] Key-value pairs which are appended to all URIs in the CZML.
* @returns {Promise.<CzmlDataSource>} A promise that resolves to the new instance once the data is processed.
*/
CzmlDataSource.load = function(czml, options) {
Expand Down
54 changes: 51 additions & 3 deletions Specs/DataSources/CzmlDataSourceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ defineSuite([
'Core/Iso8601',
'Core/JulianDate',
'Core/loadJson',
'Core/loadWithXhr',
'Core/Math',
'Core/NearFarScalar',
'Core/Quaternion',
Expand Down Expand Up @@ -51,6 +52,7 @@ defineSuite([
Iso8601,
JulianDate,
loadJson,
loadWithXhr,
CesiumMath,
NearFarScalar,
Quaternion,
Expand Down Expand Up @@ -258,13 +260,13 @@ defineSuite([

it('process loads expected data', function() {
var dataSource = new CzmlDataSource();
dataSource.process(simple, simpleUrl);
dataSource.process(simple);
expect(dataSource.entities.values.length).toEqual(10);
});

it('process loads data on top of existing', function() {
var dataSource = new CzmlDataSource();
dataSource.process(simple, simpleUrl);
dataSource.process(simple);
expect(dataSource.entities.values.length === 10);

dataSource.process(vehicle, vehicleUrl);
Expand All @@ -273,7 +275,7 @@ defineSuite([

it('load replaces data', function() {
var dataSource = new CzmlDataSource();
dataSource.process(simple, simpleUrl);
dataSource.process(simple);
expect(dataSource.entities.values.length).toEqual(10);

dataSource.load(vehicle, vehicleUrl);
Expand Down Expand Up @@ -554,6 +556,52 @@ defineSuite([
expect(imageProperty.getValue(JulianDate.fromIso8601('2013-01-01T01:00:00Z'))).toEqual(source + 'image2.png');
});

it('appends query to all uri', function() {
var source = 'http://some.url.invalid/';
var packet = {
billboard : {
image : [{
interval : '2013-01-01T00:00:00Z/2013-01-01T01:00:00Z',
uri : 'image.png'
}, {
interval : '2013-01-01T01:00:00Z/2013-01-01T02:00:00Z',
uri : 'image2.png'
}]
}
};

var dataSource = new CzmlDataSource();
dataSource.load(makePacket(packet), {
sourceUri : source,
query : {
token : 34570,
password : "Passw0rd"
}
});
var entity = dataSource.entities.values[0];
var imageProperty = entity.billboard.image;
expect(imageProperty.getValue(JulianDate.fromIso8601('2013-01-01T00:00:00Z'))).toEqual(source + 'image.png' + '?token=34570&password=Passw0rd');
expect(imageProperty.getValue(JulianDate.fromIso8601('2013-01-01T01:00:00Z'))).toEqual(source + 'image2.png' + '?token=34570&password=Passw0rd');
});

it('appends query tokens to source URL', function() {
var dataSource = new CzmlDataSource();
var requestNetworkLink = when.defer();

spyOn(loadWithXhr, 'load').and.callFake(function(url, responseType, method, data, headers, deferred, overrideMimeType) {
requestNetworkLink.resolve(url);
deferred.reject();
});

dataSource.process(simpleUrl, { query : {
"token" : 30203,
"pass" : "passw0rd"
}});
return requestNetworkLink.promise.then(function(url) {
expect(url).toEqual(simpleUrl + '?token=30203&pass=passw0rd');
});
});

it('CZML adds data for constrained billboard.', function() {
var billboardPacket = {
billboard : {
Expand Down

0 comments on commit c2bb133

Please sign in to comment.