Skip to content

Commit 36295f5

Browse files
author
Joshua
committed
Important change to the Route and Router!
Route and Router does no longer require a "path" argument in their constructor! This will be set by using WebserverBuilder#addRoute(String, Route), WebserverBuilder#addRouter(String, Router) or Router#addRoute(String, Route) Removed Debug messages in Webserver#handle Made Webserver#handle compatible with the new Route and Router updated the version to 1.2 updated the README.md to explain the new Route and Router
1 parent 8745c5d commit 36295f5

7 files changed

Lines changed: 85 additions & 86 deletions

File tree

README.md

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ You can also download the newest version from the [releases](https://github.com/
1414
<dependency>
1515
<groupId>de.joshicodes</groupId>
1616
<artifactId>webapi</artifactId>
17-
<version>1.1.2</version>
17+
<version>1.2</version>
1818
</dependency>
1919
```
2020

@@ -34,17 +34,15 @@ To add a new route, create a new class that extends the `Route` class and add it
3434
The `Route` class has a constructor that takes a path as a parameter. The path is the path that the route will be available on.
3535

3636
```java
37+
// This will create a new route that is available on /testRoute
3738
builder.addRoute(
38-
// You can also create a new Route object and add it here
39-
40-
// This will create a new route that is available on /testRoute
41-
new Route("/testRoute") {
39+
"/testRoute",
40+
new Route() {
4241
@Override
4342
public ResponseData handle(RequestData request) {
4443
// Do something and return a ResponseData object
4544
}
4645
}
47-
4846
);
4947
```
5048
You can also add multiple routes at once using `builder.addRoutes(Route...)`.
@@ -54,27 +52,35 @@ To register a new Router, you can use `WebserverBuilder#addRouter(Router)`.
5452
The `Router` class has a constructor that takes a path as a parameter. The path is the base path that the router will be available on.
5553

5654
```java
57-
// This will create a new router that is available on /testRouter
58-
Router router = new Router("/testRouter");
55+
// This will create a new router without any path, that will be set by registering it to the builder
56+
// This router will be available on /testRouter
57+
Router router = new Router();
5958

6059
// This will create a new route that is available on /testRouter
6160
// If there is no Route for "/" in the Router, "/testRouter" will return a 404
62-
router.addRoute(new Route("/") {
63-
@Override
64-
public ResponseData handle(RequestData request) {
65-
// Do something and return a ResponseData object
66-
}
67-
});
61+
router.addRoute(
62+
"/",
63+
new Route() {
64+
@Override
65+
public ResponseData handle(RequestData request) {
66+
// Do something and return a ResponseData object
67+
}
68+
}
69+
);
6870

6971
// This will create a new route that is available on /testRouter/testRoute
70-
router.addRoute(new Route("/testRoute") {
71-
@Override
72-
public ResponseData handle(RequestData request) {
73-
// Do something and return a ResponseData object
74-
}
75-
});
72+
router.addRoute(
73+
"/testRoute",
74+
new Route() {
75+
@Override
76+
public ResponseData handle(RequestData request) {
77+
// Do something and return a ResponseData object
78+
}
79+
}
80+
);
7681

77-
builder.addRouter(router);
82+
// This will register the router to the builder
83+
builder.addRouter("/testRouter", router);
7884
```
7985

8086
The `Route` class has a method called `handle(RequestData request)`. This method is called when a request is made to the route. The `RequestData` object contains information about the request. The `handle` Method should return a ResponseData object.

pom.xml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>de.joshicodes</groupId>
88
<artifactId>webapi</artifactId>
9-
<version>1.1.2</version>
9+
<version>1.2</version>
1010

1111
<build>
1212
<plugins>
@@ -33,6 +33,15 @@
3333
<groupId>org.apache.maven.plugins</groupId>
3434
<artifactId>maven-javadoc-plugin</artifactId>
3535
<version>3.5.0</version>
36+
<executions>
37+
<execution>
38+
<phase>package</phase>
39+
<goals>
40+
<goal>javadoc</goal>
41+
<goal>jar</goal>
42+
</goals>
43+
</execution>
44+
</executions>
3645
<configuration>
3746
<source>17</source>
3847
<doclint>none</doclint>

src/main/java/de/joshicodes/webapi/Webserver.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ public class Webserver {
2424

2525
private HttpServer server;
2626

27-
private final List<Router> routers;
28-
private final List<Route> routes;
27+
private final HashMap<String, Router> routers;
2928
private final HashMap<Integer, ErrorRoute> errorHandlers;
3029

3130
private final String path;
@@ -34,7 +33,6 @@ public class Webserver {
3433
this.host = builder.getHost();
3534
this.port = builder.getPort();
3635
this.routers = builder.getRouters();
37-
this.routes = builder.getRoutes();
3836
this.errorHandlers = builder.getErrorHandlers();
3937
this.path = builder.getPath();
4038
create();
@@ -64,12 +62,14 @@ public void stop() {
6462

6563
private void handle(URI uri, HttpExchange exchange) throws IOException {
6664
// Check if there is a router for the path
67-
for (Router router : routers) {
68-
System.out.println(uri.getPath() + " " + router.getPath());
69-
if(uri.getPath().startsWith(router.getPath())) {
70-
String path = uri.getPath().replace(router.getPath(), "");
71-
if(!path.endsWith("/"))
72-
path = path.concat("/");
65+
for (String routePath : routers.keySet()) {
66+
Router router = routers.get(routePath);
67+
if(uri.getPath().startsWith(routePath)) {
68+
String path = uri.getPath().replace(routePath, "");
69+
if(!path.startsWith("/"))
70+
path = "/" + path; // Add slash if not present
71+
if(path.endsWith("/") && !path.equals("/"))
72+
path = path.substring(0, path.length() - 1); // Remove last slash
7373
Route request = router.getRoute(path);
7474
if(request == null) {
7575
continue;
@@ -81,13 +81,17 @@ private void handle(URI uri, HttpExchange exchange) throws IOException {
8181
}
8282
}
8383
// Check if there is a route for the path
84-
for (Route request : routes) {
85-
if (request.getPath().equals(uri.getPath())) {
84+
Router defaultRouter = routers.get("/");
85+
if(defaultRouter != null) {
86+
String path = uri.getPath();
87+
if(!path.startsWith("/"))
88+
path = "/" + path;
89+
if(defaultRouter.hasRoute(path)) {
90+
Route request = defaultRouter.getRoute(path);
8691
if(handleRoute(request, exchange)) {
8792
return;
8893
}
8994
}
90-
continue;
9195
}
9296
if(errorHandlers.containsKey(HttpErrorCode.NOT_FOUND)) {
9397
ResponseData response = errorHandlers.get(HttpErrorCode.NOT_FOUND).handle(new RequestData(exchange));

src/main/java/de/joshicodes/webapi/WebserverBuilder.java

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,36 @@
1111

1212
public class WebserverBuilder {
1313

14-
private final List<Router> routers;
15-
private final List<Route> routes;
14+
private final HashMap<String, Router> routers;
1615
private final HashMap<Integer, ErrorRoute> errorHandlers;
1716
private String host;
1817
private int port = -1;
1918

2019
private String path;
2120

21+
private Router defaultRouter;
22+
2223
public WebserverBuilder() {
23-
this.routers = new ArrayList<>();
24-
this.routes = new ArrayList<>();
24+
this.routers = new HashMap<>();
2525
this.errorHandlers = new HashMap<>();
2626
this.host = "0.0.0.0";
27-
path = null;
28-
}
29-
30-
public WebserverBuilder addRouter(Router router) {
31-
this.routers.add(router);
32-
return this;
27+
path = "/";
3328
}
3429

35-
public WebserverBuilder addRoute(Route route) {
36-
this.routes.add(route);
30+
public WebserverBuilder addRouter(String path, Router router) {
31+
if(!path.startsWith("/"))
32+
path = "/" + path;
33+
this.routers.put(path, router);
3734
return this;
3835
}
3936

40-
public WebserverBuilder addRoutes(Route... routes) {
41-
this.routes.addAll(Arrays.asList(routes));
37+
public WebserverBuilder addRoute(String path, Route route) {
38+
if(!path.startsWith("/"))
39+
path = "/" + path;
40+
if(defaultRouter == null)
41+
defaultRouter = new Router();
42+
defaultRouter.addRoute(path, route);
43+
routers.put(this.path, defaultRouter);
4244
return this;
4345
}
4446

@@ -76,14 +78,10 @@ public int getPort() {
7678
return port;
7779
}
7880

79-
public List<Router> getRouters() {
81+
public HashMap<String, Router> getRouters() {
8082
return routers;
8183
}
8284

83-
public List<Route> getRoutes() {
84-
return routes;
85-
}
86-
8785
public HashMap<Integer, ErrorRoute> getErrorHandlers() {
8886
return errorHandlers;
8987
}

src/main/java/de/joshicodes/webapi/router/Router.java

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,50 +4,40 @@
44

55
import java.util.ArrayList;
66
import java.util.Arrays;
7+
import java.util.HashMap;
78
import java.util.List;
89

910
public class Router {
1011

11-
private final String path;
12-
private final List<Route> routes;
12+
private final HashMap<String, Route> routes;
1313

14-
public Router(String path) {
15-
this.path = path;
16-
this.routes = new ArrayList<>();
14+
public Router() {
15+
this.routes = new HashMap<>();
1716
}
1817

19-
public void addRoute(Route route) {
20-
this.routes.add(route);
18+
public void addRoute(String path, Route route) {
19+
this.routes.put(path, route);
2120
}
2221

23-
public void addRoutes(Route... routes) {
24-
this.routes.addAll(Arrays.asList(routes));
22+
public void addRoutes(HashMap<String, Route> routes) {
23+
this.routes.putAll(routes);
2524
}
2625

27-
public String getPath() {
28-
return path;
29-
}
30-
31-
public List<Route> getRoutes() {
26+
public HashMap<String, Route> getRoutes() {
3227
return routes;
3328
}
3429

35-
public boolean hasRoute(String path) {
36-
for (Route route : routes) {
37-
if (route.getPath().equals(path)) {
30+
public boolean hasRoute(String requestPath) {
31+
for (String path : routes.keySet()) {
32+
if (path.equalsIgnoreCase(requestPath)) {
3833
return true;
3934
}
4035
}
4136
return false;
4237
}
4338

4439
public Route getRoute(String path) {
45-
for (Route route : routes) {
46-
if (route.getPath().equals(path)) {
47-
return route;
48-
}
49-
}
50-
return null;
40+
return routes.get(path);
5141
}
5242

5343
}

src/main/java/de/joshicodes/webapi/router/route/ErrorRoute.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
public abstract class ErrorRoute extends Route {
44

55
public ErrorRoute() {
6-
super(null);
6+
super();
77
}
88

99
}

src/main/java/de/joshicodes/webapi/router/route/Route.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,8 @@
55

66
public abstract class Route {
77

8-
private String path;
9-
10-
public Route(String path) {
11-
this.path = path;
12-
}
8+
public Route() {}
139

1410
abstract public ResponseData handle(RequestData request);
1511

16-
public String getPath() {
17-
return path;
18-
}
19-
2012
}

0 commit comments

Comments
 (0)