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.core.request.mapper;
018
019import org.apache.wicket.request.IRequestMapper;
020import org.apache.wicket.request.Url;
021import org.apache.wicket.request.mapper.info.PageComponentInfo;
022import org.apache.wicket.util.lang.Args;
023import org.apache.wicket.util.string.Strings;
024
025/**
026 * Utility class that performs common functions used by {@link IRequestMapper}s.
027 */
028public class MapperUtils
029{
030        private MapperUtils()
031        {
032        }
033
034        /**
035         * Attempts to parse a {@link Url.QueryParameter} which may hold {@link PageComponentInfo}.
036         *
037         * @param parameter
038         *              The {@link Url.QueryParameter} to parse.
039         *
040         * @return The parsed {@link PageComponentInfo}, or {@code null} if the parameter could not be parsed.
041         */
042        public static PageComponentInfo parsePageComponentInfoParameter(final Url.QueryParameter parameter)
043        {
044                Args.notNull(parameter, "parameter");
045
046                if (Strings.isEmpty(parameter.getName()) == false && Strings.isEmpty(parameter.getValue()) == true)
047                {
048                        return PageComponentInfo.parse(parameter.getName());
049                }
050
051                return null;
052        }
053
054        /**
055         * Extracts the {@link PageComponentInfo} from the URL. The {@link PageComponentInfo} is encoded
056         * as the very first query parameter and the parameter consists of name only (no value).
057         *
058         * @param url
059         *
060         * @return PageComponentInfo instance if one was encoded in URL, <code>null</code> otherwise.
061         */
062        public static PageComponentInfo getPageComponentInfo(final Url url)
063        {
064                Args.notNull(url, "url");
065
066                for (Url.QueryParameter queryParameter : url.getQueryParameters())
067                {
068                        PageComponentInfo pageComponentInfo = parsePageComponentInfoParameter(queryParameter);
069
070                        if (pageComponentInfo != null)
071                        {
072                                return pageComponentInfo;
073                        }
074                }
075
076                return null;
077        }
078}