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.parameter;
018
019import java.util.ArrayList;
020import java.util.Collections;
021import java.util.LinkedHashSet;
022import java.util.List;
023import java.util.Set;
024
025import org.apache.wicket.request.IRequestParameters;
026import org.apache.wicket.request.Url;
027import org.apache.wicket.request.Url.QueryParameter;
028import org.apache.wicket.util.lang.Args;
029import org.apache.wicket.util.lang.Objects;
030import org.apache.wicket.util.string.StringValue;
031
032/**
033 * Utility class that expresses query parameters from {@link Url} as {@link IRequestParameters}.
034 * 
035 * @author Matej Knopp
036 */
037public class UrlRequestParametersAdapter implements IRequestParameters
038{
039        private final Url url;
040
041        /**
042         * Construct.
043         * 
044         * @param url
045         */
046        public UrlRequestParametersAdapter(final Url url)
047        {
048                Args.notNull(url, "url");
049
050                this.url = url;
051        }
052
053        /**
054         * @see org.apache.wicket.request.IRequestParameters#getParameterNames()
055         */
056        @Override
057        public Set<String> getParameterNames()
058        {
059                Set<String> result = new LinkedHashSet<>();
060                for (QueryParameter parameter : url.getQueryParameters())
061                {
062                        result.add(parameter.getName());
063                }
064                return Collections.unmodifiableSet(result);
065        }
066
067        /**
068         * @see org.apache.wicket.request.IRequestParameters#getParameterValue(java.lang.String)
069         */
070        @Override
071        public StringValue getParameterValue(final String name)
072        {
073                return url.getQueryParameterValue(name);
074        }
075
076        /**
077         * @see org.apache.wicket.request.IRequestParameters#getParameterValues(java.lang.String)
078         */
079        @Override
080        public List<StringValue> getParameterValues(final String name)
081        {
082                List<StringValue> values = null;
083                for (QueryParameter parameter : url.getQueryParameters())
084                {
085                        if (Objects.equal(name, parameter.getName()))
086                        {
087                                if (values == null)
088                                {
089                                        values = new ArrayList<>();
090                                }
091                                values.add(StringValue.valueOf(parameter.getValue()));
092                        }
093                }
094                return values != null ? Collections.unmodifiableList(values) : null;
095        }
096}