Subversion Repositories Applications.papyrus

Rev

Rev 1422 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1318 alexandre_ 1
/*
2
	Copyright (c) 2004-2006, The Dojo Foundation
3
	All Rights Reserved.
4
 
5
	Licensed under the Academic Free License version 2.1 or above OR the
6
	modified BSD license. For more information on Dojo licensing, see:
7
 
8
		http://dojotoolkit.org/community/licensing.shtml
9
*/
10
 
1422 alexandre_ 11
 
12
 
1318 alexandre_ 13
dojo.provide("dojo.io.RhinoIO");
14
dojo.require("dojo.io.common");
15
dojo.require("dojo.lang.func");
16
dojo.require("dojo.lang.array");
17
dojo.require("dojo.string.extras");
18
dojo.io.RhinoHTTPTransport = new function () {
19
	this.canHandle = function (req) {
20
		if (dojo.lang.find(["text/plain", "text/html", "text/xml", "text/javascript", "text/json", "application/json"], (req.mimetype.toLowerCase() || "")) < 0) {
21
			return false;
22
		}
23
		if (req.url.substr(0, 7) != "http://") {
24
			return false;
25
		}
26
		return true;
27
	};
28
	function doLoad(req, conn) {
29
		var ret;
30
		if (req.method.toLowerCase() == "head") {
31
		} else {
32
			var stream = conn.getContent();
33
			var reader = new java.io.BufferedReader(new java.io.InputStreamReader(stream));
34
			var text = "";
35
			var line = null;
36
			while ((line = reader.readLine()) != null) {
37
				text += line;
38
			}
39
			if (req.mimetype == "text/javascript") {
40
				try {
41
					ret = dj_eval(text);
42
				}
43
				catch (e) {
44
					dojo.debug(e);
45
					dojo.debug(text);
46
					ret = null;
47
				}
48
			} else {
49
				if (req.mimetype == "text/json" || req.mimetype == "application/json") {
50
					try {
51
						ret = dj_eval("(" + text + ")");
52
					}
53
					catch (e) {
54
						dojo.debug(e);
55
						dojo.debug(text);
56
						ret = false;
57
					}
58
				} else {
59
					ret = text;
60
				}
61
			}
62
		}
63
		req.load("load", ret, req);
64
	}
65
	function connect(req) {
66
		var content = req.content || {};
67
		var query;
68
		if (req.sendTransport) {
69
			content["dojo.transport"] = "rhinohttp";
70
		}
71
		if (req.postContent) {
72
			query = req.postContent;
73
		} else {
74
			query = dojo.io.argsFromMap(content, req.encoding);
75
		}
76
		var url_text = req.url;
77
		if (req.method.toLowerCase() == "get" && query != "") {
78
			url_text = url_text + "?" + query;
79
		}
80
		var url = new java.net.URL(url_text);
81
		var conn = url.openConnection();
82
		conn.setRequestMethod(req.method.toUpperCase());
83
		if (req.headers) {
84
			for (var header in req.headers) {
85
				if (header.toLowerCase() == "content-type" && !req.contentType) {
86
					req.contentType = req.headers[header];
87
				} else {
88
					conn.setRequestProperty(header, req.headers[header]);
89
				}
90
			}
91
		}
92
		if (req.contentType) {
93
			conn.setRequestProperty("Content-Type", req.contentType);
94
		}
95
		if (req.method.toLowerCase() == "post") {
96
			conn.setDoOutput(true);
97
			var output_stream = conn.getOutputStream();
98
			var byte_array = (new java.lang.String(query)).getBytes();
99
			output_stream.write(byte_array, 0, byte_array.length);
100
		}
101
		conn.connect();
102
		doLoad(req, conn);
103
	}
104
	this.bind = function (req) {
105
		var async = req["sync"] ? false : true;
106
		if (async) {
107
			setTimeout(dojo.lang.hitch(this, function () {
108
				connect(req);
109
			}), 1);
110
		} else {
111
			connect(req);
112
		}
113
	};
114
	dojo.io.transports.addTransport("RhinoHTTPTransport");
115
};
116