Global.helma.Http ()
Sourcecode in /home/hannes/helma/modules/helma/Http.js:
22:   helma.Http = function() {
23:       var proxy = null;
24:       var content = "";
25:       var userAgent = "Helma Http Client";
26:       var method = "GET";
27:       var credentials = null;
28:       var followRedirects = true;
29:       var headers = {};
30:       var timeout = {
31:          "connect": 0,
32:          "socket": 0
33:      };
34:  
35:      var setTimeout = function(type, value) {
36:          var v = java.lang.System.getProperty("java.specification.version");
37:          if (parseFloat(v, 10) >= 1.5) {
38:              timeout[type] = value;
39:          } else {
40:              app.log("[Helma Http Client] WARNING: timeouts can only be set with Java Runtime version >= 1.5");
41:          }
42:          return true;
43:      }
44:  
45:      /**
46:       * sets the proxy host and port for later use
47:       */
48:      this.setProxy = function(proxyString) {
49:          var idx = proxyString.indexOf(":");
50:          var host = proxyString.substring(0, idx);
51:          var port = proxyString.substring(idx+1);
52:          if (java.lang.Class.forName("java.net.Proxy") != null) {
53:              // construct a proxy instance
54:              var socket = new java.net.InetSocketAddress(host, port);
55:              proxy = new java.net.Proxy(java.net.Proxy.Type.HTTP, socket);
56:          } else {
57:              // the pre jdk1.5 way: set the system properties
58:              var sys = java.lang.System.getProperties();
59:              if (host) {
60:                  app.log("[Helma Http Client] WARNING: setting system http proxy to " + host + ":" + port);
61:                  sys.put("http.proxySet", "true");
62:                  sys.put("http.proxyHost", host);
63:                  sys.put("http.proxyPort", port);
64:              }
65:          }
66:          return true;
67:      };
68:  
69:      /**
70:       * returns the proxy in "host:port" format
71:       */
72:      this.getProxy = function() {
73:          if (proxy != null) {
74:              return proxy.address().getHostName() + ":" + proxy.address().getPort();
75:          } else if (sys.get("http.proxySet") == "true") {
76:              return sys.get("http.proxyHost") + ":" + sys.get("http.proxyPort");
77:          } else {
78:              return null;
79:          }
80:      };
81:  
82:      /**
83:       * sets the credentials for basic http authentication
84:       */
85:      this.setCredentials = function(username, password) {
86:          var str = new java.lang.String(username + ":" + password);
87:          credentials = (new Packages.sun.misc.BASE64Encoder()).encode(str.getBytes());
88:          return true;
89:      }
90:  
91:      this.setContent = function(stringOrObject) {
92:          if (stringOrObject.constructor == Object) {
93:              res.push();
94:              var value;
95:              for (var key in stringOrObject) {
96:                  value = stringOrObject[key];
97:                  res.write(encodeURIComponent(key));
98:                  res.write("=");
99:                  res.write(encodeURIComponent(value));
100:                  res.write("&");
101:              }
102:              content = res.pop();
103:              content = content.substring(0, content.length-1);
104:          } else
105:              content = stringOrObject.toString();
106:          return;
107:      };
108:      
109:      /**
110:       * getter/setter for method
111:       */
112:      this.setMethod = function(m) {
113:          method = m;
114:          return true;
115:      };
116:      this.getMethod = function() {
117:          return method;
118:      };
119:  
120:      this.setHeader = function(name, value) {
121:         headers[name] = value;
122:         return;
123:     };
124:     this.getHeader = function(name) {
125:         return headers[name];
126:     };
127: 
128:     /**
129:      * getter/setter for timeouts
130:      */
131:     this.setTimeout = function(timeout) {
132:         setTimeout("connect", timeout);
133:         return true;
134:     };
135:     this.setReadTimeout = function(timeout) {
136:         setTimeout("socket", timeout);
137:         return true;
138:     };
139:     this.getTimeout = function() {
140:         return timeout.connect;
141:     };
142:     this.getReadTimeout = function() {
143:         return timeout.socket;
144:     };
145: 
146:     /**
147:      * getter/setter for following redirects
148:      */
149:     this.setFollowRedirects = function(value) {
150:         followRedirects = value;
151:         return;
152:     };
153:     this.getFollowRedirects = function() {
154:         return followRedirects;
155:     };
156: 
157:     /**
158:      * getter/setter for user agent string
159:      */
160:     this.setUserAgent = function(agent) {
161:         userAgent = agent;
162:         return true;
163:     }
164:     this.getUserAgent = function() {
165:         return userAgent;
166:     }
167: 
168:     /**
169:      * executes a http request
170:      */
171:     this.getUrl = function(url, opt) {
172:         if (typeof url == "string") {
173:             if (!(url = helma.Http.evalUrl(url)))
174:                 throw new Error("'" + url + "' is not a valid URL.");
175:         } else if (!(url instanceof java.net.URL)) {
176:             throw new Error("'" + url + "' is not a valid URL.");
177:         }
178:         
179:         var conn = proxy ? url.openConnection(proxy) : url.openConnection();
180:         conn.setFollowRedirects(followRedirects);
181:         conn.setAllowUserInteraction(false);
182:         conn.setRequestMethod(method);
183:         conn.setRequestProperty("User-Agent", userAgent);
184: 
185:         if (opt) {
186:             if (opt instanceof Date)
187:                 conn.setIfModifiedSince(opt.getTime());
188:             else if ((typeof opt == "string") && (opt.length > 0))
189:                 conn.setRequestProperty("If-None-Match", opt);
190:         }
191:         if (credentials != null) {
192:             conn.setRequestProperty("Authorization", "Basic " + credentials);
193:         }
194:         if (parseFloat(java.lang.System.getProperty("java.specification.version"), 10) >= 1.5) {
195:             conn.setConnectTimeout(timeout.connect);
196:             conn.setReadTimeout(timeout.socket);
197:         } else {
198:             app.debug("WARNING: timeouts can only be set due to Java version < 1.5");
199:         }
200: 
201:         for (var i in headers) {
202:             if (typeof(headers[i]) != "function")
203:                 conn.setRequestProperty(i, headers[i]);
204:         }
205: 
206:         if (content) {
207:             conn.setRequestProperty("Content-Length", content.length);
208:             conn.setDoOutput(true);
209:             var out = new java.io.OutputStreamWriter(conn.getOutputStream());
210:             out.write(content);
211:             out.flush();
212:             out.close();
213:         }
214: 
215:         var result = {
216:             url: conn.getURL(),
217:             location: conn.getHeaderField("location"),
218:             code: conn.getResponseCode(),
219:             message: conn.getResponseMessage(),
220:             length: conn.getContentLength(),
221:             type: conn.getContentType(),
222:             encoding: conn.getContentEncoding(),
223:             lastModified: null,
224:             eTag: null,
225:             content: null
226:         }
227:         var lastmod = conn.getLastModified();
228:         if (lastmod)
229:             result.lastModified = new Date(lastmod);
230:         result.eTag = conn.getHeaderField("ETag");
231:     
232:         if (result.length != 0 && result.code == 200) {
233:             var body = new java.io.ByteArrayOutputStream();
234:             var input = new java.io.BufferedInputStream(conn.getInputStream());
235:             var buf = java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, 1024);
236:             var str;
237:             while ((str = input.read(buf)) > -1) {
238:                 body.write(buf, 0, str);
239:             }
240:             input.close();
241:             result.content = result.encoding ? body.toString(result.encoding) : body.toString();
242:             result.length = result.content.length;
243:         }
244:         conn.disconnect();
245:         return result;
246:     }
247: 
248:     this.toString = function() {
249:         return "[Helma Http Client]";
250:     };
251: 
252:     for (var i in this)
253:         this.dontEnum(i);
254: 
255:     return this;
256: }