001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet;
016    
017    import com.liferay.portal.kernel.portlet.Route;
018    import com.liferay.portal.kernel.util.InheritableMap;
019    import com.liferay.portal.kernel.util.MapUtil;
020    import com.liferay.portal.kernel.util.StringEncoder;
021    import com.liferay.portal.kernel.util.StringParser;
022    import com.liferay.portal.kernel.util.URLStringEncoder;
023    
024    import java.util.HashMap;
025    import java.util.LinkedHashSet;
026    import java.util.Map;
027    import java.util.Set;
028    
029    /**
030     * @author Connor McKay
031     * @author Brian Wing Shun Chan
032     */
033    public class RouteImpl implements Route {
034    
035            public RouteImpl(String pattern) {
036                    _stringParser = new StringParser(pattern);
037    
038                    _stringParser.setStringEncoder(_urlEncoder);
039            }
040    
041            public void addGeneratedParameter(String name, String pattern) {
042                    StringParser stringParser = new StringParser(pattern);
043    
044                    _generatedParameters.put(name, stringParser);
045            }
046    
047            public void addIgnoredParameter(String name) {
048                    _ignoredParameters.add(name);
049            }
050    
051            public void addImplicitParameter(String name, String value) {
052                    _implicitParameters.put(name, value);
053            }
054    
055            public void addOverriddenParameter(String name, String value) {
056                    _overriddenParameters.put(name, value);
057            }
058    
059            public Map<String, StringParser> getGeneratedParameters() {
060                    return _generatedParameters;
061            }
062    
063            public Set<String> getIgnoredParameters() {
064                    return _ignoredParameters;
065            }
066    
067            public Map<String, String> getImplicitParameters() {
068                    return _implicitParameters;
069            }
070    
071            public Map<String, String> getOverriddenParameters() {
072                    return _overriddenParameters;
073            }
074    
075            public String parametersToUrl(Map<String, String> parameters) {
076                    InheritableMap<String, String> allParameters =
077                            new InheritableMap<String, String>();
078    
079                    allParameters.setParentMap(parameters);
080    
081                    // The order is important because virtual parameters may sometimes be
082                    // checked by implicit parameters
083    
084                    for (Map.Entry<String, StringParser> entry :
085                                    _generatedParameters.entrySet()) {
086    
087                            String name = entry.getKey();
088                            StringParser stringParser = entry.getValue();
089    
090                            String value = MapUtil.getString(allParameters, name);
091    
092                            if (!stringParser.parse(value, allParameters)) {
093                                    return null;
094                            }
095                    }
096    
097                    for (Map.Entry<String, String> entry : _implicitParameters.entrySet()) {
098                            String name = entry.getKey();
099                            String value = entry.getValue();
100    
101                            if (!value.equals(MapUtil.getString(allParameters, name))) {
102                                    return null;
103                            }
104                    }
105    
106                    String url = _stringParser.build(allParameters);
107    
108                    if (url == null) {
109                            return null;
110                    }
111    
112                    for (String name : _generatedParameters.keySet()) {
113    
114                            // Virtual parameters will never be placed in the query string,
115                            // so parameters is modified directly instead of allParameters
116    
117                            parameters.remove(name);
118                    }
119    
120                    for (String name : _implicitParameters.keySet()) {
121                            parameters.remove(name);
122                    }
123    
124                    for (String name : _ignoredParameters) {
125                            parameters.remove(name);
126                    }
127    
128                    return url;
129            }
130    
131            public boolean urlToParameters(String url, Map<String, String> parameters) {
132                    if (!_stringParser.parse(url, parameters)) {
133                            return false;
134                    }
135    
136                    parameters.putAll(_implicitParameters);
137                    parameters.putAll(_overriddenParameters);
138    
139                    // The order is important because generated parameters may be dependent
140                    // on implicit parameters or overridden parameters
141    
142                    for (Map.Entry<String, StringParser> entry :
143                                    _generatedParameters.entrySet()) {
144    
145                            String name = entry.getKey();
146                            StringParser stringParser = entry.getValue();
147    
148                            String value = stringParser.build(parameters);
149    
150                            // Generated parameters are not guaranteed to be created. The format
151                            // of the virtual parameters in the route pattern must match their
152                            // format in the generated parameter.
153    
154                            if (value != null) {
155                                    parameters.put(name, value);
156                            }
157                    }
158    
159                    return true;
160            }
161    
162            private static StringEncoder _urlEncoder = new URLStringEncoder();
163    
164            private Map<String, StringParser> _generatedParameters =
165                    new HashMap<String, StringParser>();
166            private Set<String> _ignoredParameters = new LinkedHashSet<String>();
167            private Map<String, String> _implicitParameters =
168                    new HashMap<String, String>();
169            private Map<String, String> _overriddenParameters =
170                    new HashMap<String, String>();
171            private StringParser _stringParser;
172    
173    }