001
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
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 @Override
043 public void addGeneratedParameter(String name, String pattern) {
044 StringParser stringParser = new StringParser(pattern);
045
046 _generatedParameters.put(name, stringParser);
047 }
048
049 @Override
050 public void addIgnoredParameter(String name) {
051 _ignoredParameters.add(name);
052 }
053
054 @Override
055 public void addImplicitParameter(String name, String value) {
056 _implicitParameters.put(name, value);
057 }
058
059 @Override
060 public void addOverriddenParameter(String name, String value) {
061 _overriddenParameters.put(name, value);
062 }
063
064 @Override
065 public Map<String, StringParser> getGeneratedParameters() {
066 return _generatedParameters;
067 }
068
069 @Override
070 public Set<String> getIgnoredParameters() {
071 return _ignoredParameters;
072 }
073
074 @Override
075 public Map<String, String> getImplicitParameters() {
076 return _implicitParameters;
077 }
078
079 @Override
080 public Map<String, String> getOverriddenParameters() {
081 return _overriddenParameters;
082 }
083
084 @Override
085 public String parametersToUrl(Map<String, String> parameters) {
086 InheritableMap<String, String> allParameters =
087 new InheritableMap<String, String>();
088
089 allParameters.setParentMap(parameters);
090
091
092
093
094 for (Map.Entry<String, StringParser> entry :
095 _generatedParameters.entrySet()) {
096
097 String name = entry.getKey();
098 StringParser stringParser = entry.getValue();
099
100 String value = MapUtil.getString(allParameters, name);
101
102 if (!stringParser.parse(value, allParameters)) {
103 return null;
104 }
105 }
106
107 for (Map.Entry<String, String> entry : _implicitParameters.entrySet()) {
108 String name = entry.getKey();
109 String value = entry.getValue();
110
111 if (!value.equals(MapUtil.getString(allParameters, name))) {
112 return null;
113 }
114 }
115
116 String url = _stringParser.build(allParameters);
117
118 if (Validator.isNull(url)) {
119 return null;
120 }
121
122 for (String name : _generatedParameters.keySet()) {
123
124
125
126
127 parameters.remove(name);
128 }
129
130 for (String name : _implicitParameters.keySet()) {
131 parameters.remove(name);
132 }
133
134 for (String name : _ignoredParameters) {
135 parameters.remove(name);
136 }
137
138 return url;
139 }
140
141 @Override
142 public boolean urlToParameters(String url, Map<String, String> parameters) {
143 if (!_stringParser.parse(url, parameters)) {
144 return false;
145 }
146
147 parameters.putAll(_implicitParameters);
148 parameters.putAll(_overriddenParameters);
149
150
151
152
153 for (Map.Entry<String, StringParser> entry :
154 _generatedParameters.entrySet()) {
155
156 String name = entry.getKey();
157 StringParser stringParser = entry.getValue();
158
159 String value = stringParser.build(parameters);
160
161
162
163
164
165 if (value != null) {
166 parameters.put(name, value);
167 }
168 }
169
170 return true;
171 }
172
173 private static StringEncoder _urlEncoder = new URLStringEncoder();
174
175 private Map<String, StringParser> _generatedParameters =
176 new HashMap<String, StringParser>();
177 private Set<String> _ignoredParameters = new LinkedHashSet<String>();
178 private Map<String, String> _implicitParameters =
179 new HashMap<String, String>();
180 private Map<String, String> _overriddenParameters =
181 new HashMap<String, String>();
182 private StringParser _stringParser;
183
184 }