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 3525d20  Core API refactoring.
3525d20 is described below

commit 3525d2075919319b49ad5a3c43caecfb6c49fd10
Author: JamesBognar <[email protected]>
AuthorDate: Sat Oct 23 11:02:51 2021 -0400

    Core API refactoring.
---
 .../org/apache/juneau/ExecutableException.java     | 12 ++++++++++
 .../apache/juneau/rest/HttpRuntimeException.java   | 10 ++++++---
 .../java/org/apache/juneau/rest/RestOpInvoker.java | 26 +++++++++++++++++-----
 .../java/org/apache/juneau/rest/RestOpSession.java | 14 +++---------
 .../java/org/apache/juneau/rest/RestSession.java   |  2 ++
 5 files changed, 45 insertions(+), 19 deletions(-)

diff --git 
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/ExecutableException.java
 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/ExecutableException.java
index bc0728c..9d36faf 100644
--- 
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/ExecutableException.java
+++ 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/ExecutableException.java
@@ -12,6 +12,7 @@
 // 
***************************************************************************************************************************
 package org.apache.juneau;
 
+import java.lang.reflect.*;
 import java.text.MessageFormat;
 
 /**
@@ -50,4 +51,15 @@ public class ExecutableException extends 
BasicRuntimeException {
        public ExecutableException(String message, Object...args) {
                super(message, args);
        }
+
+       /**
+        * If the thrown exception was an {@link InvocationTargetException} 
returns the target exception.  
+        * Otherwise returns the inner exception which is typically {@link 
IllegalArgumentException} or {@link IllegalAccessException}.
+        *
+        * @return The inner throwable.
+        */
+       public Throwable getTargetException() {
+               Throwable c = this.getCause();
+               return c instanceof InvocationTargetException ? 
((InvocationTargetException)c).getTargetException() : c;
+       }
 }
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 38d8268..6a9b108 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
@@ -69,6 +69,13 @@ public final class HttpRuntimeException extends 
BasicRuntimeException {
         * @return RuntimeException The new exception to throw.
         */
        public static RuntimeException toHttpException(Throwable t, Class<?> 
ec, String msg, Object...args) {
+
+               if (t instanceof InvocationTargetException)
+                       t = ((InvocationTargetException)t).getCause();
+               
+               if (t instanceof ExecutableException)
+                       t = ((ExecutableException)t).getTargetException();
+
                ClassInfo ci = ClassInfo.ofc(t);
 
                // If it's any RuntimeException annotated with @Response, it 
can be rethrown.
@@ -83,9 +90,6 @@ public final class HttpRuntimeException extends 
BasicRuntimeException {
                if (ci.hasAnnotation(Response.class))
                        return new HttpRuntimeException(t);
 
-               if (ci.is(InvocationTargetException.class))
-                       return new 
HttpRuntimeException(((InvocationTargetException)t).getCause());
-
                if (ec == null)
                        ec = InternalServerError.class;
                ClassInfo eci = ClassInfo.ofc(ec);
diff --git 
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestOpInvoker.java
 
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestOpInvoker.java
index 010354a..5f059c6 100644
--- 
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestOpInvoker.java
+++ 
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestOpInvoker.java
@@ -46,10 +46,9 @@ public class RestOpInvoker extends MethodInvoker {
         * Invokes this method from the specified {@link RestSession}.
         *
         * @param opSession The REST call.
-        * @return The results of the call.
         * @throws Throwable If an error occurred during either parameter 
resolution or method invocation.
         */
-       public Object invoke(RestOpSession opSession) throws Throwable {
+       public void invoke(RestOpSession opSession) throws Throwable {
                Object[] args = new Object[opArgs.length];
                for (int i = 0; i < opArgs.length; i++) {
                        ParamInfo pi = inner().getParam(i);
@@ -60,7 +59,24 @@ public class RestOpInvoker extends MethodInvoker {
                        }
                }
                try {
-                       return invoke(opSession.getRestSession().getResource(), 
args);
+                       RestSession session = opSession.getRestSession();
+                       RestRequest req = opSession.getRequest();
+                       RestResponse res = opSession.getResponse();
+
+                       Object output = super.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 (! inner().hasReturnType(Void.TYPE))
+                               if (output != null || ! 
res.getOutputStreamCalled())
+                                       res.setOutput(output);
+
                } catch (IllegalAccessException|IllegalArgumentException e) {
                        throw InternalServerError.create().message("Error 
occurred invoking method ''{0}''.", inner().getFullName()).causedBy(e).build();
                } catch (InvocationTargetException e) {
@@ -70,9 +86,9 @@ public class RestOpInvoker extends MethodInvoker {
                        Class<?> c = e2.getClass();
                        if (e2 instanceof HttpResponse || 
c.getAnnotation(Response.class) != null || c.getAnnotation(ResponseBody.class) 
!= null) {
                                res.setOutput(e2);
-                               return null;
+                       } else {
+                               throw toHttpException(e2, 
InternalServerError.class, "Method ''{0}'' threw an unexpected exception.", 
getFullName());
                        }
-                       throw toHttpException(e2, InternalServerError.class, 
"Method ''{0}'' threw an unexpected exception.", getFullName());
                }
        }
 }
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 7cfa02b..40c0da8 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
@@ -183,13 +183,11 @@ public class RestOpSession extends ContextSession {
                                session.debug(false);
                        }
 
-                       if (res.getStatus() == 0)
-                               res.setStatus(200);
-                       if (! m.getReturnType().equals(Void.TYPE)) {
+                       if (! m.getReturnType().equals(Void.TYPE))
                                if (output != null || ! 
res.getOutputStreamCalled())
                                        res.setOutput(output);
-                       }
-               } catch (IllegalAccessException e) {
+
+               } 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.
@@ -200,12 +198,6 @@ public class RestOpSession extends ContextSession {
                        } else {
                                throw e2;
                        }
-               } catch (IllegalArgumentException e) {
-                       MethodInfo mi = MethodInfo.of(m);
-                       throw new BadRequest(e,
-                               "Invalid argument type passed to the following 
method: ''{0}''.\n\tArgument types: {1}",
-                               mi.getShortName(), mi.getFullName()
-                       );
                }
 
                Optional<Optional<Object>> o = res.getOutput();
diff --git 
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestSession.java
 
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestSession.java
index 5ee6ff5..f975492 100644
--- 
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestSession.java
+++ 
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestSession.java
@@ -549,6 +549,8 @@ public class RestSession extends ContextSession {
                        context.preCall(opSession);
                        opSession.run();
                        context.postCall(opSession);
+                       if (res.getStatus() == 0)
+                               res.setStatus(200);
                        if (opSession.getResponse().hasOutput()) {
                                // Now serialize the output if there was any.
                                // Some subclasses may write to the 
OutputStream or Writer directly.

Reply via email to