1 /* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
2 * license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
3 * full text of the license. */
7 * @requires OpenLayers/Layer.js
11 * Class: OpenLayers.Layer.HTTPRequest
14 * - <OpenLayers.Layer>
16 OpenLayers.Layer.HTTPRequest = OpenLayers.Class(OpenLayers.Layer, {
19 * Constant: URL_HASH_FACTOR
20 * {Float} Used to hash URL param strings for multi-WMS server selection.
21 * Set to the Golden Ratio per Knuth's recommendation.
23 URL_HASH_FACTOR: (Math.sqrt(5) - 1) / 2,
27 * {Array(String) or String} This is either an array of url strings or
28 * a single url string.
34 * {Object} Hashtable of key/value parameters
39 * APIProperty: reproject
40 * *Deprecated*. See http://trac.openlayers.org/wiki/SpatialMercator
41 * for information on the replacement for this functionality.
42 * {Boolean} Whether layer should reproject itself based on base layer
43 * locations. This allows reprojection onto commercial layers.
44 * Default is false: Most layers can't reproject, but layers
45 * which can create non-square geographic pixels can, like WMS.
51 * Constructor: OpenLayers.Layer.HTTPRequest
55 * url - {Array(String) or String}
57 * options - {Object} Hashtable of extra options to tag onto the layer
59 initialize: function(name, url, params, options) {
60 var newArguments = arguments;
61 newArguments = [name, options];
62 OpenLayers.Layer.prototype.initialize.apply(this, newArguments);
64 this.params = OpenLayers.Util.extend( {}, params);
73 OpenLayers.Layer.prototype.destroy.apply(this, arguments);
83 * {<OpenLayers.Layer.HTTPRequest>} An exact clone of this
84 * <OpenLayers.Layer.HTTPRequest>
86 clone: function (obj) {
89 obj = new OpenLayers.Layer.HTTPRequest(this.name,
95 //get all additions from superclasses
96 obj = OpenLayers.Layer.prototype.clone.apply(this, [obj]);
98 // copy/set any non-init, non-simple values here
109 setUrl: function(newUrl) {
114 * APIMethod: mergeNewParams
117 * newParams - {Object}
120 * redrawn: {Boolean} whether the layer was actually redrawn.
122 mergeNewParams:function(newParams) {
123 this.params = OpenLayers.Util.extend(this.params, newParams);
124 return this.redraw();
129 * Redraws the layer. Returns true if the layer was redrawn, false if not.
132 * force - {Boolean} Force redraw by adding random parameter.
135 * {Boolean} The layer was redrawn.
137 redraw: function(force) {
139 return this.mergeNewParams({"_olSalt": Math.random()});
141 return OpenLayers.Layer.prototype.redraw.apply(this, []);
147 * selectUrl() implements the standard floating-point multiplicative
148 * hash function described by Knuth, and hashes the contents of the
149 * given param string into a float between 0 and 1. This float is then
150 * scaled to the size of the provided urls array, and used to select
154 * paramString - {String}
155 * urls - {Array(String)}
158 * {String} An entry from the urls array, deterministically selected based
159 * on the paramString.
161 selectUrl: function(paramString, urls) {
163 for (var i=0, len=paramString.length; i<len; i++) {
164 product *= paramString.charCodeAt(i) * this.URL_HASH_FACTOR;
165 product -= Math.floor(product);
167 return urls[Math.floor(product * urls.length)];
171 * Method: getFullRequestString
172 * Combine url with layer's params and these newParams.
174 * does checking on the serverPath variable, allowing for cases when it
175 * is supplied with trailing ? or &, as well as cases where not.
177 * return in formatted string like this:
178 * "server?key1=value1&key2=value2&key3=value3"
180 * WARNING: The altUrl parameter is deprecated and will be removed in 3.0.
183 * newParams - {Object}
184 * altUrl - {String} Use this as the url instead of the layer's url
189 getFullRequestString:function(newParams, altUrl) {
191 // if not altUrl passed in, use layer's url
192 var url = altUrl || this.url;
194 // create a new params hashtable with all the layer params and the
195 // new params together. then convert to string
196 var allParams = OpenLayers.Util.extend({}, this.params);
197 allParams = OpenLayers.Util.extend(allParams, newParams);
198 var paramsString = OpenLayers.Util.getParameterString(allParams);
200 // if url is not a string, it should be an array of strings,
201 // in which case we will deterministically select one of them in
202 // order to evenly distribute requests to different urls.
204 if (url instanceof Array) {
205 url = this.selectUrl(paramsString, url);
208 // ignore parameters that are already in the url search string
210 OpenLayers.Util.upperCaseObject(OpenLayers.Util.getParameters(url));
211 for(var key in allParams) {
212 if(key.toUpperCase() in urlParams) {
213 delete allParams[key];
216 paramsString = OpenLayers.Util.getParameterString(allParams);
218 // requestString always starts with url
219 var requestString = url;
221 if (paramsString != "") {
222 var lastServerChar = url.charAt(url.length - 1);
223 if ((lastServerChar == "&") || (lastServerChar == "?")) {
224 requestString += paramsString;
226 if (url.indexOf('?') == -1) {
227 //serverPath has no ? -- add one
228 requestString += '?' + paramsString;
230 //serverPath contains ?, so must already have
231 // paramsString at the end
232 requestString += '&' + paramsString;
236 return requestString;
239 CLASS_NAME: "OpenLayers.Layer.HTTPRequest"