Skip to content
This repository was archived by the owner on Mar 3, 2026. It is now read-only.

Commit 9867c11

Browse files
committed
Replace Response.when with Response.format
* small fixes
1 parent b7905f0 commit 9867c11

11 files changed

Lines changed: 53 additions & 88 deletions

File tree

examples/src/main/java/jooby/MyApp.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,18 +209,17 @@ public class MyApp extends Jooby {
209209

210210
{
211211

212-
use(new HibernatePersistence(User.class));
213212
use(new Jackson());
214213
use(new Hbs());
215214

216-
get("/default/users", (req, resp) -> {
215+
get("/default/user/:id", (req, resp) -> {
217216

218217
EntityManager em = req.getInstance(EntityManager.class);
219218

220219
User user1 = new User();
221220
user1.setFirstName("edgar");
222221
user1.setLastName("espina");
223-
user1.setId("1");
222+
user1.setId(req.param("id").stringValue());
224223
em.persist(user1);
225224

226225
resp.send(user1);
@@ -233,6 +232,8 @@ public class MyApp extends Jooby {
233232
resp.send(em.createQuery("from User").getResultList());
234233
});
235234

235+
use(new HibernatePersistence(User.class));
236+
236237
}
237238

238239
public static void main(final String[] args) throws Exception {

jooby-core/src/main/java/jooby/ForwardingResponse.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
import java.util.Map;
1111
import java.util.Optional;
1212

13-
import jooby.fn.ExSupplier;
14-
1513
public class ForwardingResponse implements Response {
1614

1715
private Response response;
@@ -151,13 +149,8 @@ public void send(final Object body, final BodyConverter converter) throws Except
151149
}
152150

153151
@Override
154-
public ContentNegotiation when(final String type, final ExSupplier<Object> supplier) {
155-
return response.when(type, supplier);
156-
}
157-
158-
@Override
159-
public ContentNegotiation when(final MediaType type, final ExSupplier<Object> supplier) {
160-
return response.when(type, supplier);
152+
public Formatter format() {
153+
return response.format();
161154
}
162155

163156
@Override

jooby-core/src/main/java/jooby/Response.java

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import jooby.fn.ExSupplier;
1515

1616
import com.google.common.annotations.Beta;
17+
import com.sun.xml.internal.ws.client.ContentNegotiation;
1718

1819
/**
1920
* Give you access to the actual HTTP response. You can read/write headers and write HTTP body.
@@ -44,7 +45,7 @@ public interface Response {
4445
* @author edgar
4546
* @since 0.1.0
4647
*/
47-
interface ContentNegotiation {
48+
interface Formatter {
4849

4950
/**
5051
* Add a new when clause for a custom media-type.
@@ -54,7 +55,7 @@ interface ContentNegotiation {
5455
* @return A {@link ContentNegotiation}.
5556
*/
5657
@Nonnull
57-
default ContentNegotiation when(final String mediaType, final ExSupplier<Object> supplier) {
58+
default Formatter when(final String mediaType, final ExSupplier<Object> supplier) {
5859
return when(MediaType.valueOf(mediaType), supplier);
5960
}
6061

@@ -66,7 +67,7 @@ default ContentNegotiation when(final String mediaType, final ExSupplier<Object>
6667
* @return A {@link ContentNegotiation}.
6768
*/
6869
@Nonnull
69-
ContentNegotiation when(MediaType mediaType, ExSupplier<Object> supplier);
70+
Formatter when(MediaType mediaType, ExSupplier<Object> supplier);
7071

7172
/**
7273
* Write the response and send it.
@@ -172,23 +173,7 @@ default Response cookie(final String name, final String value) {
172173
*/
173174
void send(@Nonnull Object body, @Nonnull BodyConverter converter) throws Exception;
174175

175-
/**
176-
* Add a new when clause for a custom media-type.
177-
*
178-
* @param mediaType A media type to test for.
179-
* @param supplier An object provider.
180-
* @return A {@link ContentNegotiation}.
181-
*/
182-
@Nonnull ContentNegotiation when(String type, ExSupplier<Object> supplier);
183-
184-
/**
185-
* Add a new when clause for a custom media-type.
186-
*
187-
* @param mediaType A media type to test for.
188-
* @param supplier An object provider.
189-
* @return A {@link ContentNegotiation}.
190-
*/
191-
@Nonnull ContentNegotiation when(MediaType type, ExSupplier<Object> supplier);
176+
@Nonnull Formatter format();
192177

193178
default void redirect(final String location) throws Exception {
194179
redirect(HttpStatus.FOUND, location);

jooby-core/src/main/java/jooby/RouteDefinition.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,9 @@ private RouteDefinition(final String verb, final String pattern, final Filter fi
119119
requireNonNull(filter, "A filter is required.");
120120

121121
this.verb = verb;
122-
this.pattern = pattern;
123122
this.compiledPattern = new RoutePattern(verb, pattern);
123+
// normalized pattern
124+
this.pattern = compiledPattern.pattern();
124125
this.filter = filter;
125126
}
126127

@@ -143,11 +144,6 @@ public Optional<Route> matches(final String verb, final String path, final Media
143144
return Optional.empty();
144145
}
145146

146-
private Route asRoute(final RouteMatcher matcher, final List<MediaType> produces) {
147-
return new RouteImpl(filter, verb, matcher.path(), pattern, name, index, matcher.vars(), consumes,
148-
produces);
149-
}
150-
151147
public String verb() {
152148
return verb;
153149
}
@@ -252,4 +248,9 @@ public static RouteDefinition newRoute(final String verb, final String path,
252248
return new RouteDefinition(verb, path, filter);
253249
}
254250

251+
private Route asRoute(final RouteMatcher matcher, final List<MediaType> produces) {
252+
return new RouteImpl(filter, verb, matcher.path(), pattern, name, index, matcher.vars(), consumes,
253+
produces);
254+
}
255+
255256
}

jooby-core/src/main/java/jooby/internal/ResponseImpl.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -304,22 +304,14 @@ public void send(final Object message, final BodyConverter converter) throws Exc
304304
}
305305

306306
@Override
307-
public ContentNegotiation when(final String type, final ExSupplier<Object> supplier) {
308-
return when(MediaType.valueOf(type), supplier);
309-
}
310-
311-
@Override
312-
public ContentNegotiation when(final MediaType type, final ExSupplier<Object> supplier) {
307+
public Formatter format() {
313308
final Map<MediaType, ExSupplier<Object>> strategies = new LinkedHashMap<>();
314309
List<MediaType> types = new LinkedList<>();
315310

316-
strategies.put(type, supplier);
317-
types.add(type);
318-
319-
return new ContentNegotiation() {
311+
return new Formatter() {
320312

321313
@Override
322-
public ContentNegotiation when(final MediaType type, final ExSupplier<Object> supplier) {
314+
public Formatter when(final MediaType type, final ExSupplier<Object> supplier) {
323315
requireNonNull(type, "A media type is required.");
324316
requireNonNull(supplier, "A supplier fn is required.");
325317
strategies.put(type, supplier);

jooby-core/src/main/java/jooby/internal/RouteHandler.java

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public void handle(final HttpServletRequest request, final HttpServletResponse r
156156
// TODO: move me to an error handler feature
157157
Map<String, Object> model = errorModel(req, ex, status);
158158
try {
159-
res
159+
res.format()
160160
.when(MediaType.html, () -> Viewable.of("/status/" + status.value(), model))
161161
.when(MediaType.all, () -> model)
162162
.send();
@@ -401,26 +401,23 @@ private HttpException handle406or415(final String verb, final String path,
401401
@Override
402402
public String toString() {
403403
StringBuilder buffer = new StringBuilder();
404-
int routeMax = 0, consumesMax = 0, producesMax = 0;
405-
for (RouteDefinition routeDefinition : routeDefs) {
406-
int routeLen = routeDefinition.pattern().toString().length();
407-
if (routeLen > routeMax) {
408-
routeMax = routeLen;
409-
}
410-
//
411-
int consumeLen = routeDefinition.consumes().toString().length();
412-
if (consumeLen > consumesMax) {
413-
consumesMax = consumeLen;
414-
}
415-
int producesLen = routeDefinition.produces().toString().length();
416-
if (producesLen > producesMax) {
417-
producesMax = producesLen;
418-
}
404+
int verbMax = 0, routeMax = 0, consumesMax = 0, producesMax = 0;
405+
for (RouteDefinition routeDef : routeDefs) {
406+
verbMax = Math.max(verbMax, routeDef.verb().length());
407+
408+
routeMax = Math.max(routeMax, routeDef.pattern().length());
409+
410+
consumesMax = Math.max(consumesMax, routeDef.consumes().toString().length());
411+
412+
producesMax = Math.max(producesMax, routeDef.produces().toString().length());
419413
}
420-
String format = " %-" + routeMax + "s %" + consumesMax + "s %" + producesMax + "s\n";
421-
for (RouteDefinition routeDefinition : routeDefs) {
422-
buffer.append(String.format(format, routeDefinition.pattern(), routeDefinition.consumes(),
423-
routeDefinition.produces()));
414+
415+
String format = " %-" + verbMax + "s %-" + routeMax + "s %" + consumesMax + "s %"
416+
+ producesMax + "s (%s)\n";
417+
418+
for (RouteDefinition routeDef : routeDefs) {
419+
buffer.append(String.format(format, routeDef.verb(), routeDef.pattern(),
420+
routeDef.consumes(), routeDef.produces(), routeDef.name()));
424421
}
425422
return buffer.toString();
426423
}

jooby-core/src/main/java/jooby/internal/RoutePattern.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ public class RoutePattern {
2424
public RoutePattern(final String verb, final String pattern) {
2525
requireNonNull(verb, "A HTTP verb is required.");
2626
requireNonNull(pattern, "A path pattern is required.");
27-
this.pattern = pattern(verb, pattern);
28-
this.matcher = rewrite(this, this.pattern);
27+
this.pattern = normalize(pattern);
28+
this.matcher = rewrite(this, verb.toUpperCase() + this.pattern);
2929
}
3030

3131
public String pattern() {
@@ -103,8 +103,8 @@ private static String quote(final String s, final int start, final int end) {
103103
return Pattern.quote(s.substring(start, end));
104104
}
105105

106-
private static String pattern(final String verb, final String pattern) {
107-
StringBuilder buffer = new StringBuilder(verb.toUpperCase());
106+
private static String normalize(final String pattern) {
107+
StringBuilder buffer = new StringBuilder();
108108
if (pattern.equals("/")) {
109109
return buffer.append(pattern).toString();
110110
}

jooby-core/src/main/java/jooby/internal/mvc/MvcRoute.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import jooby.MediaType;
1111
import jooby.Request;
1212
import jooby.Response;
13-
import jooby.Response.ContentNegotiation;
1413
import jooby.Router;
1514
import jooby.Viewable;
1615
import jooby.fn.ExSupplier;
@@ -62,21 +61,17 @@ public void handle(final Request request, final Response response) throws Except
6261
ExSupplier<Object> notViewable = () -> result;
6362

6463
// viewable is apply when content type is text/html or accept header is size 1 matches text/html
65-
// and template annotiation is present.
64+
// and template annotation is present.
6665
boolean htmlLike = accept.size() == 1 && accept.get(0).matches(MediaType.html) &&
6766
router.getAnnotation(Template.class) != null;
6867
Function<MediaType, ExSupplier<Object>> provider =
6968
(type) -> MediaType.html.equals(type) || htmlLike ? viewable : notViewable;
7069

71-
ContentNegotiation negotiator = null;
72-
for (MediaType type : accept) {
73-
if (negotiator == null) {
74-
negotiator = response.when(type, provider.apply(type));
75-
} else {
76-
negotiator = negotiator.when(type, provider.apply(type));
77-
}
78-
}
79-
// enough, now send
80-
negotiator.send();
70+
Response.Formatter formatter = response.format();
71+
72+
// add formatters
73+
accept.forEach(type -> formatter.when(type, provider.apply(type)));
74+
// send it!
75+
formatter.send();
8176
}
8277
}

jooby-core/src/main/java/jooby/internal/mvc/Routes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public static List<RouteDefinition> routes(final Mode mode, final Class<?> route
9898
return RouteDefinition.newRoute(verb, path, new MvcRoute(m, provider))
9999
.produces(produces(m))
100100
.consumes(consumes(m))
101-
.name(routeClass.getName() + "." + m.getName());
101+
.name(routeClass.getSimpleName() + "." + m.getName());
102102
})
103103
.collect(Collectors.toList());
104104
}

jooby-hibernate/src/main/java/jooby/HibernatePersistence.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public void configure(final Mode mode, final Config config, final Binder binder)
8484
Multibinder<RouteDefinition> routes = Multibinder.newSetBinder(binder, RouteDefinition.class);
8585

8686
routes.addBinding()
87-
.toInstance(RouteDefinition.newRoute("*", "/**", readWriteTrx(key)));
87+
.toInstance(RouteDefinition.newRoute("*", "/**", readWriteTrx(key)).name("hibernate"));
8888

8989
Multibinder.newSetBinder(binder, RequestModule.class).addBinding().toInstance((b) -> {
9090
EntityManager em = emf.createEntityManager();

0 commit comments

Comments
 (0)