function response = java_http_request(url, varargin)
% Send data string by http post request to specified url.
% 
% Version 1.0
%
%	Syntax
% 
% response = java_http_request(url, s_request, options)
%
%	Description
%
%   Input arguments:
% url       - [1xN2] string, address string.
%                            For test purpose url httpbin.org may be used 
%                             'http://httpbin/org/post'
%                             'http://httpbin.org/anything'
%                             'http://httpbin.org/anything/{anything}'
%                             'https://postman-echo.com'
% varargin:
%           s_request - [1xN2] string, data string
%                                      when sending a json request,
%                                      s_request must have appropriate formatting:
%                                      1) jsonlab library, https://github.com/NeuroJSON/jsonlab,
%                                       s_request = savejson('',jdataencode(<struct>)) )
%									   2) json package https://github.com/gnu-octave/pkg-json
%           options   - [1x1]  struct, request parameter structure, from weboptions_java
%
%   Output arguments:
% response  - [1xN3] string, url response. 
%                            When data is encoded in json, use functions to decode res
%                           1) jsonlab library, https://github.com/NeuroJSON/jsonlab,
%                             <struct> = loadjson(response) )
% 							2) json package https://github.com/gnu-octave/pkg-json
% 
%
%   Additional information:
%       If an option CharacterEncoding = 'auto' is set in function weboptions_java,
%       will be chosen encoding 'UTF-8' 
%
%   See also webwrite_java, weboptions_java, java_http_request, java_http_set_options.

    response = [];
    
    try
        % process url
        j_url = javaObject('java.net.URL',url);

        j_conn  = javaMethod('openConnection',j_url);
        
        % parse options
        s_request   = varargin{1}; % request data string
%        s_request   = varargin(1:end-1); % request data string
        options     = varargin{end};     % weboptions_java

        % set options
        java_http_set_options(j_conn, options);
        
        % check encoding, set 'UTF-8', if it was 'auto'
        if (~ strcmp (options.CharacterEncoding, 'auto'))
            s_encoding = options.CharacterEncoding;
        else
            s_encoding = 'UTF-8';
        end

        % enable writing to output stream
        javaMethod('setDoOutput',j_conn,true(1));

        javaMethod('connect',j_conn);

        % open output stream
        j_out_stream = javaMethod('getOutputStream',j_conn);

        % write string
        jstr = javaObject('java.lang.String',s_request);

        jbytes = javaMethod('getBytes',jstr,s_encoding);
        l = length(jbytes);

        javaMethod('write',j_out_stream,jbytes,0,l);

        % open input stream
        j_input_stream = javaMethod('getInputStream',j_conn);

        j_buff_in_s = javaObject('java.io.InputStreamReader',j_input_stream,s_encoding);
        j_buff_in = javaObject('java.io.BufferedReader',j_buff_in_s);

        % read input stream
        i_str = true(1);
        jStrB = javaObject('java.lang.StringBuilder','');

        while i_str
            jstr_i = javaMethod('readLine',j_buff_in);
            i_str = ~isempty(jstr_i);

            if ~i_str
                break;
            end

            javaMethod('append',jStrB,jstr_i);
            1+1;
        end

        response = char(jStrB);
        
%         disp(response);

        i_code = javaMethod('getResponseCode',j_conn);

        % closing
        javaMethod('disconnect',j_conn);
        javaMethod('close',j_buff_in);
        javaMethod('close',j_out_stream);
        
        % methodsview(j_buff_in)
        % methods(j_buff_in)
%         error('1\n');
        
        1+1;
        
    catch exep
        fprintf(2,'error:\nin function: %s\n%s\n',mfilename,exep.message);
        
    end
    

end