Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | 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
 
11
dojo.provide("dojo.io.RhinoIO");
12
dojo.require("dojo.io.common");
13
dojo.require("dojo.lang.func");
14
dojo.require("dojo.lang.array");
15
dojo.require("dojo.string.extras");
16
dojo.io.RhinoHTTPTransport = new function () {
17
	this.canHandle = function (req) {
18
		if (dojo.lang.find(["text/plain", "text/html", "text/xml", "text/javascript", "text/json", "application/json"], (req.mimetype.toLowerCase() || "")) < 0) {
19
			return false;
20
		}
21
		if (req.url.substr(0, 7) != "http://") {
22
			return false;
23
		}
24
		return true;
25
	};
26
	function doLoad(req, conn) {
27
		var ret;
28
		if (req.method.toLowerCase() == "head") {
29
		} else {
30
			var stream = conn.getContent();
31
			var reader = new java.io.BufferedReader(new java.io.InputStreamReader(stream));
32
			var text = "";
33
			var line = null;
34
			while ((line = reader.readLine()) != null) {
35
				text += line;
36
			}
37
			if (req.mimetype == "text/javascript") {
38
				try {
39
					ret = dj_eval(text);
40
				}
41
				catch (e) {
42
					dojo.debug(e);
43
					dojo.debug(text);
44
					ret = null;
45
				}
46
			} else {
47
				if (req.mimetype == "text/json" || req.mimetype == "application/json") {
48
					try {
49
						ret = dj_eval("(" + text + ")");
50
					}
51
					catch (e) {
52
						dojo.debug(e);
53
						dojo.debug(text);
54
						ret = false;
55
					}
56
				} else {
57
					ret = text;
58
				}
59
			}
60
		}
61
		req.load("load", ret, req);
62
	}
63
	function connect(req) {
64
		var content = req.content || {};
65
		var query;
66
		if (req.sendTransport) {
67
			content["dojo.transport"] = "rhinohttp";
68
		}
69
		if (req.postContent) {
70
			query = req.postContent;
71
		} else {
72
			query = dojo.io.argsFromMap(content, req.encoding);
73
		}
74
		var url_text = req.url;
75
		if (req.method.toLowerCase() == "get" && query != "") {
76
			url_text = url_text + "?" + query;
77
		}
78
		var url = new java.net.URL(url_text);
79
		var conn = url.openConnection();
80
		conn.setRequestMethod(req.method.toUpperCase());
81
		if (req.headers) {
82
			for (var header in req.headers) {
83
				if (header.toLowerCase() == "content-type" && !req.contentType) {
84
					req.contentType = req.headers[header];
85
				} else {
86
					conn.setRequestProperty(header, req.headers[header]);
87
				}
88
			}
89
		}
90
		if (req.contentType) {
91
			conn.setRequestProperty("Content-Type", req.contentType);
92
		}
93
		if (req.method.toLowerCase() == "post") {
94
			conn.setDoOutput(true);
95
			var output_stream = conn.getOutputStream();
96
			var byte_array = (new java.lang.String(query)).getBytes();
97
			output_stream.write(byte_array, 0, byte_array.length);
98
		}
99
		conn.connect();
100
		doLoad(req, conn);
101
	}
102
	this.bind = function (req) {
103
		var async = req["sync"] ? false : true;
104
		if (async) {
105
			setTimeout(dojo.lang.hitch(this, function () {
106
				connect(req);
107
			}), 1);
108
		} else {
109
			connect(req);
110
		}
111
	};
112
	dojo.io.transports.addTransport("RhinoHTTPTransport");
113
};
114