Skip to content

Commit e13faa8

Browse files
committed
use try with resources
1 parent 9b088db commit e13faa8

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

plugins/src/main/java/opengrok/auth/plugin/util/RestfulClient.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2019, 2021, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2020, Chris Fraire <[email protected]>.
2323
*/
2424
package opengrok.auth.plugin.util;
@@ -44,27 +44,28 @@ private RestfulClient() {
4444
}
4545

4646
/**
47-
* Perform HTTP PUT request.
47+
* Perform HTTP POST request.
4848
* @param uri URI
4949
* @param input JSON string contents
5050
* @return HTTP status or -1
5151
*/
5252
public static int postIt(String uri, String input) {
5353
try {
54-
Client client = ClientBuilder.newClient();
54+
try (Client client = ClientBuilder.newClient()) {
55+
LOGGER.log(Level.FINEST, "sending REST POST request to {0}: {1}",
56+
new Object[]{uri, input});
57+
try (Response response = client.target(uri)
58+
.request(MediaType.APPLICATION_JSON)
59+
.post(Entity.entity(input, MediaType.APPLICATION_JSON))) {
5560

56-
LOGGER.log(Level.FINEST, "sending REST POST request to {0}: {1}",
57-
new Object[]{uri, input});
58-
Response response = client.target(uri)
59-
.request(MediaType.APPLICATION_JSON)
60-
.post(Entity.entity(input, MediaType.APPLICATION_JSON));
61+
int status = response.getStatus();
62+
if (status != HttpServletResponse.SC_CREATED) {
63+
LOGGER.log(Level.WARNING, "REST request failed: HTTP error code : {0}", status);
64+
}
6165

62-
int status = response.getStatus();
63-
if (status != HttpServletResponse.SC_CREATED) {
64-
LOGGER.log(Level.WARNING, "REST request failed: HTTP error code : {0}", status);
66+
return status;
67+
}
6568
}
66-
67-
return status;
6869
} catch (Exception e) {
6970
LOGGER.log(Level.WARNING, "REST request failed", e);
7071
return -1;

0 commit comments

Comments
 (0)