Skip to content

Commit 1f28f4e

Browse files
committed
MVC: multiple path values
1 parent a6e7320 commit 1f28f4e

File tree

6 files changed

+56
-28
lines changed

6 files changed

+56
-28
lines changed

jooby/src/main/java/io/jooby/annotations/GET.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,4 +225,11 @@
225225
@Retention(RetentionPolicy.RUNTIME)
226226
@Target(ElementType.METHOD)
227227
public @interface GET {
228+
String[] value() default {};
229+
230+
String[] path() default {};
231+
232+
String[] produces() default {};
233+
234+
String[] consumes() default {};
228235
}

jooby/src/main/java/io/jooby/internal/RouterImpl.java

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import javax.inject.Provider;
4545
import java.io.FileNotFoundException;
4646
import java.lang.reflect.AnnotatedElement;
47+
import java.lang.reflect.Method;
4748
import java.lang.reflect.Modifier;
4849
import java.nio.file.Path;
4950
import java.nio.file.Paths;
@@ -71,17 +72,12 @@ private static class Stack {
7172
private String pattern;
7273
private Executor executor;
7374
private List<Route.Decorator> filters = new ArrayList<>();
74-
private List<Renderer> renderers = new ArrayList<>();
7575
private List<Route.After> afters = new ArrayList<>();
7676

7777
public Stack(String pattern) {
7878
this.pattern = pattern;
7979
}
8080

81-
public void then(Renderer renderer) {
82-
renderers.add(renderer);
83-
}
84-
8581
public void then(Route.Decorator filter) {
8682
filters.add(filter);
8783
}
@@ -98,13 +94,8 @@ public Stream<Route.After> toAfter() {
9894
return afters.stream();
9995
}
10096

101-
public Stream<Renderer> toRenderer() {
102-
return renderers.stream();
103-
}
104-
10597
public void clear() {
10698
this.filters.clear();
107-
this.renderers.clear();
10899
this.afters.clear();
109100
executor = null;
110101
}
@@ -133,7 +124,6 @@ public Stack executor(Executor executor) {
133124
}
134125
};
135126

136-
137127
private ErrorHandler err;
138128

139129
private Map<String, StatusCode> errorCodes;
@@ -593,19 +583,21 @@ private void mvc(String prefix, Class type, Provider provider) {
593583
}
594584

595585
private void find(String prefix, Class type, Throwing.Consumer<MvcMethod> consumer) {
596-
String classPath = prefix == null ? path(type) : prefix + "/" + path(type);
597586
MvcMetadata mvcMetadata = new MvcMetadata(source);
598587
mvcMetadata.parse(type);
599588
List<MvcMethod> routes = new ArrayList<>();
600589
Stream.of(type.getDeclaredMethods())
601590
.forEach(method -> {
602591
if (Modifier.isPublic(method.getModifiers())) {
603592
mvcAnnotations.httpMethod(method).forEach(httpMethod -> {
604-
MvcMethod mvc = mvcMetadata.get(method);
605-
mvc.setPattern(classPath + "/" + path(method));
606-
mvc.setMethod(method);
607-
mvc.setHttpMethod(httpMethod);
608-
routes.add(mvc);
593+
String[] paths = pathPrefix(prefix, path(method));
594+
for (String path : paths) {
595+
MvcMethod mvc = mvcMetadata.create(method);
596+
mvc.setPattern(path);
597+
mvc.setMethod(method);
598+
mvc.setHttpMethod(httpMethod);
599+
routes.add(mvc);
600+
}
609601
});
610602
}
611603
});
@@ -614,9 +606,38 @@ private void find(String prefix, Class type, Throwing.Consumer<MvcMethod> consum
614606
mvcMetadata.destroy();
615607
}
616608

617-
private String path(AnnotatedElement type) {
618-
String path = mvcAnnotations.pathPattern(type);
619-
return path == null ? "" : path;
609+
private String[] pathPrefix(String prefix, String[] path) {
610+
if (prefix == null) {
611+
return path;
612+
}
613+
String[] result = new String[path.length];
614+
for (int i = 0; i < path.length; i++) {
615+
result[i] = prefix + "/" + path[i];
616+
}
617+
return result;
618+
}
619+
620+
private String[] path(Method m) {
621+
String[] path = mvcAnnotations.pathPattern(m);
622+
String[] root = mvcAnnotations.pathPattern(m.getDeclaringClass());
623+
if (root == null) {
624+
if (path == null) {
625+
return new String[] {"/"};
626+
}
627+
return path;
628+
}
629+
if (path == null) {
630+
return root;
631+
}
632+
String[] result = new String[root.length * path.length];
633+
int k = 0;
634+
for (String base : root) {
635+
for (String element : path) {
636+
result[k] = base + "/" + element;
637+
k += 1;
638+
}
639+
}
640+
return result;
620641
}
621642

622643
private void checkMvcAnnotations() {

jooby/src/main/java/io/jooby/internal/mvc/DefaultMvcAnnotation.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ public class DefaultMvcAnnotation implements MvcAnnotation {
6868
return Collections.singletonList(Path.class);
6969
}
7070

71-
@Override public String pathPattern(AnnotatedElement type) {
71+
@Override public String[] pathPattern(AnnotatedElement type) {
7272
Path path = type.getAnnotation(Path.class);
73-
return path == null ? null : path.value()[0];
73+
return path == null ? null : path.value();
7474
}
7575

7676
@Override public boolean isQueryParam(Parameter parameter) {

jooby/src/main/java/io/jooby/internal/mvc/JaxrsAnnotation.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ public class JaxrsAnnotation implements MvcAnnotation {
6464
return M_ANN;
6565
}
6666

67-
@Override public String pathPattern(AnnotatedElement type) {
67+
@Override public String[] pathPattern(AnnotatedElement type) {
6868
Path path = type.getAnnotation(Path.class);
69-
return path == null ? null : path.value();
69+
return path == null ? null : new String[]{path.value()};
7070
}
7171

7272
@Override public List<Class<? extends Annotation>> pathAnnotation() {

jooby/src/main/java/io/jooby/internal/mvc/MvcAnnotation.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public interface MvcAnnotation {
4545

4646
boolean isFormParam(Parameter parameter);
4747

48-
String pathPattern(AnnotatedElement type);
48+
String[] pathPattern(AnnotatedElement type);
4949

5050
default String paramName(AnnotatedElement type) {
5151
try {
@@ -108,8 +108,8 @@ static MvcAnnotation create(ClassLoader loader) {
108108
return methodAnnotations;
109109
}
110110

111-
@Override public String pathPattern(AnnotatedElement type) {
112-
String path = jaxrs.pathPattern(type);
111+
@Override public String[] pathPattern(AnnotatedElement type) {
112+
String[] path = jaxrs.pathPattern(type);
113113
return path == null ? def.pathPattern(type) : path;
114114
}
115115

jooby/src/main/java/io/jooby/internal/mvc/MvcMetadata.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public void parse(Class router) {
4343
reader.accept(visitor(metadata), 0);
4444
}
4545

46-
public MvcMethod get(Method method) {
46+
public MvcMethod create(Method method) {
4747
String key =
4848
Type.getType(method.getDeclaringClass()).getInternalName() + "." + method.getName() + Type
4949
.getMethodDescriptor(method);

0 commit comments

Comments
 (0)