/** * Groovy DEWD Client * * @author Tony Landis * @copyright 2007 Tony Landis * @website http://www.tonylandis.com * @license BSD License (http://www.opensource.org/licenses/bsd-license.php) * @example groovy DEWDClient.groovy http://www.yoursite.com */ // map extensions to a specific application def extensionMap = [ 'html':'C:\\Program Files\\Macromedia\\Dreamweaver 8\\Dreamweaver.exe', 'htm':'C:\\Program Files\\e\\e.exe', 'default':'notepad' ] // custom handler classes def customClasses = ['DiffHandler'] // temp dir for saving files def tmpdir = 'c:\\tmp\\' // default handler function def handler = 'DefaultHandler' // determine file location and download def url = args[0].replace('dewd://','http://').toString() h = new GroovyHTTP(url) h.setMethod('GET') h.open() h.write() h.read() def fileName = h.getHeader('DEWD_FILE_ID') + '_' + h.getHeader('DEWD_FILE_NAME') def fileExtension = h.getHeader('DEWD_FILE_EXT') def fileContent = h.getContent() h.close() // debug response println h.headers.flatten() println fileContent; // generate command string for the DEWD Server def commandstring = fileName +'|'+ h.getHeader('DEWD_FILE_ID') +'|'+ h.getHeader('DEWD_URL') +'|'+ h.getHeader('DEWD_AUTH_CODE') +'|'+ h.getHeader('DEWD_AUTH_FIELD') +'|'+ h.getHeader('DEWD_FILE_ID_FIELD') +'|'+ h.getHeader('DEWD_CONTENT_FIELD') // Application to open file with def application = extensionMap['default']; if(extensionMap[fileExtension] != null) application = extensionMap[fileExtension] // Name of the class to handle this file in the DWED Client def handlerClass = h.getHeader('DEWD_CLASS') // determine handler function if(handler != handlerClass) customClasses.each { if(it==handlerClass) handler=handlerClass } "$handler"(fileName, fileContent, commandstring, tmpdir, application) // basic file handler def DefaultHandler(fileName, fileContent, commandstring, tmpdir, application) { // send to the DEWD Server for monitoring def socket=null try { socket = new Socket('localhost', 4544); } catch (ConnectException ex) { println "\r\n\r\nCONNECTION REFUSED - make sure the DEWD Server is running!\r\n\r\n" socket = null return false } // save the file def file = new FileOutputStream(tmpdir + fileName + '.xxx') def out = new BufferedOutputStream(file) out << fileContent out.close() // send the command write = new PrintWriter(socket.getOutputStream(), true) write.print('mon:' + commandstring) write.flush() socket.close() // open the file editor def command = application + ' ' + tmpdir + fileName; def proc = command.execute() } class GroovyHTTP { public method='POST' public uri public host public path public port public params=null public socket=null public writer=null public reader=null public writedata public headers = [] public content // set the url and create new URI object def GroovyHTTP(url) { uri = new URI(url) host = uri.getHost() path = uri.getRawPath() port = uri.getPort() def tpar = uri.getQuery() if(tpar != null && tpar != '') { tpar.tokenize('&').each{ def pp = it.tokenize('='); this.setParam(pp[0],pp[1]); } } if(port == null || port < 0) port = 80 if(path == null || path.length() == 0) path = "/" } // sets the method (GET or POST) def setMethod(setmethod) { method = setmethod } // push params into this request def setParam(var,value) { if(params != null) params += '&' else params='' params += var +'='+URLEncoder.encode(value) } // clear params def clearParams() { params = null } // open a new socket def open() { socket = new Socket(host, port) } // write data to the socket def write() { def contentLen = 0 if(params!=null) contentLen = params.length() def writedata = ''; if(this.method == 'GET') writedata += "GET " + path +'?'+ params + " HTTP/1.0\r\n" else writedata += "POST " + path + " HTTP/1.0\r\n" writedata += "Host: " + host + "\r\n" + "Content-Type: application/x-www-form-urlencoded\r\n" + "Content-Length: " + contentLen + "\r\n\r\n" + params + "\r\n" "Connection: close\r\n\r\n" writer = new PrintWriter(socket.getOutputStream(), true) writer.write(writedata) writer.flush() } // read response from the server def read() { reader = new DataInputStream(socket.getInputStream()) def c = null while (null != ((c = reader.readLine()))) { if(c=='') break headers.add(c) } } // get header value by name def getHeader(name) { def pattern = name + ': ' def result headers.each{ if(it ==~ /${pattern}.*/) { result = it.replace(pattern,'').trim() return 2 } } return result } // get the response content def getContent() { def row content = '' while (null != ((row = reader.readLine()))) content += row + "\r\n" return content = content.trim(); } // close the socket def close() { reader.close() writer.close() socket.close() } }