Skip to content

Commit daa3686

Browse files
IgorZaitsev-GBIHaiHoang-GBI
authored andcommitted
[SAD-290] Fixed URI builder issue. URI contained '/null'
1 parent fe75f5b commit daa3686

1 file changed

Lines changed: 31 additions & 6 deletions

File tree

src/main/java/com/groupbyinc/util/UrlBeautifier.java

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.groupbyinc.common.apache.http.client.utils.URIBuilder;
1313
import com.groupbyinc.injector.StaticInjector;
1414
import com.groupbyinc.injector.StaticInjectorFactory;
15+
import org.apache.logging.log4j.util.Strings;
1516

1617
import java.net.URI;
1718
import java.net.URISyntaxException;
@@ -115,6 +116,9 @@ public String toUrl(String searchString, String existingRefinements) throws UrlB
115116
addAppend(uri);
116117
addUnmappedRefinements(navigations, uri);
117118
String uriString = uri.toString();
119+
// uriString.startsWith("null") in my mind this is not correct decision because
120+
// we are trying to modify result without finding and fixing the cause
121+
// The issue has been resolved. This code left here just in case
118122
return uriString.startsWith("null") ? uriString.substring(4) : uriString;
119123
}
120124

@@ -164,9 +168,15 @@ private void addRefinements(
164168
pathSegmentLookup.append(getToken(n.getName()));
165169
RefinementValue rv = (RefinementValue) r;
166170
rv.setValue(applyReplacementRule(n, rv.getValue(), indexOffSet, replacements));
167-
String encodedRefValue = "/" + UrlEncoder.encode(rv.getValue());
168-
indexOffSet += rv.getValue().length() + 1;
169-
uri.setPath(uri.getPath() + encodedRefValue);
171+
if(Strings.isNotBlank(UrlEncoder.encode(rv.getValue()))){
172+
String encodedRefValue = "/" + UrlEncoder.encode(rv.getValue());
173+
indexOffSet += rv.getValue().length() + 1;
174+
if(Strings.isNotBlank(uri.getPath())){
175+
uri.setPath(uri.getPath() + encodedRefValue);
176+
}else {
177+
uri.setPath(encodedRefValue);
178+
}
179+
}
170180
ri.remove();
171181
break;
172182
case Range:
@@ -187,13 +197,21 @@ private void addRefinements(
187197

188198
private void addReferenceBlock(StringBuilder reference, URIBuilder uri) {
189199
if (reference.length() > 1) {
190-
uri.setPath(uri.getPath() + reference.toString());
200+
if(Strings.isNotBlank(uri.getPath())){
201+
uri.setPath(uri.getPath() + reference);
202+
}else{
203+
uri.setPath(reference.toString());
204+
}
191205
}
192206
}
193207

194208
private void addAppend(URIBuilder uri) {
195209
if (StringUtils.isNotBlank(append)) {
196-
uri.setPath(uri.getPath() + append);
210+
if(Strings.isNotBlank(uri.getPath())){
211+
uri.setPath(uri.getPath() + append);
212+
}else{
213+
uri.setPath(append);
214+
}
197215
}
198216
}
199217

@@ -227,7 +245,11 @@ private String applyReplacementRule(
227245

228246
private void addSearchString(String searchString, StringBuilder reference, URIBuilder pUri) {
229247
if (StringUtils.isNotBlank(searchString)) {
230-
pUri.setPath(pUri.getPath() + "/" + UrlEncoder.encode(searchString));
248+
if(Strings.isNotBlank(pUri.getPath())){
249+
pUri.setPath(pUri.getPath() + "/" + UrlEncoder.encode(searchString));
250+
}else{
251+
pUri.setPath(UrlEncoder.encode(searchString));
252+
}
231253
reference.append(SEARCH_NAVIGATION.getDisplayName());
232254
}
233255
}
@@ -278,6 +300,9 @@ public String toUrl(String searchString, Map<String, Navigation> navigations) th
278300
addAppend(uri);
279301
addUnmappedRefinements(groupedRefinements, uri);
280302
String uriString = uri.toString();
303+
// uriString.startsWith("null") in my mind this is not correct decision because
304+
// we are trying to modify result without finding and fixing the cause
305+
// The issue has been resolved. This code left here just in case
281306
return uriString.startsWith("null") ? uriString.substring(4) : uriString;
282307
}
283308

0 commit comments

Comments
 (0)