HTTPRequest.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* Copyright (c) 2006-2011 by OpenLayers Contributors (see authors.txt for
  2. * full list of contributors). Published under the Clear BSD license.
  3. * See http://svn.openlayers.org/trunk/openlayers/license.txt for the
  4. * full text of the license. */
  5. /**
  6. * @requires OpenLayers/Layer.js
  7. */
  8. /**
  9. * Class: OpenLayers.Layer.HTTPRequest
  10. *
  11. * Inherits from:
  12. * - <OpenLayers.Layer>
  13. */
  14. OpenLayers.Layer.HTTPRequest = OpenLayers.Class(OpenLayers.Layer, {
  15. /**
  16. * Constant: URL_HASH_FACTOR
  17. * {Float} Used to hash URL param strings for multi-WMS server selection.
  18. * Set to the Golden Ratio per Knuth's recommendation.
  19. */
  20. URL_HASH_FACTOR: (Math.sqrt(5) - 1) / 2,
  21. /**
  22. * Property: url
  23. * {Array(String) or String} This is either an array of url strings or
  24. * a single url string.
  25. */
  26. url: null,
  27. /**
  28. * Property: params
  29. * {Object} Hashtable of key/value parameters
  30. */
  31. params: null,
  32. /**
  33. * APIProperty: reproject
  34. * *Deprecated*. See http://docs.openlayers.org/library/spherical_mercator.html
  35. * for information on the replacement for this functionality.
  36. * {Boolean} Whether layer should reproject itself based on base layer
  37. * locations. This allows reprojection onto commercial layers.
  38. * Default is false: Most layers can't reproject, but layers
  39. * which can create non-square geographic pixels can, like WMS.
  40. *
  41. */
  42. reproject: false,
  43. /**
  44. * Constructor: OpenLayers.Layer.HTTPRequest
  45. *
  46. * Parameters:
  47. * name - {String}
  48. * url - {Array(String) or String}
  49. * params - {Object}
  50. * options - {Object} Hashtable of extra options to tag onto the layer
  51. */
  52. initialize: function(name, url, params, options) {
  53. OpenLayers.Layer.prototype.initialize.apply(this, [name, options]);
  54. this.url = url;
  55. this.params = OpenLayers.Util.extend( {}, params);
  56. },
  57. /**
  58. * APIMethod: destroy
  59. */
  60. destroy: function() {
  61. this.url = null;
  62. this.params = null;
  63. OpenLayers.Layer.prototype.destroy.apply(this, arguments);
  64. },
  65. /**
  66. * APIMethod: clone
  67. *
  68. * Parameters:
  69. * obj - {Object}
  70. *
  71. * Returns:
  72. * {<OpenLayers.Layer.HTTPRequest>} An exact clone of this
  73. * <OpenLayers.Layer.HTTPRequest>
  74. */
  75. clone: function (obj) {
  76. if (obj == null) {
  77. obj = new OpenLayers.Layer.HTTPRequest(this.name,
  78. this.url,
  79. this.params,
  80. this.getOptions());
  81. }
  82. //get all additions from superclasses
  83. obj = OpenLayers.Layer.prototype.clone.apply(this, [obj]);
  84. // copy/set any non-init, non-simple values here
  85. return obj;
  86. },
  87. /**
  88. * APIMethod: setUrl
  89. *
  90. * Parameters:
  91. * newUrl - {String}
  92. */
  93. setUrl: function(newUrl) {
  94. this.url = newUrl;
  95. },
  96. /**
  97. * APIMethod: mergeNewParams
  98. *
  99. * Parameters:
  100. * newParams - {Object}
  101. *
  102. * Returns:
  103. * redrawn: {Boolean} whether the layer was actually redrawn.
  104. */
  105. mergeNewParams:function(newParams) {
  106. this.params = OpenLayers.Util.extend(this.params, newParams);
  107. var ret = this.redraw();
  108. if(this.map != null) {
  109. this.map.events.triggerEvent("changelayer", {
  110. layer: this,
  111. property: "params"
  112. });
  113. }
  114. return ret;
  115. },
  116. /**
  117. * APIMethod: redraw
  118. * Redraws the layer. Returns true if the layer was redrawn, false if not.
  119. *
  120. * Parameters:
  121. * force - {Boolean} Force redraw by adding random parameter.
  122. *
  123. * Returns:
  124. * {Boolean} The layer was redrawn.
  125. */
  126. redraw: function(force) {
  127. if (force) {
  128. return this.mergeNewParams({"_olSalt": Math.random()});
  129. } else {
  130. return OpenLayers.Layer.prototype.redraw.apply(this, []);
  131. }
  132. },
  133. /**
  134. * Method: selectUrl
  135. * selectUrl() implements the standard floating-point multiplicative
  136. * hash function described by Knuth, and hashes the contents of the
  137. * given param string into a float between 0 and 1. This float is then
  138. * scaled to the size of the provided urls array, and used to select
  139. * a URL.
  140. *
  141. * Parameters:
  142. * paramString - {String}
  143. * urls - {Array(String)}
  144. *
  145. * Returns:
  146. * {String} An entry from the urls array, deterministically selected based
  147. * on the paramString.
  148. */
  149. selectUrl: function(paramString, urls) {
  150. var product = 1;
  151. for (var i=0, len=paramString.length; i<len; i++) {
  152. product *= paramString.charCodeAt(i) * this.URL_HASH_FACTOR;
  153. product -= Math.floor(product);
  154. }
  155. return urls[Math.floor(product * urls.length)];
  156. },
  157. /**
  158. * Method: getFullRequestString
  159. * Combine url with layer's params and these newParams.
  160. *
  161. * does checking on the serverPath variable, allowing for cases when it
  162. * is supplied with trailing ? or &, as well as cases where not.
  163. *
  164. * return in formatted string like this:
  165. * "server?key1=value1&key2=value2&key3=value3"
  166. *
  167. * WARNING: The altUrl parameter is deprecated and will be removed in 3.0.
  168. *
  169. * Parameters:
  170. * newParams - {Object}
  171. * altUrl - {String} Use this as the url instead of the layer's url
  172. *
  173. * Returns:
  174. * {String}
  175. */
  176. getFullRequestString:function(newParams, altUrl) {
  177. // if not altUrl passed in, use layer's url
  178. var url = altUrl || this.url;
  179. // create a new params hashtable with all the layer params and the
  180. // new params together. then convert to string
  181. var allParams = OpenLayers.Util.extend({}, this.params);
  182. allParams = OpenLayers.Util.extend(allParams, newParams);
  183. var paramsString = OpenLayers.Util.getParameterString(allParams);
  184. // if url is not a string, it should be an array of strings,
  185. // in which case we will deterministically select one of them in
  186. // order to evenly distribute requests to different urls.
  187. //
  188. if (OpenLayers.Util.isArray(url)) {
  189. url = this.selectUrl(paramsString, url);
  190. }
  191. // ignore parameters that are already in the url search string
  192. var urlParams =
  193. OpenLayers.Util.upperCaseObject(OpenLayers.Util.getParameters(url));
  194. for(var key in allParams) {
  195. if(key.toUpperCase() in urlParams) {
  196. delete allParams[key];
  197. }
  198. }
  199. paramsString = OpenLayers.Util.getParameterString(allParams);
  200. return OpenLayers.Util.urlAppend(url, paramsString);
  201. },
  202. CLASS_NAME: "OpenLayers.Layer.HTTPRequest"
  203. });