Skip to content

Commit 6751ab2

Browse files
authored
Merge pull request #1116 from amvanbaren/java-null-pointers
Fix a "NullPointerException" could be thrown;
2 parents 53ad2d7 + e9ea04b commit 6751ab2

File tree

5 files changed

+45
-22
lines changed

5 files changed

+45
-22
lines changed

server/src/main/java/org/eclipse/openvsx/UpstreamRegistryService.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,14 @@ public ExtensionJson getExtension(String namespace, String extension, String tar
116116

117117
try {
118118
var json = restTemplate.getForObject(urlTemplate, ExtensionJson.class, uriVariables);
119-
makeDownloadsCompatible(json);
120-
return proxy != null ? proxy.rewriteUrls(json) : json;
119+
if(json != null) {
120+
makeDownloadsCompatible(json);
121+
if(proxy != null) {
122+
proxy.rewriteUrls(json);
123+
}
124+
}
125+
126+
return json;
121127
} catch (RestClientException exc) {
122128
if(!isNotFound(exc)) {
123129
var url = UriComponentsBuilder.fromUriString(urlTemplate).build(uriVariables);
@@ -144,8 +150,14 @@ public ExtensionJson getExtension(String namespace, String extension, String tar
144150

145151
try {
146152
var json = restTemplate.getForObject(urlTemplate, ExtensionJson.class, uriVariables);
147-
makeDownloadsCompatible(json);
148-
return proxy != null ? proxy.rewriteUrls(json) : json;
153+
if(json != null) {
154+
makeDownloadsCompatible(json);
155+
if(proxy != null) {
156+
proxy.rewriteUrls(json);
157+
}
158+
}
159+
160+
return json;
149161
} catch (RestClientException exc) {
150162
if(!isNotFound(exc)) {
151163
var url = UriComponentsBuilder.fromUriString(urlTemplate).build(uriVariables);

server/src/main/java/org/eclipse/openvsx/adapter/UpstreamVSCodeService.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -178,20 +178,22 @@ public String download(String namespace, String extension, String version, Strin
178178
throw propagateRestException(exc, method, urlTemplate, uriVariables);
179179
}
180180

181+
URI location = null;
181182
var statusCode = response.getStatusCode();
182183
if(statusCode.is3xxRedirection()) {
183-
var location = response.getHeaders().getLocation();
184-
if(proxy != null) {
185-
location = proxy.rewriteUrl(location);
186-
}
187-
188-
return location.toString();
184+
location = response.getHeaders().getLocation();
189185
}
190186
if(statusCode.isError() && statusCode != HttpStatus.NOT_FOUND) {
191187
handleResponseError(urlTemplate, uriVariables, response);
192188
}
189+
if(location == null) {
190+
throw new NotFoundException();
191+
}
193192

194-
throw new NotFoundException();
193+
if(proxy != null) {
194+
location = proxy.rewriteUrl(location);
195+
}
196+
return location.toString();
195197
}
196198

197199
@Override
@@ -207,20 +209,22 @@ public String getItemUrl(String namespace, String extension) {
207209
throw propagateRestException(exc, method, urlTemplate, uriVariables);
208210
}
209211

212+
URI location = null;
210213
var statusCode = response.getStatusCode();
211214
if(statusCode.is3xxRedirection()) {
212-
var location = response.getHeaders().getLocation();
213-
if(proxy != null) {
214-
location = proxy.rewriteUrl(location);
215-
}
216-
217-
return location.toString();
215+
location = response.getHeaders().getLocation();
218216
}
219217
if(statusCode.isError() && statusCode != HttpStatus.NOT_FOUND) {
220218
handleResponseError(urlTemplate, uriVariables, response);
221219
}
220+
if(location == null) {
221+
throw new NotFoundException();
222+
}
222223

223-
throw new NotFoundException();
224+
if(proxy != null) {
225+
location = proxy.rewriteUrl(location);
226+
}
227+
return location.toString();
224228
}
225229

226230
@Override

server/src/main/java/org/eclipse/openvsx/adapter/VSCodeIdService.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,9 @@ private ExtensionQueryResult.Extension getUpstreamExtension(Extension extension)
105105
headers.setContentType(MediaType.APPLICATION_JSON);
106106
headers.set(HttpHeaders.ACCEPT, "application/json;api-version=" + API_VERSION);
107107
var result = vsCodeIdRestTemplate.postForObject(requestUrl, new HttpEntity<>(requestData, headers), ExtensionQueryResult.class);
108-
109-
if (result.results() != null && result.results().size() > 0) {
108+
if (result != null && result.results() != null && !result.results().isEmpty()) {
110109
var item = result.results().get(0);
111-
if (item.extensions() != null && item.extensions().size() > 0) {
110+
if (item.extensions() != null && !item.extensions().isEmpty()) {
112111
return item.extensions().get(0);
113112
}
114113
}

server/src/main/java/org/eclipse/openvsx/eclipse/EclipseService.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,10 @@ public EclipseProfile getUserProfile(String accessToken) {
258258

259259
private EclipseProfile parseEclipseProfile(ResponseEntity<String> response) {
260260
var json = response.getBody();
261+
if(json == null) {
262+
return new EclipseProfile();
263+
}
264+
261265
try {
262266
if (json.startsWith("[\"")) {
263267
var error = objectMapper.readValue(json, TYPE_LIST_STRING);
@@ -373,6 +377,10 @@ public PublisherAgreement signPublisherAgreement(UserData user) {
373377

374378
private PublisherAgreement parseAgreementResponse(ResponseEntity<String> response) {
375379
var json = response.getBody();
380+
if(json == null) {
381+
return null;
382+
}
383+
376384
try {
377385
PublisherAgreementResponse agreementResponse;
378386
if (json.startsWith("[\"")) {

server/src/main/java/org/eclipse/openvsx/search/ElasticSearchService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ private void sortResults(NativeQueryBuilder queryBuilder, String sortOrder, Stri
377377
private long getMaxResultWindow() {
378378
if(maxResultWindow == null) {
379379
var settings = searchOperations.indexOps(ExtensionSearch.class).getSettings(true);
380-
maxResultWindow = Long.parseLong(settings.get("index.max_result_window").toString());
380+
maxResultWindow = Long.parseLong(settings.getOrDefault("index.max_result_window", "10000").toString());
381381
}
382382

383383
return maxResultWindow;

0 commit comments

Comments
 (0)