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 42945cf REST refactoring.
42945cf is described below
commit 42945cfe249944edc3c6760d18574e93e6469f87
Author: JamesBognar <[email protected]>
AuthorDate: Tue Feb 9 17:52:23 2021 -0500
REST refactoring.
---
.../main/java/org/apache/juneau/rest/RestCall.java | 26 +++++++---------
.../java/org/apache/juneau/rest/RestContext.java | 4 +--
.../apache/juneau/rest/RestOperationContext.java | 5 +--
.../java/org/apache/juneau/rest/RestResponse.java | 36 ++++++++++------------
.../juneau/rest/RrpcRestOperationContext.java | 7 +++--
.../juneau/rest/reshandlers/DefaultHandler.java | 10 +++---
6 files changed, 43 insertions(+), 45 deletions(-)
diff --git
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestCall.java
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestCall.java
index f9f35ca..00d45d7 100644
---
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestCall.java
+++
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestCall.java
@@ -12,6 +12,8 @@
//
***************************************************************************************************************************
package org.apache.juneau.rest;
+import static java.util.Optional.*;
+
import java.io.*;
import java.lang.reflect.*;
import java.util.*;
@@ -466,25 +468,19 @@ public class RestCall {
}
/**
- * Shortcut for calling <c>getRestResponse().hasOutput()</c>.
- *
- * @return <jk>true</jk> if response has output.
- */
- public boolean hasOutput() {
- if (rres != null)
- return rres.hasOutput();
- return false;
- }
-
- /**
- * Shortcut for calling <c>getRestResponse().getOutput()</c>.
+ * Returns the output that was set by calling {@link
RestResponse#setOutput(Object)}.
+ *
+ * <p>
+ * If it's empty, then {@link RestResponse#setOutput(Object)} wasn't
called.
+ * <br>If it's not empty but contains an empty, then
<c>response.setObject(<jk>null</jk>)</c> was called.
+ * <br>Otherwise, {@link RestResponse#setOutput(Object)} was called
with a non-null value.
*
- * @return The response output.
+ * @return The output object. Never <jk>null</jk>.
*/
- public Object getOutput() {
+ public Optional<Optional<Object>> getOutput() {
if (rres != null)
return rres.getOutput();
- return null;
+ return rres == null ? empty() : rres.getOutput();
}
/**
diff --git
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestContext.java
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestContext.java
index 47a4df4..e15c39a 100644
---
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestContext.java
+++
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestContext.java
@@ -6733,7 +6733,7 @@ public class RestContext extends BeanContext {
handleNotFound(call);
}
- if (call.hasOutput()) {
+ if (call.getOutput().isPresent()) {
// Now serialize the output if there was any.
// Some subclasses may write to the
OutputStream or Writer directly.
handleResponse(call);
@@ -6791,7 +6791,7 @@ public class RestContext extends BeanContext {
if (h.handle(req, res))
return;
- Object output = res.getOutput();
+ Object output = res.getOutput().get().orElse(null);
throw new NotImplemented("No response handlers found to process
output of type '"+(output == null ? null : output.getClass().getName())+"'");
}
diff --git
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestOperationContext.java
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestOperationContext.java
index 479d593..1ccb4bc 100644
---
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestOperationContext.java
+++
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestOperationContext.java
@@ -1929,9 +1929,10 @@ public class RestOperationContext extends BeanContext
implements Comparable<Rest
context.postCall(call);
- if (res.hasOutput())
+ Optional<Optional<Object>> o = res.getOutput();
+ if (o.isPresent())
for (RestConverter converter : converters)
- res.setOutput(converter.convert(req,
res.getOutput()));
+ res.setOutput(converter.convert(req,
o.get().orElse(null)));
} catch (IllegalArgumentException e) {
throw new BadRequest(e,
diff --git
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestResponse.java
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestResponse.java
index ad659af..3c2df0b 100644
---
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestResponse.java
+++
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestResponse.java
@@ -14,6 +14,7 @@ package org.apache.juneau.rest;
import static org.apache.juneau.internal.StringUtils.*;
import static org.apache.juneau.httppart.HttpPartType.*;
+import static java.util.Optional.*;
import java.io.*;
import java.nio.charset.*;
@@ -62,8 +63,7 @@ public final class RestResponse extends
HttpServletResponseWrapper {
private HttpServletResponse inner;
private final RestRequest request;
- private Object output; // The POJO being sent to
the output.
- private boolean isNullOutput; // The output is null (as
opposed to not being set at all)
+ private Optional<Optional<Object>> output = empty(); // The POJO being
sent to the output.
private ServletOutputStream sos;
private FinishableServletOutputStream os;
private FinishablePrintWriter w;
@@ -163,7 +163,6 @@ public final class RestResponse extends
HttpServletResponseWrapper {
* Calling this method with a <jk>null</jk> value is NOT
the same as not calling this method at all.
* <br>A <jk>null</jk> output value means we want to
serialize <jk>null</jk> as a response (e.g. as a JSON <c>null</c>).
* <br>Not calling this method or returning a value means
you're handing the response yourself via the underlying stream or writer.
- * <br>This distinction affects the {@link #hasOutput()}
method behavior.
* </ul>
*
* <ul class='seealso'>
@@ -175,8 +174,7 @@ public final class RestResponse extends
HttpServletResponseWrapper {
* @return This object (for method chaining).
*/
public RestResponse setOutput(Object output) {
- this.output = output;
- this.isNullOutput = output == null;
+ this.output = of(ofNullable(output));
return this;
}
@@ -217,26 +215,22 @@ public final class RestResponse extends
HttpServletResponseWrapper {
* @return This object (for method chaining).
*/
public RestResponse setOutputs(Object...output) {
- this.output = output;
+ this.output = of(of(output));
return this;
}
/**
* Returns the output that was set by calling {@link
#setOutput(Object)}.
*
- * @return The output object.
- */
- public Object getOutput() {
- return output;
- }
-
- /**
- * Returns <jk>true</jk> if this response has any output associated
with it.
+ * <p>
+ * If it's empty, then {@link #setOutput(Object)} wasn't called.
+ * <br>If it's not empty but contains an empty, then
<c>setObject(<jk>null</jk>)</c> was called.
+ * <br>Otherwise, {@link #setOutput(Object)} was called with a non-null
value.
*
- * @return <jk>true</jk> if {@link #setOutput(Object)} has been called,
even if the value passed was <jk>null</jk>.
+ * @return The output object. Never <jk>null</jk>.
*/
- public boolean hasOutput() {
- return output != null || isNullOutput;
+ public Optional<Optional<Object>> getOutput() {
+ return output;
}
/**
@@ -635,7 +629,7 @@ public final class RestResponse extends
HttpServletResponseWrapper {
* @return <jk>true</jk> if this response object is of the specified
type.
*/
public boolean isOutputType(Class<?> c) {
- return c.isInstance(output);
+ return c.isInstance(getRawOutput());
}
/**
@@ -646,7 +640,7 @@ public final class RestResponse extends
HttpServletResponseWrapper {
*/
@SuppressWarnings("unchecked")
public <T> T getOutput(Class<T> c) {
- return (T)output;
+ return (T)getRawOutput();
}
/**
@@ -666,4 +660,8 @@ public final class RestResponse extends
HttpServletResponseWrapper {
os.flush();
super.flushBuffer();
}
+
+ private Object getRawOutput() {
+ return output.isPresent() ? output.get().orElse(null) : null;
+ }
}
\ No newline at end of file
diff --git
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RrpcRestOperationContext.java
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RrpcRestOperationContext.java
index 82cdf13..13f5c81 100644
---
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RrpcRestOperationContext.java
+++
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RrpcRestOperationContext.java
@@ -17,6 +17,7 @@ import static org.apache.juneau.rest.HttpRuntimeException.*;
import java.io.*;
import java.lang.reflect.*;
+import java.util.*;
import javax.servlet.*;
@@ -53,7 +54,8 @@ public class RrpcRestOperationContext extends
RestOperationContext {
super.invoke(call);
- final Object o = call.getOutput();
+ Optional<Optional<Object>> x = call.getOutput();
+ final Object o = x.isPresent() ? x.get().orElse(null) : null;
if ("GET".equals(call.getMethod())) {
call.output(meta.getMethodsByPath().keySet());
@@ -79,8 +81,7 @@ public class RrpcRestOperationContext extends
RestOperationContext {
args = p.parseArgs(in,
m.getGenericParameterTypes());
}
}
- Object output = m.invoke(o, args);
- call.output(output);
+ call.output(m.invoke(o, args));
return;
} catch (Exception e) {
throw toHttpException(e,
InternalServerError.class);
diff --git
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/reshandlers/DefaultHandler.java
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/reshandlers/DefaultHandler.java
index b59d320..a80b00a 100644
---
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/reshandlers/DefaultHandler.java
+++
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/reshandlers/DefaultHandler.java
@@ -59,7 +59,8 @@ public class DefaultHandler implements ResponseHandler {
SerializerMatch sm = g.getSerializerMatch(accept);
HttpPartSchema schema = null;
- Object o = res.getOutput();
+ Optional<Optional<Object>> output = res.getOutput();
+ Object o = output.isPresent() ? output.get().orElse(null) :
null;
ResponseBeanMeta rm = res.getResponseMeta();
if (rm == null)
@@ -68,11 +69,12 @@ public class DefaultHandler implements ResponseHandler {
if (rm != null) {
boolean isThrowable =
rm.getClassMeta().isType(Throwable.class);
- if (isThrowable) {
+ if (isThrowable && o != null) {
+ Throwable t = (Throwable)o;
res.setHeaderSafe("Exception-Name",
rm.getClassMeta().getName());
- res.setHeaderSafe("Exception-Message",
((Throwable)o).getMessage());
+ res.setHeaderSafe("Exception-Message",
t.getMessage());
if (req.isDebug())
- ((Throwable)o).printStackTrace();
+ t.printStackTrace();
}
ResponseBeanPropertyMeta stm = rm.getStatusMethod();