Skip to content

Commit

Permalink
Replace resource handling with try-with-resources
Browse files Browse the repository at this point in the history
  • Loading branch information
sfuhrm committed Jul 13, 2023
1 parent fae548e commit 12ec97c
Showing 1 changed file with 35 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,8 @@ private Map<String, Integer> retrieveValueStationCountList(

Entity<Form> entity = Entity.form(requestParams);

Response response = null;
try {
response = builder(webTarget.path(subPath))
.post(entity);

try (Response response = builder(webTarget.path(subPath))
.post(entity)) {
List<Map<String, String>> map = response.readEntity(
new GenericType<List<Map<String, String>>>() {
});
Expand All @@ -177,8 +174,6 @@ private Map<String, Integer> retrieveValueStationCountList(
m -> m.get("name"),
m -> Integer.parseInt(m.get("stationcount")),
(a, b) -> a));
} finally {
close(response);
}
}

Expand Down Expand Up @@ -236,16 +231,12 @@ private List<Station> listStationsPathWithPaging(
paging.ifPresent(p -> p.apply(requestParams));
Arrays.stream(listParam).forEach(lp -> lp.apply(requestParams));
Entity<Form> entity = Entity.form(requestParams);
Response response = null;
try {
response = builder(webTarget.path(path))
.post(entity);
try (Response response = builder(webTarget.path(path))
.post(entity)) {
checkResponseStatus(response);

return response.readEntity(new GenericType<List<Station>>() {
});
} finally {
close(response);
}
}

Expand All @@ -265,20 +256,16 @@ private List<Station> listStationsPathWithLimit(

Arrays.stream(listParam).forEach(lp -> lp.apply(requestParams));
Entity<Form> entity = Entity.form(requestParams);
Response response = null;
try {
WebTarget target = webTarget.path(path);
if (limit.isPresent()) {
target = target.path(Integer.toString(limit.get().getSize()));
}
response = builder(target)
.post(entity);
WebTarget target = webTarget.path(path);
if (limit.isPresent()) {
target = target.path(Integer.toString(limit.get().getSize()));
}
try (Response response = builder(target)
.post(entity)) {
checkResponseStatus(response);

return response.readEntity(new GenericType<List<Station>>() {
});
} finally {
close(response);
}
}

Expand Down Expand Up @@ -473,19 +460,15 @@ public List<Station> listStationsBy(@NonNull final Paging paging,
paging.apply(requestParams);
Arrays.stream(listParam).forEach(l -> l.apply(requestParams));
Entity<Form> entity = Entity.form(requestParams);
Response response = null;

try {
response = builder(webTarget
.path("json/stations")
.path(searchMode.name().toLowerCase())
.path(searchTerm))
.post(entity);

try (Response response = builder(webTarget
.path("json/stations")
.path(searchMode.name().toLowerCase())
.path(searchTerm))
.post(entity)) {
checkResponseStatus(response);
return response.readEntity(new GenericType<List<Station>>() {
});
} finally {
close(response);
}
}

Expand All @@ -506,19 +489,16 @@ public Stream<Station> listStationsBy(
p.apply(requestParams);
Arrays.stream(listParam).forEach(l -> l.apply(requestParams));
Entity<Form> entity = Entity.form(requestParams);
Response response = null;

try {
response = builder(webTarget
.path("json/stations")
.path(searchMode.name().toLowerCase())
.path(searchTerm))
.post(entity);
try (Response response = builder(webTarget
.path("json/stations")
.path(searchMode.name().toLowerCase())
.path(searchTerm))
.post(entity)) {

checkResponseStatus(response);
return response.readEntity(new GenericType<List<Station>>() {
});
} finally {
close(response);
}
};

Expand All @@ -533,12 +513,9 @@ public Stream<Station> listStationsBy(
* @throws RadioBrowserException if the URL could not be retrieved
*/
public URL resolveStreamUrl(@NonNull final UUID stationUUID) {
Response response = null;
try {
response = builder(webTarget.path("json/url")
.path(stationUUID.toString()))
.get();

try (Response response = builder(webTarget.path("json/url")
.path(stationUUID.toString()))
.get()) {
checkResponseStatus(response);
log.debug("URI is {}", webTarget.getUri());
try {
Expand All @@ -551,8 +528,6 @@ public URL resolveStreamUrl(@NonNull final UUID stationUUID) {
} catch (MalformedURLException e) {
throw new RadioBrowserException(e);
}
} finally {
close(response);
}
}

Expand All @@ -578,20 +553,16 @@ public UUID postNewStation(@NonNull final Station station) {
* voting for the station.
*/
public void voteForStation(@NonNull final UUID stationUUID) {
Response response = null;
try {
response = builder(webTarget
.path("json/vote/").path(stationUUID.toString()))
.get();
try (Response response = builder(webTarget
.path("json/vote/").path(stationUUID.toString()))
.get()) {

logResponseStatus(response);
UrlResponse urlResponse = response.readEntity(UrlResponse.class);

if (!urlResponse.isOk()) {
throw new RadioBrowserException(urlResponse.getMessage());
}
} finally {
close(response);
}
}

Expand Down Expand Up @@ -624,16 +595,14 @@ public Stream<Station> listStationsWithAdvancedSearch(
p.apply(requestParams);
advancedSearch.apply(requestParams);
Entity<Form> entity = Entity.form(requestParams);
Response response = null;

try {
response = builder(webTarget
.path("/json/stations/search")).post(entity);
try (Response response = builder(webTarget
.path("/json/stations/search"))
.post(entity);) {

checkResponseStatus(response);
return response.readEntity(new GenericType<List<Station>>() {
});
} finally {
close(response);
}
};

Expand All @@ -660,11 +629,9 @@ private UUID postNewOrEditStation(@NonNull final Station station,
station.apply(requestParams);
Entity<Form> entity = Entity.form(requestParams);

Response response = null;
try {
response = builder(webTarget
.path(path))
.post(entity);
try (Response response = builder(webTarget
.path(path))
.post(entity)) {

logResponseStatus(response);
UrlResponse urlResponse = response.readEntity(
Expand All @@ -679,8 +646,6 @@ private UUID postNewOrEditStation(@NonNull final Station station,
}

return urlResponse.getUuid();
} finally {
close(response);
}
}

Expand Down Expand Up @@ -716,13 +681,4 @@ private static void checkResponseStatus(final Response response) {
response.getStatusInfo().getReasonPhrase());
}
}

/** Close the response if non-null.
* @param response the response to close.
* */
private static void close(final Response response) {
if (response != null) {
response.close();
}
}
}

0 comments on commit 12ec97c

Please sign in to comment.