001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.wicket.request.handler.render; 018 019import org.apache.wicket.Application; 020import org.apache.wicket.Session; 021import org.apache.wicket.core.request.handler.IPageProvider; 022import org.apache.wicket.core.request.handler.RenderPageRequestHandler; 023import org.apache.wicket.core.request.handler.RenderPageRequestHandler.RedirectPolicy; 024import org.apache.wicket.request.component.IRequestablePage; 025import org.apache.wicket.request.cycle.RequestCycle; 026import org.apache.wicket.settings.RequestCycleSettings; 027 028/** 029 * Delegate responsible for rendering the page. Depending on the implementation (web, test, portlet, 030 * etc.) the delegate may or may not support the redirect policy set in the 031 * {@link RenderPageRequestHandler}. 032 * 033 * @author Matej Knopp 034 */ 035public abstract class PageRenderer 036{ 037 private final RenderPageRequestHandler renderPageRequestHandler; 038 039 /** 040 * Construct. 041 * 042 * @param renderPageRequestHandler 043 */ 044 public PageRenderer(RenderPageRequestHandler renderPageRequestHandler) 045 { 046 this.renderPageRequestHandler = renderPageRequestHandler; 047 } 048 049 /** 050 * @return page provider 051 */ 052 protected IPageProvider getPageProvider() 053 { 054 return renderPageRequestHandler.getPageProvider(); 055 } 056 057 /** 058 * @return redirect policy 059 */ 060 protected RedirectPolicy getRedirectPolicy() 061 { 062 return renderPageRequestHandler.getRedirectPolicy(); 063 } 064 065 /** 066 * @return the request handler 067 */ 068 protected RenderPageRequestHandler getRenderPageRequestHandler() 069 { 070 return renderPageRequestHandler; 071 } 072 073 /** 074 * @return page instance 075 */ 076 protected IRequestablePage getPage() 077 { 078 return getPageProvider().getPageInstance(); 079 } 080 081 protected boolean isOnePassRender() 082 { 083 return Application.get().getRequestCycleSettings().getRenderStrategy() == 084 RequestCycleSettings.RenderStrategy.ONE_PASS_RENDER; 085 } 086 087 protected boolean isRedirectToRender() 088 { 089 return Application.get().getRequestCycleSettings().getRenderStrategy() == 090 RequestCycleSettings.RenderStrategy.REDIRECT_TO_RENDER; 091 } 092 093 protected boolean isRedirectToBuffer() 094 { 095 return Application.get().getRequestCycleSettings().getRenderStrategy() == 096 RequestCycleSettings.RenderStrategy.REDIRECT_TO_BUFFER; 097 } 098 099 /** 100 * @return the current session id for stateful pages and <code>null</code> for stateless pages 101 */ 102 protected String getSessionId() 103 { 104 return Session.exists() ? Session.get().getId() : null; 105 } 106 107 /** 108 * @return whether the current session is temporary 109 */ 110 protected boolean isSessionTemporary() 111 { 112 return Session.exists() ? Session.get().isTemporary() : true; 113 } 114 115 /** 116 * When the page renders to buffer and it is still stateless after rendering, this flag 117 * determines whether the redirect will take place or not. 118 * <p> 119 * By default we will redirect. This is so we do not end up having the browser be on a listener 120 * URL. A simple scenario is calling {@code setResponsePage(new StatelessPage())} inside form's 121 * {@code onSubmit()} or link's {@code onClick()} callbacks, or any other request listener 122 * callback. What will happen is that the browser will be on URL like 123 * {@code ./wicket/page?0-2.IFormSubmitListener-form}, and we will not redirect - leaving the 124 * browser on such URL. This is a worse alternative then saving one redirect because it may 125 * cause problems if user presses the refresh button in the browser. 126 * 127 * @return redirect flag 128 */ 129 protected boolean enableRedirectForStatelessPage() 130 { 131 return true; 132 } 133 134 /** 135 * Render the response using give {@link RequestCycle}. 136 * 137 * @param requestCycle 138 */ 139 public abstract void respond(RequestCycle requestCycle); 140}