Revision 4307
Added by ben leinfelder over 15 years ago
WebServiceRequest.js | ||
---|---|---|
2 | 2 |
Author: Mike Adair mike.adairATccrs.nrcan.gc.ca |
3 | 3 |
License: LGPL as per: http://www.gnu.org/copyleft/lesser.html |
4 | 4 |
|
5 |
$Id$ |
|
5 |
$Id: WebServiceRequest.js 3881 2008-02-27 15:41:07Z gjvoosten $
|
|
6 | 6 |
*/ |
7 | 7 |
|
8 | 8 |
// Ensure this object's dependancies are loaded. |
... | ... | |
28 | 28 |
//this.debug=true; |
29 | 29 |
|
30 | 30 |
//get the request name to add listener to |
31 |
var requestName = toolNode.selectSingleNode("mb:requestName"); |
|
32 |
if (requestName) { |
|
33 |
this.requestName = requestName.firstChild.nodeValue; |
|
34 |
} |
|
31 |
this.requestName = this.getProperty("mb:requestName"); |
|
35 | 32 |
|
36 | 33 |
//get the request filter to add to the request |
37 |
var requestFilter = toolNode.selectSingleNode("mb:requestFilter"); |
|
38 |
if (requestFilter) { |
|
39 |
this.requestFilter = requestFilter.firstChild.nodeValue; |
|
40 |
} |
|
34 |
this.requestFilter = this.getProperty("mb:requestFilter"); |
|
41 | 35 |
|
42 |
var styleUrl = baseDir+"/tool/xsl/"+this.requestName.replace(/:/,"_")+".xsl"; |
|
36 |
var styleUrl = toolNode.selectSingleNode("mb:stylesheet"); |
|
37 |
styleUrl = styleUrl ? getNodeValue(styleUrl) : |
|
38 |
baseDir+"/tool/xsl/"+this.requestName.replace(/:/,"_")+".xsl"; |
|
43 | 39 |
this.requestStylesheet = new XslProcessor(styleUrl); |
44 | 40 |
|
45 | 41 |
// Set stylesheet parameters for all the child nodes from the config file |
46 | 42 |
for (var j=0;j<toolNode.childNodes.length;j++) { |
47 |
if (toolNode.childNodes[j].firstChild && toolNode.childNodes[j].firstChild.nodeValue) {
|
|
48 |
this.requestStylesheet.setParameter(toolNode.childNodes[j].nodeName,toolNode.childNodes[j].firstChild.nodeValue);
|
|
43 |
if (getNodeValue(toolNode.childNodes[j])) {
|
|
44 |
this.requestStylesheet.setParameter(toolNode.childNodes[j].nodeName,getNodeValue(toolNode.childNodes[j]));
|
|
49 | 45 |
} |
50 | 46 |
} |
51 | 47 |
|
52 |
/** |
|
53 |
* Function which create the HTTP payload for a request |
|
54 |
* @param requestName the name of the web service operation to execute |
|
55 |
* @param featureNodeId the id of the node in the doc to be processed by the stylesheet |
|
56 |
*/ |
|
57 |
this.createHttpPayload = function(feature) { |
|
58 |
//confirm inputs |
|
59 |
if (this.debug) alert("source:"+Sarissa.serialize(feature)); |
|
60 |
//if (this.debug) alert("stylesheet:"+Sarissa.serialize(this.requestStylesheet.xslDom)); |
|
48 |
this.model.addListener("init", this.init, this); |
|
49 |
this.model.addListener(this.requestName.replace(/:/,"_"), this.doRequest, this); |
|
50 |
} |
|
61 | 51 |
|
52 |
/** |
|
53 |
* Function which create the HTTP payload for a request |
|
54 |
* @param feature the feature object |
|
55 |
*/ |
|
56 |
WebServiceRequest.prototype.createHttpPayload = function(feature) { |
|
57 |
//confirm inputs |
|
58 |
if (this.debug) mbDebugMessage(this, "source:"+(new XMLSerializer()).serializeToString(feature)); |
|
59 |
//if (this.debug) mbDebugMessage(this, "stylesheet:"+(new XMLSerializer()).serializeToString(this.requestStylesheet.xslDom)); |
|
62 | 60 |
|
63 |
//prepare the stylesheet |
|
64 |
var httpPayload = new Object(); |
|
65 |
httpPayload.method = this.targetModel.method; |
|
66 |
this.requestStylesheet.setParameter("httpMethod", httpPayload.method ); |
|
67 |
this.requestStylesheet.setParameter("version", this.model.getVersion(feature) ); |
|
68 |
if (this.requestFilter) { |
|
69 |
var filter = config.objects[this.requestFilter]; |
|
70 |
this.requestStylesheet.setParameter("filter", escape(Sarissa.serialize(filter.doc).replace(/[\n\f\r\t]/g,'') )); |
|
71 |
if (this.debug) alert(Sarissa.serialize(filter.doc)); |
|
72 |
} |
|
73 | 61 |
|
74 |
//process the doc with the stylesheet |
|
75 |
httpPayload.postData = this.requestStylesheet.transformNodeToObject(feature); |
|
76 |
if (this.debug) { |
|
77 |
alert("request data:"+Sarissa.serialize(httpPayload.postData)); |
|
78 |
if (config.serializeUrl) var response = postLoad(config.serializeUrl, httpPayload.postData); |
|
79 |
} |
|
62 |
//prepare the stylesheet |
|
63 |
var httpPayload = new Object(); |
|
64 |
httpPayload.method = this.targetModel.method; |
|
65 |
this.requestStylesheet.setParameter("httpMethod", httpPayload.method ); |
|
66 |
this.requestStylesheet.setParameter("version", this.model.getVersion(feature) ); |
|
67 |
if (this.requestFilter) { |
|
68 |
var filter = config.objects[this.requestFilter]; |
|
69 |
this.requestStylesheet.setParameter("filter", escape((new XMLSerializer()).serializeToString(filter.doc).replace(/[\n\f\r\t]/g,'') )); |
|
70 |
if (this.debug) mbDebugMessage(this, (new XMLSerializer()).serializeToString(filter.doc)); |
|
71 |
} |
|
80 | 72 |
|
81 |
//allow the tool to have a serverUrl property which overrides the model server URL |
|
82 |
//TBD: this still used? |
|
83 |
if (this.serverUrl) { |
|
84 |
httpPayload.url = this.serverUrl; |
|
73 |
//process the doc with the stylesheet |
|
74 |
httpPayload.postData = this.requestStylesheet.transformNodeToObject(feature); |
|
75 |
if (this.debug) { |
|
76 |
mbDebugMessage(this, "request data:"+(new XMLSerializer()).serializeToString(httpPayload.postData)); |
|
77 |
if (config.serializeUrl) var response = postLoad(config.serializeUrl, httpPayload.postData); |
|
78 |
} |
|
79 |
|
|
80 |
httpPayload.url = this.model.getServerUrl(this.requestName, httpPayload.method, feature); |
|
81 |
|
|
82 |
//extract the URL from the transformation result for GET method |
|
83 |
if (httpPayload.method.toLowerCase() == "get") { |
|
84 |
httpPayload.postData.setProperty("SelectionLanguage", "XPath"); |
|
85 |
Sarissa.setXpathNamespaces(httpPayload.postData, "xmlns:mb='http://mapbuilder.sourceforge.net/mapbuilder'"); |
|
86 |
var queryString = httpPayload.postData.selectSingleNode("//mb:QueryString"); |
|
87 |
if (httpPayload.url.indexOf("?") < 0) { |
|
88 |
httpPayload.url += "?"; |
|
85 | 89 |
} else { |
86 |
httpPayload.url = this.model.getServerUrl(this.requestName, httpPayload.method, feature);
|
|
90 |
httpPayload.url += "&";
|
|
87 | 91 |
} |
88 |
|
|
89 |
//extract the URL from the transformation result for GET method |
|
90 |
if (httpPayload.method.toLowerCase() == "get") { |
|
91 |
httpPayload.postData.setProperty("SelectionLanguage", "XPath"); |
|
92 |
Sarissa.setXpathNamespaces(httpPayload.postData, "xmlns:mb='http://mapbuilder.sourceforge.net/mapbuilder'"); |
|
93 |
var queryString = httpPayload.postData.selectSingleNode("//mb:QueryString"); |
|
94 |
if (httpPayload.url.indexOf("?") < 0) httpPayload.url += "?"; |
|
95 |
httpPayload.url += queryString.firstChild.nodeValue; |
|
96 |
httpPayload.postData = null; |
|
97 |
} |
|
98 |
if (this.debug) alert("URL:"+httpPayload.url); |
|
99 |
return httpPayload; |
|
92 |
httpPayload.url += getNodeValue(queryString); |
|
93 |
httpPayload.postData = null; |
|
100 | 94 |
} |
95 |
mbDebugMessage(this, "URL:"+httpPayload.url); |
|
96 |
return httpPayload; |
|
97 |
} |
|
101 | 98 |
|
102 | 99 |
|
103 |
/**
|
|
104 |
* Listener function which will actually issue the request. This method
|
|
105 |
* will prepare the HTTP payload for a particular featureName.
|
|
106 |
* @param requestName the name of the web service operation to execute
|
|
107 |
* @param featureNodeId the id of the node in the doc to be processed by the stylesheet
|
|
108 |
*/
|
|
109 |
this.doRequest = function(objRef, featureName) {
|
|
110 |
objRef.targetModel.featureName = featureName;
|
|
100 |
/** |
|
101 |
* Listener function which will actually issue the request. This method |
|
102 |
* will prepare the HTTP payload for a particular featureName. |
|
103 |
* @param requestName the name of the web service operation to execute |
|
104 |
* @param featureNodeId the id of the node in the doc to be processed by the stylesheet |
|
105 |
*/ |
|
106 |
WebServiceRequest.prototype.doRequest = function(objRef, featureName) {
|
|
107 |
objRef.targetModel.featureName = featureName; |
|
111 | 108 |
|
112 |
var feature = objRef.model.getFeatureNode(featureName);
|
|
113 |
if (objRef.model.setRequestParameters) objRef.model.setRequestParameters(featureName, objRef.requestStylesheet);
|
|
114 |
var httpPayload = objRef.createHttpPayload(feature);
|
|
115 |
objRef.targetModel.newRequest(objRef.targetModel,httpPayload);
|
|
109 |
var feature = objRef.model.getFeatureNode(featureName); |
|
110 |
if (!feature) {
|
|
111 |
alert(mbGetMessage("featureNotFoundWebServiceRequest", featureName));
|
|
112 |
return;
|
|
116 | 113 |
} |
117 |
this.model.addListener(this.requestName.replace(/:/,"_"), this.doRequest, this); |
|
114 |
if (objRef.model.setRequestParameters) objRef.model.setRequestParameters(featureName, objRef.requestStylesheet); |
|
115 |
var httpPayload = objRef.createHttpPayload(feature); |
|
116 |
objRef.targetModel.newRequest(objRef.targetModel,httpPayload); |
|
117 |
} |
|
118 | 118 |
|
119 |
this.setAoiParameters = function(objRef,bbox) { |
|
120 |
//TBD: this depends on the targetModel having a containerModel to extract the AOI from. |
|
121 |
//we probably need a config property to point to the AOI model to handle this properly. |
|
122 |
if (objRef.targetModel.containerModel) { |
|
123 |
var featureSRS = null; |
|
124 |
var containerSRS = "EPSG:4326"; |
|
125 |
//var bbox = objRef.targetModel.containerModel.getBoundingBox(); |
|
126 |
/* |
|
127 |
//convert the BBOX to the feature SRS for the request |
|
128 |
var containerSRS = objRef.targetModel.containerModel.getSRS(); |
|
129 |
if (featureSRS) { |
|
130 |
var sourceProj = new Proj(featureSRS.firstChild.nodeValue); |
|
131 |
if ( !sourceProj.matchSrs( containerSRS )) { |
|
132 |
var containerProj = new Proj(objRef.targetModel.containerModel.getSRS()); |
|
133 |
var llTemp = containerProj.Inverse(new Array(bbox[0],bbox[1])); |
|
134 |
var xy = sourceProj.Forward(llTemp); |
|
135 |
bbox[0] = xy[0]; bbox[1] = xy[1]; |
|
136 |
llTemp = containerProj.Inverse(new Array(bbox[2],bbox[3])); |
|
137 |
xy = sourceProj.Forward(llTemp); |
|
138 |
bbox[2] = xy[0]; bbox[3] = xy[1]; |
|
139 |
} |
|
119 |
WebServiceRequest.prototype.setAoiParameters = function(objRef) { |
|
120 |
//TBD: this depends on the targetModel having a containerModel to extract the AOI from. |
|
121 |
//we probably need a config property to point to the AOI model to handle this properly. |
|
122 |
if (objRef.containerModel) { |
|
123 |
var featureSRS = null; |
|
124 |
var containerSRS = "EPSG:4326"; |
|
125 |
var bbox = objRef.containerModel.getBoundingBox(); |
|
126 |
/* |
|
127 |
TBD: figure out when to use AOI or BBOX |
|
128 |
var aoi = objRef.containerModel.getParam("aoi"); |
|
129 |
if (aoi) { |
|
130 |
bbox[0] = aoi[0][0]; |
|
131 |
bbox[1] = aoi[1][1]; |
|
132 |
bbox[2] = aoi[1][0]; |
|
133 |
bbox[3] = aoi[0][1]; |
|
134 |
} |
|
135 |
*/ |
|
136 |
var containerSRS = objRef.containerModel.getSRS(); |
|
137 |
/* |
|
138 |
//convert the BBOX to the feature SRS for the request |
|
139 |
if (featureSRS) { |
|
140 |
var sourceProj = new Proj4js.Proj(getNodeValue(featureSRS)); |
|
141 |
if ( !sourceProj.matchSrs( containerSRS )) { |
|
142 |
var containerProj = new Proj4js.Proj(objRef.containerModel.getSRS()); |
|
143 |
var llTemp = containerProj.Inverse(new Array(bbox[0],bbox[1])); |
|
144 |
var xy = sourceProj.Forward(llTemp); |
|
145 |
bbox[0] = xy[0]; bbox[1] = xy[1]; |
|
146 |
llTemp = containerProj.Inverse(new Array(bbox[2],bbox[3])); |
|
147 |
xy = sourceProj.Forward(llTemp); |
|
148 |
bbox[2] = xy[0]; bbox[3] = xy[1]; |
|
140 | 149 |
} |
141 |
*/ |
|
142 |
objRef.requestStylesheet.setParameter("bBoxMinX", bbox[0][0] ); |
|
143 |
objRef.requestStylesheet.setParameter("bBoxMinY", bbox[1][1] ); |
|
144 |
objRef.requestStylesheet.setParameter("bBoxMaxX", bbox[1][0] ); |
|
145 |
objRef.requestStylesheet.setParameter("bBoxMaxY", bbox[0][1] ); |
|
146 |
objRef.requestStylesheet.setParameter("srs", containerSRS ); |
|
147 |
objRef.requestStylesheet.setParameter("width", objRef.targetModel.containerModel.getWindowWidth() ); |
|
148 |
objRef.requestStylesheet.setParameter("height", objRef.targetModel.containerModel.getWindowHeight() ); |
|
149 | 150 |
} |
151 |
*/ |
|
152 |
objRef.requestStylesheet.setParameter("bBoxMinX", bbox[0]); |
|
153 |
objRef.requestStylesheet.setParameter("bBoxMinY", bbox[1]); |
|
154 |
objRef.requestStylesheet.setParameter("bBoxMaxX", bbox[2]); |
|
155 |
objRef.requestStylesheet.setParameter("bBoxMaxY", bbox[3]); |
|
156 |
objRef.requestStylesheet.setParameter("srs", containerSRS ); |
|
157 |
objRef.requestStylesheet.setParameter("width", objRef.containerModel.getWindowWidth() ); |
|
158 |
objRef.requestStylesheet.setParameter("height", objRef.containerModel.getWindowHeight() ); |
|
150 | 159 |
} |
160 |
} |
|
151 | 161 |
|
152 |
this.init = function(objRef) {
|
|
153 |
if (objRef.targetModel.containerModel) {
|
|
154 |
objRef.targetModel.containerModel.addListener("aoi", objRef.setAoiParameters, objRef);
|
|
155 |
//TBD: another one for bbox
|
|
156 |
}
|
|
162 |
WebServiceRequest.prototype.init = function(objRef) {
|
|
163 |
if (objRef.targetModel.containerModel) { |
|
164 |
objRef.containerModel = objRef.targetModel.containerModel;
|
|
165 |
} else if (objRef.model.containerModel) {
|
|
166 |
objRef.containerModel = objRef.model.containerModel;
|
|
157 | 167 |
} |
158 |
this.model.addListener("init", this.init, this); |
|
168 |
if (objRef.containerModel) { |
|
169 |
objRef.containerModel.addListener("aoi", objRef.setAoiParameters, objRef); |
|
170 |
objRef.containerModel.addListener("bbox", objRef.setAoiParameters, objRef); |
|
171 |
objRef.containerModel.addListener("selectedLayer", objRef.selectFeature, objRef); |
|
172 |
objRef.containerModel.addListener("loadModel", objRef.mapInit, objRef); |
|
173 |
objRef.containerModel.addListener("newModel", objRef.clear, objRef); |
|
174 |
} |
|
175 |
} |
|
159 | 176 |
|
177 |
/** |
|
178 |
* set map events needed for this tool |
|
179 |
* @param objRef reference to this tool |
|
180 |
*/ |
|
181 |
WebServiceRequest.prototype.mapInit = function(objRef) { |
|
182 |
// register OpenLayers event to do updates onmouseup |
|
183 |
objRef.containerModel.map.events.registerPriority('mouseup', objRef, objRef.setClickPosition); |
|
160 | 184 |
} |
185 |
|
|
186 |
/** |
|
187 |
* remove map events for this tool |
|
188 |
* @param objRef reference to this tool |
|
189 |
*/ |
|
190 |
WebServiceRequest.prototype.clear = function(objRef) { |
|
191 |
if (objRef.containerModel.map && objRef.containerModel.map.events) { |
|
192 |
objRef.containerModel.map.events.unregister('mouseup', objRef, objRef.setClickPosition); |
|
193 |
} |
|
194 |
} |
|
195 |
|
|
196 |
/** |
|
197 |
* Listener function that will clear the templates and set the mouse |
|
198 |
* positions when the user clicks on the map. |
|
199 |
* @param e OpenLayers event |
|
200 |
*/ |
|
201 |
WebServiceRequest.prototype.setClickPosition = function(e) { |
|
202 |
this.targetModel.deleteTemplates(); |
|
203 |
this.requestStylesheet.setParameter("xCoord", e.xy.x); |
|
204 |
this.requestStylesheet.setParameter("yCoord", e.xy.y); |
|
205 |
} |
|
206 |
|
|
207 |
/** |
|
208 |
* Listener function which will actually issue the request. This method |
|
209 |
* will prepare the HTTP payload for a particular featureName. |
|
210 |
* @param requestName the name of the web service operation to execute |
|
211 |
* @param featureNodeId the id of the node in the doc to be processed by the stylesheet |
|
212 |
*/ |
|
213 |
WebServiceRequest.prototype.selectFeature = function(objRef, featureName) { |
|
214 |
objRef.requestStylesheet.setParameter("queryLayer", featureName); |
|
215 |
} |
|
216 |
|
Also available in: Unified diff
upgrade to MapBuilder 1.5rc2 - includes support for Firefox 3 compatibility (yes, it is also EOLed)