Browse Source

Back to parsing by hand, since invalid inputs are not processed by Jersey how the test expects

denka 10 years ago
parent
commit
53981eec83
1 changed files with 13 additions and 3 deletions
  1. 13 3
      frameworks/Java/grizzly-jersey/src/main/java/hello/DbResource.java

+ 13 - 3
frameworks/Java/grizzly-jersey/src/main/java/hello/DbResource.java

@@ -11,7 +11,6 @@ import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
 import java.util.concurrent.ThreadLocalRandom;
 
-import javax.ws.rs.DefaultValue;
 import javax.ws.rs.GET;
 import javax.ws.rs.Path;
 import javax.ws.rs.Produces;
@@ -34,10 +33,10 @@ public class DbResource {
   
   @GET
   @Produces(APPLICATION_JSON + "; charset=utf-8")
-  public Object db(@QueryParam("queries") @DefaultValue("1") int queryNumber, @QueryParam("single") boolean isSingle)
+  public Object db(@QueryParam("queries") String queryParam, @QueryParam("single") boolean isSingle)
       throws ExecutionException, InterruptedException {
 
-    final int queries = Math.min(500, Math.max(1, queryNumber));
+    final int queries = getQueries(queryParam);
     final World[] worlds = new World[queries];
     final Random random = ThreadLocalRandom.current();
 
@@ -62,4 +61,15 @@ public class DbResource {
 
     return isSingle ? worlds[0] : worlds;
   }
+
+  private int getQueries(String proto) {
+    int result = 1;
+    try {
+        if (proto != null && !proto.trim().isEmpty()) {
+            result = Integer.parseInt(proto);
+        }
+    } catch (NumberFormatException e) {/* by test contract */}
+
+    return Math.min(500, Math.max(1, result));
+  }
 }