use of javax.servlet.Servlet in project sling by apache.
the class RequestData method service.
/**
* Helper method to call the servlet for the current content data. If the
* current content data has no servlet, <em>NOT_FOUND</em> (404) error is
* sent and the method terminates.
* <p>
* If the the servlet exists, the
* {@link org.apache.sling.api.SlingConstants#SLING_CURRENT_SERVLET_NAME} request attribute is set
* to the name of that servlet and that servlet name is also set as the
* {@link #setActiveServletName(String) currently active servlet}. After
* the termination of the servlet (normal or throwing a Throwable) the
* request attribute is reset to the previous value. The name of the
* currently active servlet is only reset to the previous value if the
* servlet terminates normally. In case of a Throwable, the active servlet
* name is not reset and indicates which servlet caused the potential abort
* of the request.
*
* @param request The request object for the service method
* @param response The response object for the service method
* @throws IOException May be thrown by the servlet's service method
* @throws ServletException May be thrown by the servlet's service method
*/
public static void service(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException, ServletException {
RequestData requestData = RequestData.getRequestData(request);
Servlet servlet = requestData.getContentData().getServlet();
if (servlet == null) {
response.sendError(HttpServletResponse.SC_NOT_FOUND, "No Servlet to handle request");
} else {
String name = RequestUtil.getServletName(servlet);
// verify the number of service calls in this request
if (requestData.hasServletMaxCallCount(request)) {
throw new TooManyCallsException(name);
}
// replace the current servlet name in the request
Object oldValue = request.getAttribute(SLING_CURRENT_SERVLET_NAME);
request.setAttribute(SLING_CURRENT_SERVLET_NAME, name);
// setup the tracker for this service call
String timerName = name + "#" + requestData.servletCallCounter;
requestData.servletCallCounter++;
requestData.getRequestProgressTracker().startTimer(timerName);
try {
String callerServlet = requestData.setActiveServletName(name);
servlet.service(request, response);
requestData.setActiveServletName(callerServlet);
} finally {
request.setAttribute(SLING_CURRENT_SERVLET_NAME, oldValue);
requestData.getRequestProgressTracker().logTimer(timerName);
}
}
}
use of javax.servlet.Servlet in project sling by apache.
the class VirtualInstance method startJetty.
public synchronized void startJetty() throws Throwable {
if (jettyServer != null) {
return;
}
servletContext = new ServletContextHandler(ServletContextHandler.NO_SECURITY);
servletContext.setContextPath("/");
TopologyConnectorServlet servlet = new TopologyConnectorServlet();
PrivateAccessor.setField(servlet, "config", config);
PrivateAccessor.setField(servlet, "clusterViewService", clusterViewService);
PrivateAccessor.setField(servlet, "announcementRegistry", announcementRegistry);
Mockery context = new JUnit4Mockery();
final HttpService httpService = context.mock(HttpService.class);
context.checking(new Expectations() {
{
allowing(httpService).registerServlet(with(any(String.class)), with(any(Servlet.class)), with(any(Dictionary.class)), with(any(HttpContext.class)));
}
});
PrivateAccessor.setField(servlet, "httpService", httpService);
ComponentContext cc = null;
PrivateAccessor.invoke(servlet, "activate", new Class[] { ComponentContext.class }, new Object[] { cc });
ServletHolder holder = new ServletHolder(servlet);
servletContext.addServlet(holder, "/system/console/topology/*");
jettyServer = new Server();
jettyServer.setHandler(servletContext);
Connector connector = new SelectChannelConnector();
jettyServer.setConnectors(new Connector[] { connector });
jettyServer.start();
}
use of javax.servlet.Servlet in project sling by apache.
the class SlingWebDavServlet method activate.
// ---------- SCR integration
protected void activate(ComponentContext context) throws NamespaceException, ServletException {
this.ioManager.setComponentContext(context);
this.propertyManager.setComponentContext(context);
this.copyMoveManager.setComponentContext(context);
this.deleteManager.setComponentContext(context);
resourceConfig = new SlingResourceConfig(mimeTypeService, context.getProperties(), ioManager, propertyManager, copyMoveManager, deleteManager);
// Register servlet, and set the contextPath field to signal successful
// registration
Servlet simpleServlet = new SlingSimpleWebDavServlet(resourceConfig, getRepository());
httpService.registerServlet(resourceConfig.getServletContextPath(), simpleServlet, resourceConfig.getServletInitParams(), null);
simpleWebDavServletRegistered = true;
}
use of javax.servlet.Servlet in project sling by apache.
the class EvalTagHandler method doEndTag.
/**
* Called after the body has been processed.
*
* @return whether additional evaluations of the body are desired
*/
public int doEndTag() throws JspException {
log.debug("EvalTagHandler doEndTag");
final SlingBindings bindings = (SlingBindings) pageContext.getRequest().getAttribute(SlingBindings.class.getName());
final SlingScriptHelper scriptHelper = bindings.getSling();
final ServletResolver servletResolver = scriptHelper.getService(ServletResolver.class);
final Servlet servlet;
if (!this.ignoreResourceTypeHierarchy) {
// detecte resource
final Resource resource;
if (this.resource != null) {
resource = this.resource;
} else if (this.resourceType != null) {
resource = new SyntheticResource(bindings.getRequest().getResourceResolver(), bindings.getResource().getPath(), this.resourceType);
} else {
resource = bindings.getResource();
}
servlet = servletResolver.resolveServlet(resource, this.script);
} else {
final ResourceResolver rr = bindings.getRequest().getResourceResolver();
final String scriptPath;
if (!script.startsWith("/")) {
// resolve relative script
String parentPath = ResourceUtil.getParent(scriptHelper.getScript().getScriptResource().getPath());
// check if parent resides on search path
for (String sp : rr.getSearchPath()) {
if (parentPath.startsWith(sp)) {
parentPath = parentPath.substring(sp.length());
break;
}
}
scriptPath = parentPath + '/' + script;
} else {
scriptPath = this.script;
}
servlet = servletResolver.resolveServlet(rr, scriptPath);
}
if (servlet == null) {
throw new JspException("Could not find script '" + script + "' referenced in jsp " + scriptHelper.getScript().getScriptResource().getPath());
}
try {
if (flush && !(pageContext.getOut() instanceof BodyContent)) {
// might throw an IOException of course
pageContext.getOut().flush();
}
// wrap the response to get the correct output order
SlingHttpServletResponse response = new JspSlingHttpServletResponseWrapper(pageContext);
servlet.service(pageContext.getRequest(), response);
return EVAL_PAGE;
} catch (Exception e) {
log.error("Error while executing script " + script, e);
throw new JspException("Error while executing script " + script, e);
}
}
use of javax.servlet.Servlet in project sling by apache.
the class JspServletWrapper method loadServlet.
@SuppressWarnings("unchecked")
private Servlet loadServlet() throws ServletException, IOException {
Servlet servlet = null;
try {
if (log.isDebugEnabled()) {
log.debug("Loading servlet " + jspUri);
}
servlet = (Servlet) ctxt.load().newInstance();
AnnotationProcessor annotationProcessor = (AnnotationProcessor) config.getServletContext().getAttribute(AnnotationProcessor.class.getName());
if (annotationProcessor != null) {
annotationProcessor.processAnnotations(servlet);
annotationProcessor.postConstruct(servlet);
}
// update dependents
final List<String> oldDeps = this.dependents;
if (servlet != null && servlet instanceof JspSourceDependent) {
this.dependents = (List<String>) ((JspSourceDependent) servlet).getDependants();
if (this.dependents == null) {
this.dependents = Collections.EMPTY_LIST;
}
this.ctxt.getRuntimeContext().addJspDependencies(this, this.dependents);
}
if (!equals(oldDeps, this.dependents)) {
this.persistDependencies();
}
} catch (final IllegalAccessException e) {
throw new JasperException(e);
} catch (final InstantiationException e) {
throw new JasperException(e);
} catch (final Exception e) {
throw new JasperException(e);
}
servlet.init(config);
return servlet;
}
Aggregations