Fix OkHttp connection leaks on connection error
When request fails, entire response needs to be wrapped in try with resources in order to close the connection properly and not only response body. Signed-off-by: Tomas Slusny <slusnucky@gmail.com>
This commit is contained in:
@@ -33,7 +33,6 @@ import net.runelite.http.api.RuneliteAPI;
|
|||||||
import okhttp3.HttpUrl;
|
import okhttp3.HttpUrl;
|
||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
import okhttp3.Response;
|
import okhttp3.Response;
|
||||||
import okhttp3.ResponseBody;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@@ -65,11 +64,9 @@ public class AccountClient
|
|||||||
.url(url)
|
.url(url)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Response response = RuneliteAPI.CLIENT.newCall(request).execute();
|
try (Response response = RuneliteAPI.CLIENT.newCall(request).execute())
|
||||||
|
|
||||||
try (ResponseBody body = response.body())
|
|
||||||
{
|
{
|
||||||
InputStream in = body.byteStream();
|
InputStream in = response.body().byteStream();
|
||||||
return RuneliteAPI.GSON.fromJson(new InputStreamReader(in), OAuthResponse.class);
|
return RuneliteAPI.GSON.fromJson(new InputStreamReader(in), OAuthResponse.class);
|
||||||
}
|
}
|
||||||
catch (JsonParseException ex)
|
catch (JsonParseException ex)
|
||||||
@@ -92,9 +89,7 @@ public class AccountClient
|
|||||||
.url(url)
|
.url(url)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Response response = RuneliteAPI.CLIENT.newCall(request).execute();
|
try (Response response = RuneliteAPI.CLIENT.newCall(request).execute())
|
||||||
|
|
||||||
try (ResponseBody body = response.body())
|
|
||||||
{
|
{
|
||||||
logger.debug("Sent logout request");
|
logger.debug("Sent logout request");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,7 +35,6 @@ import okhttp3.MediaType;
|
|||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
import okhttp3.RequestBody;
|
import okhttp3.RequestBody;
|
||||||
import okhttp3.Response;
|
import okhttp3.Response;
|
||||||
import okhttp3.ResponseBody;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@@ -65,11 +64,9 @@ public class ConfigClient
|
|||||||
.url(url)
|
.url(url)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Response response = RuneliteAPI.CLIENT.newCall(request).execute();
|
try (Response response = RuneliteAPI.CLIENT.newCall(request).execute())
|
||||||
|
|
||||||
try (ResponseBody body = response.body())
|
|
||||||
{
|
{
|
||||||
InputStream in = body.byteStream();
|
InputStream in = response.body().byteStream();
|
||||||
return RuneliteAPI.GSON.fromJson(new InputStreamReader(in), Configuration.class);
|
return RuneliteAPI.GSON.fromJson(new InputStreamReader(in), Configuration.class);
|
||||||
}
|
}
|
||||||
catch (JsonParseException ex)
|
catch (JsonParseException ex)
|
||||||
|
|||||||
@@ -32,7 +32,6 @@ import net.runelite.http.api.RuneliteAPI;
|
|||||||
import okhttp3.HttpUrl;
|
import okhttp3.HttpUrl;
|
||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
import okhttp3.Response;
|
import okhttp3.Response;
|
||||||
import okhttp3.ResponseBody;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@@ -55,11 +54,9 @@ public class HiscoreClient
|
|||||||
.url(url)
|
.url(url)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Response response = RuneliteAPI.CLIENT.newCall(request).execute();
|
try (Response response = RuneliteAPI.CLIENT.newCall(request).execute())
|
||||||
|
|
||||||
try (ResponseBody body = response.body())
|
|
||||||
{
|
{
|
||||||
InputStream in = body.byteStream();
|
InputStream in = response.body().byteStream();
|
||||||
return RuneliteAPI.GSON.fromJson(new InputStreamReader(in), HiscoreResult.class);
|
return RuneliteAPI.GSON.fromJson(new InputStreamReader(in), HiscoreResult.class);
|
||||||
}
|
}
|
||||||
catch (JsonParseException ex)
|
catch (JsonParseException ex)
|
||||||
@@ -76,24 +73,22 @@ public class HiscoreClient
|
|||||||
public SingleHiscoreSkillResult lookup(String username, HiscoreSkill skill, HiscoreEndpoint endpoint) throws IOException
|
public SingleHiscoreSkillResult lookup(String username, HiscoreSkill skill, HiscoreEndpoint endpoint) throws IOException
|
||||||
{
|
{
|
||||||
HttpUrl.Builder builder = RuneliteAPI.getApiBase().newBuilder()
|
HttpUrl.Builder builder = RuneliteAPI.getApiBase().newBuilder()
|
||||||
.addPathSegment("hiscore")
|
.addPathSegment("hiscore")
|
||||||
.addPathSegment(endpoint.name())
|
.addPathSegment(endpoint.name())
|
||||||
.addPathSegment(skill.toString().toLowerCase())
|
.addPathSegment(skill.toString().toLowerCase())
|
||||||
.addQueryParameter("username", username);
|
.addQueryParameter("username", username);
|
||||||
|
|
||||||
HttpUrl url = builder.build();
|
HttpUrl url = builder.build();
|
||||||
|
|
||||||
logger.debug("Built URI: {}", url);
|
logger.debug("Built URI: {}", url);
|
||||||
|
|
||||||
Request request = new Request.Builder()
|
Request request = new Request.Builder()
|
||||||
.url(url)
|
.url(url)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Response response = RuneliteAPI.CLIENT.newCall(request).execute();
|
try (Response response = RuneliteAPI.CLIENT.newCall(request).execute())
|
||||||
|
|
||||||
try (ResponseBody body = response.body())
|
|
||||||
{
|
{
|
||||||
InputStream in = body.byteStream();
|
InputStream in = response.body().byteStream();
|
||||||
return RuneliteAPI.GSON.fromJson(new InputStreamReader(in), SingleHiscoreSkillResult.class);
|
return RuneliteAPI.GSON.fromJson(new InputStreamReader(in), SingleHiscoreSkillResult.class);
|
||||||
}
|
}
|
||||||
catch (JsonParseException ex)
|
catch (JsonParseException ex)
|
||||||
|
|||||||
@@ -32,7 +32,6 @@ import net.runelite.http.api.RuneliteAPI;
|
|||||||
import okhttp3.HttpUrl;
|
import okhttp3.HttpUrl;
|
||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
import okhttp3.Response;
|
import okhttp3.Response;
|
||||||
import okhttp3.ResponseBody;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@@ -54,17 +53,15 @@ public class ItemClient
|
|||||||
.url(url)
|
.url(url)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Response response = RuneliteAPI.CLIENT.newCall(request).execute();
|
try (Response response = RuneliteAPI.CLIENT.newCall(request).execute())
|
||||||
|
|
||||||
if (!response.isSuccessful())
|
|
||||||
{
|
{
|
||||||
logger.debug("Error looking up item {}: {}", itemId, response.message());
|
if (!response.isSuccessful())
|
||||||
return null;
|
{
|
||||||
}
|
logger.debug("Error looking up item {}: {}", itemId, response.message());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
try (ResponseBody body = response.body())
|
InputStream in = response.body().byteStream();
|
||||||
{
|
|
||||||
InputStream in = body.byteStream();
|
|
||||||
return RuneliteAPI.GSON.fromJson(new InputStreamReader(in), ItemPrice.class);
|
return RuneliteAPI.GSON.fromJson(new InputStreamReader(in), ItemPrice.class);
|
||||||
}
|
}
|
||||||
catch (JsonParseException ex)
|
catch (JsonParseException ex)
|
||||||
@@ -76,28 +73,26 @@ public class ItemClient
|
|||||||
public SearchResult search(String itemName) throws IOException
|
public SearchResult search(String itemName) throws IOException
|
||||||
{
|
{
|
||||||
HttpUrl url = RuneliteAPI.getApiBase().newBuilder()
|
HttpUrl url = RuneliteAPI.getApiBase().newBuilder()
|
||||||
.addPathSegment("item")
|
.addPathSegment("item")
|
||||||
.addPathSegment("search")
|
.addPathSegment("search")
|
||||||
.addQueryParameter("query", itemName)
|
.addQueryParameter("query", itemName)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
logger.debug("Built URI: {}", url);
|
logger.debug("Built URI: {}", url);
|
||||||
|
|
||||||
Request request = new Request.Builder()
|
Request request = new Request.Builder()
|
||||||
.url(url)
|
.url(url)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Response response = RuneliteAPI.CLIENT.newCall(request).execute();
|
try (Response response = RuneliteAPI.CLIENT.newCall(request).execute())
|
||||||
|
|
||||||
if (!response.isSuccessful())
|
|
||||||
{
|
{
|
||||||
logger.debug("Error looking up item {}: {}", itemName, response.message());
|
if (!response.isSuccessful())
|
||||||
return null;
|
{
|
||||||
}
|
logger.debug("Error looking up item {}: {}", itemName, response.message());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
try (ResponseBody body = response.body())
|
InputStream in = response.body().byteStream();
|
||||||
{
|
|
||||||
InputStream in = body.byteStream();
|
|
||||||
return RuneliteAPI.GSON.fromJson(new InputStreamReader(in), SearchResult.class);
|
return RuneliteAPI.GSON.fromJson(new InputStreamReader(in), SearchResult.class);
|
||||||
}
|
}
|
||||||
catch (JsonParseException ex)
|
catch (JsonParseException ex)
|
||||||
|
|||||||
@@ -36,7 +36,6 @@ import okhttp3.MediaType;
|
|||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
import okhttp3.RequestBody;
|
import okhttp3.RequestBody;
|
||||||
import okhttp3.Response;
|
import okhttp3.Response;
|
||||||
import okhttp3.ResponseBody;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@@ -83,11 +82,9 @@ public class XteaClient
|
|||||||
.url(url)
|
.url(url)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Response response = RuneliteAPI.CLIENT.newCall(request).execute();
|
try (Response response = RuneliteAPI.CLIENT.newCall(request).execute())
|
||||||
|
|
||||||
try (ResponseBody body = response.body())
|
|
||||||
{
|
{
|
||||||
InputStream in = body.byteStream();
|
InputStream in = response.body().byteStream();
|
||||||
// CHECKSTYLE:OFF
|
// CHECKSTYLE:OFF
|
||||||
return RuneliteAPI.GSON.fromJson(new InputStreamReader(in), new TypeToken<List<XteaKey>>() { }.getType());
|
return RuneliteAPI.GSON.fromJson(new InputStreamReader(in), new TypeToken<List<XteaKey>>() { }.getType());
|
||||||
// CHECKSTYLE:ON
|
// CHECKSTYLE:ON
|
||||||
@@ -109,11 +106,9 @@ public class XteaClient
|
|||||||
.url(url)
|
.url(url)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Response response = RuneliteAPI.CLIENT.newCall(request).execute();
|
try (Response response = RuneliteAPI.CLIENT.newCall(request).execute())
|
||||||
|
|
||||||
try (ResponseBody body = response.body())
|
|
||||||
{
|
{
|
||||||
InputStream in = body.byteStream();
|
InputStream in = response.body().byteStream();
|
||||||
return RuneliteAPI.GSON.fromJson(new InputStreamReader(in), XteaKey.class);
|
return RuneliteAPI.GSON.fromJson(new InputStreamReader(in), XteaKey.class);
|
||||||
}
|
}
|
||||||
catch (JsonParseException ex)
|
catch (JsonParseException ex)
|
||||||
|
|||||||
@@ -26,14 +26,17 @@ package net.runelite.http.service.hiscore;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import net.runelite.http.api.RuneliteAPI;
|
import net.runelite.http.api.RuneliteAPI;
|
||||||
import net.runelite.http.api.hiscore.*;
|
import net.runelite.http.api.hiscore.HiscoreEndpoint;
|
||||||
|
import net.runelite.http.api.hiscore.HiscoreResult;
|
||||||
|
import net.runelite.http.api.hiscore.HiscoreSkill;
|
||||||
|
import net.runelite.http.api.hiscore.SingleHiscoreSkillResult;
|
||||||
|
import net.runelite.http.api.hiscore.Skill;
|
||||||
import net.runelite.http.service.util.HiscoreEndpointEditor;
|
import net.runelite.http.service.util.HiscoreEndpointEditor;
|
||||||
import net.runelite.http.service.util.exception.InternalServerErrorException;
|
import net.runelite.http.service.util.exception.InternalServerErrorException;
|
||||||
import net.runelite.http.service.util.exception.NotFoundException;
|
import net.runelite.http.service.util.exception.NotFoundException;
|
||||||
import okhttp3.HttpUrl;
|
import okhttp3.HttpUrl;
|
||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
import okhttp3.Response;
|
import okhttp3.Response;
|
||||||
import okhttp3.ResponseBody;
|
|
||||||
import org.apache.commons.csv.CSVFormat;
|
import org.apache.commons.csv.CSVFormat;
|
||||||
import org.apache.commons.csv.CSVParser;
|
import org.apache.commons.csv.CSVParser;
|
||||||
import org.apache.commons.csv.CSVRecord;
|
import org.apache.commons.csv.CSVRecord;
|
||||||
@@ -41,7 +44,11 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.web.bind.WebDataBinder;
|
import org.springframework.web.bind.WebDataBinder;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.InitBinder;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/hiscore")
|
@RequestMapping("/hiscore")
|
||||||
@@ -66,24 +73,22 @@ public class HiscoreService
|
|||||||
.url(url)
|
.url(url)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Response okresponse = RuneliteAPI.CLIENT.newCall(okrequest).execute();
|
|
||||||
|
|
||||||
if (!okresponse.isSuccessful())
|
|
||||||
{
|
|
||||||
switch (HttpStatus.valueOf(okresponse.code()))
|
|
||||||
{
|
|
||||||
case NOT_FOUND:
|
|
||||||
throw new NotFoundException();
|
|
||||||
default:
|
|
||||||
throw new InternalServerErrorException("Error retrieving data from Jagex Hiscores: " + okresponse.message());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
String responseStr;
|
String responseStr;
|
||||||
|
|
||||||
try (ResponseBody body = okresponse.body())
|
try (Response okresponse = RuneliteAPI.CLIENT.newCall(okrequest).execute())
|
||||||
{
|
{
|
||||||
responseStr = body.string();
|
if (!okresponse.isSuccessful())
|
||||||
|
{
|
||||||
|
switch (HttpStatus.valueOf(okresponse.code()))
|
||||||
|
{
|
||||||
|
case NOT_FOUND:
|
||||||
|
throw new NotFoundException();
|
||||||
|
default:
|
||||||
|
throw new InternalServerErrorException("Error retrieving data from Jagex Hiscores: " + okresponse.message());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
responseStr = okresponse.body().string();
|
||||||
}
|
}
|
||||||
|
|
||||||
CSVParser parser = CSVParser.parse(responseStr, CSVFormat.DEFAULT);
|
CSVParser parser = CSVParser.parse(responseStr, CSVFormat.DEFAULT);
|
||||||
|
|||||||
@@ -45,7 +45,6 @@ import net.runelite.http.api.item.SearchResult;
|
|||||||
import okhttp3.HttpUrl;
|
import okhttp3.HttpUrl;
|
||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
import okhttp3.Response;
|
import okhttp3.Response;
|
||||||
import okhttp3.ResponseBody;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -487,16 +486,14 @@ public class ItemService
|
|||||||
|
|
||||||
private <T> T fetchJson(Request request, Class<T> clazz) throws IOException
|
private <T> T fetchJson(Request request, Class<T> clazz) throws IOException
|
||||||
{
|
{
|
||||||
Response response = RuneliteAPI.CLIENT.newCall(request).execute();
|
try (Response response = RuneliteAPI.CLIENT.newCall(request).execute())
|
||||||
|
|
||||||
if (!response.isSuccessful())
|
|
||||||
{
|
{
|
||||||
throw new IOException("Unsuccessful http response: " + response.message());
|
if (!response.isSuccessful())
|
||||||
}
|
{
|
||||||
|
throw new IOException("Unsuccessful http response: " + response.message());
|
||||||
|
}
|
||||||
|
|
||||||
try (ResponseBody body = response.body())
|
InputStream in = response.body().byteStream();
|
||||||
{
|
|
||||||
InputStream in = body.byteStream();
|
|
||||||
return RuneliteAPI.GSON.fromJson(new InputStreamReader(in), clazz);
|
return RuneliteAPI.GSON.fromJson(new InputStreamReader(in), clazz);
|
||||||
}
|
}
|
||||||
catch (JsonParseException ex)
|
catch (JsonParseException ex)
|
||||||
@@ -513,16 +510,14 @@ public class ItemService
|
|||||||
.url(httpUrl)
|
.url(httpUrl)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Response response = RuneliteAPI.CLIENT.newCall(request).execute();
|
try (Response response = RuneliteAPI.CLIENT.newCall(request).execute())
|
||||||
|
|
||||||
if (!response.isSuccessful())
|
|
||||||
{
|
{
|
||||||
throw new IOException("Unsuccessful http response: " + response.message());
|
if (!response.isSuccessful())
|
||||||
}
|
{
|
||||||
|
throw new IOException("Unsuccessful http response: " + response.message());
|
||||||
|
}
|
||||||
|
|
||||||
try (ResponseBody body = response.body())
|
return response.body().bytes();
|
||||||
{
|
|
||||||
return body.bytes();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,7 +34,6 @@ import net.runelite.http.api.worlds.WorldResult;
|
|||||||
import okhttp3.HttpUrl;
|
import okhttp3.HttpUrl;
|
||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
import okhttp3.Response;
|
import okhttp3.Response;
|
||||||
import okhttp3.ResponseBody;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
@@ -53,12 +52,11 @@ public class WorldsService
|
|||||||
.url(url)
|
.url(url)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Response okresponse = RuneliteAPI.CLIENT.newCall(okrequest).execute();
|
|
||||||
byte[] b;
|
byte[] b;
|
||||||
|
|
||||||
try (ResponseBody body = okresponse.body())
|
try (Response okresponse = RuneliteAPI.CLIENT.newCall(okrequest).execute())
|
||||||
{
|
{
|
||||||
b = body.bytes();
|
b = okresponse.body().bytes();
|
||||||
}
|
}
|
||||||
|
|
||||||
List<World> worlds = new ArrayList<>();
|
List<World> worlds = new ArrayList<>();
|
||||||
|
|||||||
@@ -31,13 +31,11 @@ import java.util.HashMap;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import net.runelite.http.api.RuneliteAPI;
|
import net.runelite.http.api.RuneliteAPI;
|
||||||
import okhttp3.HttpUrl;
|
import okhttp3.HttpUrl;
|
||||||
import okhttp3.OkHttpClient;
|
|
||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
import okhttp3.Response;
|
import okhttp3.Response;
|
||||||
|
|
||||||
public class ConfigLoader
|
public class ConfigLoader
|
||||||
{
|
{
|
||||||
private static final OkHttpClient CLIENT = RuneliteAPI.CLIENT;
|
|
||||||
private static final HttpUrl CONFIG_URL = HttpUrl.parse("http://oldschool.runescape.com/jav_config.ws"); // https redirects us to rs3
|
private static final HttpUrl CONFIG_URL = HttpUrl.parse("http://oldschool.runescape.com/jav_config.ws"); // https redirects us to rs3
|
||||||
|
|
||||||
public static final String CODEBASE = "codebase";
|
public static final String CODEBASE = "codebase";
|
||||||
@@ -53,7 +51,7 @@ public class ConfigLoader
|
|||||||
.url(CONFIG_URL)
|
.url(CONFIG_URL)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
try (Response response = CLIENT.newCall(request).execute();
|
try (Response response = RuneliteAPI.CLIENT.newCall(request).execute();
|
||||||
BufferedReader in = new BufferedReader(new InputStreamReader(response.body().byteStream())))
|
BufferedReader in = new BufferedReader(new InputStreamReader(response.body().byteStream())))
|
||||||
{
|
{
|
||||||
String str;
|
String str;
|
||||||
|
|||||||
Reference in New Issue
Block a user