/**
 * A http wrapper for using from JS tests.
 *
 * Request:
 * {
 *    url: "http://host:port/path",
 *    method: "GET",
 *    headers: {
 *      "Content-Type":"application/json"
 *    },
 *    data: {...}
 * }
 *
 * Response:
 * {
 *    code: 200,
 *    headers: {
 *      "Content-Type":"application/json"
 *    },
 *    data: {...}
 * }
 *
 * Also see: https://wiki.openjdk.java.net/display/Nashorn/Nashorn+extensions
 */
var http = new (function() {
    this.debug = false;
    var Url = java.net.URL;
    var thiz = this;
    function processResponse(conn) {
        var headers = {};

        for(var headerKey in conn.headerFields) {
            if(!headerKey) {
                //skip  '"null":"HTTP/1.1 200 OK"'
                continue;
            }
            headers[headerKey] = conn.getHeaderField(headerKey);
        }
        var resp = {
            code: conn.responseCode,
            message: conn.responseMessage,
            contentType: conn.contentType,
            headers: headers
        };
        if(thiz.debug) {
            console.debug("Got response:", JSON.stringify(resp));
        }
        // data may be too big for logging
        var data = io.readFully(conn.inputStream);
        if(resp.contentType.indexOf('application/json') == 0) {
            data = JSON.parse(data);
        }
        resp.data = data;
        return resp;
    }
    this.execute = function(req, ctx) {
        if(this.debug) {
            console.debug("Execute request:", JSON.stringify(req));
        }
        var url = new Url(req.url);
        var conn = url.openConnection();
        if(req.method) {
            //default method detected inner by exists of request body, se we not need to specify it
            conn.requestMethod = req.method;
        }
        var contentType;
        for(var headerKey in req.headers) {
            var headerVal = req.headers[headerKey];
            if("content-type" === headerKey.toLowerCase()) {
                contentType = headerVal;
            }
            conn.setRequestProperty(headerKey, headerVal);
        }
        if(!contentType) {
            //set default content type
            conn.setRequestProperty("Content-Type", "application/json");
        }
        if(req.data) {
            var data = req.data;
            if(typeof data !== "string") {
                data = JSON.stringify(data);
            }
            conn.doOutput = true;
            io.writeFully(data, conn.getOutputStream());
        }
        conn.connect();
        return processResponse(conn);
    };
})();

@@ -600,78 +600,142 @@
     
-for(var headerKey in conn.headerFields) %7B%0A            if(!headerKey) %7B
+var resp = %7B%0A            code: conn.responseCode,%0A            message: conn.responseMessage,%0A            contentType: conn.contentType
 %0A   
@@ -739,24 +739,27 @@
         
+%7D;%0A
         
 //skip  
@@ -754,67 +754,142 @@
     
-//skip  '%22null%22:%22HTTP/1.1 200 OK%22'%0A                continue
+var hasError = resp.code %3E= 400;%0A        if(hasError %7C%7C thiz.debug) %7B%0A            console.debug(%22Got response:%22, JSON.stringify(resp))
 ;%0A  
@@ -886,37 +886,57 @@
 resp));%0A        
+%7D%0A
     
-%7D
+    if(!hasError) %7B
 %0A            hea
@@ -932,24 +932,24 @@
         
-headers%5B
+for(var 
 headerKe
@@ -953,21 +953,18 @@
 rKey
-%5D =
+ in
  conn.
-getH
+h
 eade
@@ -969,17 +969,41 @@
 derField
-(
+s) %7B%0A                if(!
 headerKe
@@ -1004,17 +1004,18 @@
 aderKey)
-;
+ %7B
 %0A       
@@ -1011,34 +1011,32 @@
 ) %7B%0A        
-%7D%0A
         
 var resp = %7B
@@ -1027,143 +1027,94 @@
     
-var resp = %7B%0A            code: conn.responseCode,%0A            message: conn.responseMessage,%0A            contentType: conn.contentType,
+    //skip  '%22null%22:%22HTTP/1.1 200 OK%22'%0A                    continue;%0A                %7D
 %0A   
@@ -1126,137 +1126,119 @@
     
+    
 headers
-: 
+%5BheaderKey%5D = conn.getHeaderField(
 header
-s
+Key);
 %0A     
-   %7D;%0A
        
- if(thiz.debug) %7B%0A            console.debug(%22Got response:%22, JSON.stringify(resp))
+%7D%0A            resp.headers = headers
 ;%0A    
- 
-   %7D%0A
     
@@ -1276,32 +1276,36 @@
 logging%0A        
+    
 var data = io.re
@@ -1335,24 +1335,28 @@
 m);%0A        
+    
 if(resp.cont
@@ -1362,16 +1362,36 @@
 tentType
+ && resp.contentType
 .indexOf
@@ -1423,32 +1423,36 @@
 ) %7B%0A            
+  
+  
 data = JSON.pars
@@ -1460,34 +1460,42 @@
 (data);%0A        
-%7D%0A
+    %7D%0A    
         resp.dat
@@ -1500,24 +1500,34 @@
 ata = data;%0A
+        %7D%0A
         retu

fix http.js on server errors