Module: Asciidoctor::Diagram::Java
- Defined in:
- lib/asciidoctor-diagram/util/java.rb,
lib/asciidoctor-diagram/util/java_jruby.rb,
lib/asciidoctor-diagram/util/java_socket.rb
Defined Under Namespace
Classes: CommandServer
Constant Summary collapse
- CRLF =
"\r\n".encode(Encoding::US_ASCII)
- STATUS_LINE =
Regexp.new("HTTP/1.1 (\\d+) (.*)\r\n".encode(Encoding::US_ASCII))
Class Method Summary collapse
- .classpath ⇒ Object
- .format_request(req, io) ⇒ Object
- .instance ⇒ Object
- .load ⇒ Object
- .parse_response(io) ⇒ Object
- .send_request(req) ⇒ Object
Class Method Details
.classpath ⇒ Object
5 6 7 8 9 |
# File 'lib/asciidoctor-diagram/util/java.rb', line 5 def self.classpath @classpath ||= [ File.(File.join('../..', 'asciidoctor-diagram-java-1.3.4.jar'), File.dirname(__FILE__)) ] end |
.format_request(req, io) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/asciidoctor-diagram/util/java.rb', line 13 def self.format_request(req, io) io.set_encoding Encoding::US_ASCII io.write "POST #{req[:url]} HTTP/1.1" io.write CRLF headers = req[:headers] if headers headers.each_pair do |key, value| io.write "#{key}: #{value}" io.write CRLF end end if req[:body] unless headers && headers['Content-Length'] io.write 'Content-Length: ' io.write req[:body].bytesize.to_s io.write CRLF end unless headers && headers['Content-Type'] io.write 'Content-Type: text/plain; charset=' io.write req[:body].encoding.name io.write CRLF end end io.write CRLF io.set_encoding Encoding::BINARY io.write req[:body] end |
.instance ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/asciidoctor-diagram/util/java_socket.rb', line 43 def self.instance @java_exe ||= find_java raise "Could not find Java executable" unless @java_exe unless @command_server server = CommandServer.new(@java_exe, classpath) @command_server = server at_exit do server.shutdown end end @command_server end |
.load ⇒ Object
8 9 10 11 12 13 14 15 |
# File 'lib/asciidoctor-diagram/util/java_jruby.rb', line 8 def self.load if @loaded return end classpath.flatten.each { |j| require j } @loaded = true end |
.parse_response(io) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/asciidoctor-diagram/util/java.rb', line 48 def self.parse_response(io) resp = {} io.set_encoding Encoding::US_ASCII status_line = io.readline(CRLF) status_line_parts = STATUS_LINE.match status_line resp[:code] = status_line_parts[1].to_i resp[:reason] = status_line_parts[2] headers = {} until (header = io.readline(CRLF).strip).empty? key, value = header.split ':', 2 headers[key] = value.strip end resp[:headers] = headers content_length = headers['Content-Length'] if content_length io.set_encoding Encoding::BINARY resp[:body] = io.read(content_length.to_i) end resp end |
.send_request(req) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/asciidoctor-diagram/util/java_jruby.rb', line 17 def self.send_request(req) cp = ::Java.org.asciidoctor.diagram.CommandProcessor.new() req_io = StringIO.new format_request(req, req_io) req_io.close response = cp.processRequest(req_io.string.to_java_bytes) resp_io = StringIO.new(String.from_java_bytes(response)) resp = parse_response(resp_io) resp_io.close resp end |