Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added javadoc and license

  • Loading branch information...
commit 60ae415331e0d21e5bc29f1e90e8a39190d72d0d 1 parent 97a0397
Per Wendel authored
View
13 LICENSE
@@ -0,0 +1,13 @@
+ Copyright 2011 Per Wendel
+
+ 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.
View
72 README
@@ -1,78 +1,24 @@
Spark - a Sinatra inspired micro web framework
----------------------------------------------
-More documentation is on the way!
-
-Simple example:
----------------
+Getting started:
-import spark.Request;
-import spark.Response;
-import spark.Route;
-import spark.Spark;
+import spark.*;
-/**
- * A simple example just showing some basic functionality
- */
-public class SimpleExample {
-
+public class HelloWorld {
public static void main(String[] args) {
-
- // Spark.setPort(5678); <- Uncomment this if you wan't spark to listen on a port different than 4567.
-
Spark.get(new Route("/hello") {
@Override
public Object handle(Request request, Response response) {
return "Hello World!";
}
});
-
- Spark.post(new Route("/hello") {
- @Override
- public Object handle(Request request, Response response) {
- return "Hello World: " + request.body();
- }
- });
-
- Spark.get(new Route("/private") {
- @Override
- public Object handle(Request request, Response response) {
- response.status(401);
- return "Go Away!!!";
- }
-
- });
-
- Spark.get(new Route("/users/:name") {
- @Override
- public Object handle(Request request, Response response) {
- return "Selected user: " + request.params(":name");
- }
- });
-
- Spark.get(new Route("/news/:section") {
- @Override
- public Object handle(Request request, Response response) {
- response.type("text/xml");
- return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><news>" + request.params("section") + "</news>";
- }
- });
-
- Spark.get(new Route("/redirect") {
- @Override
- public Object handle(Request request, Response response) {
- response.redirect("/news/world");
- return null;
- }
- });
-
- Spark.get(new Route("/") {
- @Override
- public Object handle(Request request, Response response) {
- return "root";
- }
- });
-
}
}
+View at: http://localhost:4567/hello
+
+More documentation is on the way!
+
+Check out and try the examples in the source code.
+
View
48 src/main/java/spark/Route.java
@@ -9,51 +9,41 @@
**************************************************************/
package spark;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
-import org.apache.log4j.Logger;
-
-import spark.route.RouteMatch;
/**
- *
+ * A Route is built up by a path (for url-matching) and the implementation of the 'handle' method.
+ * When a request is made, if present, the matching routes 'handle' method is invoked.
*
* @author Per Wendel
*/
public abstract class Route {
- private static Logger LOG = Logger.getLogger(Route.class);
-
-// private HttpServletResponse response;
-
- protected Request request;
-
- private String route;
+ private String path;
- protected Route(String route) {
- this.route = route;
+ /**
+ * Constructor
+ *
+ * @param path The route path which is used for matching. (e.g. /hello, users/:name)
+ */
+ protected Route(String path) {
+ this.path = path;
}
/**
- * TODO: javadoc
- * @return
+ * Invoked when a request is made on this route's corresponding path e.g. '/hello'
+ *
+ * @param request The request object providing information about the HTTP request
+ * @param response The response object providing functionality for modifying the response
+ *
+ * @return The content to be set in the response
*/
public abstract Object handle(Request request, Response response);
- String getRoute() {
- return this.route;
- }
-
-
/**
- * Sets the needed information
+ * Returns this route's path
*/
- public final void set(RouteMatch match, HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
- request = new Request(match, servletRequest);
- Response response = new Response(servletResponse);
-// this.response = response;
+ String getPath() {
+ return this.path;
}
-
}
View
40 src/main/java/spark/Spark.java
@@ -15,10 +15,7 @@
import spark.webserver.SparkServer;
import spark.webserver.SparkServerFactory;
-// get, post, put, delete, head, trace, connect, options
/**
- * TODO: Routes are matched in the order they are defined. The rirst route that matches the request is invoked. ???
- *
* In Spark, a route is an HTTP method paired with a URL-matching pattern. Each route is associated with a runction:
*
* TODO: Add author on all files, maybe apache2 license text!!!, javadoc
@@ -79,7 +76,7 @@ public static void options(Route route) {
private static void addRoute(String httpMethod, Route route) {
init();
- routeMatcher.parseValidateAddRoute(httpMethod + " '" + route.getRoute() + "'", route);
+ routeMatcher.parseValidateAddRoute(httpMethod + " '" + route.getPath() + "'", route);
}
private synchronized static final void init() {
@@ -96,4 +93,39 @@ public void run() {
}
}
+ /*
+ * TODO: discover new TODOs.
+ *
+ * TODO: Before method for filters...check sinatra page
+ *
+ * TODO: Make available as maven dependency, upload on repo etc...
+ * TODO: Add *, splat possibility
+ * TODO: Add validation of routes, invalid characters and stuff, also validate parameters, check static, ONGOING
+ *
+ * TODO: Javadoc
+ *
+ * TODO: Create maven archetype, "ONGOING"
+ * TODO: Add cache-control helpers
+ *
+ * advanced TODO list:
+ * TODO: sessions? (use session servlet context?)
+ * TODO: Add regexp URIs
+ * TODO: Routes are matched in the order they are defined. The rirst route that matches the request is invoked. ???
+ *
+ * Ongoing
+ *
+ * Done
+ * TODO: Setting Headers
+ * TODO: Do we want get-prefixes for all *getters* or do we want a more ruby like approach??? (Maybe have two choices?)
+ * TODO: Setting Body, Status Code
+ * TODO: Add possibility to set content type on return, DONE
+ * TODO: Add possibility to access HttpServletContext in method impl, DONE
+ * TODO: Redirect func in web context, DONE
+ * TODO: Refactor, extract interfaces, DONE
+ * TODO: Figure out a nice name, DONE - SPARK
+ * TODO: Add /uri/{param} possibility, DONE
+ * TODO: Tweak log4j config, DONE
+ * TODO: Query string in web context, DONE
+ * TODO: Add URI-param fetching from webcontext ie. ?param=value&param2=...etc, AND headers, DONE
+ */
}
View
14 src/main/java/spark/examples/hello/HelloWorld.java
@@ -0,0 +1,14 @@
+package spark.examples.hello;
+
+import spark.*;
+
+public class HelloWorld {
+ public static void main(String[] args) {
+ Spark.get(new Route("/hello") {
+ @Override
+ public Object handle(Request request, Response response) {
+ return "Hello World!";
+ }
+ });
+ }
+}
View
34 src/main/java/spark/webserver/MatcherFilter.java
@@ -31,38 +31,7 @@
import spark.route.RouteMatcher;
/**
- * TODO: discover new TODOs.
- *
- * TODO: Before method for filters...check sinatra page
- *
- * TODO: Make available as maven dependency, upload on repo etc...
- * TODO: Add *, splat possibility
- * TODO: Add validation of routes, invalid characters and stuff, also validate parameters, check static, ONGOING
- *
- * TODO: Javadoc
- *
- * TODO: Create maven archetype, "ONGOING"
- * TODO: Add cache-control helpers
- *
- * advanced TODO list:
- * TODO: sessions? (use session servlet context?)
- * TODO: Add regexp URIs
- *
- * Ongoing
- *
- * Done
- * TODO: Setting Headers
- * TODO: Do we want get-prefixes for all *getters* or do we want a more ruby like approach??? (Maybe have two choices?)
- * TODO: Setting Body, Status Code
- * TODO: Add possibility to set content type on return, DONE
- * TODO: Add possibility to access HttpServletContext in method impl, DONE
- * TODO: Redirect func in web context, DONE
- * TODO: Refactor, extract interfaces, DONE
- * TODO: Figure out a nice name, DONE - SPARK
- * TODO: Add /uri/{param} possibility, DONE
- * TODO: Tweak log4j config, DONE
- * TODO: Query string in web context, DONE
- * TODO: Add URI-param fetching from webcontext ie. ?param=value&param2=...etc, AND headers, DONE
+ *
*
* @author Per Wendel
*/
@@ -106,7 +75,6 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo
Route route = ((Route) target);
Request request = RequestResponseFactory.create(match, httpRequest);
Response response = RequestResponseFactory.create(httpResponse);
- route.set(match, httpRequest, httpResponse);
result = route.handle(request, response);
}
if (result != null) {
Please sign in to comment.
Something went wrong with that request. Please try again.