Added the responseSent variable to the HttpResponseController to check if the response has already been sent to prevent errors

This commit is contained in:
mathias 2021-10-02 17:11:28 +02:00
parent 0064a65c6d
commit 3aca2239f2
No known key found for this signature in database
GPG Key ID: 8950DF62139C852A

View File

@ -18,6 +18,8 @@ public class HttpResponseController {
private final Response response = new Response(); private final Response response = new Response();
private final HttpExchange exchange; private final HttpExchange exchange;
private boolean responseSent = false;
/** /**
* Constructor of the {@link HttpResponseController} * Constructor of the {@link HttpResponseController}
* *
@ -64,6 +66,10 @@ public class HttpResponseController {
* Sends the response to the AutoResponder * Sends the response to the AutoResponder
*/ */
public void send() { public void send() {
if (isResponseSent()) {
LOG.warn("Reply already sent, ignoring the message.");
return;
}
OutputStream os = exchange.getResponseBody(); OutputStream os = exchange.getResponseBody();
response response
@ -86,10 +92,19 @@ public class HttpResponseController {
exchange.sendResponseHeaders(response.getCode(), bs.length); exchange.sendResponseHeaders(response.getCode(), bs.length);
os.write(bs); os.write(bs);
} }
responseSent = true;
os.close(); os.close();
} catch (IOException e) { } catch (IOException e) {
LOG.error("Could not process response: " + e.getMessage()); LOG.error("Could not process response: " + e.getMessage());
} }
} }
/**
* Checks if the response has already been sent
*
* @return <code>true</code> if the response has already been sent, otherwise <code>false</code>
*/
public boolean isResponseSent() {
return responseSent;
}
} }