📄 abstractrequestcycleprocessor.java
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.wicket.request;import javax.servlet.http.HttpServletResponse;import org.apache.wicket.Application;import org.apache.wicket.Component;import org.apache.wicket.IRedirectListener;import org.apache.wicket.IRequestTarget;import org.apache.wicket.Page;import org.apache.wicket.PageParameters;import org.apache.wicket.RequestCycle;import org.apache.wicket.RequestListenerInterface;import org.apache.wicket.RestartResponseAtInterceptPageException;import org.apache.wicket.RestartResponseException;import org.apache.wicket.Session;import org.apache.wicket.WicketRuntimeException;import org.apache.wicket.authorization.AuthorizationException;import org.apache.wicket.authorization.UnauthorizedActionException;import org.apache.wicket.markup.MarkupException;import org.apache.wicket.markup.html.INewBrowserWindowListener;import org.apache.wicket.markup.html.pages.ExceptionErrorPage;import org.apache.wicket.protocol.http.PageExpiredException;import org.apache.wicket.protocol.http.request.WebErrorCodeResponseTarget;import org.apache.wicket.protocol.http.request.WebExternalResourceRequestTarget;import org.apache.wicket.request.target.IEventProcessor;import org.apache.wicket.request.target.component.BookmarkableListenerInterfaceRequestTarget;import org.apache.wicket.request.target.component.BookmarkablePageRequestTarget;import org.apache.wicket.request.target.component.PageRequestTarget;import org.apache.wicket.request.target.component.listener.RedirectPageRequestTarget;import org.apache.wicket.request.target.resource.SharedResourceRequestTarget;import org.apache.wicket.settings.IExceptionSettings;import org.apache.wicket.util.string.Strings;/** * Default abstract implementation of {@link IRequestCycleProcessor}. * * @author eelcohillenius */public abstract class AbstractRequestCycleProcessor implements IRequestCycleProcessor{ /** request coding strategy to use. */ private IRequestCodingStrategy requestCodingStrategy; /** * Construct. */ public AbstractRequestCycleProcessor() { } /** * @see org.apache.wicket.request.IRequestCycleProcessor#getRequestCodingStrategy() */ public IRequestCodingStrategy getRequestCodingStrategy() { if (requestCodingStrategy == null) { requestCodingStrategy = newRequestCodingStrategy(); } return requestCodingStrategy; } /** * @see org.apache.wicket.request.IRequestCycleProcessor#processEvents(org.apache.wicket.RequestCycle) */ public void processEvents(RequestCycle requestCycle) { IRequestTarget target = requestCycle.getRequestTarget(); if (target instanceof IEventProcessor) { Application.get().logEventTarget(target); ((IEventProcessor)target).processEvents(requestCycle); } } /** * @see org.apache.wicket.request.IRequestCycleProcessor#respond(org.apache.wicket.RequestCycle) */ public void respond(RequestCycle requestCycle) { IRequestTarget requestTarget = requestCycle.getRequestTarget(); if (requestTarget != null) { Application.get().logResponseTarget(requestTarget); requestTarget.respond(requestCycle); } } /** * @see org.apache.wicket.request.IRequestCycleProcessor#respond(java.lang.RuntimeException, * org.apache.wicket.RequestCycle) */ public void respond(RuntimeException e, RequestCycle requestCycle) { // If application doesn't want debug info showing up for users final Application application = Application.get(); final IExceptionSettings settings = application.getExceptionSettings(); final Page responsePage = requestCycle.getResponsePage(); Page override = onRuntimeException(responsePage, e); if (override != null) { throw new RestartResponseException(override); } else if (e instanceof AuthorizationException) { // are authorization exceptions always thrown before the real // render? // else we need to make a page (see below) or set it hard to a // redirect. Class accessDeniedPageClass = application.getApplicationSettings() .getAccessDeniedPage(); throw new RestartResponseAtInterceptPageException(accessDeniedPageClass); } else if (e instanceof PageExpiredException) { Class pageExpiredErrorPageClass = application.getApplicationSettings() .getPageExpiredErrorPage(); boolean mounted = isPageMounted(pageExpiredErrorPageClass); RequestCycle.get().setRedirect(mounted); throw new RestartResponseException(pageExpiredErrorPageClass); } else if (settings.getUnexpectedExceptionDisplay() != IExceptionSettings.SHOW_NO_EXCEPTION_PAGE) { // we do not want to redirect - we want to inline the error output // and preserve the url so when the refresh button is pressed we // rerun the code that caused the error requestCycle.setRedirect(false); // figure out which error page to show Class internalErrorPageClass = application.getApplicationSettings() .getInternalErrorPage(); Class responseClass = responsePage != null ? responsePage.getClass() : null; if (responseClass != internalErrorPageClass && settings.getUnexpectedExceptionDisplay() == IExceptionSettings.SHOW_INTERNAL_ERROR_PAGE) { throw new RestartResponseException(internalErrorPageClass); } else if (responseClass != ExceptionErrorPage.class) { // Show full details throw new RestartResponseException(new ExceptionErrorPage(e, responsePage)); } else { // give up while we're ahead! throw new WicketRuntimeException("Internal Error: Could not render error page " + internalErrorPageClass, e); } } } /** * Checks whether the given <code>pageClass</code> is mounted. * * @param pageClass * the <code>Class</code> of the <code>Page</code> to be checked * @return true if the given <code>pageClass</code> is mounted, false otherwise */ private boolean isPageMounted(Class /* <? extends Page> */pageClass) { RequestCycle cycle = RequestCycle.get(); CharSequence path = getRequestCodingStrategy().pathForTarget( new BookmarkablePageRequestTarget(pageClass)); return path != null; } /** * Creates a new request coding strategy instance. this is (typically) called once at the first * time {@link #getRequestCodingStrategy()} is called. * * @return a new request coding strategy */ protected abstract IRequestCodingStrategy newRequestCodingStrategy(); /** * This method is called when a runtime exception is thrown, just before the actual handling of * the runtime exception. This implementation passes the call through to * {@link RequestCycle#onRuntimeException(Page, RuntimeException)}. Note that if you override * this method {@link RequestCycle#onRuntimeException(Page, RuntimeException)} will not be * supported. * * @param page * Any page context where the exception was thrown * @param e * The exception * @return Any error page to redirect to */ protected Page onRuntimeException(final Page page, final RuntimeException e) { return RequestCycle.get().onRuntimeException(page, e); } /** * Resolves to a bookmarkable page target. * * @param requestCycle * the current request cycle * @param requestParameters * the request parameters object * @return the bookmarkable page as a request target */ protected IRequestTarget resolveBookmarkablePage(final RequestCycle requestCycle, final RequestParameters requestParameters) { String bookmarkablePageClass = requestParameters.getBookmarkablePageClass(); Session session = requestCycle.getSession(); Class pageClass; try { pageClass = session.getClassResolver().resolveClass(bookmarkablePageClass); } catch (ClassNotFoundException e) { return new WebErrorCodeResponseTarget(HttpServletResponse.SC_NOT_FOUND, "Unable to load Bookmarkable Page"); } try { PageParameters params = new PageParameters(requestParameters.getParameters()); if (requestParameters.getComponentPath() != null &&
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -