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.coding;
018:
019: import java.lang.ref.WeakReference;
020:
021: import org.apache.wicket.IRequestTarget;
022: import org.apache.wicket.PageParameters;
023: import org.apache.wicket.protocol.http.request.WebRequestCodingStrategy;
024: import org.apache.wicket.request.RequestParameters;
025: import org.apache.wicket.request.target.component.BookmarkableListenerInterfaceRequestTarget;
026: import org.apache.wicket.request.target.component.BookmarkablePageRequestTarget;
027: import org.apache.wicket.request.target.component.IBookmarkablePageRequestTarget;
028: import org.apache.wicket.util.string.AppendingStringBuffer;
029:
030: /**
031: * Encodes and decodes mounts for a single bookmarkable page class.
032: *
033: * @author Eelco Hillenius
034: */
035: public class BookmarkablePageRequestTargetUrlCodingStrategy extends
036: AbstractRequestTargetUrlCodingStrategy {
037: /** bookmarkable page class. */
038: protected final WeakReference/* <Class> */bookmarkablePageClassRef;
039:
040: /** page map name. */
041: private final String pageMapName;
042:
043: /**
044: * Construct.
045: *
046: * @param mountPath
047: * the mount path
048: * @param bookmarkablePageClass
049: * the class of the bookmarkable page
050: * @param pageMapName
051: * the page map name if any
052: */
053: public BookmarkablePageRequestTargetUrlCodingStrategy(
054: final String mountPath, final Class bookmarkablePageClass,
055: String pageMapName) {
056: super (mountPath);
057:
058: if (bookmarkablePageClass == null) {
059: throw new IllegalArgumentException(
060: "Argument bookmarkablePageClass must be not null");
061: }
062:
063: this .bookmarkablePageClassRef = new WeakReference(
064: bookmarkablePageClass);
065: this .pageMapName = pageMapName;
066: }
067:
068: /**
069: * @see org.apache.wicket.request.target.coding.IRequestTargetUrlCodingStrategy#decode(org.apache.wicket.request.RequestParameters)
070: */
071: public IRequestTarget decode(RequestParameters requestParameters) {
072: final String parametersFragment = requestParameters.getPath()
073: .substring(getMountPath().length());
074: final PageParameters parameters = new PageParameters(
075: decodeParameters(parametersFragment, requestParameters
076: .getParameters()));
077: String pageMapName = WebRequestCodingStrategy
078: .decodePageMapName((String) parameters
079: .remove(WebRequestCodingStrategy.PAGEMAP));
080: if (requestParameters.getPageMapName() == null) {
081: requestParameters.setPageMapName(pageMapName);
082: } else {
083: pageMapName = requestParameters.getPageMapName();
084: }
085:
086: // do some extra work for checking whether this is a normal request to a
087: // bookmarkable page, or a request to a stateless page (in which case a
088: // wicket:interface parameter should be available
089: final String interfaceParameter = (String) parameters
090: .remove(WebRequestCodingStrategy.INTERFACE_PARAMETER_NAME);
091:
092: if (interfaceParameter != null) {
093: WebRequestCodingStrategy.addInterfaceParameters(
094: interfaceParameter, requestParameters);
095: return new BookmarkableListenerInterfaceRequestTarget(
096: pageMapName,
097: (Class) bookmarkablePageClassRef.get(), parameters,
098: requestParameters.getComponentPath(),
099: requestParameters.getInterfaceName());
100: } else {
101: return new BookmarkablePageRequestTarget(pageMapName,
102: (Class) bookmarkablePageClassRef.get(), parameters);
103: }
104: }
105:
106: /**
107: * @see org.apache.wicket.request.target.coding.IRequestTargetUrlCodingStrategy#encode(org.apache.wicket.IRequestTarget)
108: */
109: public final CharSequence encode(final IRequestTarget requestTarget) {
110: if (!(requestTarget instanceof IBookmarkablePageRequestTarget)) {
111: throw new IllegalArgumentException(
112: "This encoder can only be used with "
113: + "instances of "
114: + IBookmarkablePageRequestTarget.class
115: .getName());
116: }
117: final AppendingStringBuffer url = new AppendingStringBuffer(40);
118: url.append(getMountPath());
119: final IBookmarkablePageRequestTarget target = (IBookmarkablePageRequestTarget) requestTarget;
120:
121: PageParameters pageParameters = target.getPageParameters();
122: String pagemap = pageMapName != null ? pageMapName : target
123: .getPageMapName();
124: if (pagemap != null) {
125: if (pageParameters == null) {
126: pageParameters = new PageParameters();
127: }
128: pageParameters
129: .put(WebRequestCodingStrategy.PAGEMAP,
130: WebRequestCodingStrategy
131: .encodePageMapName(pagemap));
132: }
133: appendParameters(url, pageParameters);
134: return url;
135: }
136:
137: /**
138: * @see org.apache.wicket.request.target.coding.IRequestTargetUrlCodingStrategy#matches(org.apache.wicket.IRequestTarget)
139: */
140: public boolean matches(IRequestTarget requestTarget) {
141: if (requestTarget instanceof IBookmarkablePageRequestTarget) {
142: IBookmarkablePageRequestTarget target = (IBookmarkablePageRequestTarget) requestTarget;
143: if (((Class) bookmarkablePageClassRef.get()).equals(target
144: .getPageClass())) {
145: if (this .pageMapName == null) {
146: return true;
147: } else {
148: return this .pageMapName.equals(target
149: .getPageMapName());
150: }
151: }
152: }
153: return false;
154: }
155:
156: /**
157: * @see java.lang.Object#toString()
158: */
159: public String toString() {
160: return "BookmarkablePageEncoder[page="
161: + bookmarkablePageClassRef.get() + "]";
162: }
163: }
|