Permalink
Browse files

refactoring

  • Loading branch information...
1 parent f31496d commit 08899d7f861584b471d840ae4401be1f6f6f9799 @alextkachman alextkachman committed Jul 1, 2011
View
10 Examples/Examples.iml
@@ -1,5 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
+ <component name="FacetManager">
+ <facet type="scala" name="Scala">
+ <configuration>
+ <option name="compilerLibraryLevel" value="Global" />
+ <option name="compilerLibraryName" value="scala-compiler-2.8.1" />
+ </configuration>
+ </facet>
+ </component>
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
@@ -8,7 +16,7 @@
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module" module-name="gretty" exported="" />
- <orderEntry type="library" name="groovy-1.8.0" level="project" />
+ <orderEntry type="module" module-name="gretty-scala" />
</component>
</module>
View
4 Examples/helloworld/JavaSimpleHttpServer.java
@@ -26,14 +26,14 @@ public static void main(String[] args) {
.localAddress(new InetSocketAddress("localhost", 8080))
.dir("web")
.defaultHandler(new GrettyHttpHandler() {
- public void handle(Map<String, String> pathArguments) {
+ public void handle() {
getResponse().redirect("/");
}
})
.handler("/", new GrettyRestDescription() {
public void run() {
get(new GrettyHttpHandler() {
- public void handle(Map<String, String> pathArguments) {
+ public void handle() {
Map binding = new HashMap();
binding.put("title", "Hello, World!");
binding.put("message", "Hello, Java World");
View
4 build.gradle
@@ -16,8 +16,8 @@
apply plugin: 'idea'
-def _version = '0.4.268'
-def _gppVersion = '0.4.268_1.8.0'
+def _version = '0.4.279'
+def _gppVersion = '0.4.279_1.8.0'
Set groovyProjects = ['gretty', 'gretty-hazelcast']
View
2 gretty-scala/gretty-scala.iml
@@ -26,7 +26,7 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
- <orderEntry type="library" name="scala-library-2.8.1" level="application" />
+ <orderEntry type="library" exported="" name="scala-library-2.8.1" level="application" />
<orderEntry type="module-library" exported="">
<library>
<CLASSES>
View
83 gretty-scala/src/org/mbte/gretty/ScalaSimpleHttpServer.scala
@@ -0,0 +1,83 @@
+/*
+ * Copyright 2009-2010 MBTE Sweden AB.
+ *
+ * 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.
+ */
+
+package org.mbte.gretty
+
+/*
+* Copyright 2009-2010 MBTE Sweden AB.
+*
+* 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.
+*/
+
+import java.net.InetSocketAddress
+import java.util.HashMap
+import java.util.Map
+import org.mbte.gretty.httpserver._
+
+object ScalaSimpleHttpServer {
+ def main(args: Array[String]): Unit = {
+ val server: GrettyServer = new GrettyServer().
+ localAddress(new InetSocketAddress("localhost", 8080)).
+ dir("web").
+ defaultHandler(new GrettyHttpHandler() {
+ def handle() : Unit = {
+ getResponse.redirect("/")
+ }
+ }
+ ).
+ handler("/", new GrettyRestDescription {
+ def run: Unit = {
+ get(new GrettyHttpHandler {
+ def handle(): Unit = {
+ val binding = new HashMap[String,String]
+ binding.put("title", "Hello, World!")
+ binding.put("message", "Hello, Java World")
+ getResponse.setHtml(template("web/templates/main.gpptl", binding))
+ }
+ })
+ }
+ }).
+ webContext("/myapp", new GrettyContext("myapp"))
+
+ server.start
+
+ var response: GrettyHttpResponse = null
+ response = server.doTest("/nosuchurl")
+ Predef.assert(response.getStatus.getCode == 301)
+ Predef.assert(response.getHeader("Location") == "/")
+
+ response = server.doTest("/")
+ Predef.assert(response.getStatus.getCode == 200)
+
+ var respBody = response.getContentText
+ Predef.assert(respBody.contains("<title>Hello, World!</title>"))
+ Predef.assert(respBody.contains("Hello, Dynamic World!(from template)<br>"))
+ Predef.assert(respBody.contains("<center>HEADER</center>"))
+ Predef.assert(respBody.contains("<center>FOOTER</center>"))
+
+ server.stop
+ }
+}
View
6 gretty/gretty.iml
@@ -29,11 +29,11 @@
<orderEntry type="module-library" exported="">
<library>
<CLASSES>
- <root url="jar://$USER_HOME$/.gradle/cache/org.mbte.groovypp/groovypp/jars/groovypp-0.4.268_1.8.0.jar!/" />
+ <root url="jar://$USER_HOME$/.gradle/cache/org.mbte.groovypp/groovypp/jars/groovypp-0.4.278_1.8.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
- <root url="jar://$USER_HOME$/.gradle/cache/org.mbte.groovypp/groovypp/sources/groovypp-0.4.268_1.8.0-sources.jar!/" />
+ <root url="jar://$USER_HOME$/.gradle/cache/org.mbte.groovypp/groovypp/sources/groovypp-0.4.278_1.8.0-sources.jar!/" />
</SOURCES>
</library>
</orderEntry>
@@ -146,7 +146,7 @@
</SOURCES>
</library>
</orderEntry>
- <orderEntry type="module-library" scope="TEST">
+ <orderEntry type="module-library" exported="" scope="RUNTIME">
<library>
<CLASSES>
<root url="jar://$USER_HOME$/.gradle/cache/commons-cli/commons-cli/jars/commons-cli-1.1.jar!/" />
View
6 gretty/src/org/mbte/gretty/httpclient/HttpRequestHelper.groovy
@@ -14,6 +14,8 @@
* limitations under the License.
*/
+
+
package org.mbte.gretty.httpclient
import org.jboss.netty.channel.local.LocalAddress
@@ -22,7 +24,9 @@ import groovypp.concurrent.BindLater
import org.jboss.netty.handler.codec.http.HttpResponse
import org.mbte.gretty.httpserver.GrettyHttpRequest
import org.mbte.gretty.httpserver.GrettyHttpResponse
+import sun.security.util.Debug
+@Typed
@Trait abstract class HttpRequestHelper {
SocketAddress getTestServerAddress () {
@@ -93,3 +97,5 @@ import org.mbte.gretty.httpserver.GrettyHttpResponse
res
}
}
+
+//'lala'
View
9 gretty/src/org/mbte/gretty/httpserver/GrettyContext.groovy
@@ -16,6 +16,8 @@
+
+
package org.mbte.gretty.httpserver
import org.jboss.netty.handler.codec.http.HttpHeaders
@@ -168,14 +170,17 @@ import org.jboss.netty.logging.InternalLogger
for(matcher in methodHandlers) {
def pathArgs = matcher.doesMatch(localUri)
if(pathArgs != null) {
- matcher.handler.handle(request, response, pathArgs)
+ for(e in pathArgs.entrySet()) {
+ request.parameters[e.key] = e.value
+ }
+ matcher.handler.handle(request, response)
return
}
}
// own default handler (maybe delegates to parent default)
if (defaultHandler) {
- defaultHandler.handle(request, response, Collections.emptyMap())
+ defaultHandler.handle(request, response)
return
}
View
14 gretty/src/org/mbte/gretty/httpserver/GrettyHttpHandler.groovy
@@ -16,6 +16,8 @@
+
+
package org.mbte.gretty.httpserver
import org.mbte.gretty.JacksonCategory
@@ -47,8 +49,8 @@ abstract class GrettyHttpHandler implements Cloneable {
closure.resolveStrategy = Closure.DELEGATE_FIRST
}
- void handle(Map<String, String> pathArguments) {
- closure(pathArguments)
+ void handle() {
+ closure()
}
protected def clone() {
@@ -84,15 +86,17 @@ abstract class GrettyHttpHandler implements Cloneable {
new GrettyHttpHandlerAroundClosure(closure)
}
- final void handle (GrettyHttpRequest request, GrettyHttpResponse response, Map<String,String> pathArgs) {
+ abstract void handle()
+
+ final void handle (GrettyHttpRequest request, GrettyHttpResponse response) {
GrettyHttpHandler clone = clone ()
clone.request = request
clone.response = response
def saved = grettyHandler.get()
grettyHandler.set(clone)
try {
- clone.handle (pathArgs)
+ clone.handle()
}
finally {
grettyHandler.set(saved)
@@ -153,8 +157,6 @@ abstract class GrettyHttpHandler implements Cloneable {
}
}
- abstract void handle(Map<String,String> pathArguments)
-
GrettySession getSession(boolean force = true) {
def res = response.session
if(!res) {
View
15 gretty/src/org/mbte/gretty/httpserver/GrettyHttpRequest.groovy
@@ -16,6 +16,8 @@
+
+
package org.mbte.gretty.httpserver
import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.*
@@ -30,7 +32,7 @@ import org.mbte.gretty.JacksonCategory
class GrettyHttpRequest extends DefaultHttpRequest {
private String path
- private Map<String, List<String>> params
+ private Map<String, String> params
private boolean followRedirects = false
Set<Cookie> cookies
@@ -60,9 +62,14 @@ class GrettyHttpRequest extends DefaultHttpRequest {
path
}
- Map<String, List<String>> getParameters() {
- if(params == null)
- params = new QueryStringDecoder(uri).parameters
+ Map<String, String> getParameters() {
+ if(params == null) {
+ def decoded = new QueryStringDecoder(uri).parameters
+ params = new LinkedHashMap()
+ for (e in decoded.entrySet()) {
+ params[e.key] = e.value.get(0)
+ }
+ }
params
}
View
6 gretty/src/org/mbte/gretty/httpserver/GrettyServer.groovy
@@ -14,6 +14,8 @@
* limitations under the License.
*/
+
+
package org.mbte.gretty.httpserver
import org.jboss.netty.channel.*
@@ -153,6 +155,10 @@ import org.mbte.gretty.httpclient.HttpRequestHelper
defaultContext.webContexts = webContexts
}
+ GrettyServer webContexts(Map<String,GrettyContext> webContexts) {
+ this[webContexts: webContexts]
+ }
+
void start () {
if(!defaultContext)
throw new IllegalStateException("No root context configured")
View
8 gretty/tests/org/mbte/gretty/httpserver/ContextTest.groovy
@@ -16,6 +16,8 @@
+
+
package org.mbte.gretty.httpserver
import org.jboss.netty.channel.local.LocalAddress
@@ -45,13 +47,13 @@ import sun.security.util.Debug
"/api2/:command": {
get {
- response.html = "api2: ${it.command}"
+ response.html = "api2: ${request.parameters.command}"
}
},
public: {
- get("/api/:command") { args ->
- response.html = "api: ${args.command}"
+ get("/api/:command") {
+ response.html = "api: ${request.parameters.command}"
}
}
]
View
28 gretty/tests/org/mbte/gretty/httpserver/ProxyTest.groovy
@@ -1,17 +1,17 @@
/*
- * Copyright 2009-2011 MBTE Sweden AB.
+ * Copyright 2009-2010 MBTE Sweden AB.
*
- * 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
+ * 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
+ * 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.
+ * 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.
*/
package org.mbte.gretty.httpserver
@@ -56,8 +56,8 @@ import org.mbte.gretty.httpclient.HttpRequestHelper
"/" : [
public: {
get("/data/:mapId/set/:objectId") {
- response.addHeader("mapId", it.mapId)
- response.addHeader("objectId", it.objectId)
+ response.addHeader("mapId", request.parameters.mapId)
+ response.addHeader("objectId", request.parameters.objectId)
}
get("/data/") {}
@@ -98,8 +98,8 @@ import org.mbte.gretty.httpclient.HttpRequestHelper
assertEquals "default: path: /data", text
assertEquals "true", response.getHeader("Default")
- assertEquals "[12]", response.getHeader("msg")
- assertEquals "[33]", response.getHeader("value")
+ assertEquals "12", response.getHeader("msg")
+ assertEquals "33", response.getHeader("value")
}
}
View
10 gretty/tests/org/mbte/gretty/httpserver/RestTest.groovy
@@ -14,6 +14,8 @@
* limitations under the License.
*/
+
+
package org.mbte.gretty.httpserver
import org.jboss.netty.channel.local.LocalAddress
@@ -37,16 +39,16 @@ import org.mbte.gretty.httpclient.HttpRequestHelper
public: {
rest("/data/:mapId/:objectId") {
get {
- response.addHeader("mapId", it.mapId)
- response.addHeader("objectId", it.objectId)
+ response.addHeader("mapId", request.parameters.mapId)
+ response.addHeader("objectId", request.parameters.objectId)
response.addHeader("method", "get")
response.text = "OK"
response.status = HttpResponseStatus.OK
}
post {
- response.addHeader("mapId", it.mapId)
- response.addHeader("objectId", it.objectId)
+ response.addHeader("mapId", request.parameters.mapId)
+ response.addHeader("objectId", request.parameters.objectId)
response.addHeader("method", "post")
response.text = request.content.asString().toUpperCase()
response.status = HttpResponseStatus.OK
View
12 gretty/tests/org/mbte/gretty/httpserver/ServerTest.groovy
@@ -14,6 +14,8 @@
* limitations under the License.
*/
+
+
package org.mbte.gretty.httpserver
import org.jboss.netty.channel.local.LocalAddress
@@ -32,8 +34,8 @@ import org.mbte.gretty.httpclient.HttpRequestHelper
"/" : [
public: {
get("/data/:mapId/set/:objectId") {
- response.addHeader("mapId", it.mapId)
- response.addHeader("objectId", it.objectId)
+ response.addHeader("mapId", request.parameters.mapId)
+ response.addHeader("objectId", request.parameters.objectId)
}
get("/data/") {}
@@ -46,7 +48,7 @@ import org.mbte.gretty.httpclient.HttpRequestHelper
default: {
response.addHeader "Default", "true"
for(p in request.parameters.entrySet())
- response.addHeader(p.key, p.value.toString())
+ response.addHeader(p.key, p.value)
response.text = "default: path: ${request.path}"
}
]
@@ -67,8 +69,8 @@ import org.mbte.gretty.httpclient.HttpRequestHelper
assertEquals "default: path: /data", text
assertEquals "true", response.getHeader("Default")
- assertEquals "[12]", response.getHeader("msg")
- assertEquals "[33]", response.getHeader("value")
+ assertEquals "12", response.getHeader("msg")
+ assertEquals "33", response.getHeader("value")
}
}
View
4 gretty/tests/org/mbte/gretty/httpserver/TemplateTest.groovy
@@ -16,6 +16,8 @@
+
+
package org.mbte.gretty.httpserver
import org.jboss.netty.handler.codec.http.HttpMethod
@@ -36,7 +38,7 @@ Request path: \${request.uri} URI: \${uri ?: request.uri.toUpperCase() }\
scriptFile = File.createTempFile("temp_", "_script.groovy", root)
scriptFile.text = """\
- print "issue: \${request.parameters.issue[0]} reporter: \${request.parameters.reporter[0]}"
+ print "issue: \${request.parameters.issue} reporter: \${request.parameters.reporter}"
"""
scriptFile.deleteOnExit()
View
2 settings.gradle
@@ -14,4 +14,4 @@
* limitations under the License.
*/
-include 'gretty', 'gretty-hazelcast', 'gretty-scala'
+include 'gretty', 'gretty-hazelcast' /*, 'gretty-scala' */

0 comments on commit 08899d7

Please sign in to comment.