Skip to content

Commit b1165c1

Browse files
committed
API change: Remove Http Verb with a plain String, rename Route.verb -> Route.method fix #54
1 parent cbbf369 commit b1165c1

23 files changed

Lines changed: 161 additions & 232 deletions

coverage-report/src/test/java/org/jooby/HeadRequestFeature.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
public class HeadRequestFeature extends ServerFeature {
77

88
{
9-
get("/", (req, rsp) -> rsp.send(req.route().verb()));
9+
get("/", (req, rsp) -> rsp.send(req.route().method()));
1010

1111
// custom head
1212
head("/head", (req, rsp) -> rsp.send(req.path()));

coverage-report/src/test/java/org/jooby/MvcMethodWithMultipleVerbsFeature.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public static class Resource {
1313
@POST
1414
@Path("/")
1515
public String getOrPost(final org.jooby.Request req) {
16-
return req.route().verb().toString();
16+
return req.route().method().toString();
1717
}
1818
}
1919

coverage-report/src/test/java/org/jooby/OptionsRequestFeature.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
public class OptionsRequestFeature extends ServerFeature {
77

88
{
9-
get("/", (req, rsp) -> rsp.send(req.route().verb()));
9+
get("/", (req, rsp) -> rsp.send(req.route().method()));
1010

11-
post("/", (req, rsp) -> rsp.send(req.route().verb()));
11+
post("/", (req, rsp) -> rsp.send(req.route().method()));
1212

13-
get("/sub", (req, rsp) -> rsp.send(req.route().verb()));
13+
get("/sub", (req, rsp) -> rsp.send(req.route().method()));
1414

15-
post("/sub", (req, rsp) -> rsp.send(req.route().verb()));
15+
post("/sub", (req, rsp) -> rsp.send(req.route().method()));
1616

17-
delete("/sub", (req, rsp) -> rsp.send(req.route().verb()));
17+
delete("/sub", (req, rsp) -> rsp.send(req.route().method()));
1818

1919
options();
2020
}

coverage-report/src/test/java/org/jooby/RouteReferenceFeature.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import static org.junit.Assert.assertEquals;
44
import static org.junit.Assert.assertNotNull;
55

6-
import org.jooby.Route;
76
import org.jooby.test.ServerFeature;
87
import org.junit.Test;
98

@@ -15,7 +14,7 @@ public class RouteReferenceFeature extends ServerFeature {
1514
Route route = req.route();
1615
assertNotNull(route);
1716
assertEquals("anonymous", route.name());
18-
assertEquals("GET", route.verb().name());
17+
assertEquals("GET", route.method());
1918
assertEquals("/", route.path());
2019
assertEquals("/", route.pattern());
2120
assertEquals("GET /\n" +
@@ -31,7 +30,7 @@ public class RouteReferenceFeature extends ServerFeature {
3130
Route route = req.route();
3231
assertNotNull(route);
3332
assertEquals("anonymous", route.name());
34-
assertEquals("GET", route.verb().name());
33+
assertEquals("GET", route.method());
3534
assertEquals("/" + req.param("var").value(), route.path());
3635
assertEquals("/:var", route.pattern());
3736
rsp.send("done");

coverage-report/src/test/java/org/jooby/TraceRequestFeature.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
public class TraceRequestFeature extends ServerFeature {
99

1010
{
11-
get("/", (req, rsp) -> rsp.send(req.route().verb()));
11+
get("/", (req, rsp) -> rsp.send(req.route().method()));
1212

13-
post("/", (req, rsp) -> rsp.send(req.route().verb()));
13+
post("/", (req, rsp) -> rsp.send(req.route().method()));
1414

15-
get("/sub", (req, rsp) -> rsp.send(req.route().verb()));
15+
get("/sub", (req, rsp) -> rsp.send(req.route().method()));
1616

1717
trace();
1818

jooby/src/main/java/org/jooby/Err.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public static class Default implements Err.Handler {
5555
@Override
5656
public void handle(final Request req, final Response rsp, final Exception ex)
5757
throws Exception {
58-
log.error("execution of: " + req.verb() + " " + req.path() + " resulted in exception", ex);
58+
log.error("execution of: " + req.method() + " " + req.path() + " resulted in exception", ex);
5959

6060
Map<String, Object> err = err(req, rsp, ex);
6161

jooby/src/main/java/org/jooby/Request.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ public String path() {
6969
}
7070

7171
@Override
72-
public Verb verb() {
73-
return req.verb();
72+
public String method() {
73+
return req.method();
7474
}
7575

7676
@Override
@@ -289,10 +289,10 @@ default String path() {
289289
}
290290

291291
/**
292-
* @return Current request verb.
292+
* @return HTTP method.
293293
*/
294-
default Verb verb() {
295-
return route().verb();
294+
default String method() {
295+
return route().method();
296296
}
297297

298298
/**

jooby/src/main/java/org/jooby/Route.java

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ class Definition {
390390
/**
391391
* A HTTP verb or <code>*</code>.
392392
*/
393-
private String verb;
393+
private String method;
394394

395395
/**
396396
* A path pattern.
@@ -447,25 +447,17 @@ public Definition(final @Nonnull String verb, final @Nonnull String pattern,
447447
/**
448448
* Creates a new route definition.
449449
*
450-
* @param verb A HTTP verb or <code>*</code>.
450+
* @param method A HTTP verb or <code>*</code>.
451451
* @param pattern A path pattern.
452452
* @param filter A callback to execute.
453453
*/
454-
public Definition(final @Nonnull String verb, final @Nonnull String pattern,
454+
public Definition(final @Nonnull String method, final @Nonnull String pattern,
455455
final @Nonnull Filter filter) {
456-
try {
457-
requireNonNull(verb, "HTTP verb is required.");
458-
Verb.valueOf(verb.toUpperCase());
459-
} catch (IllegalArgumentException ex) {
460-
if (!"*".equals(verb)) {
461-
throw new IllegalArgumentException("Invalid HTTP verb: " + verb);
462-
}
463-
}
464456
requireNonNull(pattern, "A route path is required.");
465457
requireNonNull(filter, "A filter is required.");
466458

467-
this.verb = verb.toUpperCase();
468-
this.compiledPattern = new RoutePattern(verb, pattern);
459+
this.method = method.toUpperCase();
460+
this.compiledPattern = new RoutePattern(method, pattern);
469461
// normalized pattern
470462
this.pattern = compiledPattern.pattern();
471463
this.filter = filter;
@@ -518,10 +510,10 @@ public Definition(final @Nonnull String verb, final @Nonnull String pattern,
518510
* @param accept The <code>Accept</code> header.
519511
* @return A route or an empty optional.
520512
*/
521-
public @Nonnull Optional<Route> matches(final @Nonnull Verb verb,
513+
public @Nonnull Optional<Route> matches(final @Nonnull String verb,
522514
final @Nonnull String path, final @Nonnull MediaType contentType,
523515
final @Nonnull List<MediaType> accept) {
524-
RouteMatcher matcher = compiledPattern.matcher(verb.name() + path);
516+
RouteMatcher matcher = compiledPattern.matcher(verb.toUpperCase() + path);
525517
if (matcher.matches()) {
526518
List<MediaType> result = MediaType.matcher(accept).filter(this.produces);
527519
if (result.size() > 0 && canConsume(contentType)) {
@@ -535,10 +527,10 @@ public Definition(final @Nonnull String verb, final @Nonnull String pattern,
535527
}
536528

537529
/**
538-
* @return HTTP verb or <code>*</code>
530+
* @return HTTP method or <code>*</code>.
539531
*/
540-
public @Nonnull String verb() {
541-
return verb;
532+
public @Nonnull String method() {
533+
return method;
542534
}
543535

544536
/**
@@ -695,7 +687,7 @@ public List<MediaType> produces() {
695687
@Override
696688
public String toString() {
697689
StringBuilder buffer = new StringBuilder();
698-
buffer.append(verb()).append(" ").append(pattern()).append("\n");
690+
buffer.append(method()).append(" ").append(pattern()).append("\n");
699691
buffer.append(" name: ").append(name()).append("\n");
700692
buffer.append(" consume: ").append(consumes()).append("\n");
701693
buffer.append(" produces: ").append(produces()).append("\n");
@@ -705,14 +697,14 @@ public String toString() {
705697
/**
706698
* Creates a new route.
707699
*
708-
* @param verb A HTTP verb.
700+
* @param method A HTTP verb.
709701
* @param matcher A route matcher.
710702
* @param produces List of produces types.
711703
* @return A new route.
712704
*/
713-
private Route asRoute(final Verb verb, final RouteMatcher matcher,
705+
private Route asRoute(final String method, final RouteMatcher matcher,
714706
final List<MediaType> produces) {
715-
return new RouteImpl(filter, verb, matcher.path(), pattern, name, matcher.vars(), consumes,
707+
return new RouteImpl(filter, method, matcher.path(), pattern, name, matcher.vars(), consumes,
716708
produces);
717709
}
718710

@@ -746,8 +738,8 @@ public String path() {
746738
}
747739

748740
@Override
749-
public Verb verb() {
750-
return route.verb();
741+
public String method() {
742+
return route.method();
751743
}
752744

753745
@Override
@@ -965,15 +957,32 @@ interface Chain {
965957
void next(@Nonnull Request req, @Nonnull Response rsp) throws Exception;
966958
}
967959

960+
/**
961+
* Well known HTTP methods.
962+
*/
963+
List<String> METHODS = ImmutableList.<String> builder()
964+
.add(
965+
"GET",
966+
"POST",
967+
"PUT",
968+
"DELETE",
969+
"PATCH",
970+
"HEAD",
971+
"CONNECT",
972+
"OPTIONS",
973+
"TRACE"
974+
)
975+
.build();
976+
968977
/**
969978
* @return Current request path.
970979
*/
971980
String path();
972981

973982
/**
974-
* @return Current request verb.
983+
* @return Current HTTP method.
975984
*/
976-
Verb verb();
985+
String method();
977986

978987
/**
979988
* @return The currently matched pattern.

jooby/src/main/java/org/jooby/Verb.java

Lines changed: 0 additions & 66 deletions
This file was deleted.

jooby/src/main/java/org/jooby/internal/AppPrinter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public String toString() {
5959
private void routes(final StringBuilder buffer) {
6060
int verbMax = 0, routeMax = 0, consumesMax = 0, producesMax = 0;
6161
for (Route.Definition route : routes) {
62-
verbMax = Math.max(verbMax, route.verb().length());
62+
verbMax = Math.max(verbMax, route.method().length());
6363

6464
routeMax = Math.max(routeMax, route.pattern().length());
6565

@@ -72,7 +72,7 @@ private void routes(final StringBuilder buffer) {
7272
+ producesMax + "s (%s)\n";
7373

7474
for (Route.Definition route : routes) {
75-
buffer.append(String.format(format, route.verb(), route.pattern(),
75+
buffer.append(String.format(format, route.method(), route.pattern(),
7676
route.consumes(), route.produces(), route.name()));
7777
}
7878

0 commit comments

Comments
 (0)