JUNEAU-142 BasicRest subclasses need programmatic access to
RestRequest/RestResponse
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 @@
)
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 @@
// 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 @@
return builder;
}
+ //-----------------------------------------------------------------------------------------------------------------
+ // Context methods.
+ //-----------------------------------------------------------------------------------------------------------------
+
/**
* Returns the read-only context object that contains all the configuration information about this resource.
*
@@ -143,12 +147,72 @@
* @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 method for calling <c>getContext().getProperties();</c>
+ *
+ * @return The resource properties as an {@link RestContextProperties}.
+ * @see RestContext#getProperties()
+ */
+ public RestContextProperties getProperties() {
+ return getContext().getProperties();
+ }
+
//-----------------------------------------------------------------------------------------------------------------
- // Other methods
+ // Convenience logger methods
+ //-----------------------------------------------------------------------------------------------------------------
+
+ @Override /* GenericServlet */
+ public void log(String msg) {
+ logger.info(msg);
+ }
+
+ @Override /* GenericServlet */
+ 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);
+ }
+
+ //-----------------------------------------------------------------------------------------------------------------
+ // Lifecycle methods
//-----------------------------------------------------------------------------------------------------------------
/**
@@ -177,58 +241,23 @@
}
@Override /* GenericServlet */
- public void log(String msg) {
- logger.info(msg);
+ public synchronized void destroy() {
+ if (context != null)
+ context.destroy();
+ super.destroy();
}
- @Override /* GenericServlet */
- public void log(String msg, Throwable cause) {
- logger.info(cause, msg);
- }
-
- /**
- * Convenience method for calling <c>getContext().getLogger().log(level, msg, args);</c>
- *
- * @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);
- }
-
- /**
- * Convenience method for calling <c>getContext().getLogger().logObjects(level, msg, args);</c>
- *
- * @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);
- }
-
- /**
- * Convenience method for calling <c>getContext().getLogger().log(level, cause, msg, args);</c>
- *
- * @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, or <jk>null</jk> if it wasn't created.
*/
- public RestRequest getRequest() {
- if (context == null)
- return null;
- return context.getRequest();
+ public synchronized RestRequest getRequest() {
+ return getContext().getRequest();
}
/**
@@ -236,26 +265,7 @@
*
* @return The current HTTP response, or <jk>null</jk> if it wasn't created.
*/
- public RestResponse getResponse() {
- if (context == null)
- return null;
- return context.getResponse();
- }
-
- @Override /* GenericServlet */
- public synchronized void destroy() {
- if (context != null)
- context.destroy();
- super.destroy();
- }
-
- /**
- * Convenience method for calling <c>getContext().getProperties();</c>
- *
- * @return The resource properties as an {@link RestContextProperties}.
- * @see RestContext#getProperties()
- */
- public RestContextProperties getProperties() {
- return getContext().getProperties();
+ public synchronized RestResponse getResponse() {
+ return getContext().getResponse();
}
}