RestEndpointServlet.java revision 7f874541de09fed43949a7a54ff99cdf3a935382
/*
* The contents of this file are subject to the terms of the Common Development and
* Distribution License (the License). You may not use this file except in compliance with the
* License.
*
* You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
* specific language governing permission and limitations under the License.
*
* When distributing Covered Software, include this CDDL Header Notice in each file and include
* the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
* Header, with the fields enclosed by brackets [] replaced by your own identifying
* information: "Portions copyright [year] [name of copyright owner]".
*
* Copyright 2014 ForgeRock AS.
*/
/**
* Root Servlet for all REST endpoint requests, which are then passed onto the correct underlying servlet, either
* CREST for Resource endpoints or Restlet for Service endpoints.
*
* @since 12.0.0
*/
public class RestEndpointServlet extends HttpServlet {
private final RestletServiceServlet restServiceServlet;
private final RestEndpointManager endpointManager;
/**
* Constructs a new RestEndpointServlet.
*/
public RestEndpointServlet() {
this.crestServlet = new CrestHttpServlet(this, InjectorHolder.getInstance(Key.get(ConnectionFactory.class,
this.restServiceServlet = new RestletServiceServlet(this);
}
/**
* Constructor for test use.
*
* @param crestServlet An instance of a CrestHttpServlet.
* @param restServiceServlet An instance of a RestletServiceServlet.
* @param endpointManager An instance of the RestEndpointManager.
*/
RestEndpointServlet(final CrestHttpServlet crestServlet, final RestletServiceServlet restServiceServlet,
final RestEndpointManager endpointManager) {
this.crestServlet = crestServlet;
this.restServiceServlet = restServiceServlet;
this.endpointManager = endpointManager;
}
/**
* Initialises the CREST and Restlet Servlets.
*
* @throws ServletException If the CREST Servlet init() call fails.
*/
public void init() throws ServletException {
crestServlet.init();
// Don't need to call restServiceServlet.init() as starts Restlet which is not needed as is not created by
// Servlet Container.
}
/**
* Delegates the request to either the CREST or Restlet servlet based on whether the request is for a resource
* or service REST endpoint.
*
* @param request The HttpServletRequest.
* @param response The HttpServletResponse.
* @throws ServletException If the CREST or Restlet Servlet service() calls fail.
* @throws IOException If the CREST or Restlet Servlet service() calls fail.
*/
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException,
if (endpointType == null) {
throw new ServletException("Endpoint Type could not be determined");
}
switch (endpointType) {
case RESOURCE: {
break;
}
case SERVICE: {
break;
}
}
}
/**
* Gets the resource name (resource path) from the HttpServletRequest.
*
* @param req The HttpServletRequest.
* @return The resource name (resource path).
*/
// Treat null path info as root resource.
if (resourceName == null) {
return "";
}
}
return resourceName;
}
/**
* Destroys the CREST and Restlet servlets.
*/
public void destroy() {
}
/**
* Normalises the resource name to ensure that the it does not begin or end with forward slashes.
*
* @param name The resource name.
* @return The normalised resource name.
*/
}
}
return tmp;
}
}