001: /*
002: * $Id: BookmarkablePageRequestTarget.java,v 1.3 2005/12/07 00:52:26 ivaynberg
003: * Exp $ $Revision: 460676 $ $Date: 2006-05-22 23:13:27 +0200 (Mon, 22 May 2006) $
004: *
005: * ==============================================================================
006: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
007: * use this file except in compliance with the License. You may obtain a copy of
008: * the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015: * License for the specific language governing permissions and limitations under
016: * the License.
017: */
018: package wicket.request.target.component;
019:
020: import wicket.IPageFactory;
021: import wicket.Page;
022: import wicket.PageParameters;
023: import wicket.RequestCycle;
024: import wicket.request.IRequestCycleProcessor;
025:
026: /**
027: * Default implementation of {@link IBookmarkablePageRequestTarget}. Target
028: * that denotes a page that is to be created from the provided page class. This
029: * is typically used for redirects to bookmarkable pages or mounted pages.
030: *
031: * @author Eelco Hillenius
032: * @author Igor Vaynberg (ivaynberg)
033: */
034: public class BookmarkablePageRequestTarget implements
035: IBookmarkablePageRequestTarget {
036: /** the page that was created in response for cleanup */
037: private Page page;
038:
039: /** the class of the page. */
040: private final Class pageClass;
041:
042: /** optional page map name. */
043: private final String pageMapName;
044:
045: /** optional page parameters. */
046: private final PageParameters pageParameters;
047:
048: /**
049: * Construct.
050: *
051: * @param pageClass
052: * the class of the page
053: */
054: public BookmarkablePageRequestTarget(Class pageClass) {
055: this (null, pageClass);
056: }
057:
058: /**
059: * Construct.
060: *
061: * @param pageClass
062: * the class of the page
063: * @param pageParameters
064: * optional page parameters
065: */
066: public BookmarkablePageRequestTarget(Class pageClass,
067: PageParameters pageParameters) {
068: this (null, pageClass, pageParameters);
069: }
070:
071: /**
072: * Construct.
073: *
074: * @param pageMapName
075: * optional page map name
076: *
077: * @param pageClass
078: * the class of the page
079: */
080: public BookmarkablePageRequestTarget(String pageMapName,
081: Class pageClass) {
082: this (null, pageClass, null);
083: }
084:
085: /**
086: * Construct.
087: *
088: * @param pageMapName
089: * optional page map name
090: * @param pageClass
091: * the class of the page
092: * @param pageParameters
093: * optional page parameters
094: */
095: public BookmarkablePageRequestTarget(String pageMapName,
096: Class pageClass, PageParameters pageParameters) {
097: if (pageClass == null) {
098: throw new IllegalArgumentException(
099: "Argument pageClass must be not null");
100: }
101:
102: if (!Page.class.isAssignableFrom(pageClass)) {
103: throw new IllegalArgumentException(
104: "Argument pageClass must be an instance of "
105: + Page.class.getName());
106: }
107: this .pageClass = pageClass;
108: this .pageParameters = (pageParameters == null) ? new PageParameters()
109: : pageParameters;
110: this .pageMapName = pageMapName;
111: }
112:
113: /**
114: * @see wicket.IRequestTarget#detach(wicket.RequestCycle)
115: */
116: public void detach(RequestCycle requestCycle) {
117: if (page != null) {
118: page.internalDetach();
119: }
120: }
121:
122: /**
123: * @see java.lang.Object#equals(java.lang.Object)
124: */
125: public boolean equals(Object obj) {
126: boolean equal = false;
127: if (obj != null
128: && (obj instanceof BookmarkablePageRequestTarget)) {
129: BookmarkablePageRequestTarget that = (BookmarkablePageRequestTarget) obj;
130: if (pageClass.equals(that.pageClass)) {
131: boolean mapMatch = false;
132:
133: if (pageMapName != null) {
134: mapMatch = (that.pageMapName != null && pageMapName
135: .equals(that.pageMapName));
136: } else {
137: mapMatch = (that.pageMapName == null);
138: }
139:
140: equal = mapMatch;
141: }
142: }
143: return equal;
144: }
145:
146: /**
147: * @return The page that was created, null if the response did not happen
148: * yet
149: */
150: public final Page getPage() {
151: return page;
152: }
153:
154: /**
155: * @see wicket.request.target.component.IBookmarkablePageRequestTarget#getPageClass()
156: */
157: public final Class getPageClass() {
158: return pageClass;
159: }
160:
161: /**
162: * @see wicket.request.target.component.IBookmarkablePageRequestTarget#getPageMapName()
163: */
164: public final String getPageMapName() {
165: return pageMapName;
166: }
167:
168: /**
169: * @see wicket.request.target.component.IBookmarkablePageRequestTarget#getPageParameters()
170: */
171: public final PageParameters getPageParameters() {
172: return pageParameters;
173: }
174:
175: /**
176: * @see java.lang.Object#hashCode()
177: */
178: public int hashCode() {
179: int result = "BookmarkablePageRequestTarget".hashCode();
180: result += pageClass.hashCode();
181: result += pageMapName != null ? pageMapName.hashCode() : 0;
182: return 17 * result;
183: }
184:
185: /**
186: * @see wicket.request.target.IEventProcessor#processEvents(wicket.RequestCycle)
187: */
188: public void processEvents(RequestCycle requestCycle) {
189: if (!requestCycle.getRedirect()) {
190: requestCycle.setUpdateSession(true);
191: page = getPage(requestCycle);
192: }
193: }
194:
195: /**
196: * @see wicket.IRequestTarget#respond(wicket.RequestCycle)
197: */
198: public void respond(RequestCycle requestCycle) {
199: if (pageClass != null) {
200: if (requestCycle.getRedirect()) {
201: IRequestCycleProcessor processor = requestCycle
202: .getProcessor();
203: String redirectUrl = processor
204: .getRequestCodingStrategy().encode(
205: requestCycle, this ).toString();
206: requestCycle.getResponse().redirect(redirectUrl);
207: } else {
208: // Let the page render itself
209: getPage(requestCycle).renderPage();
210: }
211: }
212: }
213:
214: /**
215: * @see wicket.IRequestTarget#getLock(RequestCycle)
216: */
217: public Object getLock(RequestCycle requestCycle) {
218: // we need to lock when we are not redirecting, i.e. we are
219: // actually rendering the page
220: return !requestCycle.getRedirect() ? requestCycle.getSession()
221: : null;
222: }
223:
224: /**
225: * @see java.lang.Object#toString()
226: */
227: public String toString() {
228: return "[BookmarkablePageRequestTarget@" + hashCode()
229: + " pageClass=" + pageClass.getName() + "]";
230: }
231:
232: /**
233: * Constructs a new instance of a page given its class name
234: *
235: * @param pageClass
236: * class name of the page to be created
237: * @param requestCycle
238: * request cycle
239: * @return new instance of page
240: */
241: protected Page newPage(final Class pageClass,
242: final RequestCycle requestCycle) {
243: // Construct a new instance using the default page factory
244: IPageFactory pageFactory = requestCycle.getApplication()
245: .getSessionSettings().getPageFactory();
246:
247: if (pageParameters == null || pageParameters.size() == 0) {
248: return pageFactory.newPage(pageClass);
249: } else {
250: return pageFactory.newPage(pageClass, pageParameters);
251: }
252: }
253:
254: /**
255: * Gets a newly constructed page if we are not in a redirect.
256: *
257: * @param requestCycle
258: * the request cycle
259: * @return the page
260: */
261: private final Page getPage(RequestCycle requestCycle) {
262: if (page == null && pageClass != null
263: && !requestCycle.getRedirect()) {
264: page = newPage(pageClass, requestCycle);
265: }
266: return page;
267: }
268:
269: }
|