-
Notifications
You must be signed in to change notification settings - Fork 0
/
translator.groovy
61 lines (50 loc) · 1.48 KB
/
translator.groovy
1
2
3
4
5
6
7
8
9
10
11
12
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
props = ["url" : "http://www.rambler.ru/"] //default url
port = 8080 //port for translation
putUrl = "/put"
/////////////////////////////////////
loadProps()
def processPut(req){
if(req.params.containsKey("url")){
saveUrl(req.params["url"])
req.response.end("ok!");
} else {
req.response.end("ok! " + props["lastdate"] + " : "+ props["url"]);
}
}
def saveUrl(url){
props = ["url" : url, "lastdate" : new Date()]
new File("props.ser").withObjectOutputStream { out -> out << props }
}
def loadProps(){
try {
new File("props.ser").withObjectInputStream { instream ->
instream.eachObject { props = it } }
} catch(Exception e){ /* using default url */ }
return props
}
def sendData(def req, def url){
try {
URLConnection conn = new URL(url).openConnection()
conn.setConnectTimeout(10000)
conn.setReadTimeout(20000)
def bytes = new ByteArrayOutputStream()
bytes << conn.getInputStream()
bytes.close()
req.response.putHeader("Content-Type", conn.getContentType())
req.response.putHeader("Content-Length", bytes.size())
req.response.end new org.vertx.groovy.core.buffer.Buffer(bytes.toByteArray())
} catch ( IOException e ) {
container.logger.error("ololo", e)
req.response.end() }
}
vertx.createHttpServer().requestHandler{ req ->
if(req.path.equals(putUrl)){
processPut(req)
return
}
String url = props["url"]
if(!req.path.equals("/") && url.endsWith("/") ){
url = url.substring(0, url.length()-1) + req.path
}
sendData(req, url)
}.listen(port)