Skip to content

Commit 114fcb4

Browse files
committed
Added some additional null checks to prevent NullPointerExceptions
1 parent baab948 commit 114fcb4

1 file changed

Lines changed: 78 additions & 66 deletions

File tree

‎core/src/main/java/com/ocpsoft/pretty/faces/util/PrettyURLBuilder.java‎

Lines changed: 78 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,15 @@ public String build(final UrlMapping mapping, final Map<String, String[]> parame
8080
public String build(final UrlMapping mapping, final boolean encodeUrl, final Map<String, String[]> parameters)
8181
{
8282
List<UIParameter> list = new ArrayList<UIParameter>();
83-
for (Entry<String, String[]> e : parameters.entrySet())
83+
if(parameters != null)
8484
{
85-
UIParameter p = new UIParameter();
86-
p.setName(e.getKey());
87-
p.setValue(e.getValue());
88-
list.add(p);
85+
for (Entry<String, String[]> e : parameters.entrySet())
86+
{
87+
UIParameter p = new UIParameter();
88+
p.setName(e.getKey());
89+
p.setValue(e.getValue());
90+
list.add(p);
91+
}
8992
}
9093
return build(mapping, false, list);
9194
}
@@ -110,14 +113,17 @@ public String build(final UrlMapping mapping, final Object... parameters)
110113
public String build(final UrlMapping mapping, final boolean encodeUrl, final Object... parameters)
111114
{
112115
List<UIParameter> list = new ArrayList<UIParameter>();
113-
for (Object e : parameters)
116+
if(parameters != null)
114117
{
115-
UIParameter p = new UIParameter();
116-
if (e != null)
118+
for (Object e : parameters)
117119
{
118-
p.setValue(e.toString());
120+
UIParameter p = new UIParameter();
121+
if (e != null)
122+
{
123+
p.setValue(e.toString());
124+
}
125+
list.add(p);
119126
}
120-
list.add(p);
121127
}
122128
return build(mapping, encodeUrl, list);
123129
}
@@ -142,15 +148,18 @@ public String build(final UrlMapping mapping, final RequestParameter... paramete
142148
public String build(final UrlMapping mapping, final boolean encodeUrl, final RequestParameter... parameters)
143149
{
144150
List<UIParameter> list = new ArrayList<UIParameter>();
145-
for (RequestParameter param : parameters)
151+
if(parameters != null)
146152
{
147-
UIParameter p = new UIParameter();
148-
if (param != null)
153+
for (RequestParameter param : parameters)
149154
{
150-
p.setValue(param.getName());
151-
p.setValue(param.getValue());
155+
UIParameter p = new UIParameter();
156+
if (param != null)
157+
{
158+
p.setValue(param.getName());
159+
p.setValue(param.getValue());
160+
}
161+
list.add(p);
152162
}
153-
list.add(p);
154163
}
155164
return build(mapping, false, list);
156165
}
@@ -178,70 +187,73 @@ public String build(final UrlMapping urlMapping, final boolean encodeUrl, final
178187
{
179188
URLPatternParser parser = new URLPatternParser(urlMapping.getPattern());
180189
List<String> pathParams = new ArrayList<String>();
190+
List<QueryParameter> queryParams = new ArrayList<QueryParameter>();
181191

182-
// TODO this logic should be in the components, not in the builder
183-
if (parameters.size() == 1)
192+
if(parameters != null)
184193
{
185-
UIParameter firstParam = parameters.get(0);
186-
if (((firstParam.getValue() != null)) && (firstParam.getName() == null))
194+
// TODO this logic should be in the components, not in the builder
195+
if (parameters.size() == 1)
187196
{
188-
if (firstParam.getValue() instanceof List<?>)
197+
UIParameter firstParam = parameters.get(0);
198+
if (((firstParam.getValue() != null)) && (firstParam.getName() == null))
189199
{
190-
URL url = parser.getMappedURL(firstParam.getValue());
191-
return url.toURL();
192-
}
193-
else if (firstParam.getValue().getClass().isArray())
194-
{
195-
// The Object[] cast here is required, otherwise Java treats
196-
// getValue() as a single Object.
197-
List<Object> list = Arrays.asList((Object[]) firstParam.getValue());
198-
URL url = parser.getMappedURL(list);
199-
return url.toURL();
200+
if (firstParam.getValue() instanceof List<?>)
201+
{
202+
URL url = parser.getMappedURL(firstParam.getValue());
203+
return url.toURL();
204+
}
205+
else if (firstParam.getValue().getClass().isArray())
206+
{
207+
// The Object[] cast here is required, otherwise Java treats
208+
// getValue() as a single Object.
209+
List<Object> list = Arrays.asList((Object[]) firstParam.getValue());
210+
URL url = parser.getMappedURL(list);
211+
return url.toURL();
212+
}
200213
}
201214
}
202-
}
203-
204-
List<QueryParameter> queryParams = new ArrayList<QueryParameter>();
205-
for (UIParameter parameter : parameters)
206-
{
207-
String name = parameter.getName();
208-
Object value = parameter.getValue();
209-
210-
if ((name == null) && (value != null))
211-
{
212-
pathParams.add(value.toString());
213-
}
214-
else
215+
216+
for (UIParameter parameter : parameters)
215217
{
216-
List<?> values = null;
217-
if ((value != null) && value.getClass().isArray())
218-
{
219-
values = Arrays.asList((Object[]) value);
220-
}
221-
else if (value instanceof List<?>)
222-
{
223-
values = (List<?>) value;
224-
}
225-
else if (value != null)
218+
String name = parameter.getName();
219+
Object value = parameter.getValue();
220+
221+
if ((name == null) && (value != null))
226222
{
227-
values = Arrays.asList(value);
223+
pathParams.add(value.toString());
228224
}
229-
230-
if (values != null)
225+
else
231226
{
232-
for (Object object : values)
227+
List<?> values = null;
228+
if ((value != null) && value.getClass().isArray())
229+
{
230+
values = Arrays.asList((Object[]) value);
231+
}
232+
else if (value instanceof List<?>)
233233
{
234-
String tempValue = null;
235-
if (object != null)
234+
values = (List<?>) value;
235+
}
236+
else if (value != null)
237+
{
238+
values = Arrays.asList(value);
239+
}
240+
241+
if (values != null)
242+
{
243+
for (Object object : values)
236244
{
237-
tempValue = object.toString();
245+
String tempValue = null;
246+
if (object != null)
247+
{
248+
tempValue = object.toString();
249+
}
250+
queryParams.add(new QueryParameter(name, tempValue));
238251
}
239-
queryParams.add(new QueryParameter(name, tempValue));
240252
}
241-
}
242-
else
243-
{
244-
queryParams.add(new QueryParameter(name, null));
253+
else
254+
{
255+
queryParams.add(new QueryParameter(name, null));
256+
}
245257
}
246258
}
247259
}

0 commit comments

Comments
 (0)