This is an automated email from the ASF dual-hosted git repository.

jamesbognar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/juneau.git


The following commit(s) were added to refs/heads/master by this push:
     new 3a260ae  Context API refactoring.
3a260ae is described below

commit 3a260ae26420f6927d4f00ccdcdd51a4b80da56d
Author: JamesBognar <[email protected]>
AuthorDate: Sat Oct 23 11:29:57 2021 -0400

    Context API refactoring.
---
 .../apache/juneau/rest/HttpRuntimeException.java   |  6 ++-
 .../java/org/apache/juneau/rest/RestOpContext.java | 13 ++----
 .../java/org/apache/juneau/rest/RestOpSession.java | 54 ++--------------------
 3 files changed, 11 insertions(+), 62 deletions(-)

diff --git 
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/HttpRuntimeException.java
 
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/HttpRuntimeException.java
index 6a9b108..c45f70a 100644
--- 
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/HttpRuntimeException.java
+++ 
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/HttpRuntimeException.java
@@ -17,6 +17,7 @@ import java.lang.reflect.*;
 import org.apache.juneau.*;
 import org.apache.juneau.http.annotation.*;
 import org.apache.juneau.http.response.*;
+import org.apache.juneau.parser.*;
 import org.apache.juneau.reflect.*;
 
 /**
@@ -72,10 +73,13 @@ public final class HttpRuntimeException extends 
BasicRuntimeException {
 
                if (t instanceof InvocationTargetException)
                        t = ((InvocationTargetException)t).getCause();
-               
+
                if (t instanceof ExecutableException)
                        t = ((ExecutableException)t).getTargetException();
 
+               if (t instanceof ParseException)
+                       throw new BadRequest(t);
+
                ClassInfo ci = ClassInfo.ofc(t);
 
                // If it's any RuntimeException annotated with @Response, it 
can be rethrown.
diff --git 
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestOpContext.java
 
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestOpContext.java
index 1dee266..7323bee 100644
--- 
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestOpContext.java
+++ 
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestOpContext.java
@@ -63,7 +63,6 @@ import org.apache.juneau.rest.logging.*;
 import org.apache.juneau.rest.util.*;
 import org.apache.juneau.serializer.*;
 import org.apache.juneau.svl.*;
-import org.apache.juneau.utils.*;
 
 /**
  * Represents a single Java servlet/resource method annotated with {@link 
RestOp @RestOp}.
@@ -2199,13 +2198,12 @@ public class RestOpContext extends Context implements 
Comparable<RestOpContext>
 
        private final String httpMethod;
        private final UrlPathMatcher[] pathMatchers;
-       private final RestOpArg[] opArgs;
        private final RestGuard[] guards;
        private final RestMatcher[] requiredMatchers, optionalMatchers;
        private final RestConverter[] converters;
        private final RestContext context;
        private final Method method;
-       private final MethodInvoker methodInvoker;
+       private final RestOpInvoker methodInvoker;
        private final RestOpInvoker[]
                preCallMethods,
                postCallMethods;
@@ -2252,7 +2250,6 @@ public class RestOpContext extends Context implements 
Comparable<RestOpContext>
                        else
                                debug = 
DebugEnablement.create().enable(builder.debug, "*").build();
 
-                       methodInvoker = new MethodInvoker(method, 
context.getMethodExecStats(method));
                        mi = MethodInfo.of(method).accessible();
                        Object r = context.getResource();
 
@@ -2308,9 +2305,9 @@ public class RestOpContext extends Context implements 
Comparable<RestOpContext>
 
                        responseMeta = ResponseBeanMeta.create(mi, 
builder.getApplied());
 
-                       opArgs = context.findRestOperationArgs(mi.inner(), bs);
                        preCallMethods = 
context.getPreCallMethods().stream().map(x -> new RestOpInvoker(x, 
context.findRestOperationArgs(x, bs), 
context.getMethodExecStats(x))).toArray(RestOpInvoker[]::new);
                        postCallMethods = 
context.getPostCallMethods().stream().map(x -> new RestOpInvoker(x, 
context.findRestOperationArgs(x, bs), 
context.getMethodExecStats(x))).toArray(RestOpInvoker[]::new);
+                       methodInvoker = new RestOpInvoker(method, 
context.findRestOperationArgs(method, bs), context.getMethodExecStats(method));
 
                        this.callLogger = context.getCallLogger();
                } catch (Exception e) {
@@ -2662,7 +2659,7 @@ public class RestOpContext extends Context implements 
Comparable<RestOpContext>
                }
        }
 
-       MethodInvoker getMethodInvoker() {
+       RestOpInvoker getMethodInvoker() {
                return methodInvoker;
        }
 
@@ -2674,10 +2671,6 @@ public class RestOpContext extends Context implements 
Comparable<RestOpContext>
                return converters;
        }
 
-       RestOpArg[] getOpArgs() {
-               return opArgs;
-       }
-
        RestOpInvoker[] getPreCallMethods() {
                return preCallMethods;
        }
diff --git 
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestOpSession.java
 
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestOpSession.java
index 1e9fda7..d842dbd 100644
--- 
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestOpSession.java
+++ 
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestOpSession.java
@@ -12,20 +12,13 @@
 // 
***************************************************************************************************************************
 package org.apache.juneau.rest;
 
-import static org.apache.juneau.rest.HttpRuntimeException.*;
-
 import java.io.*;
-import java.lang.reflect.*;
-import java.util.*;
 
 import org.apache.http.*;
 import org.apache.juneau.*;
 import org.apache.juneau.cp.*;
-import org.apache.juneau.http.annotation.*;
 import org.apache.juneau.http.response.*;
-import org.apache.juneau.reflect.*;
 import org.apache.juneau.rest.logging.*;
-import org.apache.juneau.utils.*;
 
 /**
  * A session for a single HTTP request.
@@ -158,52 +151,11 @@ public class RestOpSession extends ContextSession {
                        if (! guard.guard(req, res))
                                return;
 
-               java.lang.reflect.Method m = ctx.getJavaMethod();
-               RestOpArg[] opArgs = ctx.getOpArgs();
-               MethodInvoker methodInvoker = ctx.getMethodInvoker();
-
-               Object[] args = new Object[opArgs.length];
-               for (int i = 0; i < opArgs.length; i++) {
-                       ParamInfo pi = methodInvoker.inner().getParam(i);
-                       try {
-                               args[i] = opArgs[i].resolve(this);
-                       } catch (Exception e) {
-                               throw toHttpException(e, BadRequest.class, 
"Could not convert resolve parameter {0} of type ''{1}'' on method ''{2}''.", 
i, pi.getParameterType(), m.getName());
-                       }
-               }
-
-               try {
-                       Object output = 
methodInvoker.invoke(session.getResource(), args);
-
-                       // Handle manual call to req.setDebug().
-                       Boolean debug = 
req.getAttribute("Debug").asType(Boolean.class).orElse(null);
-                       if (debug == Boolean.TRUE) {
-                               session.debug(true);
-                       } else if (debug == Boolean.FALSE) {
-                               session.debug(false);
-                       }
-
-                       if (! m.getReturnType().equals(Void.TYPE))
-                               if (output != null || ! 
res.getOutputStreamCalled())
-                                       res.setOutput(output);
-
-               } catch (IllegalAccessException|IllegalArgumentException e) {
-                       throw InternalServerError.create().message("Error 
occurred invoking method ''{0}''.", 
methodInvoker.inner().getFullName()).causedBy(e).build();
-               } catch (InvocationTargetException e) {
-                       Throwable e2 = e.getTargetException();  // Get the 
throwable thrown from the doX() method.
-                       res.setStatus(500);  // May be overridden later.
-                       Class<?> c = e2.getClass();
-                       if (e2 instanceof HttpResponse || 
c.getAnnotation(Response.class) != null || c.getAnnotation(ResponseBody.class) 
!= null) {
-                               res.setOutput(e2);
-                       } else {
-                               throw e2;
-                       }
-               }
+               ctx.getMethodInvoker().invoke(this);
 
-               Optional<Object> o = res.getOutput();
-               if (o != null)
+               if (res.hasOutput())
                        for (RestConverter converter : ctx.getConverters())
-                               res.setOutput(converter.convert(req, 
o.orElse(null)));
+                               res.setOutput(converter.convert(req, 
res.getOutput().orElse(null)));
        }
 
        /**

Reply via email to