You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xap-commits@incubator.apache.org by mt...@apache.org on 2006/08/11 19:39:49 UTC
svn commit: r430873 - /incubator/xap/trunk/src/xap/util/HttpUtils.js
Author: mturyn
Date: Fri Aug 11 12:39:48 2006
New Revision: 430873
URL: http://svn.apache.org/viewvc?rev=430873&view=rev
Log:
(For P.Yee:) Improved by adding optionof passing in extant HttpRequest instead of a URI string.
Added a "head" request method to just get headers, pingURI method.to determine if a resource's present.
Modified:
incubator/xap/trunk/src/xap/util/HttpUtils.js
Modified: incubator/xap/trunk/src/xap/util/HttpUtils.js
URL: http://svn.apache.org/viewvc/incubator/xap/trunk/src/xap/util/HttpUtils.js?rev=430873&r1=430872&r2=430873&view=diff
==============================================================================
--- incubator/xap/trunk/src/xap/util/HttpUtils.js (original)
+++ incubator/xap/trunk/src/xap/util/HttpUtils.js Fri Aug 11 12:39:48 2006
@@ -1,3 +1,4 @@
+
/*
* Copyright 2006 The Apache Software Foundation.
*
@@ -5,7 +6,7 @@
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -14,11 +15,7 @@
* limitations under the License.
*
*/
- Xap.provide( "xap.util.HttpUtils" ) ;
-
-
-
- /**
+/**
* TODO either expand or replace this with another good set of utilities to deal
* with HTTP/network functionality. We need to be able to do things like post,
* set headers, etc, and we need to return the request itself rather than
@@ -32,58 +29,70 @@
* request's "Content-Type" is set to "application/octet-stream" by default.
*
* @author ikaplansky
+ * @author pyee
*/
-
-xap.util.HttpUtils = function(){}
-
-xap.util.HttpUtils.s_log = xap.util.LogFactory.getLog( "xap.util.HttpUtils" );
-
-
-
-xap.util.HttpUtils.createHttpRequest = function() {
- var request;
- try {
- if ( window.XMLHttpRequest ) {
- request = new XMLHttpRequest();
- } else {
- request = new ActiveXObject("Microsoft.XMLHTTP");
- }
- } catch( e ) {
- xap.util.HttpUtils.s_log( "XMLHttpRequest is not supported in this browser"
- + e );
- }
- return request;
+Xap.provide("xap.util.HttpUtils");
+Xap.require("xap.requestservice.HttpRequest");
+xap.util.HttpUtils = function () {
+};
+xap.util.HttpUtils.s_log = xap.util.LogFactory.getLog("HttpUtils");
+xap.util.HttpUtils.OK = 200;
+xap.util.HttpUtils.URI_NA = 404;
+if (window.XMLHttpRequest) {
+ xap.util.HttpUtils.createHttpRequest = function () {
+ return new XMLHttpRequest();
+ };
+} else {
+ xap.util.HttpUtils.createHttpRequest = function () {
+ return new ActiveXObject("Microsoft.XMLHTTP");
+ };
}
-
-
-xap.util.HttpUtils.get = function( url, callback){
+xap.util.HttpUtils._sendHttpRequest = function (type, url, callback, content) {
var request = xap.util.HttpUtils.createHttpRequest();
- request.open( "GET", url, callback!=null );
- if( callback ) {
- request.onreadystatechange = function() {
- if( request.readyState == 4 ) {
- //TODO need object here? other states?
- callback.apply( request );
- }
- }
+ var isHttpRequestType = typeof url == "object" && url.constructor == xap.requestservice.HttpRequest;
+ if (isHttpRequestType) {
+ request.open(type, url.getUri(), callback != null);
+ var headerNames = url.getHeaderNames();
+ for (var i = 0; i < headerNames.length; i++) {
+ var headerName = headerNames[i];
+ request.setRequestHeader(headerName, url.getHeader(headerName));
+ xap.util.HttpUtils.s_log.warn(headerName + " ==> " + url.getHeader(headerName));
+ }
+ } else {
+ request.open(type, url, callback != null);
}
-
- request.send( null );
- return request;
-}
-
-xap.util.HttpUtils.post = function( url, callback, content){
- var request = xap.util.HttpUtils.createHttpRequest();
- request.open( "POST", url, callback!=null );
- if( callback ) {
- request.onreadystatechange = function() {
- if( request.readyState == 4 ) {
- //TODO need object here? other states?
- callback.apply( request );
- }
- }
+ if (callback) {
+ request.onreadystatechange = function () {
+ if (request.readyState == 4) {
+ callback(request);
+ } else {
+ xap.util.HttpUtils.s_log.warn("FAILED TO LOAD URL => " + URL);
+ }
+ };
+ }
+ if (isHttpRequestType) {
+ request.send(url.getContent());
+ } else {
+ request.send(content);
}
-
- request.send( content );
return request;
-}
+};
+xap.util.HttpUtils.get = function (url, callback) {
+ return xap.util.HttpUtils._sendHttpRequest("GET", url, callback);
+};
+xap.util.HttpUtils.post = function (url, callback, content) {
+ return xap.util.HttpUtils._sendHttpRequest("POST", url, callback, content);
+};
+xap.util.HttpUtils.head = function (url, callback, content) {
+ return xap.util.HttpUtils._sendHttpRequest("HEAD", url, callback, content);
+};
+xap.util.HttpUtils.pingURI = function (uri) {
+ var callback = function (response) {
+ var result = false;
+ if (response.status == xap.util.HttpUtils.OK) {
+ result = true;
+ }
+ };
+ return xap.util.HttpUtils._sendHttpRequest("HEAD", url, callback);
+};
+