extend Protocol.HTTP for MapFish and FeatureServer. p=elemoine, r=bartvde,me (closes #2393)
git-svn-id: http://svn.openlayers.org/trunk/openlayers@10129 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
@@ -5,6 +5,9 @@
|
||||
/**
|
||||
* @requires OpenLayers/Protocol.js
|
||||
* @requires OpenLayers/Feature/Vector.js
|
||||
* @requires OpenLayers/Filter/Spatial.js
|
||||
* @requires OpenLayers/Filter/Comparison.js
|
||||
* @requires OpenLayers/Filter/Logical.js
|
||||
*/
|
||||
|
||||
/**
|
||||
@@ -62,6 +65,17 @@ OpenLayers.Protocol.HTTP = OpenLayers.Class(OpenLayers.Protocol, {
|
||||
*/
|
||||
readWithPOST: false,
|
||||
|
||||
/**
|
||||
* Property: wildcarded.
|
||||
* {Boolean} If true percent signs are added around values
|
||||
* read from LIKE filters, for example if the protocol
|
||||
* read method is passed a LIKE filter whose property
|
||||
* is "foo" and whose value is "bar" the string
|
||||
* "foo__ilike=%bar%" will be sent in the query string;
|
||||
* defaults to false.
|
||||
*/
|
||||
wildcarded: false,
|
||||
|
||||
/**
|
||||
* Constructor: OpenLayers.Protocol.HTTP
|
||||
* A class for giving layers generic HTTP protocol.
|
||||
@@ -79,6 +93,7 @@ OpenLayers.Protocol.HTTP = OpenLayers.Class(OpenLayers.Protocol, {
|
||||
* scope - {Object}
|
||||
*/
|
||||
initialize: function(options) {
|
||||
options = options || {};
|
||||
this.params = {};
|
||||
this.headers = {};
|
||||
OpenLayers.Protocol.prototype.initialize.apply(this, arguments);
|
||||
@@ -106,10 +121,8 @@ OpenLayers.Protocol.HTTP = OpenLayers.Class(OpenLayers.Protocol, {
|
||||
* url - {String} Url for the request.
|
||||
* params - {Object} Parameters to get serialized as a query string.
|
||||
* headers - {Object} Headers to be set on the request.
|
||||
* filter - {<OpenLayers.Filter.BBOX>} If a bbox filter is sent, it will be
|
||||
* serialized according to the OpenSearch Geo extension
|
||||
* (bbox=minx,miny,maxx,maxy). Note that a BBOX filter as the child
|
||||
* of a logical filter will not be serialized.
|
||||
* filter - {<OpenLayers.Filter>} Filter to get serialized as a
|
||||
* query string.
|
||||
* readWithPOST - {Boolean} If the request should be done with POST.
|
||||
*
|
||||
* Returns:
|
||||
@@ -121,18 +134,15 @@ OpenLayers.Protocol.HTTP = OpenLayers.Class(OpenLayers.Protocol, {
|
||||
read: function(options) {
|
||||
OpenLayers.Protocol.prototype.read.apply(this, arguments);
|
||||
options = OpenLayers.Util.applyDefaults(options, this.options);
|
||||
options.params = OpenLayers.Util.applyDefaults(
|
||||
options.params, this.options.params);
|
||||
if(options.filter) {
|
||||
options.params = this.filterToParams(
|
||||
options.filter, options.params);
|
||||
}
|
||||
var readWithPOST = (options.readWithPOST !== undefined) ?
|
||||
options.readWithPOST : this.readWithPOST;
|
||||
var resp = new OpenLayers.Protocol.Response({requestType: "read"});
|
||||
|
||||
if(options.filter && options.filter instanceof OpenLayers.Filter.Spatial) {
|
||||
if(options.filter.type == OpenLayers.Filter.Spatial.BBOX) {
|
||||
options.params = OpenLayers.Util.extend(options.params, {
|
||||
bbox: options.filter.value.toArray()
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if(readWithPOST) {
|
||||
resp.priv = OpenLayers.Request.POST({
|
||||
url: options.url,
|
||||
@@ -150,7 +160,6 @@ OpenLayers.Protocol.HTTP = OpenLayers.Class(OpenLayers.Protocol, {
|
||||
headers: options.headers
|
||||
});
|
||||
}
|
||||
|
||||
return resp;
|
||||
},
|
||||
|
||||
@@ -167,7 +176,122 @@ OpenLayers.Protocol.HTTP = OpenLayers.Class(OpenLayers.Protocol, {
|
||||
handleRead: function(resp, options) {
|
||||
this.handleResponse(resp, options);
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* Method: filterToParams
|
||||
* Convert an <OpenLayers.Filter> object to parameters.
|
||||
*
|
||||
* Parameters:
|
||||
* filter - {OpenLayers.Filter} filter to convert.
|
||||
* params - {Object} The parameters object.
|
||||
*
|
||||
* Returns:
|
||||
* {Object} The resulting parameters object.
|
||||
*/
|
||||
filterToParams: function(filter, params) {
|
||||
params = params || {};
|
||||
var className = filter.CLASS_NAME;
|
||||
var filterType = className.substring(className.lastIndexOf(".") + 1);
|
||||
switch(filterType) {
|
||||
case "Spatial":
|
||||
switch(filter.type) {
|
||||
case OpenLayers.Filter.Spatial.BBOX:
|
||||
params.bbox = filter.value.toArray();
|
||||
break;
|
||||
case OpenLayers.Filter.Spatial.DWITHIN:
|
||||
params.tolerance = filter.distance;
|
||||
// no break here
|
||||
case OpenLayers.Filter.Spatial.WITHIN:
|
||||
params.lon = filter.value.x;
|
||||
params.lat = filter.value.y;
|
||||
break;
|
||||
default:
|
||||
OpenLayers.Console.warn(
|
||||
"Unknown spatial filter type " + filter.type);
|
||||
}
|
||||
break;
|
||||
case "Comparison":
|
||||
var op = OpenLayers.Protocol.HTTP.COMP_TYPE_TO_OP_STR[filter.type];
|
||||
if(op !== undefined) {
|
||||
var value = filter.value;
|
||||
if(filter.type == OpenLayers.Filter.Comparison.LIKE) {
|
||||
value = this.regex2value(value);
|
||||
if(this.wildcarded) {
|
||||
value = "%" + value + "%";
|
||||
}
|
||||
}
|
||||
params[filter.property + "__" + op] = value;
|
||||
params.queryable = params.queryable || [];
|
||||
params.queryable.push(filter.property);
|
||||
} else {
|
||||
OpenLayers.Console.warn(
|
||||
"Unknown comparison filter type " + filter.type);
|
||||
}
|
||||
break;
|
||||
case "Logical":
|
||||
if(filter.type === OpenLayers.Filter.Logical.AND) {
|
||||
for(var i=0,len=filter.filters.length; i<len; i++) {
|
||||
params = this.filterToParams(filter.filters[i], params);
|
||||
}
|
||||
} else {
|
||||
OpenLayers.Console.warn(
|
||||
"Unsupported logical filter type " + filter.type);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
OpenLayers.Console.warn("Unknown filter type " + filterType);
|
||||
}
|
||||
return params;
|
||||
},
|
||||
|
||||
/**
|
||||
* Method: regex2value
|
||||
* Convert the value from a regular expression string to a LIKE/ILIKE
|
||||
* string known to the web service.
|
||||
*
|
||||
* Parameters:
|
||||
* value - {String} The regex string.
|
||||
*
|
||||
* Returns:
|
||||
* {String} The converted string.
|
||||
*/
|
||||
regex2value: function(value) {
|
||||
|
||||
// highly sensitive!! Do not change this without running the
|
||||
// Protocol/HTTP.html unit tests
|
||||
|
||||
// convert % to \%
|
||||
value = value.replace(/%/g, "\\%");
|
||||
|
||||
// convert \\. to \\_ (\\.* occurences converted later)
|
||||
value = value.replace(/\\\\\.(\*)?/g, function($0, $1) {
|
||||
return $1 ? $0 : "\\\\_";
|
||||
});
|
||||
|
||||
// convert \\.* to \\%
|
||||
value = value.replace(/\\\\\.\*/g, "\\\\%");
|
||||
|
||||
// convert . to _ (\. and .* occurences converted later)
|
||||
value = value.replace(/(\\)?\.(\*)?/g, function($0, $1, $2) {
|
||||
return $1 || $2 ? $0 : "_";
|
||||
});
|
||||
|
||||
// convert .* to % (\.* occurnces converted later)
|
||||
value = value.replace(/(\\)?\.\*/g, function($0, $1) {
|
||||
return $1 ? $0 : "%";
|
||||
});
|
||||
|
||||
// convert \. to .
|
||||
value = value.replace(/\\\./g, ".");
|
||||
|
||||
// replace \* with * (watching out for \\*)
|
||||
value = value.replace(/(\\)?\\\*/g, function($0, $1) {
|
||||
return $1 ? $0 : "*";
|
||||
});
|
||||
|
||||
return value;
|
||||
},
|
||||
|
||||
/**
|
||||
* APIMethod: create
|
||||
* Construct a request for writing newly created features.
|
||||
@@ -234,8 +358,10 @@ OpenLayers.Protocol.HTTP = OpenLayers.Class(OpenLayers.Protocol, {
|
||||
* the feature received from the server.
|
||||
*/
|
||||
update: function(feature, options) {
|
||||
var url = options.url || feature.url || this.options.url;
|
||||
options = OpenLayers.Util.applyDefaults(options, this.options);
|
||||
var url = options.url ||
|
||||
feature.url ||
|
||||
this.options.url + "/" + feature.fid;
|
||||
|
||||
var resp = new OpenLayers.Protocol.Response({
|
||||
reqFeatures: feature,
|
||||
@@ -282,8 +408,10 @@ OpenLayers.Protocol.HTTP = OpenLayers.Class(OpenLayers.Protocol, {
|
||||
* completes.
|
||||
*/
|
||||
"delete": function(feature, options) {
|
||||
var url = options.url || feature.url || this.options.url;
|
||||
options = OpenLayers.Util.applyDefaults(options, this.options);
|
||||
var url = options.url ||
|
||||
feature.url ||
|
||||
this.options.url + "/" + feature.fid;
|
||||
|
||||
var resp = new OpenLayers.Protocol.Response({
|
||||
reqFeatures: feature,
|
||||
@@ -503,3 +631,21 @@ OpenLayers.Protocol.HTTP = OpenLayers.Class(OpenLayers.Protocol, {
|
||||
|
||||
CLASS_NAME: "OpenLayers.Protocol.HTTP"
|
||||
});
|
||||
|
||||
/**
|
||||
* Property: OpenLayers.Protocol.HTTP.COMP_TYPE_TO_OP_STR
|
||||
* {Object} A private class-level property mapping the
|
||||
* OpenLayers.Filter.Comparison types to the operation
|
||||
* strings of the protocol.
|
||||
*/
|
||||
(function() {
|
||||
var o = OpenLayers.Protocol.HTTP.COMP_TYPE_TO_OP_STR = {};
|
||||
o[OpenLayers.Filter.Comparison.EQUAL_TO] = "eq";
|
||||
o[OpenLayers.Filter.Comparison.NOT_EQUAL_TO] = "ne";
|
||||
o[OpenLayers.Filter.Comparison.LESS_THAN] = "lt";
|
||||
o[OpenLayers.Filter.Comparison.LESS_THAN_OR_EQUAL_TO] = "lte";
|
||||
o[OpenLayers.Filter.Comparison.GREATER_THAN] = "gt";
|
||||
o[OpenLayers.Filter.Comparison.GREATER_THAN_OR_EQUAL_TO] = "gte";
|
||||
o[OpenLayers.Filter.Comparison.LIKE] = "ilike";
|
||||
})();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user