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 */ 017package org.apache.wicket.request.mapper.parameter; 018 019import java.util.Iterator; 020 021import org.apache.wicket.request.Url; 022import org.apache.wicket.util.lang.Args; 023import org.apache.wicket.util.string.Strings; 024 025 026/** 027 * <p> 028 * Encodes page parameters into Url path fragments instead of the query string like the default 029 * {@link PageParametersEncoder}. The parameters are encoded in the following format: 030 * {@code /param1Name/param1Value/param2Name/param2Value}. 031 * </p> 032 * <strong>Note</strong>: Because of the nature of the encoder it doesn't support POST request 033 * parameters. 034 * <p> 035 * This used to be the default way of encoding page parameters in 1.4.x applications. Newer 1.5.x+ 036 * applications use the query string, by default. This class facilitates backwards compatibility and 037 * migrations of 1.4.x application to 1.5.x+ codebase. 038 * <p> 039 * Example usage: 040 * {@code mount(new MountedMapper("/myPage", MyPage.class, new UrlPathPageParametersEncoder())); } 041 * 042 * @author Chris Colman 043 * @author James Gilbertson 044 * @author ivaynberg 045 */ 046public class UrlPathPageParametersEncoder implements IPageParametersEncoder 047{ 048 @Override 049 public Url encodePageParameters(PageParameters params) 050 { 051 Args.notNull(params, "params"); 052 Args.isTrue(params.getIndexedCount() == 0, 053 "This encoder does not support indexed page parameters. Specified parameters: %s", 054 params); 055 056 Url url = new Url(); 057 058 for (PageParameters.NamedPair pair : params.getAllNamed()) 059 { 060 url.getSegments().add(pair.getKey()); 061 url.getSegments().add(pair.getValue()); 062 } 063 064 return url; 065 } 066 067 @Override 068 public PageParameters decodePageParameters(Url url) 069 { 070 PageParameters params = new PageParameters(); 071 072 for (Iterator<String> segment = url.getSegments().iterator(); segment.hasNext();) 073 { 074 String key = segment.next(); 075 if (Strings.isEmpty(key)) 076 { 077 // keys cannot be empty 078 continue; 079 } 080 // A trailing slash can be seen as an extra segment with a "" value so check 081 // if there is a matching value for this parameter name and ignore it if not 082 if (segment.hasNext()) 083 { 084 String value = segment.next(); 085 086 params.add(key, value, INamedParameters.Type.PATH); 087 } 088 } 089 090 return params.isEmpty() ? null : params; 091 } 092}