Search in sources :

Example 1 with ErrorCodeRequestHandler

use of org.apache.wicket.request.http.handler.ErrorCodeRequestHandler in project wicket by apache.

the class DefaultExceptionMapperResourceBlockedDevModeTest method packageResourceBlockedExceptionDevMode.

/**
 * In development mode return http status 500 when a resource is blocked
 * so the developer can see the problem early (the exception will be displayed
 * on the screen)
 */
@Test
public void packageResourceBlockedExceptionDevMode() {
    DefaultExceptionMapper mapper = new DefaultExceptionMapper();
    PackageResource.PackageResourceBlockedException x = new PackageResource.PackageResourceBlockedException("test");
    IRequestHandler handler = mapper.map(x);
    assertThat(handler, instanceOf(ErrorCodeRequestHandler.class));
    ErrorCodeRequestHandler errorCodeRequestHandler = (ErrorCodeRequestHandler) handler;
    assertThat(errorCodeRequestHandler.getErrorCode(), is(HttpServletResponse.SC_INTERNAL_SERVER_ERROR));
}
Also used : PackageResource(org.apache.wicket.request.resource.PackageResource) IRequestHandler(org.apache.wicket.request.IRequestHandler) ErrorCodeRequestHandler(org.apache.wicket.request.http.handler.ErrorCodeRequestHandler) Test(org.junit.Test)

Example 2 with ErrorCodeRequestHandler

use of org.apache.wicket.request.http.handler.ErrorCodeRequestHandler in project wicket by apache.

the class DefaultUnauthorizedResourceRequestListener method onUnauthorizedRequest.

@Override
public void onUnauthorizedRequest(IResource resource, PageParameters parameters) {
    RequestCycle cycle = RequestCycle.get();
    if (cycle != null) {
        IRequestHandler handler = new ErrorCodeRequestHandler(HttpServletResponse.SC_FORBIDDEN, createErrorMessage(resource, parameters));
        cycle.replaceAllRequestHandlers(handler);
    }
}
Also used : IRequestHandler(org.apache.wicket.request.IRequestHandler) ErrorCodeRequestHandler(org.apache.wicket.request.http.handler.ErrorCodeRequestHandler) RequestCycle(org.apache.wicket.request.cycle.RequestCycle)

Example 3 with ErrorCodeRequestHandler

use of org.apache.wicket.request.http.handler.ErrorCodeRequestHandler in project wicket by apache.

the class DefaultExceptionMapper method internalMap.

/**
 * Maps exceptions to their corresponding {@link IRequestHandler}.
 *
 * @param e
 * 			the current exception
 * @return the {@link IRequestHandler} for the current exception
 */
private IRequestHandler internalMap(Exception e) {
    final Application application = Application.get();
    // check if we are processing an Ajax request and if we want to invoke the failure handler
    if (isProcessingAjaxRequest()) {
        switch(application.getExceptionSettings().getAjaxErrorHandlingStrategy()) {
            case INVOKE_FAILURE_HANDLER:
                return new ErrorCodeRequestHandler(500);
        }
    }
    IRequestHandler handleExpectedExceptions = mapExpectedExceptions(e, application);
    if (handleExpectedExceptions != null) {
        return handleExpectedExceptions;
    }
    return mapUnexpectedExceptions(e, application);
}
Also used : ErrorCodeRequestHandler(org.apache.wicket.request.http.handler.ErrorCodeRequestHandler) IRequestHandler(org.apache.wicket.request.IRequestHandler)

Example 4 with ErrorCodeRequestHandler

use of org.apache.wicket.request.http.handler.ErrorCodeRequestHandler in project wicket by apache.

the class DefaultExceptionMapperResourceBlockedProdModeTest method packageResourceBlockedException.

/**
 * In production mode return http status 404 when a resource is blocked
 */
@Test
public void packageResourceBlockedException() {
    DefaultExceptionMapper mapper = new DefaultExceptionMapper();
    PackageResource.PackageResourceBlockedException x = new PackageResource.PackageResourceBlockedException("test");
    IRequestHandler handler = mapper.map(x);
    assertThat(handler, instanceOf(ErrorCodeRequestHandler.class));
    ErrorCodeRequestHandler errorCodeRequestHandler = (ErrorCodeRequestHandler) handler;
    assertThat(errorCodeRequestHandler.getErrorCode(), is(HttpServletResponse.SC_NOT_FOUND));
}
Also used : PackageResource(org.apache.wicket.request.resource.PackageResource) IRequestHandler(org.apache.wicket.request.IRequestHandler) ErrorCodeRequestHandler(org.apache.wicket.request.http.handler.ErrorCodeRequestHandler) Test(org.junit.Test)

Example 5 with ErrorCodeRequestHandler

use of org.apache.wicket.request.http.handler.ErrorCodeRequestHandler in project wicket by apache.

the class SourcesPage method getPageTargetClass.

private Class<? extends Page> getPageTargetClass() {
    if (page == null) {
        String pageParam = getPageParameters().get(PAGE_CLASS).toOptionalString();
        if (pageParam == null) {
            log.error("key: {} is null.", PAGE_CLASS);
            getRequestCycle().replaceAllRequestHandlers(new ErrorCodeRequestHandler(404, "Could not find sources for the page you requested"));
        } else if (!pageParam.startsWith("org.apache.wicket.examples")) {
            log.error("user is trying to access class: {} which is not in the scope of org.apache.wicket.examples", pageParam);
            throw new UnauthorizedInstantiationException(getClass());
        }
        page = WicketObjects.resolveClass(pageParam);
        if (page == null) {
            getRequestCycle().replaceAllRequestHandlers(new ErrorCodeRequestHandler(404, "Could not find sources for the page you requested"));
        }
    }
    return page;
}
Also used : ErrorCodeRequestHandler(org.apache.wicket.request.http.handler.ErrorCodeRequestHandler) UnauthorizedInstantiationException(org.apache.wicket.authorization.UnauthorizedInstantiationException)

Aggregations

ErrorCodeRequestHandler (org.apache.wicket.request.http.handler.ErrorCodeRequestHandler)6 IRequestHandler (org.apache.wicket.request.IRequestHandler)4 PackageResource (org.apache.wicket.request.resource.PackageResource)2 Test (org.junit.Test)2 UnauthorizedInstantiationException (org.apache.wicket.authorization.UnauthorizedInstantiationException)1 PageProvider (org.apache.wicket.core.request.handler.PageProvider)1 ExceptionErrorPage (org.apache.wicket.markup.html.pages.ExceptionErrorPage)1 RequestCycle (org.apache.wicket.request.cycle.RequestCycle)1 ExceptionSettings (org.apache.wicket.settings.ExceptionSettings)1