Skip to content

Commit 0550908

Browse files
committed
automatically close the Response stream, even when the server fails to reply correctly
1 parent f55dc0e commit 0550908

1 file changed

Lines changed: 44 additions & 40 deletions

File tree

src/main/java/com/pokegoapi/main/RequestHandler.java

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public void request(ServerRequest requestIn) {
8181
*
8282
* @param serverRequests list of ServerRequests to be sent
8383
* @throws RemoteServerException the remote server exception
84-
* @throws LoginFailedException the login failed exception
84+
* @throws LoginFailedException the login failed exception
8585
*/
8686
public void sendServerRequests(ServerRequest... serverRequests) throws RemoteServerException, LoginFailedException {
8787
if (serverRequests.length == 0) {
@@ -107,52 +107,56 @@ public void sendServerRequests(ServerRequest... serverRequests) throws RemoteSer
107107
.url(apiEndpoint)
108108
.post(body)
109109
.build();
110-
Response response;
111-
try {
112-
response = client.newCall(httpRequest).execute();
113-
} catch (IOException e) {
114-
throw new RemoteServerException(e);
115-
}
116110

117-
if (response.code() != 200) {
118-
throw new RemoteServerException("Got a unexcepted http code : " + response.code());
119-
}
111+
try (Response response = client.newCall(httpRequest).execute()) {
112+
if (response.code() != 200) {
113+
throw new RemoteServerException("Got a unexpected http code : " + response.code());
114+
}
120115

121-
ResponseEnvelopeOuterClass.ResponseEnvelope responseEnvelop;
122-
try (InputStream content = response.body().byteStream()) {
123-
responseEnvelop = ResponseEnvelopeOuterClass.ResponseEnvelope.parseFrom(content);
124-
} catch (IOException e) {
125-
// retrieved garbage from the server
126-
throw new RemoteServerException("Received malformed response : " + e);
127-
}
116+
ResponseEnvelopeOuterClass.ResponseEnvelope responseEnvelop;
117+
try (InputStream content = response.body().byteStream()) {
118+
responseEnvelop = ResponseEnvelopeOuterClass.ResponseEnvelope.parseFrom(content);
119+
} catch (IOException e) {
120+
// retrieved garbage from the server
121+
throw new RemoteServerException("Received malformed response : " + e);
122+
}
128123

129-
if (responseEnvelop.getApiUrl() != null && responseEnvelop.getApiUrl().length() > 0) {
130-
apiEndpoint = "https://" + responseEnvelop.getApiUrl() + "/rpc";
131-
}
124+
if (responseEnvelop.getApiUrl() != null && responseEnvelop.getApiUrl().length() > 0) {
125+
apiEndpoint = "https://" + responseEnvelop.getApiUrl() + "/rpc";
126+
}
132127

133-
if (responseEnvelop.hasAuthTicket()) {
134-
lastAuth = responseEnvelop.getAuthTicket();
135-
}
128+
if (responseEnvelop.hasAuthTicket()) {
129+
lastAuth = responseEnvelop.getAuthTicket();
130+
}
136131

137-
if (responseEnvelop.getStatusCode() == 102) {
138-
throw new LoginFailedException();
139-
} else if (responseEnvelop.getStatusCode() == 53) {
140-
// 53 means that the api_endpoint was not correctly set, should be at this point, though, so redo the request
141-
sendServerRequests(serverRequests);
142-
return;
143-
}
132+
if (responseEnvelop.getStatusCode() == 102) {
133+
throw new LoginFailedException();
134+
} else if (responseEnvelop.getStatusCode() == 53) {
135+
// 53 means that the api_endpoint was not correctly set, should be at this point, though, so redo the request
136+
sendServerRequests(serverRequests);
137+
return;
138+
}
144139

145-
/* map each reply to the numeric response,
146-
ie first response = first request and send back to the requests to handle. */
147-
int count = 0;
148-
for (ByteString payload : responseEnvelop.getReturnsList()) {
149-
ServerRequest serverReq = serverRequests[count];
150-
/* TODO: Probably all other payloads are garbage as well in this case,
151-
so might as well throw an exception and leave this loop */
152-
if (payload != null) {
153-
serverReq.handleData(payload);
140+
/**
141+
* map each reply to the numeric response,
142+
* ie first response = first request and send back to the requests to handle.
143+
* */
144+
int count = 0;
145+
for (ByteString payload : responseEnvelop.getReturnsList()) {
146+
ServerRequest serverReq = serverRequests[count];
147+
/**
148+
* TODO: Probably all other payloads are garbage as well in this case,
149+
* so might as well throw an exception and leave this loop */
150+
if (payload != null) {
151+
serverReq.handleData(payload);
152+
}
153+
count++;
154154
}
155-
count++;
155+
} catch (IOException e) {
156+
throw new RemoteServerException(e);
157+
} catch (RemoteServerException e) {
158+
// catch it, so the auto-close of resources triggers, but don't wrap it in yet another RemoteServer Exception
159+
throw e;
156160
}
157161
}
158162

0 commit comments

Comments
 (0)