HTTPRequest.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for
  2. * full list of contributors). Published under the 2-clause BSD license.
  3. * See license.txt in the OpenLayers distribution or repository 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. if (!this.params) {
  56. this.params = OpenLayers.Util.extend({}, params);
  57. }
  58. },
  59. /**
  60. * APIMethod: destroy
  61. */
  62. destroy: function() {
  63. this.url = null;
  64. this.params = null;
  65. OpenLayers.Layer.prototype.destroy.apply(this, arguments);
  66. },
  67. /**
  68. * APIMethod: clone
  69. *
  70. * Parameters:
  71. * obj - {Object}
  72. *
  73. * Returns:
  74. * {<OpenLayers.Layer.HTTPRequest>} An exact clone of this
  75. * <OpenLayers.Layer.HTTPRequest>
  76. */
  77. clone: function (obj) {
  78. if (obj == null) {
  79. obj = new OpenLayers.Layer.HTTPRequest(this.name,
  80. this.url,
  81. this.params,
  82. this.getOptions());
  83. }
  84. //get all additions from superclasses
  85. obj = OpenLayers.Layer.prototype.clone.apply(this, [obj]);
  86. // copy/set any non-init, non-simple values here
  87. return obj;
  88. },
  89. /**
  90. * APIMethod: setUrl
  91. *
  92. * Parameters:
  93. * newUrl - {String}
  94. */
  95. setUrl: function(newUrl) {
  96. this.url = newUrl;
  97. },
  98. /**
  99. * APIMethod: mergeNewParams
  100. *
  101. * Parameters:
  102. * newParams - {Object}
  103. *
  104. * Returns:
  105. * redrawn: {Boolean} whether the layer was actually redrawn.
  106. */
  107. mergeNewParams:function(newParams) {
  108. this.params = OpenLayers.Util.extend(this.params, newParams);
  109. var ret = this.redraw();
  110. if(this.map != null) {
  111. this.map.events.triggerEvent("changelayer", {
  112. layer: this,
  113. property: "params"
  114. });
  115. }
  116. return ret;
  117. },
  118. /**
  119. * APIMethod: redraw
  120. * Redraws the layer. Returns true if the layer was redrawn, false if not.
  121. *
  122. * Parameters:
  123. * force - {Boolean} Force redraw by adding random parameter.
  124. *
  125. * Returns:
  126. * {Boolean} The layer was redrawn.
  127. */
  128. redraw: function(force) {
  129. if (force) {
  130. return this.mergeNewParams({"_olSalt": Math.random()});
  131. } else {
  132. return OpenLayers.Layer.prototype.redraw.apply(this, []);
  133. }
  134. },
  135. /**
  136. * Method: selectUrl
  137. * selectUrl() implements the standard floating-point multiplicative
  138. * hash function described by Knuth, and hashes the contents of the
  139. * given param string into a float between 0 and 1. This float is then
  140. * scaled to the size of the provided urls array, and used to select
  141. * a URL.
  142. *
  143. * Parameters:
  144. * paramString - {String}
  145. * urls - {Array(String)}
  146. *
  147. * Returns:
  148. * {String} An entry from the urls array, deterministically selected based
  149. * on the paramString.
  150. */
  151. selectUrl: function(paramString, urls) {
  152. var product = 1;
  153. for (var i=0, len=paramString.length; i<len; i++) {
  154. product *= paramString.charCodeAt(i) * this.URL_HASH_FACTOR;
  155. product -= Math.floor(product);
  156. }
  157. return urls[Math.floor(product * urls.length)];
  158. },
  159. /**
  160. * Method: getFullRequestString
  161. * Combine url with layer's params and these newParams.
  162. *
  163. * does checking on the serverPath variable, allowing for cases when it
  164. * is supplied with trailing ? or &, as well as cases where not.
  165. *
  166. * return in formatted string like this:
  167. * "server?key1=value1&key2=value2&key3=value3"
  168. *
  169. * WARNING: The altUrl parameter is deprecated and will be removed in 3.0.
  170. *
  171. * Parameters:
  172. * newParams - {Object}
  173. * altUrl - {String} Use this as the url instead of the layer's url
  174. *
  175. * Returns:
  176. * {String}
  177. */
  178. getFullRequestString:function(newParams, altUrl) {
  179. // if not altUrl passed in, use layer's url
  180. var url = altUrl || this.url;
  181. // create a new params hashtable with all the layer params and the
  182. // new params together. then convert to string
  183. var allParams = OpenLayers.Util.extend({}, this.params);
  184. allParams = OpenLayers.Util.extend(allParams, newParams);
  185. var paramsString = OpenLayers.Util.getParameterString(allParams);
  186. // if url is not a string, it should be an array of strings,
  187. // in which case we will deterministically select one of them in
  188. // order to evenly distribute requests to different urls.
  189. //
  190. if (OpenLayers.Util.isArray(url)) {
  191. url = this.selectUrl(paramsString, url);
  192. }
  193. // ignore parameters that are already in the url search string
  194. var urlParams =
  195. OpenLayers.Util.upperCaseObject(OpenLayers.Util.getParameters(url));
  196. for(var key in allParams) {
  197. if(key.toUpperCase() in urlParams) {
  198. delete allParams[key];
  199. }
  200. }
  201. paramsString = OpenLayers.Util.getParameterString(allParams);
  202. return OpenLayers.Util.urlAppend(url, paramsString);
  203. },
  204. CLASS_NAME: "OpenLayers.Layer.HTTPRequest"
  205. });