diff --git a/build.gradle b/build.gradle index d9e20eb..cff896b 100644 --- a/build.gradle +++ b/build.gradle @@ -35,6 +35,8 @@ repositories { dependencies { provided "org.vert-x:vertx-core:$vertxVersion" provided "org.vert-x:vertx-platform:$vertxVersion" + provided "org.vert-x:vertx-lang-groovy:$vertxVersion" + provided "org.codehaus.groovy:groovy:$groovyVersion" testCompile "org.vert-x:vertx-lang-java:$vertxVersion" testCompile "org.vert-x:vertx-lang-rhino:$vertxVersion" @@ -104,4 +106,4 @@ task collectDeps(type: Copy) { into("test") { from configurations.testCompile } -} \ No newline at end of file +} diff --git a/gradle.properties b/gradle.properties index 400b633..70ac681 100644 --- a/gradle.properties +++ b/gradle.properties @@ -2,6 +2,6 @@ modulename=vertx.formupload version=1.0 gradleVersion=1.1 vertxVersion=1.2.1.final - +groovyVersion=2.0.6 junitVersion=4.10 rhinoVersion=1.7R4 \ No newline at end of file diff --git a/src/examples/groovy/simpleform/SimpleFormServer.groovy b/src/examples/groovy/simpleform/SimpleFormServer.groovy new file mode 100644 index 0000000..139e31a --- /dev/null +++ b/src/examples/groovy/simpleform/SimpleFormServer.groovy @@ -0,0 +1,39 @@ +import org.vertx.mods.formupload.MultipartRequest + +/* + * Copyright 2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +vertx.createHttpServer().requestHandler { req -> + if (req.uri.equals("/")) { + // Serve the index page + req.response.sendFile("index.html"); + } else if (req.uri.startsWith("/form")) { + req.response.setChunked(true); + MultipartRequest mpReq = new MultipartRequest(vertx, req); + + mpReq.attributeHandler() { attr -> + req.response.write("Got attr " + attr.name + " : " + attr.value + "\n"); + } + + req.endHandler() { + req.response.end(); + } + } else { + req.response.statusCode = 404; + req.response.end(); + } +}.listen(8080, "localhost") + diff --git a/src/examples/groovy/simpleform/index.html b/src/examples/groovy/simpleform/index.html new file mode 100644 index 0000000..46bd3e6 --- /dev/null +++ b/src/examples/groovy/simpleform/index.html @@ -0,0 +1,17 @@ + + + + Groovy test + + + +
+ foo:
+ bar:
+ quux:
+ +
+ + + \ No newline at end of file diff --git a/src/examples/groovy/simpleform/run_example.sh b/src/examples/groovy/simpleform/run_example.sh new file mode 100755 index 0000000..f90bc35 --- /dev/null +++ b/src/examples/groovy/simpleform/run_example.sh @@ -0,0 +1,4 @@ +# We set VERTX_MODS to point to where we built the module +export VERTX_MODS=$(readlink -f '../../../../../build/mod') +# And then we run the example +vertx run SimpleFormServer.groovy -includes vertx.formupload-v1.0 diff --git a/src/examples/groovy/simpleupload/SimpleUploadServer.groovy b/src/examples/groovy/simpleupload/SimpleUploadServer.groovy new file mode 100644 index 0000000..5428378 --- /dev/null +++ b/src/examples/groovy/simpleupload/SimpleUploadServer.groovy @@ -0,0 +1,39 @@ +import org.vertx.mods.formupload.MultipartRequest + +/* + * Copyright 2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +vertx.createHttpServer().requestHandler { req -> + if (req.uri.equals("/")) { + // Serve the index page + req.response.sendFile("index.html"); + } else if (req.uri.startsWith("/form")) { + MultipartRequest mpReq = new MultipartRequest(vertx, req); + mpReq.uploadHandler() { upload -> + upload.streamToDisk(upload.filename) { res -> + if (res.succeeded()) { + req.response.end("Upload successful, you should see the file in the server directory"); + } else { + req.response.end("Upload failed"); + } + } + } + } else { + req.response.statusCode = 404; + req.response.end(); + } +}.listen(8080, "localhost") + diff --git a/src/examples/groovy/simpleupload/index.html b/src/examples/groovy/simpleupload/index.html new file mode 100644 index 0000000..8763570 --- /dev/null +++ b/src/examples/groovy/simpleupload/index.html @@ -0,0 +1,15 @@ + + + + Groovy Test File Upload + + + +
+ choose a file to upload:
+ +
+ + + \ No newline at end of file diff --git a/src/examples/groovy/simpleupload/run_example.sh b/src/examples/groovy/simpleupload/run_example.sh new file mode 100755 index 0000000..2b4dd4d --- /dev/null +++ b/src/examples/groovy/simpleupload/run_example.sh @@ -0,0 +1,4 @@ +# We set VERTX_MODS to point to where we built the module +export VERTX_MODS=$(readlink -f '../../../../../build/mod') +# And then we run the example +vertx run SimpleUploadServer.groovy -includes vertx.formupload-v1.0 diff --git a/src/main/java/org/vertx/mods/formupload/Attribute.java b/src/main/java/org/vertx/mods/formupload/Attribute.java index 702053b..73b7f89 100644 --- a/src/main/java/org/vertx/mods/formupload/Attribute.java +++ b/src/main/java/org/vertx/mods/formupload/Attribute.java @@ -11,4 +11,9 @@ public Attribute(String name, String value) { this.name = name; this.value = value; } + + @Override + public String toString() { + return name + " - " + value; + } } diff --git a/src/main/java/org/vertx/mods/formupload/MultipartRequest.java b/src/main/java/org/vertx/mods/formupload/MultipartRequest.java index ca61140..accdc8d 100644 --- a/src/main/java/org/vertx/mods/formupload/MultipartRequest.java +++ b/src/main/java/org/vertx/mods/formupload/MultipartRequest.java @@ -16,8 +16,11 @@ import java.util.Map; import java.util.Set; +import groovy.lang.Closure; + /** * @author Tim Fox + * @author Paulo Lopes */ public class MultipartRequest { @@ -53,14 +56,36 @@ public void handle(Buffer data) { }); } + public MultipartRequest(org.vertx.groovy.core.Vertx vertx, org.vertx.groovy.core.http.HttpServerRequest req) { + this(vertx.toJavaVertx(), req.toJavaRequest()); + } + public void attributeHandler(Handler handler) { this.attrHandler = handler; } + public void attributeHandler(final Closure closure) { + this.attrHandler = new Handler() { + @Override + public void handle(final Attribute attribute) { + closure.call(attribute); + } + }; + } + public void uploadHandler(Handler handler) { this.uploadHandler = handler; } + public void uploadHandler(final Closure closure) { + this.uploadHandler = new Handler() { + @Override + public void handle(final Upload upload) { + closure.call(upload); + } + }; + } + public String getAttribute(String name) { return attributes.get(name); } @@ -244,12 +269,6 @@ private InternalMemoryAttribute(String name, String value) throws IOException { super(name, value); } - @Override - public void setContent(ChannelBuffer channelBuffer) throws IOException { - super.setContent(channelBuffer); - attributeCreated(); - } - @Override public void addContent(ChannelBuffer channelBuffer, boolean last) throws IOException { super.addContent(channelBuffer, last); @@ -259,14 +278,9 @@ public void addContent(ChannelBuffer channelBuffer, boolean last) throws IOExcep } void attributeCreated() { - if (!getName().equals("name")) { - // Netty has a habit of adding multiple extra attributes of name 'name' and value of the name of the - // real attribute, so we screen these out. - // This is however a problem - what if the user has a real attribute called 'name'? - attributes.put(getName(), getValue()); - if (attrHandler != null) { - attrHandler.handle(new Attribute(getName(), getValue())); - } + attributes.put(getName(), getValue()); + if (attrHandler != null) { + attrHandler.handle(new Attribute(getName(), getValue())); } } } diff --git a/src/main/java/org/vertx/mods/formupload/Upload.java b/src/main/java/org/vertx/mods/formupload/Upload.java index 32e3cb5..34cb3d6 100644 --- a/src/main/java/org/vertx/mods/formupload/Upload.java +++ b/src/main/java/org/vertx/mods/formupload/Upload.java @@ -12,6 +12,8 @@ import java.nio.charset.Charset; +import groovy.lang.Closure; + /** * @author Tim Fox */ @@ -51,6 +53,15 @@ public void dataHandler(Handler handler) { this.dataHandler = handler; } + public void dataHandler(final Closure closure) { + this.dataHandler = new Handler() { + @Override + public void handle(Buffer event) { + closure.call(event); + } + }; + } + public void pause() { req.pause(); paused = true; @@ -73,10 +84,22 @@ public void resume() { public void exceptionHandler(Handler handler) { } + public void exceptionHandler(final Closure closure) { + } + public void endHandler(Handler handler) { this.endHandler = handler; } + public void endHandler(final Closure closure) { + this.endHandler = new Handler() { + @Override + public void handle(Void event) { + closure.call(event); + } + }; + } + public void bodyHandler(final Handler handler) { final Buffer buff = new Buffer(); dataHandler = new Handler() { @@ -91,6 +114,15 @@ public void handle(Void event) { }; } + public void bodyHandler(final Closure closure) { + bodyHandler(new Handler() { + @Override + public void handle(Buffer event) { + closure.call(event); + } + }); + } + public void streamToDisk(String filename) { streamToDisk(filename, new AsyncResultHandler() { public void handle(AsyncResult event) { @@ -118,6 +150,15 @@ public void handle(Void event) { }); } + public void streamToDisk(String filename, final Closure closure) { + streamToDisk(filename, new AsyncResultHandler() { + @Override + public void handle(AsyncResult event) { + closure.call(event); + } + }); + } + protected void receiveData(Buffer data) { if (!paused) { if (dataHandler != null) { @@ -138,7 +179,4 @@ protected void complete() { endHandler.handle(null); } } - - - }