You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@juneau.apache.org by ja...@apache.org on 2017/08/14 00:29:33 UTC

[1/2] incubator-juneau git commit: Add @RestHook support

Repository: incubator-juneau
Updated Branches:
  refs/heads/master 50baa1e20 -> 6d63a4183


http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/6d63a418/juneau-rest/src/main/java/org/apache/juneau/rest/RestServlet.java
----------------------------------------------------------------------
diff --git a/juneau-rest/src/main/java/org/apache/juneau/rest/RestServlet.java b/juneau-rest/src/main/java/org/apache/juneau/rest/RestServlet.java
index fc9ff2e..0c691d0 100644
--- a/juneau-rest/src/main/java/org/apache/juneau/rest/RestServlet.java
+++ b/juneau-rest/src/main/java/org/apache/juneau/rest/RestServlet.java
@@ -48,12 +48,11 @@ public abstract class RestServlet extends HttpServlet {
 	public final synchronized void init(ServletConfig servletConfig) throws ServletException {
 		try {
 			RestConfig rsc = new RestConfig(servletConfig, this.getClass(), null);
-			init(rsc);
-			if (! isInitialized) {
-				// Subclass may not have called super.init(RestServletConfig), so initialize here.
-				createContext(rsc);
-				super.init(servletConfig);
-			}
+			rsc.init(this);
+			RestContext context = createContext(rsc);
+			super.init(servletConfig);
+			context.postInit();
+			context.postInitChildFirst();
 		} catch (RestException e) {
 			// Thrown RestExceptions are simply caught and re-thrown on subsequent calls to service().
 			initException = e;
@@ -75,33 +74,21 @@ public abstract class RestServlet extends HttpServlet {
 		}
 	}
 
-	/**
-	 * Resource initialization method.
-	 *
-	 * <p>
-	 * Identical to {@link Servlet#init(ServletConfig)} except the config object provides access to the external config
-	 * file, configuration properties, and variable resolver defined for this resource.
-	 *
-	 * <p>
-	 * Classes can also use {@link HttpServlet#init()} and {@link RestServlet#getServletConfig()} as well to perform
-	 * initialization.
-	 *
-	 * <p>
-	 * Note that if you override this method, you must first call <code><jk>super</jk>.init(servletConfig)</code>!
-	 *
-	 * <p>
-	 * Resource classes that don't extend from {@link RestServlet} can add this method to their class to get access to
-	 * the config object.
-	 *
-	 * @param config The servlet configuration.
-	 * @throws Exception Any exception can be thrown to signal an initialization failure.
+	/*
+	 * Bypasses the init(ServletConfig) method and just calls the super.init(ServletConfig) method directly.
+	 * Used when subclasses of RestServlet are attached as child resources.
 	 */
-	public synchronized void init(RestConfig config) throws Exception {
-		if (isInitialized)
-			return;
-		createContext(config);
-		super.init(config);
-		init(context);
+	void innerInit(ServletConfig servletConfig) throws ServletException {
+		super.init(servletConfig);
+	}
+
+	/*
+	 * Sets the context object for this servlet.
+	 * Used when subclasses of RestServlet are attached as child resources.
+	 */
+	void setContext(RestContext context) {
+		this.config = context.config;
+		this.context = context;
 	}
 
 	/**
@@ -120,12 +107,13 @@ public abstract class RestServlet extends HttpServlet {
 	public synchronized void init(RestContext context) throws Exception {}
 
 
-	private synchronized void createContext(RestConfig config) throws Exception {
-		if (isInitialized)
-			return;
-		this.config = config;
-		this.context = new RestContext(this, this.getServletContext(), config);
-		this.isInitialized = true;
+	private synchronized RestContext createContext(RestConfig config) throws Exception {
+		if (! isInitialized) {
+			this.config = config;
+			this.context = new RestContext(this, this.getServletContext(), config);
+			this.isInitialized = true;
+		}
+		return context;
 	}
 
 
@@ -142,7 +130,6 @@ public abstract class RestServlet extends HttpServlet {
 	@Override /* Servlet */
 	public void service(HttpServletRequest r1, HttpServletResponse r2) throws ServletException, IOException {
 		try {
-
 			if (initException != null) {
 				if (initException instanceof RestException)
 					throw (RestException)initException;
@@ -166,7 +153,7 @@ public abstract class RestServlet extends HttpServlet {
 	 * Returns the read-only context object that contains all the configuration information about this resource.
 	 *
 	 * <p>
-	 * This object is <jk>null</jk> during the call to {@link #init(RestConfig)} but is populated by the time
+	 * This object is <jk>null</jk> during the call to {@link #init(ServletConfig)} but is populated by the time
 	 * {@link #init()} is called.
 	 *
 	 * <p>
@@ -183,59 +170,6 @@ public abstract class RestServlet extends HttpServlet {
 	}
 
 	/**
-	 * Callback method for listening for successful completion of requests.
-	 *
-	 * <p>
-	 * Subclasses can override this method for gathering performance statistics.
-	 *
-	 * <p>
-	 * The default implementation does nothing.
-	 *
-	 * <p>
-	 * Resources that don't extend from {@link RestServlet} can implement an equivalent method by overriding the
-	 * {@link RestCallHandler#onSuccess(RestRequest, RestResponse, long)} method.
-	 *
-	 * @param req The HTTP request.
-	 * @param res The HTTP response.
-	 * @param time The time in milliseconds it took to process the request.
-	 */
-	protected void onSuccess(RestRequest req, RestResponse res, long time) {}
-
-	/**
-	 * Callback method that gets invoked right before the REST Java method is invoked.
-	 *
-	 * <p>
-	 * Subclasses can override this method to override request headers or set request-duration properties before the
-	 * Java method is invoked.
-	 *
-	 * <p>
-	 * Resources that don't extend from {@link RestServlet} can implement an equivalent method by overriding the
-	 * {@link RestCallHandler#onPreCall(RestRequest)} method.
-	 *
-	 * @param req The HTTP servlet request object.
-	 * @throws RestException If any error occurs.
-	 */
-	protected void onPreCall(RestRequest req) throws RestException {}
-
-	/**
-	 * Callback method that gets invoked right after the REST Java method is invoked, but before the serializer is
-	 * invoked.
-	 *
-	 * <p>
-	 * Subclasses can override this method to override request and response headers, or set/override properties used by
-	 * the serializer.
-	 *
-	 * <p>
-	 * Resources that don't extend from {@link RestServlet} can implement an equivalent method by overriding the
-	 * {@link RestCallHandler#onPostCall(RestRequest,RestResponse)} method.
-	 *
-	 * @param req The HTTP servlet request object.
-	 * @param res The HTTP servlet response object.
-	 * @throws RestException If any error occurs.
-	 */
-	protected void onPostCall(RestRequest req, RestResponse res) throws RestException {}
-
-	/**
 	 * Convenience method for calling <code>getContext().getLogger().log(level, msg, args);</code>
 	 *
 	 * @param level The log level.

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/6d63a418/juneau-rest/src/main/java/org/apache/juneau/rest/annotation/HookEvent.java
----------------------------------------------------------------------
diff --git a/juneau-rest/src/main/java/org/apache/juneau/rest/annotation/HookEvent.java b/juneau-rest/src/main/java/org/apache/juneau/rest/annotation/HookEvent.java
new file mode 100644
index 0000000..74684e8
--- /dev/null
+++ b/juneau-rest/src/main/java/org/apache/juneau/rest/annotation/HookEvent.java
@@ -0,0 +1,419 @@
+// ***************************************************************************************************************************
+// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
+// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
+// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
+// * with the License.  You may obtain a copy of the License at                                                              *
+// *                                                                                                                         *
+// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
+// *                                                                                                                         *
+// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
+// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
+// * specific language governing permissions and limitations under the License.                                              *
+// ***************************************************************************************************************************
+package org.apache.juneau.rest.annotation;
+
+import java.io.*;
+import java.util.*;
+import java.util.logging.*;
+
+import javax.servlet.*;
+import javax.servlet.http.*;
+
+import org.apache.juneau.*;
+import org.apache.juneau.dto.swagger.*;
+import org.apache.juneau.http.*;
+import org.apache.juneau.ini.*;
+import org.apache.juneau.internal.*;
+import org.apache.juneau.rest.*;
+import org.apache.juneau.utils.*;
+
+/**
+ * Identifies servlet and REST call lifecycle events which cause {@link RestHook @RestHook}-annotated Java methods
+ * to be called.
+ */
+public enum HookEvent {
+
+	/**
+	 * Identifies a method that should be called immediately after the <code>HttpServlet.service(HttpServletRequest, HttpServletResponse)</code>
+	 * method is called.
+	 *
+	 * <p>
+	 * Note that you only have access to the raw request and response objects at this point.
+	 *
+	 * <p>
+	 * The list of valid parameter types are as follows:
+	 * <ul>
+	 * 	<li>Servlet request/response objects:
+	 * 		<ul>
+	 * 			<li>{@link HttpServletRequest}
+	 * 			<li>{@link HttpServletResponse}
+	 * 		</ul>
+	 * </ul>
+	 *
+	 * <h6 class='figure'>Example:</h6>
+	 * <p class='bcode'>
+	 * 	<ja>@RestResource</ja>(...)
+	 * 	<jk>public class</jk> MyResource <jk>extends</jk> RestServletDefault {
+	 *
+	 * 		<jc>// Add a request attribute to all incoming requests.</jc>
+	 * 		<ja>@RestHook</ja>(<jsf>START_CALL</jsf>)
+	 * 		<jk>public void</jk> onStartCall(HttpServletRequest req) {
+	 * 			req.setAttribute(<js>"foobar"</js>, <jk>new</jk> FooBar());
+	 * 		}
+	 * 	}
+	 * </p>
+	 *
+	 * <h5 class='section'>Notes:</h5>
+	 * <ul class='spaced-list'>
+	 * 	<li>If the method returns any value, it is ignored.
+	 * 	<li>Multiple START_CALL methods can be defined on a class.
+	 * 		<br>START_CALL methods on parent classes are invoked before START_CALL methods on child classes.
+	 * 		<br>The order of START_CALL method invocations within a class is alphabetical, then by parameter count, then by parameter types.
+	 * 	<li>The method can throw any exception.
+	 * 		<br>{@link RestException RestExceptions} can be thrown to cause a particular HTTP error status code.
+	 * 		<br>All other exceptions cause an HTTP 500 error status code.
+	 * 	<li>Note that if you override a parent method, you probably need to call <code><jk>super</jk>.parentMethod(...)</code>.
+	 * 		<br>The method is still considered part of the parent class for ordering purposes even though it's
+	 * 		overridden by the child class.
+	 * </ul>
+	 */
+	START_CALL,
+
+	/**
+	 * Identifies a method that gets called immediately before the <ja>@RestMethod</ja> annotated method gets called.
+	 *
+	 * <p>
+	 * At this point, the {@link RestRequest} object has been fully initialized, and all {@link RestGuard} and
+	 * {@link RestMatcher} objects have been called.
+	 *
+	 * <p>
+	 * The list of valid parameter types are as follows:
+	 * <ul>
+	 * 	<li>Servlet request/response objects:
+	 * 		<ul>
+	 * 			<li>{@link HttpServletRequest}
+	 * 			<li>{@link HttpServletResponse}
+	 * 		</ul>
+	 * 	<li>Extended request/response objects:
+	 * 		<ul>
+	 * 			<li>{@link RestRequest}
+	 * 			<li>{@link RestResponse}
+	 * 		</ul>
+	 * 	<li>Header objects:
+	 * 		<ul>
+	 * 			<li>{@link Accept}
+	 * 			<li>{@link AcceptCharset}
+	 * 			<li>{@link AcceptEncoding}
+	 * 			<li>{@link AcceptLanguage}
+	 * 			<li>{@link Authorization}
+	 * 			<li>{@link CacheControl}
+	 * 			<li>{@link Connection}
+	 * 			<li>{@link ContentLength}
+	 * 			<li>{@link ContentType}
+	 * 			<li>{@link org.apache.juneau.http.Date}
+	 * 			<li>{@link Expect}
+	 * 			<li>{@link From}
+	 * 			<li>{@link Host}
+	 * 			<li>{@link IfMatch}
+	 * 			<li>{@link IfModifiedSince}
+	 * 			<li>{@link IfNoneMatch}
+	 * 			<li>{@link IfRange}
+	 * 			<li>{@link IfUnmodifiedSince}
+	 * 			<li>{@link MaxForwards}
+	 * 			<li>{@link Pragma}
+	 * 			<li>{@link ProxyAuthorization}
+	 * 			<li>{@link Range}
+	 * 			<li>{@link Referer}
+	 * 			<li>{@link TE}
+	 * 			<li>{@link UserAgent}
+	 * 			<li>{@link Upgrade}
+	 * 			<li>{@link Via}
+	 * 			<li>{@link Warning}
+	 * 			<li>{@link TimeZone}
+	 * 		</ul>
+	 * 	<li>Other objects:
+	 * 		<ul>
+	 * 			<li>{@link ResourceBundle}
+	 * 			<li>{@link MessageBundle}
+	 * 			<li>{@link InputStream}
+	 * 			<li>{@link ServletInputStream}
+	 * 			<li>{@link Reader}
+	 * 			<li>{@link OutputStream}
+	 * 			<li>{@link ServletOutputStream}
+	 * 			<li>{@link Writer}
+	 * 			<li>{@link RequestHeaders}
+	 * 			<li>{@link RequestQuery}
+	 * 			<li>{@link RequestFormData}
+	 * 			<li>{@link HttpMethod}
+	 * 			<li>{@link Logger}
+	 * 			<li>{@link JuneauLogger}
+	 * 			<li>{@link RestContext}
+	 * 			<li>{@link org.apache.juneau.parser.Parser}
+	 * 			<li>{@link Locale}
+	 * 			<li>{@link Swagger}
+	 * 			<li>{@link RequestPathMatch}
+	 * 			<li>{@link RequestBody}
+	 * 			<li>{@link ConfigFile}
+	 * 			<li>{@link UriContext}
+	 * 			<li>{@link UriResolver}
+	 * 		</ul>
+	 * </ul>
+	 *
+	 * <h6 class='figure'>Example:</h6>
+	 * <p class='bcode'>
+	 * 	<ja>@RestResource</ja>(...)
+	 * 	<jk>public class</jk> MyResource <jk>extends</jk> RestServletDefault {
+	 *
+	 * 		<jc>// Log the incoming request.</jc>
+	 * 		<ja>@RestHook</ja>(<jsf>PRE_CALL</jsf>)
+	 * 		<jk>public void</jk> onPreCall(Accept accept, Logger logger) {
+	 * 			logger.fine(<js>"Accept {0} header found."</js>, accept);
+	 * 		}
+	 * 	}
+	 * </p>
+	 *
+	 * <h5 class='section'>Notes:</h5>
+	 * <ul class='spaced-list'>
+	 * 	<li>If the method returns any value, it is ignored.
+	 * 	<li>Multiple PRE_CALL methods can be defined on a class.
+	 * 		<br>PRE_CALL methods on parent classes are invoked before PRE_CALL methods on child classes.
+	 * 		<br>The order of PRE_CALL method invocations within a class is alphabetical, then by parameter count, then by parameter types.
+	 * 	<li>The method can throw any exception.
+	 * 		<br>{@link RestException RestExceptions} can be thrown to cause a particular HTTP error status code.
+	 * 		<br>All other exceptions cause an HTTP 500 error status code.
+	 * 	<li>Note that if you override a parent method, you probably need to call <code><jk>super</jk>.parentMethod(...)</code>.
+	 * 		<br>The method is still considered part of the parent class for ordering purposes even though it's
+	 * 		overridden by the child class.
+	 * 	<li>It's advisable not to mess around with the HTTP body itself since you may end up consuming the body
+	 * 		before the actual REST method has a chance to use it.
+	 * </ul>
+	 */
+	PRE_CALL,
+
+	/**
+	 * Identifies a method that gets called immediately after the <ja>@RestMethod</ja> annotated method gets called.
+	 *
+	 * <p>
+	 * At this point, the output object returned by the method call has been set on the response, but
+	 * {@link RestConverter RestConverters} have not yet been executed and the response has not yet been written.
+	 *
+	 * <p>
+	 * The list of valid parameter types are the same as {@link #PRE_CALL}.
+	 *
+	 * <h6 class='figure'>Example:</h6>
+	 * <p class='bcode'>
+	 * 	<ja>@RestResource</ja>(...)
+	 * 	<jk>public class</jk> MyResource <jk>extends</jk> RestServletDefault {
+	 *
+	 * 		<jc>// Log the result of the request.</jc>
+	 * 		<ja>@RestHook</ja>(<jsf>POST_CALL</jsf>)
+	 * 		<jk>public void</jk> onPostCall(RestResponse res, Logger logger) {
+	 * 			logger.fine(<js>Output {0} was set on the response."</js>, res.getOutput());
+	 * 		}
+	 * 	}
+	 * </p>
+	 *
+	 * <h5 class='section'>Notes:</h5>
+	 * <ul class='spaced-list'>
+	 * 	<li>If the method returns any value, it is ignored.
+	 * 	<li>Multiple POST_CALL methods can be defined on a class.
+	 * 		<br>POST_CALL methods on parent classes are invoked before POST_CALL methods on child classes.
+	 * 		<br>The order of POST_CALL method invocations within a class is alphabetical, then by parameter count, then by parameter types.
+	 * 	<li>The method can throw any exception, although at this point it is too late to set an HTTP error status code.
+	 * 	<li>Note that if you override a parent method, you probably need to call <code><jk>super</jk>.parentMethod(...)</code>.
+	 * 		<br>The method is still considered part of the parent class for ordering purposes even though it's
+	 * 		overridden by the child class.
+	 * </ul>
+	 */
+	POST_CALL,
+
+	/**
+	 * Identifies a method that gets called right before we exit the servlet service method.
+	 *
+	 * <p>
+	 * At this point, the output has been written and flushed.
+	 *
+	 * <p>
+	 * The list of valid parameter types are as follows:
+	 * <ul>
+	 * 	<li>Servlet request/response objects:
+	 * 		<ul>
+	 * 			<li>{@link HttpServletRequest}
+	 * 			<li>{@link HttpServletResponse}
+	 * 		</ul>
+	 * </ul>
+	 *
+	 * <p>
+	 * The following attributes are set on the {@link HttpServletRequest} object that can be useful for logging purposes:
+	 * <ul>
+	 * 	<li><js>"Exception"</js> - Any exceptions thrown during the request.
+	 * 	<li><js>"ExecTime"</js> - Execution time of the request.
+	 * </ul>
+	 *
+	 * <h6 class='figure'>Example:</h6>
+	 * <p class='bcode'>
+	 * 	<ja>@RestResource</ja>(...)
+	 * 	<jk>public class</jk> MyResource <jk>extends</jk> RestServletDefault {
+	 *
+	 * 		<jc>// Log the time it took to execute the request.</jc>
+	 * 		<ja>@RestHook</ja>(<jsf>END_CALL</jsf>)
+	 * 		<jk>public void</jk> onEndCall(RestRequest req, Logger logger) {
+	 * 			Exception e = (Exception)req.getAttribute(<js>"Exception"</js>);
+	 * 			Long execTime = (Long)req.getAttribute(<js>"ExecTime"</js>);
+	 * 			<jk>if</jk> (e != <jk>null</jk>)
+	 * 				logger.warn(e, <js>"Request failed in {0}ms."</js>, execTime);
+	 * 			<jk>else</jk>
+	 * 				logger.fine(<js>"Request finished in {0}ms."</js>, execTime);
+	 * 		}
+	 * 	}
+	 * </p>
+	 *
+	 * <h5 class='section'>Notes:</h5>
+	 * <ul class='spaced-list'>
+	 * 	<li>If the method returns any value, it is ignored.
+	 * 	<li>Multiple END_CALL methods can be defined on a class.
+	 * 		<br>END_CALL methods on parent classes are invoked before END_CALL methods on child classes.
+	 * 		<br>The order of END_CALL method invocations within a class is alphabetical, then by parameter count, then by parameter types.
+	 * 	<li>The method can throw any exception, although at this point it is too late to set an HTTP error status code.
+	 * 	<li>Note that if you override a parent method, you probably need to call <code><jk>super</jk>.parentMethod(...)</code>.
+	 * 		<br>The method is still considered part of the parent class for ordering purposes even though it's
+	 * 		overridden by the child class.
+	 * </ul>
+	 */
+	END_CALL,
+
+	/**
+	 * Identifies a method that gets called during servlet initialization.
+	 *
+	 * <p>
+	 * This method is called from within the {@link Servlet#init(ServletConfig)} method after the {@link RestConfig}
+	 * object has been created and initialized with the annotations defined on the class, but before the
+	 * {@link RestContext} object has been created.
+	 *
+	 * <p>
+	 * The only valid parameter type for this method is {@link RestConfig} which can be used to configure the servlet.
+	 *
+	 * <p>
+	 * An example of this is the <code>PetStoreResource</code> class that uses an init method to perform initialization
+	 * of an internal data structure.
+	 *
+	 * <h6 class='figure'>Example:</h6>
+	 * <p class='bcode'>
+	 * 	<ja>@RestResource</ja>(...)
+	 * 	<jk>public class</jk> PetStoreResource <jk>extends</jk> ResourceJena {
+	 *
+	 * 		<jc>// Our database.</jc>
+	 * 		<jk>private</jk> Map<Integer,Pet> <jf>petDB</jf>;
+	 *
+	 * 		<ja>@RestHook</ja>(<jsf>INIT</jsf>)
+	 * 		<jk>public void</jk> onInit(RestConfig config) <jk>throws</jk> Exception {
+	 * 			<jc>// Load our database from a local JSON file.</jc>
+	 * 			<jf>petDB</jf> = JsonParser.<jsf>DEFAULT</jsf>.parse(getClass().getResourceAsStream(<js>"PetStore.json"</js>), LinkedHashMap.<jk>class</jk>, Integer.<jk>class</jk>, Pet.<jk>class</jk>);
+	 * 		}
+	 * 	}
+	 * </p>
+	 *
+	 * <h5 class='section'>Notes:</h5>
+	 * <ul class='spaced-list'>
+	 * 	<li>If the method returns any value, it is ignored.
+	 * 	<li>Multiple INIT methods can be defined on a class.
+	 * 		<br>INIT methods on parent classes are invoked before INIT methods on child classes.
+	 * 		<br>The order of INIT method invocations within a class is alphabetical, then by parameter count, then by parameter types.
+	 * 	<li>The method can throw any exception causing initialization of the servlet to fail.
+	 * 	<li>Note that if you override a parent method, you probably need to call <code><jk>super</jk>.parentMethod(...)</code>.
+	 * 		<br>The method is still considered part of the parent class for ordering purposes even though it's
+	 * 		overridden by the child class.
+	 * </ul>
+	 */
+	INIT,
+
+	/**
+	 * Identifies a method that gets called immediately after servlet initialization.
+	 *
+	 * <p>
+	 * This method is called from within the {@link Servlet#init(ServletConfig)} method after the {@link RestContext}
+	 * object has been created.
+	 *
+	 * <p>
+	 * The only valid parameter type for this method is {@link RestContext} which can be used to retrieve information
+	 * about the servlet.
+	 *
+	 * <h5 class='section'>Notes:</h5>
+	 * <ul class='spaced-list'>
+	 * 	<li>If the method returns any value, it is ignored.
+	 * 	<li>Multiple POST_INIT methods can be defined on a class.
+	 * 		<br>POST_INIT methods on parent classes are invoked before POST_INIT methods on child classes.
+	 * 		<br>The order of POST_INIT method invocations within a class is alphabetical, then by parameter count, then by parameter types.
+	 * 	<li>The method can throw any exception causing initialization of the servlet to fail.
+	 * 	<li>Note that if you override a parent method, you probably need to call <code><jk>super</jk>.parentMethod(...)</code>.
+	 * 		<br>The method is still considered part of the parent class for ordering purposes even though it's
+	 * 		overridden by the child class.
+	 * </ul>
+	 */
+	POST_INIT,
+
+	/**
+	 * Identical to {@link #POST_INIT} except the order of execution is child-resources first.
+	 *
+	 * <p>
+	 * Use this annotation if you need to perform any kind of initialization on child resources before the parent resource.
+	 *
+	 * <p>
+	 * This method is called from within the {@link Servlet#init(ServletConfig)} method after the {@link RestContext}
+	 * object has been created and after the {@link #POST_INIT} methods have been called.
+	 *
+	 * <p>
+	 * The only valid parameter type for this method is {@link RestContext} which can be used to retrieve information
+	 * about the servlet.
+	 *
+	 * <h5 class='section'>Notes:</h5>
+	 * <ul class='spaced-list'>
+	 * 	<li>If the method returns any value, it is ignored.
+	 * 	<li>Multiple POST_INIT_CHILD_FIRST methods can be defined on a class.
+	 * 		<br>POST_INIT_CHILD_FIRST methods on parent classes are invoked before POST_INIT_CHILD_FIRST methods on child classes.
+	 * 		<br>The order of POST_INIT_CHILD_FIRST method invocations within a class is alphabetical, then by parameter count, then by parameter types.
+	 * 	<li>The method can throw any exception causing initialization of the servlet to fail.
+	 * </ul>
+	 */
+	POST_INIT_CHILD_FIRST,
+
+	/**
+	 * Identifies a method that gets called during servlet destroy.
+	 *
+	 * <p>
+	 * This method is called from within the {@link Servlet#destroy()}.
+	 *
+	 * <p>
+	 * The only valid parameter type for this method is {@link RestContext}, although typically no arguments will
+	 * be specified.
+	 *
+	 * <h6 class='figure'>Example:</h6>
+	 * <p class='bcode'>
+	 * 	<ja>@RestResource</ja>(...)
+	 * 	<jk>public class</jk> PetStoreResource <jk>extends</jk> ResourceJena {
+	 *
+	 * 		<jc>// Our database.</jc>
+	 * 		<jk>private</jk> Map<Integer,Pet> <jf>petDB</jf>;
+	 *
+	 * 		<ja>@RestHook</ja>(<jsf>DESTROY</jsf>)
+	 * 		<jk>public void</jk> onDestroy() {
+	 * 			<jf>petDB</jf> = <jk>null</jk>;
+	 * 		}
+	 * 	}
+	 * </p>
+	 *
+	 * <h5 class='section'>Notes:</h5>
+	 * <ul class='spaced-list'>
+	 * 	<li>If the method returns any value, it is ignored.
+	 * 	<li>Multiple DESTROY methods can be defined on a class.
+	 * 		<br>DESTROY methods on child classes are invoked before DESTROY methods on parent classes.
+	 * 		<br>The order of DESTROY method invocations within a class is alphabetical, then by parameter count, then by parameter types.
+	 * 	<li>In general, destroy methods should not throw any exceptions, although if any are thrown, the stack trace will be
+	 * 		printed to <code>System.err</code>.
+	 * 	<li>Note that if you override a parent method, you probably need to call <code><jk>super</jk>.parentMethod(...)</code>.
+	 * 		<br>The method is still considered part of the parent class for ordering purposes even though it's
+	 * 		overridden by the child class.
+	 * </ul>
+	 */
+	DESTROY
+}

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/6d63a418/juneau-rest/src/main/java/org/apache/juneau/rest/annotation/RestHook.java
----------------------------------------------------------------------
diff --git a/juneau-rest/src/main/java/org/apache/juneau/rest/annotation/RestHook.java b/juneau-rest/src/main/java/org/apache/juneau/rest/annotation/RestHook.java
new file mode 100644
index 0000000..828df43
--- /dev/null
+++ b/juneau-rest/src/main/java/org/apache/juneau/rest/annotation/RestHook.java
@@ -0,0 +1,86 @@
+// ***************************************************************************************************************************
+// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
+// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
+// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
+// * with the License.  You may obtain a copy of the License at                                                              *
+// *                                                                                                                         *
+// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
+// *                                                                                                                         *
+// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
+// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
+// * specific language governing permissions and limitations under the License.                                              *
+// ***************************************************************************************************************************
+package org.apache.juneau.rest.annotation;
+
+import static java.lang.annotation.ElementType.*;
+import static java.lang.annotation.RetentionPolicy.*;
+
+import java.lang.annotation.*;
+
+/**
+ * Identifies Java methods on a resource/servlet class that get invoked during particular lifecycle events of
+ * the servlet or REST call.
+ *
+ * <p>
+ * For example, if you want to add an initialization method to your resource:
+ * <p class='bcode'>
+ * 	<ja>@RestResource</ja>(...)
+ * 	<jk>public class</jk> MyResource  {
+ *
+ * 		<jc>// Our database.</jc>
+ * 		<jk>private</jk> Map&lt;Integer,Object&gt; <jf>myDatabase</jf>;
+ *
+ * 		<ja>@RestHook</ja>(<jsf>INIT</jsf>)
+ * 		<jk>public void</jk> initMyDatabase(RestConfig config) <jk>throws</jk> Exception {
+ * 			<jf>myDatabase</jf> = <jk>new</jk> LinkedHashMap&lt;&gt;();
+ * 		}
+ * 	}
+ * </p>
+ *
+ * <p>
+ * Or if you want to intercept REST calls:
+ * <p class='bcode'>
+ * 	<ja>@RestResource</ja>(...)
+ * 	<jk>public class</jk> MyResource {
+ *
+ * 		<jc>// Add a request attribute to all incoming requests.</jc>
+ * 		<ja>@RestHook</ja>(<jsf>PRE_CALL</jsf>)
+ * 		<jk>public void</jk> onPreCall(RestRequest req) {
+ * 			req.setAttribute(<js>"foo"</js>, <js>"bar"</js>);
+ * 		}
+ * 	}
+ * </p>
+ *
+ * <p>
+ * The hook events can be broken down into two categories:
+ * <ul class='spaced-list'>
+ * 	<li>Resource lifecycle events:
+ * 		<ul>
+ * 			<li>{@link HookEvent#INIT INIT} - Right before initialization.
+ * 			<li>{@link HookEvent#POST_INIT POST_INIT} - Right after initialization.
+ * 			<li>{@link HookEvent#POST_INIT_CHILD_FIRST POST_INIT_CHILD_FIRST} - Right after initialization, but run child methods first.
+ * 			<li>{@link HookEvent#DESTROY DESTROY} - Right before servlet destroy.
+ * 		</ul>
+ * 	<li>REST call lifecycle events:
+ * 		<ul>
+ * 			<li>{@link HookEvent#START_CALL START_CALL} - At the beginning of a REST call.
+ * 			<li>{@link HookEvent#PRE_CALL PRE_CALL} - Right before the <ja>@RestMethod</ja> method is invoked.
+ * 			<li>{@link HookEvent#POST_CALL POST_CALL} - Right after the <ja>@RestMethod</ja> method is invoked.
+ * 			<li>{@link HookEvent#END_CALL END_CALL} - At the end of the REST call after the response has been flushed.
+ * 		</ul>
+ * </ul>
+ *
+ * <p>
+ * See the {@link HookEvent} class for information about individual event types.
+ */
+@Documented
+@Target(METHOD)
+@Retention(RUNTIME)
+@Inherited
+public @interface RestHook {
+
+	/**
+	 * The lifecycle event.
+	 */
+	HookEvent value();
+}

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/6d63a418/juneau-rest/src/main/java/org/apache/juneau/rest/package.html
----------------------------------------------------------------------
diff --git a/juneau-rest/src/main/java/org/apache/juneau/rest/package.html b/juneau-rest/src/main/java/org/apache/juneau/rest/package.html
index 8097a17..e3f1427 100644
--- a/juneau-rest/src/main/java/org/apache/juneau/rest/package.html
+++ b/juneau-rest/src/main/java/org/apache/juneau/rest/package.html
@@ -2485,28 +2485,9 @@
 	<h3 class='topic' onclick='toggle(this)'>4.16 - Listener Methods</h3>	
 	<div class='topic'>
 		<p>
-			Various convenience listener methods are provided on the {@link org.apache.juneau.rest.RestCallHandler} 
-			class that subclasses can use to intercept requests:
+			Various convenience listener methods can be implemented by using the {@link org.apache.juneau.rest.annotation.RestHook @RestHook}
+			annotation on methods in your resource class.
 		</p>
-		<ul class='doctree'>
-			<li class='jac'>
-				{@link org.apache.juneau.rest.RestCallHandler}
-				<ul>
-					<li class='jm'>
-						{@link org.apache.juneau.rest.RestCallHandler#onPreCall(RestRequest) onPreCall(RestRequest)}
-						<br>Callback method that gets invoked right before the REST Java method is invoked.
-					<li class='jm'>
-						{@link org.apache.juneau.rest.RestCallHandler#onPostCall(RestRequest,RestResponse) onPostCall(RestRequest,RestResponse)}
-						<br>Callback method that gets invoked right after the REST Java method is invoked, but before the serializer is invoked.
-					<li class='jm'>
-						{@link org.apache.juneau.rest.RestCallHandler#onSuccess(RestRequest,RestResponse,long) onSuccess(RestRequest,RestResponse,long)}
-						<br>Callback method for listening for successful completion of requests.
-					<li class='jm'>
-						{@link org.apache.juneau.rest.RestLogger#onError(HttpServletRequest,HttpServletResponse,RestException) onError(HttpServletRequest,HttpServletResponse,RestException)}
-						<br>Callback method for logging errors during HTTP requests.
-				</ul>
-			</li>
-		</ul>
 	</div>
 
 	<!-- ======================================================================================================== -->


[2/2] incubator-juneau git commit: Add @RestHook support

Posted by ja...@apache.org.
Add @RestHook support

Project: http://git-wip-us.apache.org/repos/asf/incubator-juneau/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-juneau/commit/6d63a418
Tree: http://git-wip-us.apache.org/repos/asf/incubator-juneau/tree/6d63a418
Diff: http://git-wip-us.apache.org/repos/asf/incubator-juneau/diff/6d63a418

Branch: refs/heads/master
Commit: 6d63a41833f465dcc025867bdb0d20e825ad3fb6
Parents: 50baa1e
Author: JamesBognar <ja...@apache.org>
Authored: Sun Aug 13 20:29:28 2017 -0400
Committer: JamesBognar <ja...@apache.org>
Committed: Sun Aug 13 20:29:28 2017 -0400

----------------------------------------------------------------------
 .../org/apache/juneau/html/annotation/Html.java |   1 -
 .../org/apache/juneau/internal/ClassUtils.java  | 112 ++++-
 .../org/apache/juneau/internal/StringUtils.java |   2 +-
 juneau-core/src/main/javadoc/overview.html      |  46 +-
 .../juneau/examples/rest/DirectoryResource.java |   7 +-
 .../examples/rest/DockerRegistryResource.java   |  15 +-
 .../juneau/examples/rest/PetStoreResource.java  |  12 +-
 .../juneau/examples/rest/SqlQueryResource.java  |  14 +-
 .../apache/juneau/microservice/Resource.java    |  16 +-
 .../juneau/microservice/ResourceGroup.java      |  16 +-
 .../juneau/microservice/ResourceJenaGroup.java  |  16 +-
 .../org/apache/juneau/microservice/package.html |   4 +-
 .../microservice/resources/LogsResource.java    |  12 +-
 .../apache/juneau/rest/test/GroupsResource.java |   6 +-
 .../juneau/rest/test/OnPostCallResource.java    |   8 +-
 .../juneau/rest/test/OnPreCallResource.java     |   6 +-
 .../juneau/rest/test/RestHooksInitResource.java | 274 ++++++++++++
 .../juneau/rest/test/RestHooksResource.java     | 189 +++++++++
 .../java/org/apache/juneau/rest/test/Root.java  |   2 +
 .../org/apache/juneau/rest/test/TestUtils.java  |   2 +
 .../juneau/rest/test/RestHooksInitTest.java     | 112 +++++
 .../apache/juneau/rest/test/RestHooksTest.java  |  72 ++++
 .../java/org/apache/juneau/rest/CallMethod.java |   6 +-
 .../org/apache/juneau/rest/RestCallHandler.java |  66 +--
 .../java/org/apache/juneau/rest/RestConfig.java |  33 +-
 .../org/apache/juneau/rest/RestContext.java     | 286 +++++++++++--
 .../org/apache/juneau/rest/RestServlet.java     | 120 ++----
 .../juneau/rest/annotation/HookEvent.java       | 419 +++++++++++++++++++
 .../apache/juneau/rest/annotation/RestHook.java |  86 ++++
 .../java/org/apache/juneau/rest/package.html    |  23 +-
 30 files changed, 1726 insertions(+), 257 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/6d63a418/juneau-core/src/main/java/org/apache/juneau/html/annotation/Html.java
----------------------------------------------------------------------
diff --git a/juneau-core/src/main/java/org/apache/juneau/html/annotation/Html.java b/juneau-core/src/main/java/org/apache/juneau/html/annotation/Html.java
index f87dfef..a748207 100644
--- a/juneau-core/src/main/java/org/apache/juneau/html/annotation/Html.java
+++ b/juneau-core/src/main/java/org/apache/juneau/html/annotation/Html.java
@@ -117,5 +117,4 @@ public @interface Html {
 	 * </p>
 	 */
 	String anchorText() default "";
-
 }

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/6d63a418/juneau-core/src/main/java/org/apache/juneau/internal/ClassUtils.java
----------------------------------------------------------------------
diff --git a/juneau-core/src/main/java/org/apache/juneau/internal/ClassUtils.java b/juneau-core/src/main/java/org/apache/juneau/internal/ClassUtils.java
index 5903566..b32df3a 100644
--- a/juneau-core/src/main/java/org/apache/juneau/internal/ClassUtils.java
+++ b/juneau-core/src/main/java/org/apache/juneau/internal/ClassUtils.java
@@ -833,6 +833,9 @@ public final class ClassUtils {
 	/**
 	 * Returns all the fields in the specified class and all parent classes.
 	 *
+	 * <p>
+	 * Fields are ordered in either parent-to-child, or child-to-parent order, then alphabetically.
+	 *
 	 * @param c The class to get all fields on.
 	 * @param parentFirst Order them in parent-class-to-child-class order, otherwise child-class-to-parent-class order.
 	 * @return An iterable of all fields in the specified class.
@@ -844,7 +847,7 @@ public final class ClassUtils {
 			public Iterator<Field> iterator() {
 				return new Iterator<Field>(){
 					final Iterator<Class<?>> classIterator = getParentClasses(c, parentFirst, false);
-					Field[] fields = classIterator.hasNext() ? classIterator.next().getDeclaredFields() : new Field[0];
+					Field[] fields = classIterator.hasNext() ? sort(classIterator.next().getDeclaredFields()) : new Field[0];
 					int fIndex = 0;
 					Field next;
 
@@ -858,7 +861,7 @@ public final class ClassUtils {
 						if (next == null) {
 							while (fIndex >= fields.length) {
 								if (classIterator.hasNext()) {
-									fields = classIterator.next().getDeclaredFields();
+									fields = sort(classIterator.next().getDeclaredFields());
 									fIndex = 0;
 								} else {
 									fIndex = -1;
@@ -888,6 +891,9 @@ public final class ClassUtils {
 	/**
 	 * Returns all the methods in the specified class and all parent classes.
 	 *
+	 * <p>
+	 * Methods are ordered in either parent-to-child, or child-to-parent order, then alphabetically.
+	 *
 	 * @param c The class to get all methods on.
 	 * @param parentFirst Order them in parent-class-to-child-class order, otherwise child-class-to-parent-class order.
 	 * @return An iterable of all methods in the specified class.
@@ -899,7 +905,7 @@ public final class ClassUtils {
 			public Iterator<Method> iterator() {
 				return new Iterator<Method>(){
 					final Iterator<Class<?>> classIterator = getParentClasses(c, parentFirst, true);
-					Method[] methods = classIterator.hasNext() ? classIterator.next().getDeclaredMethods() : new Method[0];
+					Method[] methods = classIterator.hasNext() ? sort(classIterator.next().getDeclaredMethods()) : new Method[0];
 					int mIndex = 0;
 					Method next;
 
@@ -913,7 +919,7 @@ public final class ClassUtils {
 						if (next == null) {
 							while (mIndex >= methods.length) {
 								if (classIterator.hasNext()) {
-									methods = classIterator.next().getDeclaredMethods();
+									methods = sort(classIterator.next().getDeclaredMethods());
 									mIndex = 0;
 								} else {
 									mIndex = -1;
@@ -940,6 +946,53 @@ public final class ClassUtils {
 		};
 	}
 
+	private static Comparator<Method> METHOD_COMPARATOR = new Comparator<Method>() {
+
+		@Override
+		public int compare(Method o1, Method o2) {
+			int i = o1.getName().compareTo(o2.getName());
+			if (i == 0) {
+				i = o1.getParameterCount() - o2.getParameterCount();
+				if (i == 0) {
+					for (int j = 0; j < o1.getParameterTypes().length && i == 0; j++) {
+						i = o1.getParameterTypes()[j].getName().compareTo(o2.getParameterTypes()[j].getName());
+					}
+				}
+			}
+			return i;
+		}
+	};
+
+	/**
+	 * Sorts methods in alphabetical order.
+	 *
+	 * @param m The methods to sort.
+	 * @return The same array, but with elements sorted.
+	 */
+	public static Method[] sort(Method[] m) {
+		Arrays.sort(m, METHOD_COMPARATOR);
+		return m;
+	}
+
+	private static Comparator<Field> FIELD_COMPARATOR = new Comparator<Field>() {
+
+		@Override
+		public int compare(Field o1, Field o2) {
+			return o1.getName().compareTo(o2.getName());
+		}
+	};
+
+	/**
+	 * Sorts methods in alphabetical order.
+	 *
+	 * @param m The methods to sort.
+	 * @return The same array, but with elements sorted.
+	 */
+	public static Field[] sort(Field[] m) {
+		Arrays.sort(m, FIELD_COMPARATOR);
+		return m;
+	}
+
 	/**
 	 * Returns a list of all the parent classes of the specified class including the class itself.
 	 *
@@ -1001,4 +1054,55 @@ public final class ClassUtils {
 			.append(Double.class, 0d)
 			.append(Byte.class, (byte)0)
 	);
+
+	/**
+	 * Returns a readable representation of the specified method.
+	 *
+	 * <p>
+	 * The format of the string is <js>"full-qualified-class.method-name(parameter-simple-class-names)"</js>.
+	 *
+	 * @param m The method to stringify.
+	 * @return The stringified method.
+	 */
+	public static String toString(Method m) {
+		StringBuilder sb = new StringBuilder(m.getDeclaringClass().getName() + "." + m.getName() + "(");
+		for (int i = 0; i < m.getParameterCount(); i++) {
+			if (i > 0)
+				sb.append(",");
+			sb.append(m.getParameterTypes()[i].getSimpleName());
+		}
+		sb.append(")");
+		return sb.toString();
+	}
+
+	/**
+	 * Returns a readable representation of the specified field.
+	 *
+	 * <p>
+	 * The format of the string is <js>"full-qualified-class.field-name"</js>.
+	 *
+	 * @param f The field to stringify.
+	 * @return The stringified field.
+	 */
+	public static String toString(Field f) {
+		return f.getDeclaringClass().getName() + "." + f.getName();
+	}
+
+	/**
+	 * Throws an {@link IllegalArgumentException} if the parameters on the method are not in the specified list provided.
+	 *
+	 * @param m The method to test.
+	 * @param args The valid class types (exact) for the arguments.
+	 * @throws FormattedIllegalArgumentException If any of the parameters on the method weren't in the list.
+	 */
+	public static void assertArgsOfType(Method m, Class<?>...args) throws FormattedIllegalArgumentException {
+		for (Class<?> c1 : m.getParameterTypes()) {
+			boolean foundMatch = false;
+			for (Class<?> c2 : args)
+				if (c1 == c2)
+					foundMatch = true;
+			if (! foundMatch)
+				throw new FormattedIllegalArgumentException("Invalid argument of type {0} passed in method {1}.  Only arguments of type {2} are allowed.", c1, m, args);
+		}
+	}
 }

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/6d63a418/juneau-core/src/main/java/org/apache/juneau/internal/StringUtils.java
----------------------------------------------------------------------
diff --git a/juneau-core/src/main/java/org/apache/juneau/internal/StringUtils.java b/juneau-core/src/main/java/org/apache/juneau/internal/StringUtils.java
index 00cbd6f..5abd77d 100644
--- a/juneau-core/src/main/java/org/apache/juneau/internal/StringUtils.java
+++ b/juneau-core/src/main/java/org/apache/juneau/internal/StringUtils.java
@@ -1631,7 +1631,7 @@ public final class StringUtils {
 		if (cm.isClass())
 			return ((Class<?>)o).getName();
 		if (cm.isMethod())
-			return ((Method)o).toGenericString();
+			return ClassUtils.toString((Method)o);
 		return o.toString();
 	}
 }

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/6d63a418/juneau-core/src/main/javadoc/overview.html
----------------------------------------------------------------------
diff --git a/juneau-core/src/main/javadoc/overview.html b/juneau-core/src/main/javadoc/overview.html
index 758af57..fa5ae9a 100644
--- a/juneau-core/src/main/javadoc/overview.html
+++ b/juneau-core/src/main/javadoc/overview.html
@@ -4205,9 +4205,9 @@
 		
 		<h6 class='figure'>Hypothetical RequestEchoResource.createSerializers() method</h6>
 		<p class='bcode'>
-	<jd>/** Override the default rest serializers to add some transforms */</jd>
-	<ja>@Override</ja>
-	<jk>public synchronize void</jk> init(RestConfig config) throws Exception {
+	<jd>/** Override the default rest serializers to add some transforms through an INIT hook*/</jd>
+	<ja>@RestHook</ja>(<jsf>INIT</jsf>)
+	<jk>public void</jk> init(RestConfig config) throws Exception {
 		<jc>// Add bean filters for the HttpServletRequest, HttpSession, and ServletContext objects
 		//		so that you don't show vendor-specific properties on subclasses.
 		// Add Enumeration POJO swap to be able to render the contents of Enumeration properties.
@@ -4219,9 +4219,6 @@
 			.setProperty(<jsf>SERIALIZER_maxDepth</jsf>, 10)
 			.setProperty(<jsf>SERIALIZER_detectRecursions</jsf>, <jk>true</jk>)
 			.setPageLinks(<js>"{...}"</js>);
-		
-		<jc>// Don't forget to call this!</jc>
-		<jk>super</jk>.init(config);
 	}
 		</p>
 		<p>
@@ -6921,12 +6918,32 @@
 				New {@link org.apache.juneau.utils.CalendarUtils} class that encapsulates serialization/parsing logic from {@link org.apache.juneau.transforms.CalendarSwap} and 
 				{@link org.apache.juneau.transforms.DateSwap}.
 			<li>
-				New annotation {@link org.apache.juneau.html.Html#anchorText}.
+				New annotation {@link org.apache.juneau.html.annotation.Html#anchorText}.
 		</ul>
 		
 		<h6 class='topic'>org.apache.juneau.rest</h6>
 		<ul class='spaced-list'>
 			<li>
+				Revamped and simplified servlet and REST-call lifecycle handling through new
+				{@link org.apache.juneau.rest.annotation.RestHook @RestHook} annotation.
+				<ul>
+					<li>The {@link org.apache.juneau.rest.RestServlet#init(ServletConfig)} method is now final and can
+						no longer be extended.
+						<br>Instead, use {@link org.apache.juneau.rest.annotation.HookEvent#INIT} or 
+						{@link org.apache.juneau.rest.annotation.HookEvent#POST_INIT} for initialization.
+					<li>The following methods on {@link org.apache.juneau.rest.RestServlet} have been removed:
+						<ul>
+							<li><code>init(RestConfig)</code> 
+								- Use {@link org.apache.juneau.rest.annotation.HookEvent#INIT} instead.
+							<li><code>onSuccess(RestRequest, RestResponse, long)</code>
+								- Use {@link org.apache.juneau.rest.annotation.HookEvent#END_CALL} instead.
+							<li><code>onPreCall(RestRequest)</code>
+								- Use {@link org.apache.juneau.rest.annotation.HookEvent#PRE_CALL} instead.
+							<li><code>onPostCall(RestRequest, RestResponse)</code>
+								- Use {@link org.apache.juneau.rest.annotation.HookEvent#POST_CALL} instead.
+						</ul> 	
+				</ul>
+			<li>
 				Simplified {@link org.apache.juneau.rest.widget.MenuItemWidget}.
 				<br>Exposes an abstract method {@link org.apache.juneau.rest.widget.MenuItemWidget#getContent(RestRequest)} that
 				can return raw HTML via readers or char-sequences, or any other object (such as HTML5 beans) that will
@@ -6969,7 +6986,6 @@
 					<li>{@link org.apache.juneau.microservice.RestMicroservice#addServlet(Servlet,String) addServlet(Servlet,String)}
 					<li>{@link org.apache.juneau.microservice.RestMicroservice#addServletAttribute(String,Object) addServletAttribute(String,Object)}
 					<li>{@link org.apache.juneau.microservice.RestMicroservice#getServer() getServer()}
-					<li>{@link org.apache.juneau.microservice.RestMicroservice#getServletContextHandler() getServletContextHandler()}
 				</ul>
 		</ul>
 
@@ -7724,7 +7740,7 @@
 				The {@link org.apache.juneau.rest.RestServlet} class now has the following initialization method that allows you to override
 				the config settings define via annotations:
 				<ul>
-					<li>{@link org.apache.juneau.rest.RestServlet#init(RestConfig)} - A modifiable configuration of a resource.
+					<li><code><del>RestServlet.init(RestConfig)</del></code> - A modifiable configuration of a resource.
 				</ul>
 				Non-<code>RestServlet</code> classes must have one of the following to allow it to be instantiated:
 				<ul>
@@ -7735,8 +7751,8 @@
 				<br>
 				Non-<code>RestServlet</code> classes can optionally include the following init methods to gain access to the config and context:
 				<ul>
-					<li><code><jk>public</jk> init(RestConfig)</code>
-					<li><code><jk>public</jk> init(RestContext)</code>
+					<li><code><del><jk>public</jk> init(RestConfig)</del></code>
+					<li><code><del><jk>public</jk> init(RestContext)</del></code>
 				</ul>
 			<li>New annotations added to {@link org.apache.juneau.rest.annotation.RestResource @RestResource} to allow non-<code>RestServlet</code>
 				resources to do the same as subclassing directly from <code>RestServlet</code>:
@@ -9425,7 +9441,7 @@
 				methods that only look for parameters in the URL query string to prevent loading and parsing of URL-Encoded form posts.
 			<li>New <del><code>@QParam</code></del> and <del><code>@HasQParam</code></del> annotations for accessing query parameters from the URL query string.
 			<li><code>&amp;plainText</code> parameter can now specify a false value.
-			<li>Removed properties parameters from {@link org.apache.juneau.rest.RestServlet#onPreCall(RestRequest)} and {@link org.apache.juneau.rest.RestServlet#onPostCall(RestRequest,RestResponse)} methods
+			<li>Removed properties parameters from <code><del>RestServlet.onPreCall(RestRequest)</del></code> and <code><del>RestServlet#onPostCall(RestRequest,RestResponse)</del></code> methods
 				since the properties are already accessible through <code>RestRequest.getProperties()</code>.
 			<li>Added {@link org.apache.juneau.uon.UonSerializer} and {@link org.apache.juneau.uon.UonParser} to serializer and parser lists on 
 				{@link org.apache.juneau.rest.RestServletDefault} and {@link org.apache.juneau.rest.jena.RestServletJenaDefault}.
@@ -10390,8 +10406,8 @@
 			<li>
 				New methods on {@link org.apache.juneau.rest.RestServlet}:
 				<ul>
-					<li>{@link org.apache.juneau.rest.RestServlet#onPreCall(RestRequest)}</li>
-					<li>{@link org.apache.juneau.rest.RestServlet#onPostCall(RestRequest,RestResponse)}</li>
+					<li><code><del>RestServlet.onPreCall(RestRequest)</del></code></li>
+					<li><code><del>RestServlet.onPostCall(RestRequest,RestResponse)</del></code></li>
 				</ul>
 			</li>
 			<li>
@@ -10712,7 +10728,7 @@
 			<li>New <js>"?noTrace"</js> URL parameter to prevent stack traces from being logged (for JUnit testing of error conditions).</li>
 			<li>New <code>RestServletProperties.REST_useStackTraceHashes</code> property to prevent the same stack trace from being logged multiple times.</li>
 			<li>New <code>RestServletProperties.REST_renderResponseStackTraces</code> property for preventing stack traces in responses for security reasons.</li>
-			<li>New overridable <code>RestServlet.onError(HttpServletRequest,HttpServletResponse,RestException,boolean)</code> and {@link org.apache.juneau.rest.RestServlet#onSuccess(RestRequest,RestResponse,long)} methods for plugging in your own logging and peformance monitoring.</li>
+			<li>New overridable <code>RestServlet.onError(HttpServletRequest,HttpServletResponse,RestException,boolean)</code> and <code><del>RestServlet.onSuccess(RestRequest,RestResponse,long)</del></code> methods for plugging in your own logging and peformance monitoring.</li>
 			<li>Eliminated <code>RestServlet.getInitParams()</code> method, since it's now redundant with {@link org.apache.juneau.rest.RestServlet#getProperties()}.</li>
 			<li>Header parameters passed as URL parameters are now case-insensitive.</li>			
 		</ul>

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/6d63a418/juneau-examples-rest/src/main/java/org/apache/juneau/examples/rest/DirectoryResource.java
----------------------------------------------------------------------
diff --git a/juneau-examples-rest/src/main/java/org/apache/juneau/examples/rest/DirectoryResource.java b/juneau-examples-rest/src/main/java/org/apache/juneau/examples/rest/DirectoryResource.java
index d333bb7..101681a 100644
--- a/juneau-examples-rest/src/main/java/org/apache/juneau/examples/rest/DirectoryResource.java
+++ b/juneau-examples-rest/src/main/java/org/apache/juneau/examples/rest/DirectoryResource.java
@@ -12,6 +12,8 @@
 // ***************************************************************************************************************************
 package org.apache.juneau.examples.rest;
 
+import static org.apache.juneau.rest.annotation.HookEvent.*;
+
 import static java.util.logging.Level.*;
 import static javax.servlet.http.HttpServletResponse.*;
 import static org.apache.juneau.html.HtmlDocSerializerContext.*;
@@ -69,9 +71,8 @@ public class DirectoryResource extends Resource {
 
 	private static Logger logger = Logger.getLogger(DirectoryResource.class.getName());
 
-	@Override /* RestServlet */
-	public synchronized void init(RestConfig config) throws Exception {
-		super.init(config);
+	@RestHook(INIT)
+	public void init(RestConfig config) throws Exception {
 		ObjectMap p = config.getProperties();
 		rootDir = new File(p.getString("rootDir"));
 		allowViews = p.getBoolean("allowViews", false);

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/6d63a418/juneau-examples-rest/src/main/java/org/apache/juneau/examples/rest/DockerRegistryResource.java
----------------------------------------------------------------------
diff --git a/juneau-examples-rest/src/main/java/org/apache/juneau/examples/rest/DockerRegistryResource.java b/juneau-examples-rest/src/main/java/org/apache/juneau/examples/rest/DockerRegistryResource.java
index 4b20b7c..abad766 100644
--- a/juneau-examples-rest/src/main/java/org/apache/juneau/examples/rest/DockerRegistryResource.java
+++ b/juneau-examples-rest/src/main/java/org/apache/juneau/examples/rest/DockerRegistryResource.java
@@ -12,6 +12,8 @@
 // ***************************************************************************************************************************
 package org.apache.juneau.examples.rest;
 
+import static org.apache.juneau.rest.annotation.HookEvent.*;
+
 import java.util.*;
 
 import org.apache.juneau.ini.*;
@@ -46,10 +48,15 @@ public class DockerRegistryResource extends Resource {
 
 	RestClient rc;
 
-	@Override /* Servlet */
-	public synchronized void init(RestConfig servletConfig) throws Exception {
-		super.init(servletConfig);
-		ConfigFile cf = servletConfig.getConfigFile();
+	/**
+	 * Initializes the registry URL and rest client.
+	 * 
+	 * @param servletConfig The resource config.
+	 * @throws Exception
+	 */
+	@RestHook(INIT) 
+	public void initRegistry(RestConfig config) throws Exception {
+		ConfigFile cf = config.getConfigFile();
 		registryUrl = cf.getString("DockerRegistry/url");
 		rc = new RestClientBuilder().build();
 	}

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/6d63a418/juneau-examples-rest/src/main/java/org/apache/juneau/examples/rest/PetStoreResource.java
----------------------------------------------------------------------
diff --git a/juneau-examples-rest/src/main/java/org/apache/juneau/examples/rest/PetStoreResource.java b/juneau-examples-rest/src/main/java/org/apache/juneau/examples/rest/PetStoreResource.java
index b5475b7..868c205 100644
--- a/juneau-examples-rest/src/main/java/org/apache/juneau/examples/rest/PetStoreResource.java
+++ b/juneau-examples-rest/src/main/java/org/apache/juneau/examples/rest/PetStoreResource.java
@@ -13,6 +13,7 @@
 package org.apache.juneau.examples.rest;
 
 import static org.apache.juneau.dto.html5.HtmlBuilder.*;
+import static org.apache.juneau.rest.annotation.HookEvent.*;
 
 import java.util.*;
 import java.util.Map;
@@ -68,11 +69,16 @@ public class PetStoreResource extends ResourceJena {
 	// Our database.
 	private Map<Integer,Pet> petDB;
 
-	@Override /* Servlet */
-	public synchronized void init(RestConfig config) throws Exception {
+	/**
+	 * Initializes the pet store database.
+	 * 
+	 * @param RestConfig config The resource config.
+	 * @throws Exception
+	 */
+	@RestHook(INIT) 
+	public void initDatabase(RestConfig config) throws Exception {
 		// Load our database from a local JSON file.
 		petDB = JsonParser.DEFAULT.parse(getClass().getResourceAsStream("PetStore.json"), LinkedHashMap.class, Integer.class, Pet.class);
-		super.init(config);
 	}
 
 	// Exclude the 'breed' and 'getsAlongWith' properties from the beans.

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/6d63a418/juneau-examples-rest/src/main/java/org/apache/juneau/examples/rest/SqlQueryResource.java
----------------------------------------------------------------------
diff --git a/juneau-examples-rest/src/main/java/org/apache/juneau/examples/rest/SqlQueryResource.java b/juneau-examples-rest/src/main/java/org/apache/juneau/examples/rest/SqlQueryResource.java
index a6c0d3e..691f0d0 100644
--- a/juneau-examples-rest/src/main/java/org/apache/juneau/examples/rest/SqlQueryResource.java
+++ b/juneau-examples-rest/src/main/java/org/apache/juneau/examples/rest/SqlQueryResource.java
@@ -15,6 +15,7 @@ package org.apache.juneau.examples.rest;
 import static javax.servlet.http.HttpServletResponse.*;
 import static org.apache.juneau.dto.html5.HtmlBuilder.*;
 import static org.apache.juneau.internal.StringUtils.*;
+import static org.apache.juneau.rest.annotation.HookEvent.*;
 
 import java.sql.*;
 import java.util.*;
@@ -60,10 +61,15 @@ public class SqlQueryResource extends Resource {
 	private String driver, connectionUrl;
 	private boolean allowUpdates, allowTempUpdates, includeRowNums;
 
-	@Override /* RestServlet */
-	public synchronized void init(RestConfig servletConfig) throws Exception {
-		super.init(servletConfig);
-		ConfigFile cf = servletConfig.getConfigFile();
+	/**
+	 * Initializes the registry URL and rest client.
+	 * 
+	 * @param servletConfig The resource config.
+	 * @throws Exception
+	 */
+	@RestHook(INIT) 
+	public void initConnection(RestConfig config) throws Exception {
+		ConfigFile cf = config.getConfigFile();
 
 		driver = cf.getString("SqlQueryResource/driver");
 		connectionUrl = cf.getString("SqlQueryResource/connectionUrl");

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/6d63a418/juneau-microservice/src/main/java/org/apache/juneau/microservice/Resource.java
----------------------------------------------------------------------
diff --git a/juneau-microservice/src/main/java/org/apache/juneau/microservice/Resource.java b/juneau-microservice/src/main/java/org/apache/juneau/microservice/Resource.java
index 26a524a..0579d4b 100755
--- a/juneau-microservice/src/main/java/org/apache/juneau/microservice/Resource.java
+++ b/juneau-microservice/src/main/java/org/apache/juneau/microservice/Resource.java
@@ -12,6 +12,9 @@
 // ***************************************************************************************************************************
 package org.apache.juneau.microservice;
 
+import static org.apache.juneau.rest.annotation.HookEvent.*;
+import static javax.servlet.http.HttpServletResponse.*;
+
 import org.apache.juneau.rest.*;
 import org.apache.juneau.rest.annotation.*;
 import org.apache.juneau.svl.vars.*;
@@ -53,12 +56,19 @@ import org.apache.juneau.svl.vars.*;
 )
 public abstract class Resource extends RestServletDefault {
 
-	@Override /* RestServlet */
-	public synchronized void init(RestConfig config) throws Exception {
+	/**
+	 * Add <code>$ARGS</code> and <code>$MF</code> variable resolvers.
+	 * 
+	 * @param config The resource config.
+	 * @throws Exception
+	 */
+	@RestHook(INIT) 
+	public void addConfigVars(RestConfig config) throws Exception {
+		if (Microservice.getArgs() == null || Microservice.getConfig() == null)
+			throw new RestException(SC_INTERNAL_SERVER_ERROR, "Attempting to use Resource class outside of RestMicroservice.");
 		config
 			.addVars(ArgsVar.class, ManifestFileVar.class)
 			.addVarContextObject(ArgsVar.SESSION_args, Microservice.getArgs())
 			.addVarContextObject(ManifestFileVar.SESSION_manifest, Microservice.getManifest());
-		super.init(config);
 	}
 }

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/6d63a418/juneau-microservice/src/main/java/org/apache/juneau/microservice/ResourceGroup.java
----------------------------------------------------------------------
diff --git a/juneau-microservice/src/main/java/org/apache/juneau/microservice/ResourceGroup.java b/juneau-microservice/src/main/java/org/apache/juneau/microservice/ResourceGroup.java
index 234e000..f3293cc 100755
--- a/juneau-microservice/src/main/java/org/apache/juneau/microservice/ResourceGroup.java
+++ b/juneau-microservice/src/main/java/org/apache/juneau/microservice/ResourceGroup.java
@@ -12,6 +12,9 @@
 // ***************************************************************************************************************************
 package org.apache.juneau.microservice;
 
+import static javax.servlet.http.HttpServletResponse.*;
+import static org.apache.juneau.rest.annotation.HookEvent.*;
+
 import org.apache.juneau.rest.*;
 import org.apache.juneau.rest.annotation.*;
 import org.apache.juneau.svl.vars.*;
@@ -54,12 +57,19 @@ import org.apache.juneau.svl.vars.*;
 )
 public abstract class ResourceGroup extends RestServletGroupDefault {
 
-	@Override /* RestServlet */
-	public synchronized void init(RestConfig config) throws Exception {
+	/**
+	 * Initializes the registry URL and rest clent.
+	 * 
+	 * @param config The resource config.
+	 * @throws Exception
+	 */
+	@RestHook(INIT) 
+	public void addConfigVars(RestConfig config) throws Exception {
+		if (Microservice.getArgs() == null || Microservice.getConfig() == null)
+			throw new RestException(SC_INTERNAL_SERVER_ERROR, "Attempting to use ResourceGroup class outside of RestMicroservice.");
 		config
 			.addVars(ArgsVar.class, ManifestFileVar.class)
 			.addVarContextObject(ArgsVar.SESSION_args, Microservice.getArgs())
 			.addVarContextObject(ManifestFileVar.SESSION_manifest, Microservice.getManifest());
-		super.init(config);
 	}
 }

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/6d63a418/juneau-microservice/src/main/java/org/apache/juneau/microservice/ResourceJenaGroup.java
----------------------------------------------------------------------
diff --git a/juneau-microservice/src/main/java/org/apache/juneau/microservice/ResourceJenaGroup.java b/juneau-microservice/src/main/java/org/apache/juneau/microservice/ResourceJenaGroup.java
index f697cbc..d360850 100644
--- a/juneau-microservice/src/main/java/org/apache/juneau/microservice/ResourceJenaGroup.java
+++ b/juneau-microservice/src/main/java/org/apache/juneau/microservice/ResourceJenaGroup.java
@@ -12,6 +12,9 @@
 // ***************************************************************************************************************************
 package org.apache.juneau.microservice;
 
+import static javax.servlet.http.HttpServletResponse.*;
+import static org.apache.juneau.rest.annotation.HookEvent.*;
+
 import org.apache.juneau.rest.*;
 import org.apache.juneau.rest.annotation.*;
 import org.apache.juneau.rest.jena.*;
@@ -55,12 +58,19 @@ import org.apache.juneau.svl.vars.*;
 )
 public abstract class ResourceJenaGroup extends RestServletJenaGroupDefault {
 
-	@Override /* RestServlet */
-	public synchronized void init(RestConfig config) throws Exception {
+	/**
+	 * Add <code>$ARGS</code> and <code>$MF</code> variable resolvers.
+	 * 
+	 * @param config The resource config.
+	 * @throws Exception
+	 */
+	@RestHook(INIT) 
+	public void addConfigVars(RestConfig config) throws Exception {
+		if (Microservice.getArgs() == null || Microservice.getConfig() == null)
+			throw new RestException(SC_INTERNAL_SERVER_ERROR, "Attempting to use ResourceJenaGroup class outside of RestMicroservice.");
 		config
 			.addVars(ArgsVar.class, ManifestFileVar.class)
 			.addVarContextObject(ArgsVar.SESSION_args, Microservice.getArgs())
 			.addVarContextObject(ManifestFileVar.SESSION_manifest, Microservice.getManifest());
-		super.init(config);
 	}
 }

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/6d63a418/juneau-microservice/src/main/java/org/apache/juneau/microservice/package.html
----------------------------------------------------------------------
diff --git a/juneau-microservice/src/main/java/org/apache/juneau/microservice/package.html b/juneau-microservice/src/main/java/org/apache/juneau/microservice/package.html
index 7b00fca..2b29358 100755
--- a/juneau-microservice/src/main/java/org/apache/juneau/microservice/package.html
+++ b/juneau-microservice/src/main/java/org/apache/juneau/microservice/package.html
@@ -691,8 +691,8 @@
 	}		
 				</p>
 				<p>
-					Additional user-defined variables can be defined at this level by overriding the 
-					{@link org.apache.juneau.microservice.Resource#init(RestConfig)} method
+					Additional user-defined variables can be defined at this level by adding a  
+					{@link org.apache.juneau.rest.annotation.HookEvent#INIT} hook method
 					and using the {@link org.apache.juneau.rest.RestConfig#addVars(Class...)} method.
 				</p>
 			<li class='jm'>

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/6d63a418/juneau-microservice/src/main/java/org/apache/juneau/microservice/resources/LogsResource.java
----------------------------------------------------------------------
diff --git a/juneau-microservice/src/main/java/org/apache/juneau/microservice/resources/LogsResource.java b/juneau-microservice/src/main/java/org/apache/juneau/microservice/resources/LogsResource.java
index 6403957..1a813d9 100755
--- a/juneau-microservice/src/main/java/org/apache/juneau/microservice/resources/LogsResource.java
+++ b/juneau-microservice/src/main/java/org/apache/juneau/microservice/resources/LogsResource.java
@@ -15,6 +15,7 @@ package org.apache.juneau.microservice.resources;
 import static javax.servlet.http.HttpServletResponse.*;
 import static org.apache.juneau.html.HtmlDocSerializerContext.*;
 import static org.apache.juneau.rest.RestContext.*;
+import static org.apache.juneau.rest.annotation.HookEvent.*;
 import static org.apache.juneau.internal.StringUtils.*;
 
 import java.io.*;
@@ -63,9 +64,14 @@ public class LogsResource extends Resource {
 		}
 	};
 
-	@Override /* RestServlet */
-	public synchronized void init(RestConfig config) throws Exception {
-		super.init(config);
+	/**
+	 * Initializes the log directory and formatter.
+	 * 
+	 * @param config The resource config.
+	 * @throws Exception
+	 */
+	@RestHook(INIT) 
+	public void init(RestConfig config) throws Exception {
 		ConfigFile cf = config.getConfigFile();
 
 		logDir = new File(cf.getString("Logging/logDir", "."));

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/6d63a418/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/GroupsResource.java
----------------------------------------------------------------------
diff --git a/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/GroupsResource.java b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/GroupsResource.java
index 233e9ef..acaa410 100644
--- a/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/GroupsResource.java
+++ b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/GroupsResource.java
@@ -13,6 +13,7 @@
 package org.apache.juneau.rest.test;
 
 import static org.apache.juneau.internal.IOUtils.*;
+import static org.apache.juneau.rest.annotation.HookEvent.*;
 
 import org.apache.juneau.*;
 import org.apache.juneau.annotation.*;
@@ -30,10 +31,9 @@ import org.apache.juneau.serializer.*;
 public class GroupsResource extends RestServlet {
 	private static final long serialVersionUID = 1L;
 
-	@Override /* RestServlet */
-	public synchronized void init(RestConfig config) throws Exception {
+	@RestHook(INIT)
+	public void init(RestConfig config) throws Exception {
 		config.addSerializers(SSerializer.class).addParsers(PParser.class);
-		super.init(config);
 	}
 
 	@Produces("text/s1,text/s2")

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/6d63a418/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/OnPostCallResource.java
----------------------------------------------------------------------
diff --git a/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/OnPostCallResource.java b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/OnPostCallResource.java
index 04e1218..10172eb 100644
--- a/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/OnPostCallResource.java
+++ b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/OnPostCallResource.java
@@ -12,6 +12,8 @@
 // ***************************************************************************************************************************
 package org.apache.juneau.rest.test;
 
+import static org.apache.juneau.rest.annotation.HookEvent.*;
+
 import java.util.*;
 
 import org.apache.juneau.*;
@@ -54,7 +56,7 @@ public class OnPostCallResource extends RestServlet {
 				protected void doSerialize(SerializerPipe out, Object o) throws Exception {
 					out.getWriter().write("p1="+getProperty("p1")+",p2="+getProperty("p2")+",p3="+getProperty("p3")+",p4="+getProperty("p4")+",p5="+getProperty("p5")+",contentType="+getProperty("mediaType"));
 				}
-				
+
 				@Override /* SerializerSession */
 				public Map<String,String> getResponseHeaders() {
 					ObjectMap p = getProperties();
@@ -66,8 +68,8 @@ public class OnPostCallResource extends RestServlet {
 		}
 	}
 
-	@Override /* RestServlet */
-	protected void onPostCall(RestRequest req, RestResponse res) {
+	@RestHook(POST_CALL)
+	public void onPostCall(RestRequest req, RestResponse res) {
 		ObjectMap properties = req.getProperties();
 		properties.put("p2", "xp2");
 		properties.put("p4", "xp4");

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/6d63a418/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/OnPreCallResource.java
----------------------------------------------------------------------
diff --git a/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/OnPreCallResource.java b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/OnPreCallResource.java
index 81e24e3..21a784e 100644
--- a/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/OnPreCallResource.java
+++ b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/OnPreCallResource.java
@@ -12,6 +12,8 @@
 // ***************************************************************************************************************************
 package org.apache.juneau.rest.test;
 
+import static org.apache.juneau.rest.annotation.HookEvent.*;
+
 import org.apache.juneau.*;
 import org.apache.juneau.annotation.*;
 import org.apache.juneau.parser.*;
@@ -58,8 +60,8 @@ public class OnPreCallResource extends RestServlet {
 		}
 	}
 
-	@Override /* RestServlet */
-	protected void onPreCall(RestRequest req) {
+	@RestHook(PRE_CALL)
+	public void onPreCall(RestRequest req) {
 		ObjectMap properties = req.getProperties();
 		properties.put("p2", "xp2");
 		properties.put("p4", "xp4");

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/6d63a418/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/RestHooksInitResource.java
----------------------------------------------------------------------
diff --git a/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/RestHooksInitResource.java b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/RestHooksInitResource.java
new file mode 100644
index 0000000..4a85c2c
--- /dev/null
+++ b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/RestHooksInitResource.java
@@ -0,0 +1,274 @@
+// ***************************************************************************************************************************
+// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
+// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
+// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
+// * with the License.  You may obtain a copy of the License at                                                              *
+// *                                                                                                                         *
+// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
+// *                                                                                                                         *
+// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
+// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
+// * specific language governing permissions and limitations under the License.                                              *
+// ***************************************************************************************************************************
+package org.apache.juneau.rest.test;
+
+import static org.apache.juneau.rest.annotation.HookEvent.*;
+
+import java.util.*;
+
+import javax.servlet.*;
+
+import org.apache.juneau.rest.*;
+import org.apache.juneau.rest.annotation.*;
+
+/**
+ * Validates the behavior of the @RestHook(INIT/POST_INIT/POST_INIT_CHILD_FIRST) annotations.
+ */
+@RestResource(
+	path="/testRestHooksInit",
+	children={
+		RestHooksInitResource.Super.class,
+		RestHooksInitResource.Sub.class
+	}
+)
+public class RestHooksInitResource extends RestServletDefault {
+	private static final long serialVersionUID = 1L;
+
+	@RestResource(
+		path="/super"
+	)
+	public static class Super extends RestServletDefault {
+		private static final long serialVersionUID = 1L;
+
+		protected List<String> init = new ArrayList<String>();
+		protected List<String> postInit = new ArrayList<String>();
+		protected List<String> postInitChildFirst = new ArrayList<String>();
+
+		@RestHook(INIT)
+		public void init1c(RestConfig config) {
+			init.add("super-1c");
+		}
+
+		@RestHook(INIT)
+		public void init1a(ServletConfig config) {
+			init.add("super-1a");
+		}
+
+		@RestHook(INIT)
+		public void init1b() {
+			init.add("super-1b");
+		}
+
+		@RestHook(INIT)
+		public void init2a() {
+			init.add("super-2a");
+		}
+
+		@RestHook(POST_INIT)
+		public void postInit1c(RestContext context) {
+			postInit.add("super-1c");
+		}
+
+		@RestHook(POST_INIT)
+		public void postInit1a(RestContext context) {
+			postInit.add("super-1a");
+		}
+
+		@RestHook(POST_INIT)
+		public void postInit1b() {
+			postInit.add("super-1b");
+		}
+
+		@RestHook(POST_INIT)
+		public void postInit2a() {
+			postInit.add("super-2a");
+		}
+
+		@RestHook(POST_INIT_CHILD_FIRST)
+		public void postInitChildFirst1c(RestContext context) {
+			postInitChildFirst.add("super-1c");
+		}
+
+		@RestHook(POST_INIT_CHILD_FIRST)
+		public void postInitChildFirst1a(RestContext context) {
+			postInitChildFirst.add("super-1a");
+		}
+
+		@RestHook(POST_INIT_CHILD_FIRST)
+		public void postInitChildFirst1b() {
+			postInitChildFirst.add("super-1b");
+		}
+
+		@RestHook(POST_INIT_CHILD_FIRST)
+		public void postInitChildFirst2a() {
+			postInitChildFirst.add("super-2a");
+		}
+
+		@RestMethod(name="GET", path="/init")
+		public List<String> getInitEvents() {
+			return init;
+		}
+
+		@RestMethod(name="GET", path="/postInit")
+		public List<String> getPostInitEvents() {
+			return postInit;
+		}
+
+		@RestMethod(name="GET", path="/postInitChildFirst")
+		public List<String> getPostInitChildFirstEvents() {
+			return postInitChildFirst;
+		}
+	}
+
+	@RestResource(
+		path="/sub",
+		children={
+			Child.class
+		}
+	)
+	public static class Sub extends Super {
+		private static final long serialVersionUID = 1L;
+
+		protected static String postInitOrderTest;
+		protected static String postInitChildFirstOrderTest;
+
+		@Override
+		@RestHook(INIT)
+		public void init1c(RestConfig config) {
+			init.add("sub-1c");
+		}
+
+		@Override
+		@RestHook(INIT)
+		public void init1a(ServletConfig config) {
+			init.add("sub-1a");
+		}
+
+		@Override
+		@RestHook(INIT)
+		public void init1b() {
+			init.add("sub-1b");
+		}
+
+		@RestHook(INIT)
+		public void init2b() {
+			init.add("sub-2b");
+		}
+
+		@Override
+		@RestHook(POST_INIT)
+		public void postInit1c(RestContext context) {
+			postInit.add("sub-1c");
+		}
+
+		@Override
+		@RestHook(POST_INIT)
+		public void postInit1a(RestContext context) {
+			postInit.add("sub-1a");
+		}
+
+		@Override
+		@RestHook(POST_INIT)
+		public void postInit1b() {
+			postInit.add("sub-1b");
+		}
+
+		@RestHook(POST_INIT)
+		public void postInit2b() {
+			postInit.add("sub-2b");
+		}
+
+		@Override
+		@RestHook(POST_INIT_CHILD_FIRST)
+		public void postInitChildFirst1c(RestContext context) {
+			postInitChildFirst.add("sub-1c");
+		}
+
+		@Override
+		@RestHook(POST_INIT_CHILD_FIRST)
+		public void postInitChildFirst1a(RestContext context) {
+			postInitChildFirst.add("sub-1a");
+		}
+
+		@Override
+		@RestHook(POST_INIT_CHILD_FIRST)
+		public void postInitChildFirst1b() {
+			postInitChildFirst.add("sub-1b");
+		}
+
+		@RestHook(POST_INIT_CHILD_FIRST)
+		public void postInitChildFirst2b() {
+			postInitChildFirst.add("sub-2b");
+		}
+
+		@RestHook(POST_INIT)
+		public void postInitOrderTestSub() {
+			postInitOrderTest = "PARENT";
+		}
+
+		@RestHook(POST_INIT_CHILD_FIRST)
+		public void postInitChildFirstOrderTestSub() {
+			postInitChildFirstOrderTest = "PARENT";
+		}
+
+		@RestMethod(name="GET", path="/postInitOrder")
+		public String postInitOrderTest() {
+			return postInitOrderTest;
+		}
+
+		@RestMethod(name="GET", path="/postInitChildFirstOrder")
+		public String postInitChildFirstOrderTest() {
+			return postInitChildFirstOrderTest;
+		}
+	}
+
+	@RestResource(
+		path="/child"
+	)
+	public static class Child extends Super {
+		private static final long serialVersionUID = 1L;
+
+		@Override
+		@RestHook(INIT)
+		public void init1c(RestConfig config) {
+			init.add("child-1c");
+		}
+
+		@RestHook(INIT)
+		public void init2b() {
+			init.add("child-2b");
+		}
+
+		@Override
+		@RestHook(POST_INIT)
+		public void postInit1c(RestContext context) {
+			postInit.add("child-1c");
+		}
+
+		@RestHook(POST_INIT)
+		public void postInit2b() {
+			postInit.add("child-2b");
+		}
+
+		@Override
+		@RestHook(POST_INIT_CHILD_FIRST)
+		public void postInitChildFirst1c(RestContext context) {
+			postInitChildFirst.add("child-1c");
+		}
+
+		@RestHook(POST_INIT_CHILD_FIRST)
+		public void postInitChildFirst2b() {
+			postInitChildFirst.add("child-2b");
+		}
+
+		@RestHook(POST_INIT)
+		public void postInitOrderTestSub() {
+			Sub.postInitOrderTest = "CHILD";
+		}
+
+		@RestHook(POST_INIT_CHILD_FIRST)
+		public void postInitChildFirstOrderTestSub() {
+			Sub.postInitChildFirstOrderTest = "CHILD";
+		}
+	}
+}

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/6d63a418/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/RestHooksResource.java
----------------------------------------------------------------------
diff --git a/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/RestHooksResource.java b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/RestHooksResource.java
new file mode 100644
index 0000000..0597272
--- /dev/null
+++ b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/RestHooksResource.java
@@ -0,0 +1,189 @@
+// ***************************************************************************************************************************
+// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
+// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
+// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
+// * with the License.  You may obtain a copy of the License at                                                              *
+// *                                                                                                                         *
+// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
+// *                                                                                                                         *
+// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
+// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
+// * specific language governing permissions and limitations under the License.                                              *
+// ***************************************************************************************************************************
+package org.apache.juneau.rest.test;
+
+import static org.apache.juneau.rest.annotation.HookEvent.*;
+
+import java.util.*;
+
+import javax.servlet.http.*;
+
+import org.apache.juneau.http.*;
+import org.apache.juneau.rest.*;
+import org.apache.juneau.rest.annotation.*;
+import org.apache.juneau.utils.*;
+
+/**
+ * Validates the behavior of the @RestHook(START/PRE/POST) annotations.
+ */
+@RestResource(
+	path="/testRestHooks",
+	children={
+		RestHooksResource.Start.class,
+		RestHooksResource.Pre.class,
+		RestHooksResource.Post.class,
+	}
+)
+public class RestHooksResource extends RestServletDefault {
+	private static final long serialVersionUID = 1L;
+
+	@RestResource(
+		path="/start"
+	)
+	public static class Start extends StartParent {
+		private static final long serialVersionUID = 1L;
+
+		private boolean start3Called;
+
+		@RestHook(START_CALL)
+		public void start3() {
+			start3Called = true;
+		}
+
+		@RestHook(START_CALL)
+		public void start4(HttpServletRequest req, HttpServletResponse res) {
+			res.setHeader("start3-called", ""+start3Called);
+			start3Called = false;
+			if (res.getHeader("start4-called") != null)
+				throw new RuntimeException("start4 called multiple times.");
+			res.setHeader("start4-called", "true");
+		}
+
+		@RestMethod(path="/")
+		public Map<String,Object> getHeaders(RestRequest req, RestResponse res) {
+			return new AMap<String,Object>()
+				.append("1", res.getHeader("start1-called"))
+				.append("2", res.getHeader("start2-called"))
+				.append("3", res.getHeader("start3-called"))
+				.append("4", res.getHeader("start4-called"));
+		}
+	}
+
+	public static class StartParent extends RestServletDefault {
+		private static final long serialVersionUID = 1L;
+
+		private boolean start1Called;
+
+		@RestHook(START_CALL)
+		public void start1() {
+			start1Called = true;
+		}
+
+		@RestHook(START_CALL)
+		public void start2(HttpServletRequest req, HttpServletResponse res) {
+			res.setHeader("start1-called", ""+start1Called);
+			start1Called = false;
+			if (res.getHeader("start2-called") != null)
+				throw new RuntimeException("start2 called multiple times.");
+			res.setHeader("start2-called", "true");
+		}
+	}
+
+	@RestResource(
+		path="/pre"
+	)
+	public static class Pre extends PreParent {
+		private static final long serialVersionUID = 1L;
+
+		private boolean pre3Called;
+
+		@RestHook(PRE_CALL)
+		public void pre3() {
+			pre3Called = true;
+		}
+
+		@RestHook(PRE_CALL)
+		public void pre4(HttpServletRequest req, HttpServletResponse res) {
+			res.setHeader("pre3-called", ""+pre3Called);
+			pre3Called = false;
+			if (res.getHeader("pre4-called") != null)
+				throw new RuntimeException("pre4 called multiple times.");
+			res.setHeader("pre4-called", "true");
+		}
+
+		@RestMethod(path="/")
+		public Map<String,Object> getHeaders(RestRequest req, RestResponse res) {
+			return new AMap<String,Object>()
+				.append("1", res.getHeader("pre1-called"))
+				.append("2", res.getHeader("pre2-called"))
+				.append("3", res.getHeader("pre3-called"))
+				.append("4", res.getHeader("pre4-called"));
+		}
+	}
+
+	public static class PreParent extends RestServletDefault {
+		private static final long serialVersionUID = 1L;
+
+		private boolean pre1Called;
+
+		@RestHook(PRE_CALL)
+		public void pre1() {
+			pre1Called = true;
+		}
+
+		@RestHook(PRE_CALL)
+		public void pre2(Accept accept, RestRequest req, RestResponse res) {
+			res.setHeader("pre1-called", ""+pre1Called);
+			pre1Called = false;
+			if (res.getHeader("pre2-called") != null)
+				throw new RuntimeException("pre2 called multiple times.");
+			res.setHeader("pre2-called", "true");
+		}
+	}
+
+	@RestResource(
+		path="/post"
+	)
+	public static class Post extends PostParent {
+		private static final long serialVersionUID = 1L;
+		private boolean post3Called;
+
+		@RestHook(POST_CALL)
+		public void post3() {
+			post3Called = true;
+		}
+
+		@RestHook(POST_CALL)
+		public void post4(HttpServletRequest req, HttpServletResponse res) {
+			res.setHeader("post3-called", ""+post3Called);
+			post3Called = false;
+			if (res.getHeader("post4-called") != null)
+				throw new RuntimeException("post4 called multiple times.");
+			res.setHeader("post4-called", "true");
+		}
+
+		@RestMethod(path="/")
+		public String doGet() {
+			return "OK";
+		}
+	}
+
+	public static class PostParent extends RestServletDefault {
+		private static final long serialVersionUID = 1L;
+		private boolean post1Called;
+
+		@RestHook(POST_CALL)
+		public void post1() {
+			post1Called = true;
+		}
+
+		@RestHook(POST_CALL)
+		public void post2(Accept accept, RestRequest req, RestResponse res) {
+			res.setHeader("post1-called", ""+post1Called);
+			post1Called = false;
+			if (res.getHeader("post2-called") != null)
+				throw new RuntimeException("post2 called multiple times.");
+			res.setHeader("post2-called", "true");
+		}
+	}
+}

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/6d63a418/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/Root.java
----------------------------------------------------------------------
diff --git a/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/Root.java b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/Root.java
index f47564a..a7f51e1 100644
--- a/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/Root.java
+++ b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/Root.java
@@ -64,6 +64,8 @@ import org.apache.juneau.rest.labels.*;
 		QueryResource.class,
 		RequestBeanProxyResource.class,
 		RestClient2Resource.class,
+		RestHooksInitResource.class,
+		RestHooksResource.class,
 		SerializersResource.class,
 		StaticFilesResource.class,
 		ThirdPartyProxyResource.class,

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/6d63a418/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/TestUtils.java
----------------------------------------------------------------------
diff --git a/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/TestUtils.java b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/TestUtils.java
index 638acda..4fab49a 100644
--- a/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/TestUtils.java
+++ b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/TestUtils.java
@@ -50,6 +50,8 @@ public class TestUtils {
 	 * Assert that the object equals the specified string after running it through ws.toString().
 	 */
 	public static void assertObjectEquals(String s, Object o, WriterSerializer ws) {
+		if ("xxx".equals(s))
+			System.err.println("Actual=" + ws.toString(o));
 		Assert.assertEquals(s, ws.toString(o));
 	}
 

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/6d63a418/juneau-rest-test/src/test/java/org/apache/juneau/rest/test/RestHooksInitTest.java
----------------------------------------------------------------------
diff --git a/juneau-rest-test/src/test/java/org/apache/juneau/rest/test/RestHooksInitTest.java b/juneau-rest-test/src/test/java/org/apache/juneau/rest/test/RestHooksInitTest.java
new file mode 100644
index 0000000..0743682
--- /dev/null
+++ b/juneau-rest-test/src/test/java/org/apache/juneau/rest/test/RestHooksInitTest.java
@@ -0,0 +1,112 @@
+// ***************************************************************************************************************************
+// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
+// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
+// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
+// * with the License.  You may obtain a copy of the License at                                                              *
+// *                                                                                                                         *
+// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
+// *                                                                                                                         *
+// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
+// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
+// * specific language governing permissions and limitations under the License.                                              *
+// ***************************************************************************************************************************
+package org.apache.juneau.rest.test;
+
+import static org.apache.juneau.rest.test.TestUtils.*;
+
+import java.util.*;
+
+import org.apache.juneau.rest.client.*;
+import org.junit.*;
+
+/**
+ * Validates the behavior of the @RestHook(INIT/POST_INIT/POST_INIT_CHILD_FIRST) annotations.
+ */
+public class RestHooksInitTest extends RestTestcase {
+
+	private static String URL = "/testRestHooksInit";
+
+	//====================================================================================================
+	// @RestHook(INIT)
+	//====================================================================================================
+	@Test
+	public void testInit() throws Exception {
+		RestClient client = TestMicroservice.DEFAULT_CLIENT;
+		String e;
+		Object r;
+
+		r = client.doGet(URL + "/super/init").getResponse(List.class);
+		e = "['super-1a','super-1b','super-1c','super-2a']";
+		assertObjectEquals(e, r);
+
+		r = client.doGet(URL + "/sub/init").getResponse(List.class);
+		e = "['sub-1a','sub-1b','sub-1c','super-2a','sub-2b']";
+		assertObjectEquals(e, r);
+
+		r = client.doGet(URL + "/sub/child/init").getResponse(List.class);
+		e = "['super-1a','super-1b','child-1c','super-2a','child-2b']";
+		assertObjectEquals(e, r);
+	}
+
+	//====================================================================================================
+	// @RestHook(POST_INIT)
+	//====================================================================================================
+	@Test
+	public void testPostInit() throws Exception {
+		RestClient client = TestMicroservice.DEFAULT_CLIENT;
+		String e;
+		Object r;
+
+		r = client.doGet(URL + "/super/postInit").getResponse(List.class);
+		e = "['super-1a','super-1b','super-1c','super-2a']";
+		assertObjectEquals(e, r);
+
+		r = client.doGet(URL + "/sub/postInit").getResponse(List.class);
+		e = "['sub-1a','sub-1b','sub-1c','super-2a','sub-2b']";
+		assertObjectEquals(e, r);
+
+		r = client.doGet(URL + "/sub/child/postInit").getResponse(List.class);
+		e = "['super-1a','super-1b','child-1c','super-2a','child-2b']";
+		assertObjectEquals(e, r);
+	}
+
+	//====================================================================================================
+	// @RestHook(POST_INIT_CHILD_FIRST)
+	//====================================================================================================
+	@Test
+	public void testPostInitChildFirst() throws Exception {
+		RestClient client = TestMicroservice.DEFAULT_CLIENT;
+		String e;
+		Object r;
+
+		r = client.doGet(URL + "/super/postInitChildFirst").getResponse(List.class);
+		e = "['super-1a','super-1b','super-1c','super-2a']";
+		assertObjectEquals(e, r);
+
+		r = client.doGet(URL + "/sub/postInitChildFirst").getResponse(List.class);
+		e = "['sub-1a','sub-1b','sub-1c','super-2a','sub-2b']";
+		assertObjectEquals(e, r);
+
+		r = client.doGet(URL + "/sub/child/postInitChildFirst").getResponse(List.class);
+		e = "['super-1a','super-1b','child-1c','super-2a','child-2b']";
+		assertObjectEquals(e, r);
+	}
+
+	//====================================================================================================
+	// @RestHook(POST_INIT/POST_INIT_CHILD_FIRST) orders
+	//====================================================================================================
+	@Test
+	public void testPostInitChildFirstOrder() throws Exception {
+		RestClient client = TestMicroservice.DEFAULT_CLIENT;
+		String e;
+		Object r;
+
+		r = client.doGet(URL + "/sub/postInitOrder").getResponse(String.class);
+		e = "'CHILD'";
+		assertObjectEquals(e, r);
+
+		r = client.doGet(URL + "/sub/postInitChildFirstOrder").getResponse(String.class);
+		e = "'PARENT'";
+		assertObjectEquals(e, r);
+	}
+}

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/6d63a418/juneau-rest-test/src/test/java/org/apache/juneau/rest/test/RestHooksTest.java
----------------------------------------------------------------------
diff --git a/juneau-rest-test/src/test/java/org/apache/juneau/rest/test/RestHooksTest.java b/juneau-rest-test/src/test/java/org/apache/juneau/rest/test/RestHooksTest.java
new file mode 100644
index 0000000..0e85392
--- /dev/null
+++ b/juneau-rest-test/src/test/java/org/apache/juneau/rest/test/RestHooksTest.java
@@ -0,0 +1,72 @@
+// ***************************************************************************************************************************
+// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
+// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
+// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
+// * with the License.  You may obtain a copy of the License at                                                              *
+// *                                                                                                                         *
+// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
+// *                                                                                                                         *
+// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
+// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
+// * specific language governing permissions and limitations under the License.                                              *
+// ***************************************************************************************************************************
+package org.apache.juneau.rest.test;
+
+import static org.apache.juneau.rest.test.TestUtils.*;
+import static org.junit.Assert.*;
+
+import java.util.*;
+
+import org.apache.http.*;
+import org.apache.juneau.rest.client.*;
+import org.junit.*;
+
+/**
+ * Validates the behavior of the @RestHook(START/PRE/POST) annotations.
+ */
+public class RestHooksTest extends RestTestcase {
+
+	private static String URL = "/testRestHooks";
+
+	//====================================================================================================
+	// @RestHook(START)
+	//====================================================================================================
+	@Test
+	public void testStart() throws Exception {
+		RestClient client = TestMicroservice.DEFAULT_CLIENT;
+		String e;
+		Object r;
+
+		r = client.doGet(URL + "/start").getResponse(Map.class);
+		e = "{'1':'true','2':'true','3':'true','4':'true'}";
+		assertObjectEquals(e, r);
+	}
+
+	//====================================================================================================
+	// @RestHook(START)
+	//====================================================================================================
+	@Test
+	public void testPre() throws Exception {
+		RestClient client = TestMicroservice.DEFAULT_CLIENT;
+		String e;
+		Object r;
+
+		r = client.doGet(URL + "/pre").getResponse(Map.class);
+		e = "{'1':'true','2':'true','3':'true','4':'true'}";
+		assertObjectEquals(e, r);
+	}
+
+	//====================================================================================================
+	// @RestHook(POST)
+	//====================================================================================================
+	@Test
+	public void testPost() throws Exception {
+		RestClient client = TestMicroservice.DEFAULT_CLIENT;
+
+		HttpResponse res = client.doGet(URL + "/post").getResponse();
+		assertEquals("true", res.getFirstHeader("post1-called").getValue());
+		assertEquals("true", res.getFirstHeader("post2-called").getValue());
+		assertEquals("true", res.getFirstHeader("post3-called").getValue());
+		assertEquals("true", res.getFirstHeader("post4-called").getValue());
+	}
+}

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/6d63a418/juneau-rest/src/main/java/org/apache/juneau/rest/CallMethod.java
----------------------------------------------------------------------
diff --git a/juneau-rest/src/main/java/org/apache/juneau/rest/CallMethod.java b/juneau-rest/src/main/java/org/apache/juneau/rest/CallMethod.java
index 5f89647..04b1c89 100644
--- a/juneau-rest/src/main/java/org/apache/juneau/rest/CallMethod.java
+++ b/juneau-rest/src/main/java/org/apache/juneau/rest/CallMethod.java
@@ -397,7 +397,7 @@ class CallMethod implements Comparable<CallMethod>  {
 
 				pathPattern = new UrlPathPattern(p);
 
-				params = context.findParams(method, plainParams, pathPattern);
+				params = context.findParams(method, plainParams, pathPattern, false);
 
 				if (sgb != null) {
 					serializers = sgb.build();
@@ -809,7 +809,7 @@ class CallMethod implements Comparable<CallMethod>  {
 				return SC_PRECONDITION_FAILED;
 		}
 
-		context.getCallHandler().onPreCall(req);
+		context.preCall(req, res);
 
 		Object[] args = new Object[params.length];
 		for (int i = 0; i < params.length; i++) {
@@ -836,7 +836,7 @@ class CallMethod implements Comparable<CallMethod>  {
 				if (output != null || ! res.getOutputStreamCalled())
 					res.setOutput(output);
 
-			context.getCallHandler().onPostCall(req, res);
+			context.postCall(req, res);
 
 			if (res.hasOutput()) {
 				output = res.getOutput();

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/6d63a418/juneau-rest/src/main/java/org/apache/juneau/rest/RestCallHandler.java
----------------------------------------------------------------------
diff --git a/juneau-rest/src/main/java/org/apache/juneau/rest/RestCallHandler.java b/juneau-rest/src/main/java/org/apache/juneau/rest/RestCallHandler.java
index 07e6dfa..088d09f 100644
--- a/juneau-rest/src/main/java/org/apache/juneau/rest/RestCallHandler.java
+++ b/juneau-rest/src/main/java/org/apache/juneau/rest/RestCallHandler.java
@@ -44,7 +44,6 @@ public class RestCallHandler {
 
 	private final RestContext context;
 	private final RestLogger logger;
-	private final RestServlet restServlet;
 	private final Map<String,CallRouter> callRouters;
 
 	/**
@@ -56,7 +55,6 @@ public class RestCallHandler {
 		this.context = context;
 		this.logger = context.getLogger();
 		this.callRouters = context.getCallRouters();
-		this.restServlet = context.getRestServlet();  // Null if this isn't a RestServlet!
 	}
 
 	/**
@@ -133,6 +131,8 @@ public class RestCallHandler {
 				}
 			}
 
+			context.startCall(r1, r2);
+
 			RestRequest req = createRequest(r1);
 			RestResponse res = createResponse(req, r2);
 			String method = req.getMethod();
@@ -178,16 +178,22 @@ public class RestCallHandler {
 				handleResponse(req, res, output);
 			}
 
-			onSuccess(req, res, System.currentTimeMillis() - startTime);
-
 			// Make sure our writer in RestResponse gets written.
 			res.flushBuffer();
 
+			r1.setAttribute("ExecTime", System.currentTimeMillis() - startTime);
+
 		} catch (RestException e) {
 			handleError(r1, r2, e);
+			r1.setAttribute("Exception", e);
 		} catch (Throwable e) {
-			handleError(r1, r2, new RestException(SC_INTERNAL_SERVER_ERROR, e));
+			RestException e2 = new RestException(SC_INTERNAL_SERVER_ERROR, e);
+			handleError(r1, r2, e2);
+			r1.setAttribute("Exception", e);
 		}
+
+		context.finishCall(r1, r2);
+
 		logger.log(FINE, "HTTP: [{0} {1}] finished in {2}ms", r1.getMethod(), r1.getRequestURI(), System.currentTimeMillis()-startTime);
 	}
 
@@ -311,56 +317,6 @@ public class RestCallHandler {
 	}
 
 	/**
-	 * Callback method for listening for successful completion of requests.
-	 *
-	 * <p>
-	 * Subclasses can override this method for gathering performance statistics.
-	 *
-	 * <p>
-	 * The default implementation does nothing.
-	 *
-	 * @param req The HTTP request.
-	 * @param res The HTTP response.
-	 * @param time The time in milliseconds it took to process the request.
-	 */
-	protected void onSuccess(RestRequest req, RestResponse res, long time) {
-		if (restServlet != null)
-			restServlet.onSuccess(req, res, time);
-	}
-
-	/**
-	 * Callback method that gets invoked right before the REST Java method is invoked.
-	 *
-	 * <p>
-	 * Subclasses can override this method to override request headers or set request-duration properties before the
-	 * Java method is invoked.
-	 *
-	 * @param req The HTTP servlet request object.
-	 * @throws RestException If any error occurs.
-	 */
-	protected void onPreCall(RestRequest req) throws RestException {
-		if (restServlet != null)
-			restServlet.onPreCall(req);
-	}
-
-	/**
-	 * Callback method that gets invoked right after the REST Java method is invoked, but before the serializer is
-	 * invoked.
-	 *
-	 * <p>
-	 * Subclasses can override this method to override request and response headers, or set/override properties used by
-	 * the serializer.
-	 *
-	 * @param req The HTTP servlet request object.
-	 * @param res The HTTP servlet response object.
-	 * @throws RestException If any error occurs.
-	 */
-	protected void onPostCall(RestRequest req, RestResponse res) throws RestException {
-		if (restServlet != null)
-			restServlet.onPostCall(req, res);
-	}
-
-	/**
 	 * Returns the session objects for the specified request.
 	 *
 	 * <p>

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/6d63a418/juneau-rest/src/main/java/org/apache/juneau/rest/RestConfig.java
----------------------------------------------------------------------
diff --git a/juneau-rest/src/main/java/org/apache/juneau/rest/RestConfig.java b/juneau-rest/src/main/java/org/apache/juneau/rest/RestConfig.java
index 59e1cd0..59702d2 100644
--- a/juneau-rest/src/main/java/org/apache/juneau/rest/RestConfig.java
+++ b/juneau-rest/src/main/java/org/apache/juneau/rest/RestConfig.java
@@ -18,6 +18,7 @@ import static org.apache.juneau.internal.StringUtils.*;
 import static org.apache.juneau.rest.RestUtils.*;
 
 import java.io.*;
+import java.lang.reflect.Method;
 import java.util.*;
 
 import javax.activation.*;
@@ -273,6 +274,36 @@ public class RestConfig implements ServletConfig {
 		}
 	}
 
+	/*
+	 * Calls all @RestHook(INIT) methods on the specified resource object.
+	 */
+	void init(Object resource) throws ServletException {
+		Map<String,Method> map = new LinkedHashMap<String,Method>();
+		for (Method m : ClassUtils.getAllMethods(this.resourceClass, true)) {
+			if (m.isAnnotationPresent(RestHook.class) && m.getAnnotation(RestHook.class).value() == HookEvent.INIT) {
+				String sig = ClassUtils.getMethodSignature(m);
+				if (! map.containsKey(sig))
+					map.put(sig, m);
+			}
+		}
+		for (Method m : map.values()) {
+			ClassUtils.assertArgsOfType(m, RestConfig.class, ServletConfig.class);
+			Class<?>[] argTypes = m.getParameterTypes();
+			Object[] args = new Object[argTypes.length];
+			for (int i = 0; i < args.length; i++) {
+				if (argTypes[i] == RestConfig.class)
+					args[i] = this;
+				else
+					args[i] = this.inner;
+			}
+			try {
+				m.invoke(resource, args);
+			} catch (Exception e) {
+				throw new RestServletException("Exception thrown from @RestHook(INIT) method {0}.", m).initCause(e);
+			}
+		}
+	}
+
 	/**
 	 * Adds the specified {@link Var} classes to this config.
 	 *
@@ -1539,7 +1570,7 @@ public class RestConfig implements ServletConfig {
 	 *
 	 * <p>
 	 * These properties can be modified during servlet initialization.
-	 * However, any modifications made after {@link RestServlet#init(RestConfig)} has been called will have no effect.
+	 * However, any modifications made after {@link RestServlet#init(ServletConfig)} has been called will have no effect.
 	 *
 	 * @return The configuration properties for this resource.  Never <jk>null</jk>.
 	 */

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/6d63a418/juneau-rest/src/main/java/org/apache/juneau/rest/RestContext.java
----------------------------------------------------------------------
diff --git a/juneau-rest/src/main/java/org/apache/juneau/rest/RestContext.java b/juneau-rest/src/main/java/org/apache/juneau/rest/RestContext.java
index d70b295..2a77eb6 100644
--- a/juneau-rest/src/main/java/org/apache/juneau/rest/RestContext.java
+++ b/juneau-rest/src/main/java/org/apache/juneau/rest/RestContext.java
@@ -29,6 +29,7 @@ import java.util.concurrent.atomic.*;
 
 import javax.activation.*;
 import javax.servlet.*;
+import javax.servlet.http.*;
 
 import org.apache.juneau.*;
 import org.apache.juneau.encoders.*;
@@ -249,7 +250,7 @@ public final class RestContext extends Context {
 
 
 	private final Object resource;
-	private final RestConfig config;
+	final RestConfig config;
 	private final boolean
 		allowHeaderParams,
 		allowBodyParam,
@@ -312,6 +313,25 @@ public final class RestContext extends Context {
 	private final RestContext parentContext;
 	private final RestResourceResolver resourceResolver;
 
+	// Lifecycle methods
+	private final Method[] 
+		postInitMethods, 
+		postInitChildFirstMethods, 
+		preCallMethods, 
+		postCallMethods, 
+		startCallMethods, 
+		endCallMethods, 
+		destroyMethods;
+	private final RestParam[][] 
+		preCallMethodParams, 
+		postCallMethodParams;
+	private final Class<?>[][] 
+		postInitMethodParams, 
+		postInitChildFirstMethodParams, 
+		startCallMethodParams, 
+		endCallMethodParams, 
+		destroyMethodParams;
+
 	// In-memory cache of images and stylesheets in the org.apache.juneau.rest.htdocs package.
 	private final Map<String,StreamResource> staticFilesCache = new ConcurrentHashMap<String,StreamResource>();
 
@@ -396,6 +416,24 @@ public final class RestContext extends Context {
 			List<String> methodsFound = new LinkedList<String>();   // Temporary to help debug transient duplicate method issue.
 			Map<String,CallRouter.Builder> routers = new LinkedHashMap<String,CallRouter.Builder>();
 			Map<String,CallMethod> _javaRestMethods = new LinkedHashMap<String,CallMethod>();
+			Map<String,Method>
+				_startCallMethods = new LinkedHashMap<String,Method>(),
+				_preCallMethods = new LinkedHashMap<String,Method>(),
+				_postCallMethods = new LinkedHashMap<String,Method>(),
+				_endCallMethods = new LinkedHashMap<String,Method>(),
+				_postInitMethods = new LinkedHashMap<String,Method>(),
+				_postInitChildFirstMethods = new LinkedHashMap<String,Method>(),
+				_destroyMethods = new LinkedHashMap<String,Method>();
+			List<RestParam[]>
+				_preCallMethodParams = new ArrayList<RestParam[]>(),
+				_postCallMethodParams = new ArrayList<RestParam[]>();
+			List<Class<?>[]>
+				_startCallMethodParams = new ArrayList<Class<?>[]>(),
+				_endCallMethodParams = new ArrayList<Class<?>[]>(),
+				_postInitMethodParams = new ArrayList<Class<?>[]>(),
+				_postInitChildFirstMethodParams = new ArrayList<Class<?>[]>(),
+				_destroyMethodParams = new ArrayList<Class<?>[]>();
+
 			for (java.lang.reflect.Method method : resource.getClass().getMethods()) {
 				if (method.isAnnotationPresent(RestMethod.class)) {
 					RestMethod a = method.getAnnotation(RestMethod.class);
@@ -467,7 +505,86 @@ public final class RestContext extends Context {
 					}
 				}
 			}
+
+			for (Method m : ClassUtils.getAllMethods(resource.getClass(), true)) {
+				if (ClassUtils.isPublic(m) && m.isAnnotationPresent(RestHook.class)) {
+					HookEvent he = m.getAnnotation(RestHook.class).value();
+					String sig = ClassUtils.getMethodSignature(m);
+					switch(he) {
+						case PRE_CALL: {
+							if (! _preCallMethods.containsKey(sig)) {
+								_preCallMethods.put(sig, m);
+								_preCallMethodParams.add(findParams(m, false, null, true));
+							}
+							break;
+						}
+						case POST_CALL: {
+							if (! _postCallMethods.containsKey(sig)) {
+								_postCallMethods.put(sig, m);
+								_postCallMethodParams.add(findParams(m, false, null, true));
+							}
+							break;
+						}
+						case START_CALL: {
+							if (! _startCallMethods.containsKey(sig)) {
+								_startCallMethods.put(sig, m);
+								_startCallMethodParams.add(m.getParameterTypes());
+								ClassUtils.assertArgsOfType(m, HttpServletRequest.class, HttpServletResponse.class);
+							}
+							break;
+						}
+						case END_CALL: {
+							if (! _endCallMethods.containsKey(sig)) {
+								_endCallMethods.put(sig, m);
+								_endCallMethodParams.add(m.getParameterTypes());
+								ClassUtils.assertArgsOfType(m, HttpServletRequest.class, HttpServletResponse.class);
+							}
+							break;
+						}
+						case POST_INIT: {
+							if (! _postInitMethods.containsKey(sig)) {
+								_postInitMethods.put(sig, m);
+								_postInitMethodParams.add(m.getParameterTypes());
+								ClassUtils.assertArgsOfType(m, RestContext.class);
+							}
+							break;
+						}
+						case POST_INIT_CHILD_FIRST: {
+							if (! _postInitChildFirstMethods.containsKey(sig)) {
+								_postInitChildFirstMethods.put(sig, m);
+								_postInitChildFirstMethodParams.add(m.getParameterTypes());
+								ClassUtils.assertArgsOfType(m, RestContext.class);
+							}
+							break;
+						}
+						case DESTROY: {
+							if (! _destroyMethods.containsKey(sig)) {
+								_destroyMethods.put(sig, m);
+								_destroyMethodParams.add(m.getParameterTypes());
+								ClassUtils.assertArgsOfType(m, RestContext.class);
+							}
+							break;
+						}
+						default: // Ignore INIT
+					}
+				}
+			}
+
 			this.callMethods = Collections.unmodifiableMap(_javaRestMethods);
+			this.preCallMethods = _preCallMethods.values().toArray(new Method[_preCallMethods.size()]);
+			this.postCallMethods = _postCallMethods.values().toArray(new Method[_postCallMethods.size()]);
+			this.startCallMethods = _startCallMethods.values().toArray(new Method[_startCallMethods.size()]);
+			this.endCallMethods = _endCallMethods.values().toArray(new Method[_endCallMethods.size()]);
+			this.postInitMethods = _postInitMethods.values().toArray(new Method[_postInitMethods.size()]);
+			this.postInitChildFirstMethods = _postInitChildFirstMethods.values().toArray(new Method[_postInitChildFirstMethods.size()]);
+			this.destroyMethods = _destroyMethods.values().toArray(new Method[_destroyMethods.size()]);
+			this.preCallMethodParams = _preCallMethodParams.toArray(new RestParam[_preCallMethodParams.size()][]);
+			this.postCallMethodParams = _postCallMethodParams.toArray(new RestParam[_postCallMethodParams.size()][]);
+			this.startCallMethodParams = _startCallMethodParams.toArray(new Class[_startCallMethodParams.size()][]);
+			this.endCallMethodParams = _endCallMethodParams.toArray(new Class[_endCallMethodParams.size()][]);
+			this.postInitMethodParams = _postInitMethodParams.toArray(new Class[_postInitMethodParams.size()][]);
+			this.postInitChildFirstMethodParams = _postInitChildFirstMethodParams.toArray(new Class[_postInitChildFirstMethodParams.size()][]);
+			this.destroyMethodParams = _destroyMethodParams.toArray(new Class[_destroyMethodParams.size()][]);
 
 			Map<String,CallRouter> _callRouters = new LinkedHashMap<String,CallRouter>();
 			for (CallRouter.Builder crb : routers.values())
@@ -504,30 +621,14 @@ public final class RestContext extends Context {
 					childConfig = new RestConfig(config.inner, o.getClass(), this);
 				}
 
-				if (r instanceof RestServlet) {
-					RestServlet rs = (RestServlet)r;
-					rs.init(childConfig);
-					if (rs.getContext() == null)
-						throw new RestException(SC_INTERNAL_SERVER_ERROR, "Servlet {0} not initialized.  init(RestConfig) was not called.  This can occur if you've overridden this method but didn't call super.init(RestConfig).", rs.getClass().getName());
-					path = childConfig.path;
-					childResources.put(path, rs.getContext());
-				} else {
-
-					// Call the init(RestConfig) method.
-					java.lang.reflect.Method m2 = findPublicMethod(r.getClass(), "init", Void.class, RestConfig.class);
-					if (m2 != null)
-						m2.invoke(r, childConfig);
-
-					RestContext rc2 = new RestContext(r, servletContext, childConfig);
-
-					// Call the init(RestContext) method.
-					m2 = findPublicMethod(r.getClass(), "init", Void.class, RestContext.class);
-					if (m2 != null)
-						m2.invoke(r, rc2);
-
-					path = childConfig.path;
-					childResources.put(path, rc2);
-				}
+				childConfig.init(r);
+				if (r instanceof RestServlet)
+					((RestServlet)r).innerInit(childConfig);
+				RestContext rc2 = new RestContext(r, servletContext, childConfig);
+				if (r instanceof RestServlet)
+					((RestServlet)r).setContext(rc2);
+				path = childConfig.path;
+				childResources.put(path, rc2);
 			}
 
 			callHandler = config.callHandler == null ? new RestCallHandler(this) : resolve(resource, RestCallHandler.class, config.callHandler, this);
@@ -1446,10 +1547,11 @@ public final class RestContext extends Context {
 	 * @param method The Java method being called.
 	 * @param methodPlainParams Whether plain-params setting is specified.
 	 * @param pathPattern The parsed URL path pattern.
+	 * @param isPreOrPost Whether this is a <ja>@RestMethodPre</ja> or <ja>@RestMethodPost</ja>.
 	 * @return The array of resolvers.
 	 * @throws ServletException If an annotation usage error was detected.
 	 */
-	protected RestParam[] findParams(Method method, boolean methodPlainParams, UrlPathPattern pathPattern) throws ServletException {
+	protected RestParam[] findParams(Method method, boolean methodPlainParams, UrlPathPattern pathPattern, boolean isPreOrPost) throws ServletException {
 
 		Type[] pt = method.getGenericParameterTypes();
 		Annotation[][] pa = method.getParameterAnnotations();
@@ -1492,6 +1594,10 @@ public final class RestContext extends Context {
 			}
 
 			if (rp[i] == null) {
+
+				if (isPreOrPost)
+					throw new RestServletException("Invalid parameter specified for method ''{0}'' at index position {1}", method, i);
+
 				Path p = null;
 				for (Annotation a : pa[i])
 					if (a instanceof Path)
@@ -1503,7 +1609,7 @@ public final class RestContext extends Context {
 					int idx = attrIndex++;
 					String[] vars = pathPattern.getVars();
 					if (vars.length <= idx)
-						throw new RestServletException("Number of attribute parameters in method ''{0}'' exceeds the number of URL pattern variables.", method.getName());
+						throw new RestServletException("Number of attribute parameters in method ''{0}'' exceeds the number of URL pattern variables.", method);
 
 					// Check for {#} variables.
 					String idxs = String.valueOf(idx);
@@ -1521,6 +1627,124 @@ public final class RestContext extends Context {
 		return rp;
 	}
 
+	/*
+	 * Calls all @RestHook(PRE) methods.
+	 */
+	void preCall(RestRequest req, RestResponse res) throws RestException {
+		for (int i = 0; i < preCallMethods.length; i++)
+			preOrPost(resource, preCallMethods[i], preCallMethodParams[i], req, res);
+	}
+
+	/*
+	 * Calls all @RestHook(POST) methods.
+	 */
+	void postCall(RestRequest req, RestResponse res) throws RestException {
+		for (int i = 0; i < postCallMethods.length; i++)
+			preOrPost(resource, postCallMethods[i], postCallMethodParams[i], req, res);
+	}
+
+	private static void preOrPost(Object resource, Method m, RestParam[] mp, RestRequest req, RestResponse res) throws RestException {
+		if (m != null) {
+			Object[] args = new Object[mp.length];
+			for (int i = 0; i < mp.length; i++) {
+				try {
+					args[i] = mp[i].resolve(req, res);
+				} catch (RestException e) {
+					throw e;
+				} catch (Exception e) {
+					throw new RestException(SC_BAD_REQUEST,
+						"Invalid data conversion.  Could not convert {0} ''{1}'' to type ''{2}'' on method ''{3}.{4}''.",
+						mp[i].getParamType().name(), mp[i].getName(), mp[i].getType(), m.getDeclaringClass().getName(), m.getName()
+					).initCause(e);
+				}
+			}
+			try {
+				m.invoke(resource, args);
+			} catch (RestException e) {
+				throw e;
+			} catch (Exception e) {
+				throw new RestException(SC_INTERNAL_SERVER_ERROR, e.getLocalizedMessage()).initCause(e);
+			}
+		}
+	}
+
+	/*
+	 * Calls all @RestHook(START) methods.
+	 */
+	void startCall(HttpServletRequest req, HttpServletResponse res) {
+		for (int i = 0; i < startCallMethods.length; i++)
+			startOrFinish(resource, startCallMethods[i], startCallMethodParams[i], req, res);
+	}
+
+	/*
+	 * Calls all @RestHook(FINISH) methods.
+	 */
+	void finishCall(HttpServletRequest req, HttpServletResponse res) {
+		for (int i = 0; i < endCallMethods.length; i++)
+			startOrFinish(resource, endCallMethods[i], endCallMethodParams[i], req, res);
+	}
+
+	private static void startOrFinish(Object resource, Method m, Class<?>[] p, HttpServletRequest req, HttpServletResponse res) {
+		if (m != null) {
+			Object[] args = new Object[p.length];
+			for (int i = 0; i < p.length; i++) {
+				if (p[i] == HttpServletRequest.class)
+					args[i] = req;
+				else if (p[i] == HttpServletResponse.class)
+					args[i] = res;
+			}
+			try {
+				m.invoke(resource, args);
+			} catch (RestException e) {
+				throw e;
+			} catch (Exception e) {
+				throw new RestException(SC_INTERNAL_SERVER_ERROR, e.getLocalizedMessage()).initCause(e);
+			}
+		}
+	}
+
+	/*
+	 * Calls all @RestHook(POST_INIT) methods.
+	 */
+	void postInit() throws ServletException {
+		for (int i = 0; i < postInitMethods.length; i++)
+			postInitOrDestroy(resource, postInitMethods[i], postInitMethodParams[i]);
+		for (RestContext childContext : this.childResources.values())
+			childContext.postInit();
+	}
+
+	/*
+	 * Calls all @RestHook(POST_INIT_CHILD_FIRST) methods.
+	 */
+	void postInitChildFirst() throws ServletException {
+		for (RestContext childContext : this.childResources.values())
+			childContext.postInitChildFirst();
+		for (int i = 0; i < postInitChildFirstMethods.length; i++)
+			postInitOrDestroy(resource, postInitChildFirstMethods[i], postInitChildFirstMethodParams[i]);
+	}
+
+	private void postInitOrDestroy(Object r, Method m, Class<?>[] p) {
+		if (m != null) {
+			Object[] args = new Object[p.length];
+			for (int i = 0; i < p.length; i++) {
+				if (p[i] == RestContext.class)
+					args[i] = this;
+				else if (p[i] == RestConfig.class)
+					args[i] = this.config;
+				else if (p[i] == ServletConfig.class)
+					args[i] = this.config.inner;
+			}
+			try {
+				m.invoke(r, args);
+			} catch (RestException e) {
+				throw e;
+			} catch (Exception e) {
+				throw new RestException(SC_INTERNAL_SERVER_ERROR, e.getLocalizedMessage()).initCause(e);
+			}
+		}
+	}
+
+
 	/**
 	 * Returns the URL-encoding parser associated with this resource.
 	 *
@@ -1753,6 +1977,14 @@ public final class RestContext extends Context {
 	 * Calls {@link Servlet#destroy()} on any child resources defined on this resource.
 	 */
 	protected void destroy() {
+		for (int i = 0; i < destroyMethods.length; i++) {
+			try {
+				postInitOrDestroy(resource, destroyMethods[i], destroyMethodParams[i]);
+			} catch (Exception e) {
+				e.printStackTrace();
+			}
+		}
+
 		for (RestContext r : childResources.values()) {
 			r.destroy();
 			if (r.resource instanceof Servlet)