JRtest.groovy
import com.sun.net.httpserver.*
import net.sf.jasperreports.engine.*
import net.sf.jasperreports.engine.export.*
 
def oldStdOut = System.out
def rootDir= "c:/DROPBOX/edunet/2014/is10-99"
String groovyFile = "${rootDir}/groovy/binlog.groovy"
 
def bufStr = new ByteArrayOutputStream()
System.out = new PrintStream(bufStr)
new GroovyShell().run(new File(groovyFile), '')
System.out = oldStdOut
def xml= "$bufStr"
 
def doc=net.sf.jasperreports.engine.util.JRXmlUtils.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")))
def con = new net.sf.jasperreports.engine.data.JRXmlDataSource(doc,"/ROWSET/ROW")
def jasper_Print = JasperFillManager.fillReport("${rootDir}/jasper/binlogHTTP.jasper", [XML_URL:groovyFile], con)
 
def port = 8880
def serv = HttpServer.create(new InetSocketAddress(port),0)
 
serv.with {
  createContext("/", new reportsMaker(jasperPrint:jasper_Print))
  setExecutor(null)
  start()
}
 
class reportsMaker implements HttpHandler {
  def jasperPrint
  public void handle(HttpExchange exchange) throws IOException {
    def h = exchange.getResponseHeaders()
    //export:
    def exporter, exporterParameter
    switch(exchange.getRequestURI().getQuery()) {
    case "html":
      exporter= new JRHtmlExporter()
      exporterParameter= new JRHtmlExporterParameter()
      exporter.setParameter(exporterParameter.IS_USING_IMAGES_TO_ALIGN, false)
      h.add("Content-Type", "text/html; charset=UTF-8")
      break
    case "rtf":
      exporter= new JRRtfExporter()
      exporterParameter= new JRExporterParameter()
      h.add("Content-Type", "application/msword")
      break
    case "xls":
      exporter= new JExcelApiExporter()
      exporterParameter= new JRXlsExporterParameter()
      exporter.setParameter(exporterParameter.IS_DETECT_CELL_TYPE, false)
      //...
      exporter.setParameter(exporterParameter.IS_WHITE_PAGE_BACKGROUND, false)
      exporter.setParameter(exporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, true)
      h.add("Content-Type", "application/vnd.ms-excel")
      break
    default: //.pdf
      exporter= new JRPdfExporter()
      exporterParameter= new JRPdfExporterParameter()
      h.add("Content-Type", "application/pdf")
    }
    //
    def out = new java.io.ByteArrayOutputStream()
    exporter.setParameter(exporterParameter.JASPER_PRINT,jasperPrint)
    exporter.setParameter(exporterParameter.OUTPUT_STREAM, out)
    exporter.exportReport()
 
    h.add("Content-Disposition", "inline; filename=binlogHTTP")
    exchange.sendResponseHeaders(200, out.size())
    def os = exchange.getResponseBody()
    os.write(out.toByteArray())
    os.close
    exchange.close()
  }
}
 
println "\nGroovy-JRtest:$port startded in directory $rootDir"
//
["html","rtf","xls",""].each{
  java.awt.Desktop.getDesktop().browse(new URI("http://localhost:8880/?$it"))
}

c:\opt\Groovy\bin\groovy.bat -cp c:\DROPBOX\amazonaws\src\jrlib.jar JRtest.groovy