Groovy: Another version xmlServer.groovy
Another version xmlServer.groovy ... Reveal Code
#!/bin/env groovy import java.util.concurrent.Executors import groovy.sql.Sql import com.sun.net.httpserver.* if (System.getProperty("os.name")=="Linux"){ jdbcURL="jdbc:mysql://localhost:3306/airs" jasperDir= "/var/www/html/jasper/" } else { jdbcURL="jdbc:mysql://ec2-54-208-78-75.compute-1.amazonaws.com:3306/airs" jasperDir= "r:/DROPBOX/amazonaws/jasper/" } oldStdOut=System.out def server = HttpServer.create(new InetSocketAddress(8000),0) server.with{ createContext("/", new info(oldStdOut:oldStdOut, jasperDir:jasperDir,jdbcURL:jdbcURL)) createContext("/kill", new Kill(server:server,oldStdOut:oldStdOut)) new File(jasperDir).eachFileMatch(~/.*.groovy/) { file-> def fname=file.getName()[0..-8] println "/${fname}.php" createContext("/${fname}.php", new groovy2xml(jasperDir:jasperDir,oldStdOut:oldStdOut,jasperScript:fname)) } setExecutor(null) start() println "\nGroovy-xmlServer startded:" } class Kill implements HttpHandler { def server def oldStdOut public void handle(HttpExchange exchange) throws IOException { System.out=oldStdOut exchange.responseHeaders['Content-Type'] = 'text/html; charset='+CharsetToolkit.defaultSystemCharset exchange.sendResponseHeaders(200, 0) def out = new PrintWriter(exchange.getResponseBody()) out.println "Groovy-xmlServer killed.
" out.close() exchange.close() server.stop(3) println "\nGroovy-xmlServer killed.\n-------------------------------" if (System.properties['sun.desktop']) return System.exit(0) } } class info implements HttpHandler { def oldStdOut def jasperDir def jdbcURL public void handle(HttpExchange exchange) throws IOException { System.out=oldStdOut def out = new PrintWriter(exchange.getResponseBody()) def path = exchange.getRequestURI().getPath() def method = exchange.getRequestMethod() print "\n==> $method(${path}): " if(path in ["/sql2xml.php","/sql2json.php"]){ def mysql try{ mysql= Sql.newInstance("${jdbcURL}?useUnicode=true&characterEncoding=UTF-8", 'userairs', 'userairs', 'com.mysql.jdbc.Driver') } catch(Exception ex) { ex.printStackTrace() } // def getquery = exchange.getRequestURI().getQuery()//from GET def query, body if (getquery) query = URLDecoder.decode(getquery) println "getQuery()==>$query" try{ def isr = new InputStreamReader(exchange.getRequestBody(),"utf-8") def br = new BufferedReader(isr) body = br.readLine() println "getRequestBody()==>$body" if(body) if (!query) query=body } catch(Exception ex) {println ex.getMessage()} // if (!query) query='select 1 one' println "$query\n-------------------------------" if(path=="/sql2xml.php"){ exchange.responseHeaders['Content-Type'] = 'text/xml; charset=UTF-8' exchange.sendResponseHeaders(200, 0) out.println "" out.println " " } else {//"/sql2json.php" exchange.responseHeaders['Content-Type'] = 'application/json; charset=UTF-8' exchange.sendResponseHeaders(200, 0) out.print "[{\"autor\":\"is09-20\", \"jdbcURL\":\"${jdbcURL}\", \"SQL\":\"$query\"},\n[" def rr=0 try { mysql.rows(query).each{row-> if(rr>0) out.print ",{" else out.print "{"; rr=1 row.each { if(rr>1) out.print ","; rr=2 out.print "\"${it.key}\": \"${it.value}\"" } out.println "}" } out.println "]]" } catch(Exception ex) { out.println "], {\"error\":\"${ex.getMessage()}\"}]" println "sql2json: $query\n ${ex.getMessage()}" } } } else { exchange.responseHeaders['Content-Type'] = 'text/html; charset=UTF-8' exchange.sendResponseHeaders(200, 0) out.println """$query " try{ mysql.rows(query).each{row-> out.println "" row.each {out.println "<$it.key>$it.key>"} out.println "
" } } catch(Exception ex) { out.println "" println "sql2xml: $query\n ${ex.getMessage()}" } out.println " Groovy XML server (25-01-2014)
You can afford the following:
""" println "Groovy-xmlServer is ready!" } out.close() exchange.close() } } class groovy2xml implements HttpHandler { def jasperDir def oldStdOut def jasperScript public void handle(HttpExchange exchange) throws IOException { System.out = oldStdOut def path = exchange.getRequestURI().getPath()//.tokenize("/") def method = exchange.getRequestMethod() print "\n==> $method(${path[1..-1]}): " def getquery = exchange.getRequestURI().getQuery()//from GET def query, body if (getquery) query = URLDecoder.decode(getquery) println "getQuery()==>$query" try{ def isr = new InputStreamReader(exchange.getRequestBody(),"utf-8") def br = new BufferedReader(isr) body = br.readLine() println "getRequestBody()==>$body" if(body) if (!query) query=body } catch(Exception ex) {println ex.getMessage()} if (!query) query='' exchange.responseHeaders['Content-Type'] = 'text/xml; charset=UTF-8' exchange.sendResponseHeaders(200, 0) def out = new PrintWriter(exchange.getResponseBody()) def groovyFile="${jasperDir}${jasperScript}.groovy" def bufStr = new ByteArrayOutputStream() println "run(new File('$groovyFile') '${URLDecoder.decode(query)}')" System.out = new PrintStream(bufStr) def shell = new GroovyShell() shell.run(new File(groovyFile), URLDecoder.decode(query)) System.out = oldStdOut def xml= bufStr.toString() out.println xml println "'${groovyFile}' finished.\n-------------------------------" out.close() exchange.close() } } if (System.properties['sun.desktop']){ java.awt.Desktop.getDesktop().browse(new URI("http://localhost:8000/")) }sql2xml.php?select ... sql2json.php?select ... """ new File(jasperDir).eachFileMatch(~/.*.groovy/) { file-> def fname=file.getName()[0..-8] out.println " ${fname}.php?..." } out.println """ kill.php (с чудесным последующим воскрешением)