001    /**
002     * Copyright (c) 2000-2012 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    import com.liferay.portal.kernel.util.Validator;
024    
025    import java.util.HashMap;
026    import java.util.LinkedHashSet;
027    import java.util.Map;
028    import java.util.Set;
029    
030    /**
031     * @author Connor McKay
032     * @author Brian Wing Shun Chan
033     */
034    public class RouteImpl implements Route {
035    
036            public RouteImpl(String pattern) {
037                    _stringParser = new StringParser(pattern);
038    
039                    _stringParser.setStringEncoder(_urlEncoder);
040            }
041    
042            public void addGeneratedParameter(String name, String pattern) {
043                    StringParser stringParser = new StringParser(pattern);
044    
045                    _generatedParameters.put(name, stringParser);
046            }
047    
048            public void addIgnoredParameter(String name) {
049                    _ignoredParameters.add(name);
050            }
051    
052            public void addImplicitParameter(String name, String value) {
053                    _implicitParameters.put(name, value);
054            }
055    
056            public void addOverriddenParameter(String name, String value) {
057                    _overriddenParameters.put(name, value);
058            }
059    
060            public Map<String, StringParser> getGeneratedParameters() {
061                    return _generatedParameters;
062            }
063    
064            public Set<String> getIgnoredParameters() {
065                    return _ignoredParameters;
066            }
067    
068            public Map<String, String> getImplicitParameters() {
069                    return _implicitParameters;
070            }
071    
072            public Map<String, String> getOverriddenParameters() {
073                    return _overriddenParameters;
074            }
075    
076            public String parametersToUrl(Map<String, String> parameters) {
077                    InheritableMap<String, String> allParameters =
078                            new InheritableMap<String, String>();
079    
080                    allParameters.setParentMap(parameters);
081    
082                    // The order is important because virtual parameters may sometimes be
083                    // checked by implicit parameters
084    
085                    for (Map.Entry<String, StringParser> entry :
086                                    _generatedParameters.entrySet()) {
087    
088                            String name = entry.getKey();
089                            StringParser stringParser = entry.getValue();
090    
091                            String value = MapUtil.getString(allParameters, name);
092    
093                            if (!stringParser.parse(value, allParameters)) {
094                                    return null;
095                            }
096                    }
097    
098                    for (Map.Entry<String, String> entry : _implicitParameters.entrySet()) {
099                            String name = entry.getKey();
100                            String value = entry.getValue();
101    
102                            if (!value.equals(MapUtil.getString(allParameters, name))) {
103                                    return null;
104                            }
105                    }
106    
107                    String url = _stringParser.build(allParameters);
108    
109                    if (Validator.isNull(url)) {
110                            return null;
111                    }
112    
113                    for (String name : _generatedParameters.keySet()) {
114    
115                            // Virtual parameters will never be placed in the query string,
116                            // so parameters is modified directly instead of allParameters
117    
118                            parameters.remove(name);
119                    }
120    
121                    for (String name : _implicitParameters.keySet()) {
122                            parameters.remove(name);
123                    }
124    
125                    for (String name : _ignoredParameters) {
126                            parameters.remove(name);
127                    }
128    
129                    return url;
130            }
131    
132            public boolean urlToParameters(String url, Map<String, String> parameters) {
133                    if (!_stringParser.parse(url, parameters)) {
134                            return false;
135                    }
136    
137                    parameters.putAll(_implicitParameters);
138                    parameters.putAll(_overriddenParameters);
139    
140                    // The order is important because generated parameters may be dependent
141                    // on implicit parameters or overridden parameters
142    
143                    for (Map.Entry<String, StringParser> entry :
144                                    _generatedParameters.entrySet()) {
145    
146                            String name = entry.getKey();
147                            StringParser stringParser = entry.getValue();
148    
149                            String value = stringParser.build(parameters);
150    
151                            // Generated parameters are not guaranteed to be created. The format
152                            // of the virtual parameters in the route pattern must match their
153                            // format in the generated parameter.
154    
155                            if (value != null) {
156                                    parameters.put(name, value);
157                            }
158                    }
159    
160                    return true;
161            }
162    
163            private static StringEncoder _urlEncoder = new URLStringEncoder();
164    
165            private Map<String, StringParser> _generatedParameters =
166                    new HashMap<String, StringParser>();
167            private Set<String> _ignoredParameters = new LinkedHashSet<String>();
168            private Map<String, String> _implicitParameters =
169                    new HashMap<String, String>();
170            private Map<String, String> _overriddenParameters =
171                    new HashMap<String, String>();
172            private StringParser _stringParser;
173    
174    }