@SuppressWarnings("unchecked")
public void handleRequest(GenericContainerRequest request, GenericContainerResponse response) throws Exception
{
try
{
ApplicationContext context = new ApplicationContextImpl(request, response, ProviderBinder.getInstance());
context.getProperties().putAll(properties);
ApplicationContextImpl.setCurrent(context);
for (ObjectFactory<FilterDescriptor> factory : ProviderBinder.getInstance().getRequestFilters(
context.getPath()))
{
RequestFilter f = (RequestFilter)factory.getInstance(context);
f.doFilter(request);
}
try
{
dispatcher.dispatch(request, response);
if (response.getHttpHeaders().getFirst(ExtHttpHeaders.JAXRS_BODY_PROVIDED) == null)
{
String jaxrsHeader = getJaxrsHeader(response.getStatus());
if (jaxrsHeader != null)
{
response.getHttpHeaders().putSingle(ExtHttpHeaders.JAXRS_BODY_PROVIDED, jaxrsHeader);
}
}
}
catch (Exception e)
{
if (e instanceof WebApplicationException)
{
Response errorResponse = ((WebApplicationException)e).getResponse();
ExceptionMapper excmap = ProviderBinder.getInstance().getExceptionMapper(WebApplicationException.class);
int errorStatus = errorResponse.getStatus();
// should be some of 4xx status
if (errorStatus < 500)
{
// Warn about error in debug mode only.
if (LOG.isDebugEnabled() && e.getCause() != null)
{
LOG.warn("WebApplication exception occurs.", e.getCause());
}
}
else
{
if (e.getCause() != null)
{
LOG.warn("WebApplication exception occurs.", e.getCause());
}
}
// -----
if (errorResponse.getEntity() == null)
{
if (excmap != null)
{
errorResponse = excmap.toResponse(e);
}
else
{
if (e.getMessage() != null)
{
errorResponse = createErrorResponse(errorStatus, e.getMessage());
}
}
}
else
{
if (errorResponse.getMetadata().getFirst(ExtHttpHeaders.JAXRS_BODY_PROVIDED) == null)
{
String jaxrsHeader = getJaxrsHeader(errorStatus);
if (jaxrsHeader != null)
{
errorResponse.getMetadata().putSingle(ExtHttpHeaders.JAXRS_BODY_PROVIDED, jaxrsHeader);
}
}
}
response.setResponse(errorResponse);
}
else if (e instanceof InternalException)
{
Throwable cause = e.getCause();
Class causeClazz = cause.getClass();
ExceptionMapper excmap = ProviderBinder.getInstance().getExceptionMapper(causeClazz);
while (causeClazz != null && excmap == null)
{
excmap = ProviderBinder.getInstance().getExceptionMapper(causeClazz);
if (excmap == null)
causeClazz = causeClazz.getSuperclass();
}
if (excmap != null)
{
if (LOG.isDebugEnabled())
{
// Hide error message if exception mapper exists.
LOG.warn("Internal error occurs.", cause);
}
response.setResponse(excmap.toResponse(e.getCause()));
}
else
{
LOG.error("Internal error occurs.", cause);
throw new UnhandledException(e.getCause());
}
}
else
{
throw new UnhandledException(e);
}
}
for (ObjectFactory<FilterDescriptor> factory : ProviderBinder.getInstance().getResponseFilters(
context.getPath()))
{
ResponseFilter f = (ResponseFilter)factory.getInstance(context);
f.doFilter(response);
}