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: */
017: package org.apache.wicket.request.target.component;
018:
019: import java.lang.ref.WeakReference;
020:
021: import org.apache.wicket.IPageFactory;
022: import org.apache.wicket.Page;
023: import org.apache.wicket.PageParameters;
024: import org.apache.wicket.RequestCycle;
025: import org.apache.wicket.request.IRequestCycleProcessor;
026:
027: /**
028: * Default implementation of {@link IBookmarkablePageRequestTarget}. Target
029: * that denotes a page that is to be created from the provided page class. This
030: * is typically used for redirects to bookmarkable pages or mounted pages.
031: *
032: * @author Eelco Hillenius
033: * @author Igor Vaynberg (ivaynberg)
034: */
035: public class BookmarkablePageRequestTarget implements
036: IBookmarkablePageRequestTarget {
037: /** the page that was created in response for cleanup */
038: private Page page;
039:
040: /** the class of the page. */
041: private final WeakReference/*<Class>*/pageClassRef;
042:
043: /** optional page map name. */
044: private final String pageMapName;
045:
046: /** optional page parameters. */
047: private final PageParameters pageParameters;
048:
049: /**
050: * Construct.
051: *
052: * @param pageClass
053: * the class of the page
054: */
055: public BookmarkablePageRequestTarget(Class pageClass) {
056: this (null, pageClass);
057: }
058:
059: /**
060: * Construct.
061: *
062: * @param pageClass
063: * the class of the page
064: * @param pageParameters
065: * optional page parameters
066: */
067: public BookmarkablePageRequestTarget(Class pageClass,
068: PageParameters pageParameters) {
069: this (null, pageClass, pageParameters);
070: }
071:
072: /**
073: * Construct.
074: *
075: * @param pageMapName
076: * optional page map name
077: *
078: * @param pageClass
079: * the class of the page
080: */
081: public BookmarkablePageRequestTarget(String pageMapName,
082: Class pageClass) {
083: this (null, pageClass, null);
084: }
085:
086: /**
087: * Construct.
088: *
089: * @param pageMapName
090: * optional page map name
091: * @param pageClass
092: * the class of the page
093: * @param pageParameters
094: * optional page parameters
095: */
096: public BookmarkablePageRequestTarget(String pageMapName,
097: Class pageClass, PageParameters pageParameters) {
098: if (pageClass == null) {
099: throw new IllegalArgumentException(
100: "Argument pageClass must be not null");
101: }
102:
103: if (!Page.class.isAssignableFrom(pageClass)) {
104: throw new IllegalArgumentException(
105: "Argument pageClass must be an instance of "
106: + Page.class.getName());
107: }
108: this .pageClassRef = new WeakReference(pageClass);
109: this .pageParameters = (pageParameters == null) ? new PageParameters()
110: : pageParameters;
111: this .pageMapName = pageMapName;
112: }
113:
114: /**
115: * @see org.apache.wicket.IRequestTarget#detach(org.apache.wicket.RequestCycle)
116: */
117: public void detach(RequestCycle requestCycle) {
118: if (page != null) {
119: page.detach();
120: }
121: }
122:
123: /**
124: * @see java.lang.Object#equals(java.lang.Object)
125: */
126: public boolean equals(Object obj) {
127: boolean equal = false;
128: if (obj != null
129: && (obj instanceof BookmarkablePageRequestTarget)) {
130: BookmarkablePageRequestTarget that = (BookmarkablePageRequestTarget) obj;
131: if (getPageClass().equals(that.getPageClass())) {
132: boolean mapMatch = false;
133:
134: if (pageMapName != null) {
135: mapMatch = (that.pageMapName != null && pageMapName
136: .equals(that.pageMapName));
137: } else {
138: mapMatch = (that.pageMapName == null);
139: }
140:
141: equal = mapMatch;
142: }
143: }
144: return equal;
145: }
146:
147: /**
148: * @return The page that was created, null if the response did not happen
149: * yet
150: */
151: public final Page getPage() {
152: return page;
153: }
154:
155: /**
156: * @see org.apache.wicket.request.target.component.IBookmarkablePageRequestTarget#getPageClass()
157: */
158: public final Class getPageClass() {
159: return (Class) pageClassRef.get();
160: }
161:
162: /**
163: * @see org.apache.wicket.request.target.component.IBookmarkablePageRequestTarget#getPageMapName()
164: */
165: public final String getPageMapName() {
166: return pageMapName;
167: }
168:
169: /**
170: * @see org.apache.wicket.request.target.component.IBookmarkablePageRequestTarget#getPageParameters()
171: */
172: public final PageParameters getPageParameters() {
173: return pageParameters;
174: }
175:
176: /**
177: * @see java.lang.Object#hashCode()
178: */
179: public int hashCode() {
180: int result = "BookmarkablePageRequestTarget".hashCode();
181: result += getPageClass().hashCode();
182: result += pageMapName != null ? pageMapName.hashCode() : 0;
183: return 17 * result;
184: }
185:
186: /**
187: * @see org.apache.wicket.request.target.IEventProcessor#processEvents(org.apache.wicket.RequestCycle)
188: */
189: public void processEvents(RequestCycle requestCycle) {
190: if (!requestCycle.isRedirect()) {
191: page = getPage(requestCycle);
192: }
193: }
194:
195: /**
196: * @see org.apache.wicket.IRequestTarget#respond(org.apache.wicket.RequestCycle)
197: */
198: public void respond(RequestCycle requestCycle) {
199: if (pageClassRef != null && pageClassRef.get() != null) {
200: if (requestCycle.isRedirect()) {
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 java.lang.Object#toString()
216: */
217: public String toString() {
218: return "[BookmarkablePageRequestTarget@" + hashCode()
219: + " pageClass=" + getPageClass().getName() + "]";
220: }
221:
222: /**
223: * Constructs a new instance of a page given its class name
224: *
225: * @param pageClass
226: * class name of the page to be created
227: * @param requestCycle
228: * request cycle
229: * @return new instance of page
230: */
231: protected Page newPage(final Class pageClass,
232: final RequestCycle requestCycle) {
233: // Construct a new instance using the default page factory
234: IPageFactory pageFactory = requestCycle.getApplication()
235: .getSessionSettings().getPageFactory();
236:
237: if (pageParameters == null || pageParameters.size() == 0) {
238: return pageFactory.newPage(pageClass);
239: } else {
240: // Add bookmarkable params in for WICKET-400.
241: requestCycle.getRequest().getParameterMap().putAll(
242: pageParameters);
243: return pageFactory.newPage(pageClass, pageParameters);
244: }
245: }
246:
247: /**
248: * Gets a newly constructed page if we are not in a redirect.
249: *
250: * @param requestCycle
251: * the request cycle
252: * @return the page
253: */
254: protected final Page getPage(RequestCycle requestCycle) {
255: if (page == null && !requestCycle.isRedirect()) {
256: page = newPage(getPageClass(), requestCycle);
257: }
258: return page;
259: }
260:
261: }
|