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 870107e REST refactoring.
870107e is described below
commit 870107e9e26c21db8b9160393d12a0c14a176ebf
Author: JamesBognar <[email protected]>
AuthorDate: Tue Feb 9 10:51:08 2021 -0500
REST refactoring.
---
.../java/org/apache/juneau/rest/RestContext.java | 16 ++++---
.../apache/juneau/rest/RestOperationContext.java | 7 +--
.../juneau/rest/RestOperationContextBuilder.java | 4 --
.../java/org/apache/juneau/rest/RestRequest.java | 55 +++++++++-------------
.../java/org/apache/juneau/rest/RestResponse.java | 10 +---
.../java/org/apache/juneau/rest/Swagger_Test.java | 8 +++-
.../apache/juneau/rest/testutils/TestUtils.java | 3 +-
7 files changed, 41 insertions(+), 62 deletions(-)
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 5eb2e65..e41681c 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
@@ -6633,23 +6633,25 @@ public class RestContext extends BeanContext {
* This method is called immediately after {@link #startCall(RestCall)}
has been called.
*
* @param call The current REST call.
+ * @param opContext The context of the matched Java method.
* @return The wrapped request object.
- * @throws ServletException If any errors occur trying to interpret the
request.
+ * @throws Exception If any errors occur trying to interpret the
request.
*/
- public RestRequest createRequest(RestCall call) throws ServletException
{
- return new RestRequest(call);
+ public RestRequest createRequest(RestCall call, RestOperationContext
opContext) throws Exception {
+ return new RestRequest(call, opContext);
}
/**
* Creates a {@link RestResponse} object based on the specified
incoming {@link HttpServletResponse} object
- * and the request returned by {@link #createRequest(RestCall)}.
+ * and the request returned by {@link
#createRequest(RestCall,RestOperationContext)}.
*
* @param call The current REST call.
+ * @param opContext The context of the matched Java method.
* @return The wrapped response object.
- * @throws ServletException If any errors occur trying to interpret the
request or response.
+ * @throws Exception If any errors occur trying to interpret the
request or response.
*/
- public RestResponse createResponse(RestCall call) throws
ServletException {
- return new RestResponse(call);
+ public RestResponse createResponse(RestCall call, RestOperationContext
opContext) throws Exception {
+ return new RestResponse(call, opContext);
}
/**
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 92d0c83..7957f58 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
@@ -1838,8 +1838,8 @@ public class RestOperationContext extends BeanContext
implements Comparable<Rest
*/
protected void invoke(RestCall call) throws Throwable {
- context.createRequest(call).init(this);
- context.createResponse(call).init(this);
+ context.createRequest(call, this);
+ context.createResponse(call, this);
UrlPathMatch pm = call.getUrlPathMatch();
if (pm == null)
@@ -1857,9 +1857,6 @@ public class RestOperationContext extends BeanContext
implements Comparable<Rest
if (pm.getRemainder() != null)
rp.remainder(pm.getRemainder());
- req.init(this);
- res.init(this);
-
context.preCall(call);
call.logger(callLogger);
diff --git
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestOperationContextBuilder.java
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestOperationContextBuilder.java
index f604d40..8ba5dad 100644
---
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestOperationContextBuilder.java
+++
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestOperationContextBuilder.java
@@ -72,7 +72,6 @@ public class RestOperationContextBuilder extends
BeanContextBuilder {
this.restMethod = method;
this.beanFactory = context.getRootBeanFactory();
- String sig = method.getDeclaringClass().getName() + '.' +
method.getName();
MethodInfo mi = MethodInfo.of(context.getResourceClass(),
method);
try {
@@ -88,9 +87,6 @@ public class RestOperationContextBuilder extends
BeanContextBuilder {
}
}
- if (ro == null)
- throw new RestServletException("@RestOp
annotation not found on method ''{0}''", sig);
-
VarResolver vr = context.getVarResolver();
VarResolverSession vrs = vr.createSession();
diff --git
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestRequest.java
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestRequest.java
index d2a481e..aadb686 100644
---
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestRequest.java
+++
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestRequest.java
@@ -125,7 +125,7 @@ public final class RestRequest extends
HttpServletRequestWrapper {
/**
* Constructor.
*/
- RestRequest(RestCall call) throws ServletException {
+ RestRequest(RestCall call, RestOperationContext roc) throws Exception {
super(call.getRequest());
HttpServletRequest req = call.getRequest();
this.inner = req;
@@ -133,45 +133,33 @@ public final class RestRequest extends
HttpServletRequestWrapper {
this.context = call.getContext();
this.call = call;
- try {
- // If this is a POST, we want to parse the query
parameters ourselves to prevent
- // the servlet code from processing the HTTP body as
URL-Encoded parameters.
- queryParams = new RequestQuery(this);
- queryParams.putAll(call.getQueryParams());
+ queryParams = new RequestQuery(this);
+ queryParams.putAll(call.getQueryParams());
- method = call.getMethod();
+ method = call.getMethod();
- headers = new RequestHeaders(this);
- for (Enumeration<String> e = getHeaderNames();
e.hasMoreElements();) {
- String name = e.nextElement();
- headers.put(name, super.getHeaders(name));
- }
+ headers = new RequestHeaders(this);
+ for (Enumeration<String> e = getHeaderNames();
e.hasMoreElements();) {
+ String name = e.nextElement();
+ headers.put(name, super.getHeaders(name));
+ }
- body = new RequestBody(this);
+ body = new RequestBody(this);
- if (context.isAllowBodyParam()) {
- String b = getQuery().getString("body");
- if (b != null) {
- headers.put("Content-Type",
UonSerializer.DEFAULT.getResponseContentType());
- body.load(MediaType.UON,
UonParser.DEFAULT, b.getBytes(UTF8));
- }
+ if (context.isAllowBodyParam()) {
+ String b = getQuery().getString("body");
+ if (b != null) {
+ headers.put("Content-Type",
UonSerializer.DEFAULT.getResponseContentType());
+ body.load(MediaType.UON, UonParser.DEFAULT,
b.getBytes(UTF8));
}
+ }
- Set<String> s = context.getAllowedHeaderParams();
- if (! s.isEmpty())
- headers.queryParams(queryParams, s);
+ Set<String> s = context.getAllowedHeaderParams();
+ if (! s.isEmpty())
+ headers.queryParams(queryParams, s);
- this.pathParams = new RequestPath(call);
+ this.pathParams = new RequestPath(call);
- } catch (Exception e) {
- throw toHttpException(e, InternalServerError.class);
- }
- }
-
- /*
- * Called from RestServlet after a match has been made but before the
guard or method invocation.
- */
- final void init(RestOperationContext roc) throws IOException {
this.opContext = Optional.of(roc);
this.javaMethod = roc.getJavaMethod();
this.beanSession = roc.createSession();
@@ -196,9 +184,8 @@ public final class RestRequest extends
HttpServletRequestWrapper {
.headers(headers)
.maxInput(roc.getMaxInput());
- if (isDebug()) {
+ if (isDebug())
inner = CachingHttpServletRequest.wrap(inner);
- }
}
RestRequest setResponse(RestResponse res) {
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 ec39800..763ce01 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
@@ -73,7 +73,7 @@ public final class RestResponse extends
HttpServletResponseWrapper {
/**
* Constructor.
*/
- RestResponse(RestCall call) throws BadRequest {
+ RestResponse(RestCall call, RestOperationContext roc) throws Exception {
super(call.getResponse());
this.inner = call.getResponse();
this.request = call.getRestRequest();
@@ -91,17 +91,9 @@ public final class RestResponse extends
HttpServletResponseWrapper {
} catch (Exception e1) {
throw new BadRequest(e1, "Invalid format for header
'x-response-headers'. Must be in URL-encoded format.");
}
- }
- /*
- * Called from RestServlet after a match has been made but before the
guard or method invocation.
- */
- final void init(RestOperationContext roc) throws NotAcceptable,
IOException {
this.opContext = roc;
- if (request.isDebug())
- setDebug();
-
// Find acceptable charset
String h = request.getHeader("accept-charset");
String charset = null;
diff --git
a/juneau-utest/src/test/java/org/apache/juneau/rest/Swagger_Test.java
b/juneau-utest/src/test/java/org/apache/juneau/rest/Swagger_Test.java
index eb856af..ba28682 100644
--- a/juneau-utest/src/test/java/org/apache/juneau/rest/Swagger_Test.java
+++ b/juneau-utest/src/test/java/org/apache/juneau/rest/Swagger_Test.java
@@ -43,16 +43,20 @@ public class Swagger_Test {
// Setup
//------------------------------------------------------------------------------------------------------------------
+ public void testMethod() {}
+
private org.apache.juneau.dto.swagger.Swagger getSwaggerWithFile(Object
resource) throws Exception {
RestContext rc =
RestContext.create(resource).fileFinder(TestClasspathFileFinder.class).build();
- RestRequest req = rc.createRequest(new RestCall(resource, rc,
new MockServletRequest(), null));
+ RestOperationContext roc =
RestOperationContext.create(Swagger_Test.class.getMethod("testMethod"),
rc).build();
+ RestRequest req = rc.createRequest(new RestCall(resource, rc,
new MockServletRequest(), null), roc);
SwaggerProvider ip = rc.getSwaggerProvider();
return ip.getSwagger(rc, req.getLocale());
}
private static org.apache.juneau.dto.swagger.Swagger getSwagger(Object
resource) throws Exception {
RestContext rc = RestContext.create(resource).build();
- RestRequest req = rc.createRequest(new RestCall(resource, rc,
new MockServletRequest(), null));
+ RestOperationContext roc =
RestOperationContext.create(Swagger_Test.class.getMethod("testMethod"),
rc).build();
+ RestRequest req = rc.createRequest(new RestCall(resource, rc,
new MockServletRequest(), null), roc);
SwaggerProvider ip = rc.getSwaggerProvider();
return ip.getSwagger(rc, req.getLocale());
}
diff --git
a/juneau-utest/src/test/java/org/apache/juneau/rest/testutils/TestUtils.java
b/juneau-utest/src/test/java/org/apache/juneau/rest/testutils/TestUtils.java
index 24c80a0..29d9767 100755
--- a/juneau-utest/src/test/java/org/apache/juneau/rest/testutils/TestUtils.java
+++ b/juneau-utest/src/test/java/org/apache/juneau/rest/testutils/TestUtils.java
@@ -28,7 +28,8 @@ public class TestUtils extends
org.apache.juneau.testutils.TestUtils {
try {
Object r = c.newInstance();
RestContext rc = RestContext.create(r).build();
- RestRequest req = rc.createRequest(new RestCall(r, rc,
new MockServletRequest(), null));
+ RestOperationContext ctx =
RestOperationContext.create(TestUtils.class.getMethod("getSwagger",
Class.class), rc).build();
+ RestRequest req = rc.createRequest(new RestCall(r, rc,
new MockServletRequest(), null), ctx);
SwaggerProvider ip = rc.getSwaggerProvider();
return ip.getSwagger(rc, req.getLocale());
} catch (Exception e) {