use of jakarta.servlet.ServletException in project tomcat by apache.
the class StandardHostValve method throwable.
/**
* Handle the specified Throwable encountered while processing
* the specified Request to produce the specified Response. Any
* exceptions that occur during generation of the exception report are
* logged and swallowed.
*
* @param request The request being processed
* @param response The response being generated
* @param throwable The exception that occurred (which possibly wraps
* a root cause exception
*/
protected void throwable(Request request, Response response, Throwable throwable) {
Context context = request.getContext();
if (context == null) {
return;
}
Throwable realError = throwable;
if (realError instanceof ServletException) {
realError = ((ServletException) realError).getRootCause();
if (realError == null) {
realError = throwable;
}
}
// If this is an aborted request from a client just log it and return
if (realError instanceof ClientAbortException) {
if (log.isDebugEnabled()) {
log.debug(sm.getString("standardHost.clientAbort", realError.getCause().getMessage()));
}
return;
}
ErrorPage errorPage = context.findErrorPage(throwable);
if ((errorPage == null) && (realError != throwable)) {
errorPage = context.findErrorPage(realError);
}
if (errorPage != null) {
if (response.setErrorReported()) {
response.setAppCommitted(false);
request.setAttribute(Globals.DISPATCHER_REQUEST_PATH_ATTR, errorPage.getLocation());
request.setAttribute(Globals.DISPATCHER_TYPE_ATTR, DispatcherType.ERROR);
request.setAttribute(RequestDispatcher.ERROR_STATUS_CODE, Integer.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR));
request.setAttribute(RequestDispatcher.ERROR_MESSAGE, throwable.getMessage());
request.setAttribute(RequestDispatcher.ERROR_EXCEPTION, realError);
Wrapper wrapper = request.getWrapper();
if (wrapper != null) {
request.setAttribute(RequestDispatcher.ERROR_SERVLET_NAME, wrapper.getName());
}
request.setAttribute(RequestDispatcher.ERROR_REQUEST_URI, request.getRequestURI());
request.setAttribute(RequestDispatcher.ERROR_EXCEPTION_TYPE, realError.getClass());
if (custom(request, response, errorPage)) {
try {
response.finishResponse();
} catch (IOException e) {
container.getLogger().warn("Exception Processing " + errorPage, e);
}
}
}
} else {
// A custom error-page has not been defined for the exception
// that was thrown during request processing. Check if an
// error-page for error code 500 was specified and if so,
// send that page back as the response.
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
// The response is an error
response.setError();
status(request, response);
}
}
use of jakarta.servlet.ServletException in project tomcat by apache.
the class ApplicationContext method addListener.
@Override
public void addListener(Class<? extends EventListener> listenerClass) {
EventListener listener;
try {
listener = createListener(listenerClass);
} catch (ServletException e) {
throw new IllegalArgumentException(sm.getString("applicationContext.addListener.iae.init", listenerClass.getName()), e);
}
addListener(listener);
}
use of jakarta.servlet.ServletException in project tomcat by apache.
the class ApplicationDispatcher method checkSameObjects.
private void checkSameObjects(ServletRequest appRequest, ServletResponse appResponse) throws ServletException {
ServletRequest originalRequest = ApplicationFilterChain.getLastServicedRequest();
ServletResponse originalResponse = ApplicationFilterChain.getLastServicedResponse();
// Some forwards, eg from valves will not set original values
if (originalRequest == null || originalResponse == null) {
return;
}
boolean same = false;
ServletRequest dispatchedRequest = appRequest;
// find the request that was passed into the service method
while (originalRequest instanceof ServletRequestWrapper && ((ServletRequestWrapper) originalRequest).getRequest() != null) {
originalRequest = ((ServletRequestWrapper) originalRequest).getRequest();
}
// compare with the dispatched request
while (!same) {
if (originalRequest.equals(dispatchedRequest)) {
same = true;
}
if (!same && dispatchedRequest instanceof ServletRequestWrapper) {
dispatchedRequest = ((ServletRequestWrapper) dispatchedRequest).getRequest();
} else {
break;
}
}
if (!same) {
throw new ServletException(sm.getString("applicationDispatcher.specViolation.request"));
}
same = false;
ServletResponse dispatchedResponse = appResponse;
// find the response that was passed into the service method
while (originalResponse instanceof ServletResponseWrapper && ((ServletResponseWrapper) originalResponse).getResponse() != null) {
originalResponse = ((ServletResponseWrapper) originalResponse).getResponse();
}
// compare with the dispatched response
while (!same) {
if (originalResponse.equals(dispatchedResponse)) {
same = true;
}
if (!same && dispatchedResponse instanceof ServletResponseWrapper) {
dispatchedResponse = ((ServletResponseWrapper) dispatchedResponse).getResponse();
} else {
break;
}
}
if (!same) {
throw new ServletException(sm.getString("applicationDispatcher.specViolation.response"));
}
}
use of jakarta.servlet.ServletException in project tomcat by apache.
the class CorsFilter method doFilter.
@Override
public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse, final FilterChain filterChain) throws IOException, ServletException {
if (!(servletRequest instanceof HttpServletRequest) || !(servletResponse instanceof HttpServletResponse)) {
throw new ServletException(sm.getString("corsFilter.onlyHttp"));
}
// Safe to downcast at this point.
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
// Determines the CORS request type.
CorsFilter.CORSRequestType requestType = checkRequestType(request);
// Adds CORS specific attributes to request.
if (isDecorateRequest()) {
CorsFilter.decorateCORSProperties(request, requestType);
}
switch(requestType) {
case SIMPLE:
// Handles a Simple CORS request.
case ACTUAL:
// Handles an Actual CORS request.
this.handleSimpleCORS(request, response, filterChain);
break;
case PRE_FLIGHT:
// Handles a Pre-flight CORS request.
this.handlePreflightCORS(request, response, filterChain);
break;
case NOT_CORS:
// Handles a Normal request that is not a cross-origin request.
this.handleNonCORS(request, response, filterChain);
break;
default:
// Handles a CORS request that violates specification.
this.handleInvalidCORS(request, response, filterChain);
break;
}
}
use of jakarta.servlet.ServletException in project tomcat by apache.
the class CorsFilter method parseAndStore.
/**
* Parses each param-value and populates configuration variables. If a param
* is provided, it overrides the default.
*
* @param allowedOrigins
* A {@link String} of comma separated origins.
* @param allowedHttpMethods
* A {@link String} of comma separated HTTP methods.
* @param allowedHttpHeaders
* A {@link String} of comma separated HTTP headers.
* @param exposedHeaders
* A {@link String} of comma separated headers that needs to be
* exposed.
* @param supportsCredentials
* "true" if support credentials needs to be enabled.
* @param preflightMaxAge
* The amount of seconds the user agent is allowed to cache the
* result of the pre-flight request.
* @throws ServletException If the configuration is invalid
*/
private void parseAndStore(final String allowedOrigins, final String allowedHttpMethods, final String allowedHttpHeaders, final String exposedHeaders, final String supportsCredentials, final String preflightMaxAge, final String decorateRequest) throws ServletException {
if (allowedOrigins.trim().equals("*")) {
this.anyOriginAllowed = true;
} else {
this.anyOriginAllowed = false;
Set<String> setAllowedOrigins = parseStringToSet(allowedOrigins);
this.allowedOrigins.clear();
this.allowedOrigins.addAll(setAllowedOrigins);
}
Set<String> setAllowedHttpMethods = parseStringToSet(allowedHttpMethods);
this.allowedHttpMethods.clear();
this.allowedHttpMethods.addAll(setAllowedHttpMethods);
Set<String> setAllowedHttpHeaders = parseStringToSet(allowedHttpHeaders);
Set<String> lowerCaseHeaders = new HashSet<>();
for (String header : setAllowedHttpHeaders) {
String lowerCase = header.toLowerCase(Locale.ENGLISH);
lowerCaseHeaders.add(lowerCase);
}
this.allowedHttpHeaders.clear();
this.allowedHttpHeaders.addAll(lowerCaseHeaders);
Set<String> setExposedHeaders = parseStringToSet(exposedHeaders);
this.exposedHeaders.clear();
this.exposedHeaders.addAll(setExposedHeaders);
// For any value other than 'true' this will be false.
this.supportsCredentials = Boolean.parseBoolean(supportsCredentials);
if (this.supportsCredentials && this.anyOriginAllowed) {
throw new ServletException(sm.getString("corsFilter.invalidSupportsCredentials"));
}
try {
if (!preflightMaxAge.isEmpty()) {
this.preflightMaxAge = Long.parseLong(preflightMaxAge);
} else {
this.preflightMaxAge = 0L;
}
} catch (NumberFormatException e) {
throw new ServletException(sm.getString("corsFilter.invalidPreflightMaxAge"), e);
}
// For any value other than 'true' this will be false.
this.decorateRequest = Boolean.parseBoolean(decorateRequest);
}
Aggregations