Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Groovy #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -104,4 +106,4 @@ task collectDeps(type: Copy) {
into("test") {
from configurations.testCompile
}
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
39 changes: 39 additions & 0 deletions src/examples/groovy/simpleform/SimpleFormServer.groovy
Original file line number Diff line number Diff line change
@@ -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")

17 changes: 17 additions & 0 deletions src/examples/groovy/simpleform/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Groovy test</title>
</head>
<body>

<form action="/form" ENCTYPE="multipart/form-data" method="POST" name="wibble">
foo:<input type="text" name="foo"/><br>
bar:<input type="text" name="bar"/><br>
quux:<input type="text" name="quux"/><br>
<input type="submit"/>
</form>

</body>
</html>
4 changes: 4 additions & 0 deletions src/examples/groovy/simpleform/run_example.sh
Original file line number Diff line number Diff line change
@@ -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
39 changes: 39 additions & 0 deletions src/examples/groovy/simpleupload/SimpleUploadServer.groovy
Original file line number Diff line number Diff line change
@@ -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")

15 changes: 15 additions & 0 deletions src/examples/groovy/simpleupload/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Groovy Test File Upload</title>
</head>
<body>

<form action="/form" ENCTYPE="multipart/form-data" method="POST" name="wibble">
choose a file to upload:<input type="file" name="myfile"/><br>
<input type="submit"/>
</form>

</body>
</html>
4 changes: 4 additions & 0 deletions src/examples/groovy/simpleupload/run_example.sh
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions src/main/java/org/vertx/mods/formupload/Attribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ public Attribute(String name, String value) {
this.name = name;
this.value = value;
}

@Override
public String toString() {
return name + " - " + value;
}
}
42 changes: 28 additions & 14 deletions src/main/java/org/vertx/mods/formupload/MultipartRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
import java.util.Map;
import java.util.Set;

import groovy.lang.Closure;

/**
* @author <a href="http://tfox.org">Tim Fox</a>
* @author <a href="http://www.jetdrone.com">Paulo Lopes</a>
*/
public class MultipartRequest {

Expand Down Expand Up @@ -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<Attribute> handler) {
this.attrHandler = handler;
}

public void attributeHandler(final Closure<Void> closure) {
this.attrHandler = new Handler<Attribute>() {
@Override
public void handle(final Attribute attribute) {
closure.call(attribute);
}
};
}

public void uploadHandler(Handler<Upload> handler) {
this.uploadHandler = handler;
}

public void uploadHandler(final Closure<Void> closure) {
this.uploadHandler = new Handler<Upload>() {
@Override
public void handle(final Upload upload) {
closure.call(upload);
}
};
}

public String getAttribute(String name) {
return attributes.get(name);
}
Expand Down Expand Up @@ -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);
Expand All @@ -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()));
}
}
}
Expand Down
44 changes: 41 additions & 3 deletions src/main/java/org/vertx/mods/formupload/Upload.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

import java.nio.charset.Charset;

import groovy.lang.Closure;

/**
* @author <a href="http://tfox.org">Tim Fox</a>
*/
Expand Down Expand Up @@ -51,6 +53,15 @@ public void dataHandler(Handler<Buffer> handler) {
this.dataHandler = handler;
}

public void dataHandler(final Closure<Void> closure) {
this.dataHandler = new Handler<Buffer>() {
@Override
public void handle(Buffer event) {
closure.call(event);
}
};
}

public void pause() {
req.pause();
paused = true;
Expand All @@ -73,10 +84,22 @@ public void resume() {
public void exceptionHandler(Handler<Exception> handler) {
}

public void exceptionHandler(final Closure<Void> closure) {
}

public void endHandler(Handler<Void> handler) {
this.endHandler = handler;
}

public void endHandler(final Closure<Void> closure) {
this.endHandler = new Handler<Void>() {
@Override
public void handle(Void event) {
closure.call(event);
}
};
}

public void bodyHandler(final Handler<Buffer> handler) {
final Buffer buff = new Buffer();
dataHandler = new Handler<Buffer>() {
Expand All @@ -91,6 +114,15 @@ public void handle(Void event) {
};
}

public void bodyHandler(final Closure<Void> closure) {
bodyHandler(new Handler<Buffer>() {
@Override
public void handle(Buffer event) {
closure.call(event);
}
});
}

public void streamToDisk(String filename) {
streamToDisk(filename, new AsyncResultHandler<Void>() {
public void handle(AsyncResult<Void> event) {
Expand Down Expand Up @@ -118,6 +150,15 @@ public void handle(Void event) {
});
}

public void streamToDisk(String filename, final Closure<Void> closure) {
streamToDisk(filename, new AsyncResultHandler<Void>() {
@Override
public void handle(AsyncResult<Void> event) {
closure.call(event);
}
});
}

protected void receiveData(Buffer data) {
if (!paused) {
if (dataHandler != null) {
Expand All @@ -138,7 +179,4 @@ protected void complete() {
endHandler.handle(null);
}
}



}