/*******************************************************************************
 * Copyright (c) 2015 SAP and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * Contributors:
 * SAP - initial API and implementation
 *******************************************************************************/

/* globals $ java */
/* eslint-env node, dirigible */

exports.get = function(url, options) {
	return handleRequest(url, 'GET', options);
};

exports.post = function(url, options) {
	return handleRequest(url, 'POST', options);
};

exports.put = function(url, options) {
	return handleRequest(url, 'PUT', options);
};

exports.delete = function(url, options) {
	return handleRequest(url, 'DELETE', options);
};

exports.request = function(options) {
	return handleRequest(null, options.method, options);
};

function handleRequest(url, method, options) {
    if (url === null) {
	    url = options.host + ":" + (options.port ? options.port : 80) + (options.path ? options.path : '/');
    }
    if (method === null) {
        method = "GET";
    }
    
	var request = createRequest(method, url);
	if (options) {
	    addHeaders(request, options.headers);
    }


	var httpClient = $.getHttpUtils().createHttpClient(true);
	return createResponse(httpClient.execute(request), options);
}

function createRequest(method, url) {
	var request = null;
	switch(method) {
		case 'POST':
			request = $.getHttpUtils().createPost(url);
			break;
		case 'PUT':
			request = $.getHttpUtils().createPut(url);
			break;
		case 'DELETE':
			request = $.getHttpUtils().createDelete(url);
			break;
		default:
			request = $.getHttpUtils().createGet(url);
	}
	return request;
}

function addHeaders(httpRequest, headers) {
	for (var nextHeader in headers) {
		httpRequest.addHeader(nextHeader, headers[nextHeader]);
	}
}

function createResponse(httpResponse, options) {
	return {
		'statusCode': httpResponse.getStatusLine().getStatusCode(),
		'statusMessage': httpResponse.getStatusLine().getReasonPhrase(),
		'data': getResponseData(httpResponse, options),
		'httpVersion': httpResponse.getProtocolVersion(),
		'headers': getResponseHeaders(httpResponse)
	};
}

function getResponseData(httpResponse, options) {
    var entity = httpResponse.getEntity();
    var content = entity.getContent();

    var data = $.getIOUtils().toByteArray(content);

    $.getHttpUtils().consume(entity);
    var isBinary = false;
    if (options) {
        isBinary = options.binary;
    }

    return isBinary ? data : new java.lang.String(data);
}

function getResponseHeaders(httpResponse) {
	var headers = [];
	var httpResponseHeaders = httpResponse.getAllHeaders();
	for (var i = 0; i < httpResponseHeaders.length; i ++) {
		var header = httpResponseHeaders[i];
		headers.push({
			'name': header.getName(),
			'value': header.getValue()
		});
	}
	return headers;
}

@@ -1184,24 +1184,25 @@
 '/');%0A    %7D%0A
+%0A
     if (meth
@@ -1246,20 +1246,16 @@
 ;%0A    %7D%0A
-    
 %0A%09var re
@@ -1291,76 +1291,19 @@
  url
-);%0A%09if (options) %7B%0A%09    addHeaders(request, options.headers);%0A    %7D%0A
+, options);
 %0A%0A%09v
@@ -1458,16 +1458,25 @@
 hod, url
+, options
 ) %7B%0A%09var
@@ -1564,32 +1564,62 @@
 reatePost(url);%0A
+%09%09%09addBody(request, options);%0A
 %09%09%09break;%0A%09%09case
@@ -1668,24 +1668,54 @@
 tePut(url);%0A
+%09%09%09addBody(request, options);%0A
 %09%09%09break;%0A%09%09
@@ -1844,24 +1844,89 @@
 et(url);%0A%09%7D%0A
+%09if (options) %7B%0A%09    addHeaders(request, options.headers);%0A    %7D%0A
 %09return requ
@@ -2072,24 +2072,232 @@
 er%5D);%0A%09%7D%0A%7D%0A%0A
+function addBody(request, options) %7B%0A%09if (options.body) %7B%0A%09%09var charset = options.charset ? options.charset: %22ISO-8859-1%22;%0A%09%09request.setEntity($.getHttpUtils().createHttpEntity(options.body, charset));%0A%09%7D%0A%7D%0A%0A
 function cre

Update client.js