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 b044191 JUNEAU-142 BasicRest subclasses need programmatic access to
RestRequest/RestResponse
b044191 is described below
commit b04419139f4886cb4fe61d6f03688f68bdf54fa7
Author: JamesBognar <[email protected]>
AuthorDate: Fri Sep 6 22:17:09 2019 -0400
JUNEAU-142 BasicRest subclasses need programmatic access to
RestRequest/RestResponse
---
juneau-doc/docs/ReleaseNotes/8.1.1.html | 15 +++
.../java/org/apache/juneau/rest/BasicRest.java | 112 +++++++++++++++++++++
.../java/org/apache/juneau/rest/RestServlet.java | 106 ++++++++++---------
3 files changed, 185 insertions(+), 48 deletions(-)
diff --git a/juneau-doc/docs/ReleaseNotes/8.1.1.html
b/juneau-doc/docs/ReleaseNotes/8.1.1.html
index fd78f42..fe2213d 100644
--- a/juneau-doc/docs/ReleaseNotes/8.1.1.html
+++ b/juneau-doc/docs/ReleaseNotes/8.1.1.html
@@ -59,6 +59,21 @@
</p>
The fix involves resolving the original bean class for
resolving parameterized type while leaving
method invocation on the proxy method so as not to bypass
Spring features.
+ <li>
+ New methods on {@link oajr.BasicRest} to provide feature-parity
with {@link oajr.RestServlet}:
+ <ul class='javatree'>
+ <li class='jc'>{@link oajr.BasicRest}
+ <ul>
+ <li class='jm'>{@link
oajr.BasicRest#getContext() getContext()}
+ <li class='jm'>{@link
oajr.BasicRest#getRequest() getRequest()}
+ <li class='jm'>{@link
oajr.BasicRest#getResponse() getResponse()}
+ <li class='jm'>{@link
oajr.BasicRest#log(String) log(String)}
+ <li class='jm'>{@link
oajr.BasicRest#log(String,Throwable) log(String,Throwable)}
+ <li class='jm'>{@link
oajr.BasicRest#log(Level,String,Object[]) log(Level,String,Object[])}
+ <li class='jm'>{@link
oajr.BasicRest#logObjects(Level,String,Object[])
logObjects(Level,String,Object[])}
+ <li class='jm'>{@link
oajr.BasicRest#log(Level,Throwable,String,Object[])
log(Level,Throwable,String,Object[])}
+ </ul>
+ </ul>
</ul>
<h5 class='topic w800'>juneau-rest-client</h5>
diff --git
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/BasicRest.java
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/BasicRest.java
index 088b327..056a806 100644
---
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/BasicRest.java
+++
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/BasicRest.java
@@ -12,11 +12,16 @@
//
***************************************************************************************************************************
package org.apache.juneau.rest;
+import java.text.*;
+import java.util.logging.*;
+
import javax.servlet.http.*;
import org.apache.juneau.dto.swagger.*;
import org.apache.juneau.html.annotation.*;
+import org.apache.juneau.internal.*;
import org.apache.juneau.rest.annotation.*;
+import org.apache.juneau.http.exception.*;
/**
* Identical to {@link BasicRestServlet} but doesn't extend from {@link
HttpServlet}
@@ -38,6 +43,19 @@ import org.apache.juneau.rest.annotation.*;
)
public abstract class BasicRest implements BasicRestConfig {
+ private JuneauLogger logger = JuneauLogger.getLogger(getClass());
+ private volatile RestContext context;
+
+ /**
+ * Post-initialization hook to retrieve the {@link RestContext} object
for this resource.
+ *
+ * @param context The context for this resource.
+ */
+ @RestHook(HookEvent.POST_INIT)
+ public synchronized void onPostInit(RestContext context) {
+ this.context = context;
+ }
+
/**
* [OPTIONS /*] - Show resource options.
*
@@ -49,4 +67,98 @@ public abstract class BasicRest implements BasicRestConfig {
// Localized Swagger for this resource is available through the
RestRequest object.
return req.getSwagger();
}
+
+
//-----------------------------------------------------------------------------------------------------------------
+ // Context methods.
+
//-----------------------------------------------------------------------------------------------------------------
+
+ /**
+ * Returns the read-only context object that contains all the
configuration information about this resource.
+ *
+ * @return The context information on this servlet.
+ */
+ protected synchronized RestContext getContext() {
+ if (context == null)
+ throw new InternalServerError("RestContext object not
set on resource.");
+ return context;
+ }
+
+
//-----------------------------------------------------------------------------------------------------------------
+ // Convenience logger methods
+
//-----------------------------------------------------------------------------------------------------------------
+
+ /**
+ * Log a message.
+ *
+ * @param msg The message to log.
+ */
+ public void log(String msg) {
+ logger.info(msg);
+ }
+
+ /**
+ * Log a message.
+ *
+ * @param msg The message to log.
+ * @param cause The cause.
+ */
+ public void log(String msg, Throwable cause) {
+ logger.info(cause, msg);
+ }
+
+ /**
+ * Log a message.
+ *
+ * @param level The log level.
+ * @param msg The message to log.
+ * @param args Optional {@link MessageFormat}-style arguments.
+ */
+ public void log(Level level, String msg, Object...args) {
+ logger.log(level, msg, args);
+ }
+
+ /**
+ * Log a message.
+ *
+ * @param level The log level.
+ * @param msg The message to log.
+ * @param args Optional {@link MessageFormat}-style arguments.
+ */
+ public void logObjects(Level level, String msg, Object...args) {
+ logger.logObjects(level, msg, args);
+ }
+
+ /**
+ * Log a message.
+ *
+ * @param level The log level.
+ * @param cause The cause.
+ * @param msg The message to log.
+ * @param args Optional {@link MessageFormat}-style arguments.
+ */
+ public void log(Level level, Throwable cause, String msg,
Object...args) {
+ logger.log(level, cause, msg, args);
+ }
+
+
//-----------------------------------------------------------------------------------------------------------------
+ // Request-time methods.
+
//-----------------------------------------------------------------------------------------------------------------
+
+ /**
+ * Returns the current HTTP request.
+ *
+ * @return The current HTTP request.
+ */
+ public synchronized RestRequest getRequest() {
+ return getContext().getRequest();
+ }
+
+ /**
+ * Returns the current HTTP response.
+ *
+ * @return The current HTTP response
+ */
+ public synchronized RestResponse getResponse() {
+ return getContext().getResponse();
+ }
}
diff --git
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestServlet.java
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestServlet.java
index f06096c..6a9443f 100644
---
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestServlet.java
+++
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestServlet.java
@@ -126,6 +126,10 @@ public abstract class RestServlet extends HttpServlet {
return builder;
}
+
//-----------------------------------------------------------------------------------------------------------------
+ // Context methods.
+
//-----------------------------------------------------------------------------------------------------------------
+
/**
* Returns the read-only context object that contains all the
configuration information about this resource.
*
@@ -143,38 +147,25 @@ public abstract class RestServlet extends HttpServlet {
* @return The context information on this servlet.
*/
protected synchronized RestContext getContext() {
+ if (context == null)
+ throw new InternalServerError("RestContext object not
set on resource.");
return context;
}
-
-
//-----------------------------------------------------------------------------------------------------------------
- // Other methods
-
//-----------------------------------------------------------------------------------------------------------------
-
/**
- * The main service method.
+ * Convenience method for calling <c>getContext().getProperties();</c>
*
- * <p>
- * Subclasses can optionally override this method if they want to
tailor the behavior of requests.
+ * @return The resource properties as an {@link RestContextProperties}.
+ * @see RestContext#getProperties()
*/
- @Override /* Servlet */
- public void service(HttpServletRequest r1, HttpServletResponse r2)
throws ServletException, InternalServerError, IOException {
- try {
- // To avoid checking the volatile field context on
every call, use the non-volatile isInitialized field as a first-check check.
- if (! isInitialized) {
- if (initException != null)
- throw initException;
- if (context == null)
- throw new InternalServerError("Servlet
{0} not initialized. init(ServletConfig) was not called. This can occur if
you've overridden this method but didn't call super.init(RestConfig).",
getClass().getName());
- isInitialized = true;
- }
+ public RestContextProperties getProperties() {
+ return getContext().getProperties();
+ }
- context.getCallHandler().service(r1, r2);
- } catch (Throwable e) {
- r2.sendError(SC_INTERNAL_SERVER_ERROR,
e.getLocalizedMessage());
- }
- }
+
//-----------------------------------------------------------------------------------------------------------------
+ // Convenience logger methods
+
//-----------------------------------------------------------------------------------------------------------------
@Override /* GenericServlet */
public void log(String msg) {
@@ -187,7 +178,7 @@ public abstract class RestServlet extends HttpServlet {
}
/**
- * Convenience method for calling
<c>getContext().getLogger().log(level, msg, args);</c>
+ * Log a message.
*
* @param level The log level.
* @param msg The message to log.
@@ -198,7 +189,7 @@ public abstract class RestServlet extends HttpServlet {
}
/**
- * Convenience method for calling
<c>getContext().getLogger().logObjects(level, msg, args);</c>
+ * Log a message.
*
* @param level The log level.
* @param msg The message to log.
@@ -209,7 +200,7 @@ public abstract class RestServlet extends HttpServlet {
}
/**
- * Convenience method for calling
<c>getContext().getLogger().log(level, cause, msg, args);</c>
+ * Log a message.
*
* @param level The log level.
* @param cause The cause.
@@ -220,26 +211,33 @@ public abstract class RestServlet extends HttpServlet {
logger.log(level, cause, msg, args);
}
- /**
- * Returns the current HTTP request.
- *
- * @return The current HTTP request, or <jk>null</jk> if it wasn't
created.
- */
- public RestRequest getRequest() {
- if (context == null)
- return null;
- return context.getRequest();
- }
+
//-----------------------------------------------------------------------------------------------------------------
+ // Lifecycle methods
+
//-----------------------------------------------------------------------------------------------------------------
/**
- * Returns the current HTTP response.
+ * The main service method.
*
- * @return The current HTTP response, or <jk>null</jk> if it wasn't
created.
+ * <p>
+ * Subclasses can optionally override this method if they want to
tailor the behavior of requests.
*/
- public RestResponse getResponse() {
- if (context == null)
- return null;
- return context.getResponse();
+ @Override /* Servlet */
+ public void service(HttpServletRequest r1, HttpServletResponse r2)
throws ServletException, InternalServerError, IOException {
+ try {
+ // To avoid checking the volatile field context on
every call, use the non-volatile isInitialized field as a first-check check.
+ if (! isInitialized) {
+ if (initException != null)
+ throw initException;
+ if (context == null)
+ throw new InternalServerError("Servlet
{0} not initialized. init(ServletConfig) was not called. This can occur if
you've overridden this method but didn't call super.init(RestConfig).",
getClass().getName());
+ isInitialized = true;
+ }
+
+ context.getCallHandler().service(r1, r2);
+
+ } catch (Throwable e) {
+ r2.sendError(SC_INTERNAL_SERVER_ERROR,
e.getLocalizedMessage());
+ }
}
@Override /* GenericServlet */
@@ -249,13 +247,25 @@ public abstract class RestServlet extends HttpServlet {
super.destroy();
}
+
//-----------------------------------------------------------------------------------------------------------------
+ // Request-time methods.
+
//-----------------------------------------------------------------------------------------------------------------
+
/**
- * Convenience method for calling <c>getContext().getProperties();</c>
+ * Returns the current HTTP request.
*
- * @return The resource properties as an {@link RestContextProperties}.
- * @see RestContext#getProperties()
+ * @return The current HTTP request, or <jk>null</jk> if it wasn't
created.
*/
- public RestContextProperties getProperties() {
- return getContext().getProperties();
+ public synchronized RestRequest getRequest() {
+ return getContext().getRequest();
+ }
+
+ /**
+ * Returns the current HTTP response.
+ *
+ * @return The current HTTP response, or <jk>null</jk> if it wasn't
created.
+ */
+ public synchronized RestResponse getResponse() {
+ return getContext().getResponse();
}
}