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.PageParameters;
022: import org.apache.wicket.WicketRuntimeException;
023: import org.apache.wicket.protocol.http.request.WebRequestCodingStrategy;
024: import org.apache.wicket.util.string.AppendingStringBuffer;
025: import org.apache.wicket.util.value.ValueMap;
026:
027: /**
028: * {@link HybridUrlCodingStrategy} subclass that encodes the parameters in
029: * an indexed way like {@link IndexedParamUrlCodingStrategy} does.
030: *
031: * @author Matej Knopp
032: */
033: public class IndexedHybridUrlCodingStrategy extends
034: HybridUrlCodingStrategy {
035:
036: /**
037: *
038: * Construct.
039: *
040: * @param mountPath
041: * @param pageClass
042: */
043: public IndexedHybridUrlCodingStrategy(String mountPath,
044: Class pageClass) {
045: super (mountPath, pageClass);
046: }
047:
048: protected void appendParameters(AppendingStringBuffer url,
049: Map parameters) {
050: int i = 0;
051: while (parameters.containsKey(String.valueOf(i))) {
052: String value = (String) parameters.get(String.valueOf(i));
053: if (!url.endsWith("/")) {
054: url.append("/");
055: }
056: url.append(urlEncode(value)).append("/");
057: i++;
058: }
059:
060: String pageMap = (String) parameters
061: .get(WebRequestCodingStrategy.PAGEMAP);
062: if (pageMap != null) {
063: i++;
064: pageMap = WebRequestCodingStrategy
065: .encodePageMapName(pageMap);
066: if (!url.endsWith("/")) {
067: url.append("/");
068: }
069: url.append(WebRequestCodingStrategy.PAGEMAP).append("/")
070: .append(urlEncode(pageMap)).append("/");
071: }
072:
073: if (i != parameters.size()) {
074: throw new WicketRuntimeException(
075: "Not all parameters were encoded. Make sure all parameter names are integers in consecutive order starting with zero. Current parameter names are: "
076: + parameters.keySet().toString());
077: }
078: }
079:
080: protected ValueMap decodeParameters(String urlFragment,
081: Map urlParameters) {
082: PageParameters params = new PageParameters();
083: if (urlFragment == null) {
084: return params;
085: }
086: if (urlFragment.startsWith("/")) {
087: urlFragment = urlFragment.substring(1);
088: }
089: if (urlFragment.length() > 0 && urlFragment.endsWith("/")) {
090: urlFragment = urlFragment.substring(0,
091: urlFragment.length() - 1);
092: }
093:
094: String[] parts = urlFragment.split("/");
095: for (int i = 0; i < parts.length; i++) {
096: if (WebRequestCodingStrategy.PAGEMAP.equals(parts[i])) {
097: i++;
098: params
099: .put(
100: WebRequestCodingStrategy.PAGEMAP,
101: WebRequestCodingStrategy
102: .decodePageMapName(urlDecode(parts[i])));
103: } else {
104: params.put(String.valueOf(i), urlDecode(parts[i]));
105: }
106: }
107: return params;
108: }
109:
110: }
|