use of org.apache.felix.http.base.internal.handler.ServletHandler in project felix by apache.
the class ServletResponseWrapper method sendError.
@Override
public void sendError(final int code, final String message) throws IOException {
resetBuffer();
setStatus(code);
boolean invokeSuper = true;
if (invocationCount.incrementAndGet() == 1) {
// If we are allowed to have a body
if (code != SC_NO_CONTENT && code != SC_NOT_MODIFIED && code != SC_PARTIAL_CONTENT && code >= SC_OK) {
final Throwable exception = (Throwable) request.getAttribute(RequestDispatcher.ERROR_EXCEPTION);
final ServletHandler errorResolution = (errorRegistry == null ? null : errorRegistry.getErrorHandler(code, exception));
if (errorResolution != null) {
try {
request.setAttribute(RequestDispatcher.ERROR_STATUS_CODE, new Integer(code));
if (message != null) {
request.setAttribute(RequestDispatcher.ERROR_MESSAGE, message);
}
request.setAttribute(RequestDispatcher.ERROR_REQUEST_URI, request.getRequestURI());
if (this.servletName != null) {
request.setAttribute(RequestDispatcher.ERROR_SERVLET_NAME, this.servletName);
}
final String servletPath = null;
final String pathInfo = request.getRequestURI();
// XXX
final String queryString = null;
final RequestInfo requestInfo = new RequestInfo(servletPath, pathInfo, queryString, pathInfo);
final FilterHandler[] filterHandlers = errorRegistry.getFilterHandlers(errorResolution, DispatcherType.ERROR, request.getRequestURI());
final ServletRequestWrapper reqWrapper = new ServletRequestWrapper(request, errorResolution.getContext(), requestInfo, null, false, null, null);
final FilterChain filterChain = new InvocationChain(errorResolution, filterHandlers);
filterChain.doFilter(reqWrapper, this);
invokeSuper = false;
} catch (final ServletException e) {
// ignore
} finally {
request.removeAttribute(RequestDispatcher.ERROR_STATUS_CODE);
request.removeAttribute(RequestDispatcher.ERROR_MESSAGE);
request.removeAttribute(RequestDispatcher.ERROR_REQUEST_URI);
request.removeAttribute(RequestDispatcher.ERROR_SERVLET_NAME);
request.removeAttribute(RequestDispatcher.ERROR_EXCEPTION);
request.removeAttribute(RequestDispatcher.ERROR_EXCEPTION_TYPE);
}
}
}
}
if (invokeSuper) {
super.sendError(code, message);
}
}
use of org.apache.felix.http.base.internal.handler.ServletHandler in project felix by apache.
the class ErrorPageRegistry method get.
/**
* Get the servlet handling the exception
* @param exception Error exception
* @return The servlet handling the error or {@code null}
*/
private ServletHandler get(final Throwable exception) {
if (exception == null) {
return null;
}
ServletHandler servletHandler = null;
Class<?> throwableClass = exception.getClass();
while (servletHandler == null && throwableClass != null) {
final List<ServletHandler> list = this.errorMapping.get(throwableClass.getName());
if (list != null) {
servletHandler = list.get(0);
}
if (servletHandler == null) {
throwableClass = throwableClass.getSuperclass();
if (!Throwable.class.isAssignableFrom(throwableClass)) {
throwableClass = null;
}
}
}
return servletHandler;
}
use of org.apache.felix.http.base.internal.handler.ServletHandler in project felix by apache.
the class ErrorPageRegistry method removeErrorHandling.
private void removeErrorHandling(final ServletInfo info, final long code, final String exception) {
final String key = (exception != null ? exception : String.valueOf(code));
final List<ServletHandler> list = errorMapping.get(key);
if (list != null) {
int index = 0;
final Iterator<ServletHandler> i = list.iterator();
while (i.hasNext()) {
final ServletHandler handler = i.next();
if (handler.getServletInfo().equals(info)) {
final List<ServletHandler> newList = new ArrayList<ServletHandler>(list);
newList.remove(handler);
if (index == 0) {
handler.destroy();
index++;
while (index < list.size()) {
final ServletHandler next = list.get(index);
ErrorRegistrationStatus nextStatus = null;
for (final ErrorRegistrationStatus s : this.status) {
if (s.handler.getServletInfo().equals(next.getServletInfo())) {
nextStatus = s;
break;
}
}
this.removeReason(nextStatus, code, exception, DTOConstants.FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE);
final int reason = next.init();
this.addReason(nextStatus, code, exception, reason);
if (reason == -1) {
break;
} else {
newList.remove(next);
}
}
}
if (newList.isEmpty()) {
errorMapping.remove(key);
} else {
errorMapping.put(key, newList);
}
break;
}
index++;
}
}
}
use of org.apache.felix.http.base.internal.handler.ServletHandler in project felix by apache.
the class ServletContextImpl method getNamedDispatcher.
@Override
public RequestDispatcher getNamedDispatcher(final String name) {
if (name == null) {
return null;
}
final RequestDispatcher dispatcher;
final ServletHandler servletHandler = this.handlerRegistry.resolveServletByName(name);
if (servletHandler != null) {
final ServletResolution resolution = new ServletResolution();
resolution.handler = servletHandler;
resolution.handlerRegistry = this.handlerRegistry;
// TODO - what is the path of a named servlet?
final RequestInfo requestInfo = new RequestInfo("", null, null, null);
dispatcher = new RequestDispatcherImpl(resolution, requestInfo);
} else {
dispatcher = null;
}
return dispatcher;
}
use of org.apache.felix.http.base.internal.handler.ServletHandler in project felix by apache.
the class SharedHttpServiceImpl method registerServlet.
/**
* Register a servlet
*/
public void registerServlet(@Nonnull final String alias, @Nonnull final ExtServletContext httpContext, @Nonnull final Servlet servlet, @Nonnull final ServletInfo servletInfo) throws ServletException, NamespaceException {
final ServletHandler handler = new HttpServiceServletHandler(httpContext, servletInfo, servlet);
synchronized (this.aliasMap) {
if (this.aliasMap.containsKey(alias)) {
throw new NamespaceException("Alias " + alias + " is already in use.");
}
this.handlerRegistry.getRegistry(handler.getContextServiceId()).registerServlet(handler);
this.aliasMap.put(alias, handler);
}
}
Aggregations