Groovyã§Vert.x - 2. HTTPãªã¯ã¨ã¹ããã³ãã© ãã®1 -
ãã¦
ä»åãHTTPãµã¼ãã®ç¶ãã§ãHTTPãªã¯ã¨ã¹ããã³ãã©ãæãä¸ãããã¨æãã¾ãã
å®è¡ç°å¢
ä»åã¯ã以ä¸ã®ç°å¢ã§å®è¡ãã¾ãã
- OS : Ubuntu 12.04 (32 bit) on VMware Fusion on OS X 10.6.8
- JDK : 1.7.0_04
- Vert.x : 1.0.1 final
ãªãã1.0.1 finalã«ã¯JavaScriptç¨ã®jarãã¡ã¤ã«ãå«ã¾ãã¦ããªãããã
vertx run HelloWorld.groovy
ãå®è¡ããã¨ã
Exception in thread "main" java.util.ServiceConfigurationError: org.vertx.java.deploy.VerticleFactory: Provider org.vertx.java.deploy.impl.rhino.RhinoVerticleFactory could not be instantiated: java.lang.NoClassDefFoundError: org/mozilla/javascript/ContextFactory
ã¨ããä¾å¤ãçºçãèµ·åãã¾ããã
ããã«å¯¾å¦ããããã«ã¯ã1.0 finalã®tarãã¼ã«ããããã¯ã½ã¼ã¹ããjs.jarãåå¾ãã${VERTX_HOME}/lib/jarsã«ã³ãã¼ãã¦ãã ããã
ååã®ãããã
ååã¯"Hello, World!"ãè¿ãHTTPãµã¼ããå®è¡ãã¾ããã
vertx.createHttpServer().requestHandler { request -> request.response.end('<html><body><h1>Hello, World!</h1></body></html>') }.listen(8080, 'localhost')
ãã®ã³ã¼ãã§ã¯ãã©ã®ãããªãªã¯ã¨ã¹ãã§ãä¸å¾"Hello, World!"ãè¿ããã¨ã«ãªãããªã¯ã¨ã¹ãã«å¿ããå 容ãè¿ããã¨ãã§ãã¾ãããã¾ããè¿ãå 容ããã¼ãã³ã¼ãã£ã³ã°ããã¦ãã¾ãã
ä»åã¯
ä»åã¨æ¬¡åã§ãªã¯ã¨ã¹ããã³ãã©ã®å¼æ°ã§ãããªã¯ã¨ã¹ããããããHttpServerRequestã¨ãã¬ã¹ãã³ã¹ãããããHttpServerResponseãç´¹ä»ããååã®ãµã³ãã«ã³ã¼ããæ¹è¯ãããã¨æãã¾ãã
HttpServerRequest
HTTPãµã¼ãã®ãªã¯ã¨ã¹ããã³ãã©ã¯ããªã¯ã¨ã¹ããããããHttpServerRequestã¯ã©ã¹ã®ãªãã¸ã§ã¯ããå¼æ°ã«åãã¾ãããã®HttpServerRequestã¯ã©ã¹ã®ãªãã¸ã§ã¯ãããæ§ã ãªãªã¯ã¨ã¹ãæ å ±ãåå¾ãããã¨ãã§ãã¾ãã
ãªã¯ã¨ã¹ãã®HTTPã¡ã½ããã®åå¾
ãªã¯ã¨ã¹ãã®HTTPã¡ã½ããã¯ãHttpServerRequest#getMethod()ã§åå¾ã§ãã¾ãã
vertx.createHttpServer().requestHandler { request -> request.response.end("<html><body><h1>Your method is ${request.method}</h1></body></html>") }.listen(8080, 'localhost')
ä¾ãã°ãWebãã©ã¦ã¶ã§http://localhost:8080/aaa/bbb/ccc/index.html?xxx=1&yyy=2&zzz=3ã«ã¢ã¯ã»ã¹ããå ´åã
Your method is GET
ã¨ãªãã¾ãã
ãªã¯ã¨ã¹ãã®URIã®åå¾
ãªã¯ã¨ã¹ãã®URIã¯ãHttpServerRequest#getUri()ã§åå¾ã§ãã¾ãã
vertx.createHttpServer().requestHandler { request -> request.response.end("<html><body><h1>Your URI is ${request.uri}</h1></body></html>") }.listen(8080, 'localhost')
ä¾ãã°ãWebãã©ã¦ã¶ã§http://localhost:8080/aaa/bbb/ccc/index.html?xxx=1&yyy=2&zzz=3ã«ã¢ã¯ã»ã¹ããå ´åã
Your URI is /aaa/bbb/ccc/index.html?xxx=1&yyy=2&zzz=3
ã¨ãªãã¾ãã
ãªã¯ã¨ã¹ãã®ãã¹ã®åå¾
ãªã¯ã¨ã¹ãã®ãã¹ã¯ãHttpServerRequest#getPath()ã§åå¾ã§ãã¾ãã
vertx.createHttpServer().requestHandler { request -> request.response.end("<html><body><h1>Your path is ${request.path}</h1></body></html>") }.listen(8080, 'localhost')
ä¾ãã°ãWebãã©ã¦ã¶ã§http://localhost:8080/aaa/bbb/ccc/index.html?xxx=1&yyy=2&zzz=3ã«ã¢ã¯ã»ã¹ããå ´åã
Your path is /aaa/bbb/ccc/index.html
ã¨ãªãã¾ãã
ãªã¯ã¨ã¹ãã®ã¯ã¨ãªã®åå¾
ãªã¯ã¨ã¹ãã®ã¯ã¨ãªã¯ãHttpServerRequest#getQuery()ã§Stringåã§åå¾ã§ãã¾ãã
vertx.createHttpServer().requestHandler { request -> request.response.end("<html><body><h1>Your query is ${request.query}</h1></body></html>") }.listen(8080, 'localhost')
ä¾ãã°ãWebãã©ã¦ã¶ã§http://localhost:8080/aaa/bbb/ccc/index.html?xxx=1&yyy=2&zzz=3ã«ã¢ã¯ã»ã¹ããå ´åã
Your query is xxx=1&yyy=2&zzz=3
ã¨ãªãã¾ãã
ãªã¯ã¨ã¹ããããã®åå¾
ãªã¯ã¨ã¹ããããã¯ãHttpServerRequest#getHeaders()ã§java.util.Mapåã§åå¾ã§ãã¾ãã
vertx.createHttpServer().requestHandler { request -> def sb = new StringBuilder() for (def h : request.headers) { sb << "<li>${h.key} : ${h.value}</li>" } request.response.end("<html><body><h1>Your headers are <ul>${sb.toString()}</ul></h1></body></html>") }.listen(8080, 'localhost')
ä¾ãã°ãWebãã©ã¦ã¶ã§http://localhost:8080/aaa/bbb/ccc/index.html?xxx=1&yyy=2&zzz=3ã«ã¢ã¯ã»ã¹ããå ´åã
Your headers are * Accept-Language : ja,en-US;q=0.8,en;q=0.6 * Host : localhost:8080 * Accept-Charset : Shift_JIS,utf-8;q=0.7,*;q=0.3 * Accept-Encoding : gzip,deflate,sdch * User-Agent : Mozilla/5.0 (X11; Linux i686) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.52 Safari/536.5 * Accept : text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 * Connection : keep-alive * Cache-Control : max-age=0
ã¨ãªãã¾ãã
ãªã¯ã¨ã¹ããã©ã¡ã¼ã¿ã®åå¾
ãªã¯ã¨ã¹ããã©ã¡ã¼ã¿ã¯ãHttpServerRequest#getQuery()ã§Stringåã§åå¾ã§ãã¾ãããHttpServerRequest#getParams()ã§java.util.Mapåã§åå¾ã§ãã¾ãã
vertx.createHttpServer().requestHandler { request -> def sb = new StringBuilder() for (def h : request.params) { sb << "<li>${h.key} : ${h.value}</li>" } request.response.end("<html><body><h1>Your params are <ul>${sb.toString()}</ul></h1></body></html>") }.listen(8080, 'localhost')
ä¾ãã°ãWebãã©ã¦ã¶ã§http://localhost:8080/aaa/bbb/ccc/index.html?xxx=1&yyy=2&zzz=3ã«ã¢ã¯ã»ã¹ããå ´åã
Your params are * xxx : 1 * yyy : 2 * zzz : 3
ã¨ãªãã¾ãã
ãã ããhttp://localhost:8080/aaa/bbb/ccc/index.html?xxx=1&yyy=2&zzz=3&xxx=4&yyy=5&zzz=6ã¨ãåããã©ã¡ã¼ã¿åãè¤æ°æå®ããå ´åã
Your params are * xxx : 1 * yyy : 2 * zzz : 3
ã¨ãªããã¯ã¨ãªã®æåã«æå®ãããå¤ãæ¡ç¨ãããããã§ãã
ãªã¯ã¨ã¹ãããã£ã®åå¾ - dataHandler
POSTã§ãã¼ã¿ãéä¿¡ãããããªå ´åãéä¿¡ããããã¼ã¿ã¯HttpServerRequest#getQuery()ã¡ã½ãããããã¯HttpServerRequest#getParams()ã¡ã½ããã§ã¯åå¾ã§ãã¾ããã
ã©ããã£ã¦åå¾ãããã¨ããã¨ããªã¯ã¨ã¹ããã³ãã©ã®HttpServerRequestãªãã¸ã§ã¯ãã«å¯¾ãã¦ãªã¯ã¨ã¹ãããã£ã対å¦ãããã³ãã©(ã¯ãã¼ã¸ã£)ãè¨å®ãããã®ãã³ãã©å
ã§åå¾ãã¾ãã
vertx.createHttpServer().requestHandler { request -> request.dataHandler { buffer -> println "buffer = ${buffer.toString()}" } request.response.end("<html><body><h1>You send data.</h1></body></html>") }.listen(8080, 'localhost')
ãªã¯ã¨ã¹ãããã£ã対å¦ãããã¼ã¿ãã³ãã©ãHttpServerRequest#dataHandler(groovy.lang.Closure)ã¡ã½ããã§è¨å®ãã¾ãã
HttpServerRequest#dataHandlerã«è¨å®ãããã¼ã¿ãã³ãã©ã¯ããªã¯ã¨ã¹ãããã£ç¨ã®ãããã¡ã¯ã©ã¹ã§ããorg.vertx.groovy.core.buffer.Bufferã¯ã©ã¹ã®ãªãã¸ã§ã¯ãã1ã¤å¼æ°ã«ã¨ãã¾ãã
HttpServerRequest#dataHandlerã«è¨å®ãããã¼ã¿ãã³ãã©ã¯ããªã¯ã¨ã¹ãããã£ã®ãã£ã³ã¯ãã¨ã«å¼ã³åºããã¾ãã
ãªã¯ã¨ã¹ãããã£ã®åå¾ - endHandler
1ãªã¯ã¨ã¹ãã®ããã£ã1ãã£ã³ã¯ã§ããã°ä¸è¨ã®ã³ã¼ãã§è¯ãã§ãããè¤æ°ã®ãã£ã³ã¯ã«åããã¦ããå ´åã¯ããã£ã³ã¯ãã¨ã«ãªã¯ã¨ã¹ãããã£ã®å 容ãä¿æããããã«ãã¾ãã
import org.vertx.groovy.core.buffer.Buffer vertx.createHttpServer().requestHandler { request -> def body = new Buffer(0) request.dataHandler { buffer -> body << buffer } request.endHandler { request.response.end("<html><body><h1>You send data - ${body.toString()}</body></html>") } }.listen(8080, 'localhost')
ãªã¯ã¨ã¹ããã³ãã©å
ã§Bufferã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ãçæãããã¼ã¿ãã³ãã©å
ã§ãå¼æ°ã§æ¸¡ãããããã£ãã£ã³ã¯ã®å
容ãBufferã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ã«è¿½å ãã¾ãã
ãã¼ã¿ãã³ãã©ã¯ãã£ã³ã¯ãã¨ã«å¼ã°ããããããã£ã³ã¯ã®çµãã(= ããã£ã®åä¿¡ã®çµãã)ãç¥ãå¿
è¦ãããã¾ããVert.xã®HTTPãµã¼ãã®å ´åããã£ã³ã¯ã®åä¿¡ãå®äºããæã«å°ç¨ã®ã¤ãã³ããçºçããã対å¿ããã¤ãã³ããã³ãã©ãå®è¡ãã¾ãããã®å ´åãã¨ã³ããã³ãã©ã«ãªãã¾ãã
ã¨ã³ããã³ãã©ãHttpServerRequest#endHandler(groovy.lang.Closure)ã¡ã½ããã§è¨å®ãã¾ããã¨ã³ããã³ãã©ã«æ¸¡ãããå¼æ°ã¯ããã¾ãããã¨ã³ããã³ãã©å
ã§ãBufferã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ã«å¯¾å¿ãã¾ãã
ãªã¯ã¨ã¹ãããã£ã®åå¾ - bodyHandler
HTTPãã£ã³ã¯ãã¨ã§ã¯ãªããå ¨ã¦ã®ãªã¯ã¨ã¹ãããã£ãåä¿¡ããå¾ã§å¦çããããå ´åãããã¨æãã¾ãããã®å ´åã¯ãããã£ãã³ãã©ãè¨å®ãããã¨ã§å¯¾å¿ã§ãã¾ãã
vertx.createHttpServer().requestHandler { request -> request.bodyHandler { buffer -> request.response.end("<html><body><h1>You send data - ${buffer.toString()}</body></html>") } }.listen(8080, 'localhost')
ããã£ãã³ãã©ã¯ããã¹ã¦ã®ãªã¯ã¨ã¹ãããã£ãåä¿¡ããå¾ã§ä¸åº¦ã ãå¼ã³åºããã¾ãã
çµããã«
ä»åã¯ãåä¿¡ãããªã¯ã¨ã¹ããå¦çããã¨ããã主ã«è¦ã¦ãã¾ããã次åã¯ãã¬ã¹ãã³ã¹é¨åãè¦ã¦ã¿ããã¨æãã¾ãã