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.util.Map;
020:
021: import org.apache.wicket.PageMap;
022: import org.apache.wicket.PageParameters;
023: import org.apache.wicket.WicketRuntimeException;
024: import org.apache.wicket.protocol.http.request.WebRequestCodingStrategy;
025: import org.apache.wicket.util.string.AppendingStringBuffer;
026: import org.apache.wicket.util.value.ValueMap;
027:
028: /**
029: * Url coding strategy for bookmarkable pages that encodes index based
030: * parameters.
031: *
032: * Strategy looks for parameters whose name is an integer in an incremented
033: * order starting with zero. Found parameters will be appended to the url in the
034: * form /mount-path/paramvalue0/paramvalue1/paramvalue2
035: *
036: * When decoded these parameters will once again be available under their index (
037: * PageParameters.getString("0"); )
038: *
039: * @author Igor Vaynberg (ivaynberg)
040: */
041: public class IndexedParamUrlCodingStrategy extends
042: BookmarkablePageRequestTargetUrlCodingStrategy {
043: /**
044: * Construct.
045: *
046: * @param mountPath
047: * mount path
048: * @param bookmarkablePageClass
049: * class of mounted page
050: */
051: public IndexedParamUrlCodingStrategy(String mountPath,
052: Class bookmarkablePageClass) {
053: super (mountPath, bookmarkablePageClass, PageMap.DEFAULT_NAME);
054: }
055:
056: /**
057: * Construct.
058: *
059: * @param mountPath
060: * mount path
061: * @param bookmarkablePageClass
062: * class of mounted page
063: * @param pageMapName
064: * name of pagemap
065: */
066: public IndexedParamUrlCodingStrategy(String mountPath,
067: Class bookmarkablePageClass, String pageMapName) {
068: super (mountPath, bookmarkablePageClass, pageMapName);
069: }
070:
071: protected void appendParameters(AppendingStringBuffer url,
072: Map parameters) {
073: int i = 0;
074: while (parameters.containsKey(String.valueOf(i))) {
075: String value = (String) parameters.get(String.valueOf(i));
076: if (!url.endsWith("/")) {
077: url.append("/");
078: }
079: url.append(urlEncode(value)).append("/");
080: i++;
081: }
082:
083: String pageMap = (String) parameters
084: .get(WebRequestCodingStrategy.PAGEMAP);
085: if (pageMap != null) {
086: i++;
087: pageMap = WebRequestCodingStrategy
088: .encodePageMapName(pageMap);
089: if (!url.endsWith("/")) {
090: url.append("/");
091: }
092: url.append(WebRequestCodingStrategy.PAGEMAP).append("/")
093: .append(urlEncode(pageMap)).append("/");
094: }
095:
096: if (i != parameters.size()) {
097: throw new WicketRuntimeException(
098: "Not all parameters were encoded. Make sure all parameter names are integers in consecutive order starting with zero. Current parameter names are: "
099: + parameters.keySet().toString());
100: }
101: }
102:
103: protected ValueMap decodeParameters(String urlFragment,
104: Map urlParameters) {
105: PageParameters params = new PageParameters();
106: if (urlFragment == null) {
107: return params;
108: }
109: if (urlFragment.startsWith("/")) {
110: urlFragment = urlFragment.substring(1);
111: }
112: if (urlFragment.length() > 0 && urlFragment.endsWith("/")) {
113: urlFragment = urlFragment.substring(0,
114: urlFragment.length() - 1);
115: }
116:
117: String[] parts = urlFragment.split("/");
118: for (int i = 0; i < parts.length; i++) {
119: if (WebRequestCodingStrategy.PAGEMAP.equals(parts[i])) {
120: i++;
121: params
122: .put(
123: WebRequestCodingStrategy.PAGEMAP,
124: WebRequestCodingStrategy
125: .decodePageMapName(urlDecode(parts[i])));
126: } else {
127: params.put(String.valueOf(i), urlDecode(parts[i]));
128: }
129: }
130: return params;
131: }
132:
133: }
|