Merge branch 'feat/34-make-response-use-requestcontext' into 'main'
Refactor response classes to use RequestContext instead of SessionId + request id Closes #34 See merge request cs108-fs26/Gruppe-13!64
This commit was merged in pull request #220.
This commit is contained in:
+4
-7
@@ -1,23 +1,20 @@
|
||||
package ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response;
|
||||
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.RequestContext;
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.builder.ResponseBody;
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.sessions.SessionId;
|
||||
|
||||
/** Response representing an error outcome for a client's request. */
|
||||
public class ErrorResponse extends Response {
|
||||
/**
|
||||
* Construct an error response with a code and message.
|
||||
*
|
||||
* @param sessionId the target session id
|
||||
* @param requestId the originating request id
|
||||
* @param context the RequestContext of the request
|
||||
* @param errorCode a short error code identifying the failure
|
||||
* @param errorMessage a human readable error message
|
||||
*/
|
||||
public ErrorResponse(
|
||||
SessionId sessionId, int requestId, String errorCode, String errorMessage) {
|
||||
public ErrorResponse(RequestContext context, String errorCode, String errorMessage) {
|
||||
super(
|
||||
sessionId,
|
||||
requestId,
|
||||
context,
|
||||
ResponseBody.builder().param("CODE", errorCode).param("MSG", errorMessage).build());
|
||||
}
|
||||
|
||||
|
||||
+4
-5
@@ -1,7 +1,7 @@
|
||||
package ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response;
|
||||
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.RequestContext;
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.builder.ResponseBody;
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.sessions.SessionId;
|
||||
|
||||
/**
|
||||
* A simple success response with an empty body.
|
||||
@@ -12,10 +12,9 @@ public class OkResponse extends SuccessResponse {
|
||||
/**
|
||||
* Create a minimal successful response (no body content).
|
||||
*
|
||||
* @param sessionId the target session id
|
||||
* @param requestId the originating request id
|
||||
* @param context the RequestContext of the request
|
||||
*/
|
||||
public OkResponse(SessionId sessionId, int requestId) {
|
||||
super(sessionId, requestId, ResponseBody.builder().build());
|
||||
public OkResponse(RequestContext context) {
|
||||
super(context, ResponseBody.builder().build());
|
||||
}
|
||||
}
|
||||
|
||||
+8
-10
@@ -1,24 +1,22 @@
|
||||
package ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response;
|
||||
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.RequestContext;
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.builder.ResponseBody;
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.sessions.SessionId;
|
||||
|
||||
/** Abstract base class for all server responses sent to clients. */
|
||||
public abstract class Response {
|
||||
private final SessionId sessionId;
|
||||
private final int requestId;
|
||||
private final RequestContext context;
|
||||
private final ResponseBody body;
|
||||
|
||||
/**
|
||||
* Create a new {@code Response}.
|
||||
*
|
||||
* @param sessionId the id of the session this response targets
|
||||
* @param requestId the request identifier this response corresponds to
|
||||
* @param context the RequestContext of the request
|
||||
* @param body the structured response body
|
||||
*/
|
||||
protected Response(SessionId sessionId, int requestId, ResponseBody body) {
|
||||
this.sessionId = sessionId;
|
||||
this.requestId = requestId;
|
||||
protected Response(RequestContext context, ResponseBody body) {
|
||||
this.context = context;
|
||||
this.body = body;
|
||||
}
|
||||
|
||||
@@ -30,12 +28,12 @@ public abstract class Response {
|
||||
public abstract String prefix();
|
||||
|
||||
/**
|
||||
* Returns the session id that should receive this response.
|
||||
* Returns the session id of the session that should receive this response.
|
||||
*
|
||||
* @return the target {@link SessionId}
|
||||
*/
|
||||
public SessionId getSessionId() {
|
||||
return sessionId;
|
||||
return context.sessionId();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -44,7 +42,7 @@ public abstract class Response {
|
||||
* @return the numeric request id
|
||||
*/
|
||||
public int getRequestId() {
|
||||
return requestId;
|
||||
return context.requestId();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+4
-5
@@ -1,7 +1,7 @@
|
||||
package ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response;
|
||||
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.RequestContext;
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.builder.ResponseBody;
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.sessions.SessionId;
|
||||
|
||||
/**
|
||||
* Abstract {@link Response} specialization indicating a successful outcome.
|
||||
@@ -13,12 +13,11 @@ public abstract class SuccessResponse extends Response {
|
||||
/**
|
||||
* Create a successful response with the provided body.
|
||||
*
|
||||
* @param sessionId the session id this response targets
|
||||
* @param requestId the originating request id
|
||||
* @param context the RequestContext of the request
|
||||
* @param body the response body
|
||||
*/
|
||||
protected SuccessResponse(SessionId sessionId, int requestId, ResponseBody body) {
|
||||
super(sessionId, requestId, body);
|
||||
protected SuccessResponse(RequestContext context, ResponseBody body) {
|
||||
super(context, body);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+6
-9
@@ -46,11 +46,13 @@ public class SessionReader implements Runnable {
|
||||
while (!Thread.currentThread().isInterrupted()) {
|
||||
RawPacket rawPacket = null;
|
||||
RawRequest rawRequest = null;
|
||||
RequestContext requestContext = null;
|
||||
try {
|
||||
// Step 1: Read from transport
|
||||
rawPacket = transport.read();
|
||||
session.updateLastInboundActivity();
|
||||
logger.debug("Recieved: {}", rawPacket);
|
||||
requestContext = new RequestContext(session.getId(), rawPacket.requestId());
|
||||
|
||||
// Step 2: Syntax validation and conversion into transport object
|
||||
rawRequest = ProtocolParser.parse(rawPacket.payload());
|
||||
@@ -58,9 +60,7 @@ public class SessionReader implements Runnable {
|
||||
|
||||
PrimitiveRequest primitiveRequest =
|
||||
new PrimitiveRequest(
|
||||
new RequestContext(session.getId(), rawPacket.requestId()),
|
||||
rawRequest.command(),
|
||||
rawRequest.parameters());
|
||||
requestContext, rawRequest.command(), rawRequest.parameters());
|
||||
logger.debug("Converted to {}", primitiveRequest);
|
||||
|
||||
// Step 3: Parse into Request and execute Request
|
||||
@@ -76,8 +76,7 @@ public class SessionReader implements Runnable {
|
||||
|
||||
sendErrorResponse(
|
||||
new ErrorResponse(
|
||||
session.getId(),
|
||||
rawPacket.requestId(),
|
||||
requestContext,
|
||||
"PARSING_ERROR",
|
||||
"Error occured during parsing. Likely due to malformed payload."));
|
||||
|
||||
@@ -85,8 +84,7 @@ public class SessionReader implements Runnable {
|
||||
logger.error("Recieved unknown command '{}' from client", rawRequest.command(), e);
|
||||
sendErrorResponse(
|
||||
new ErrorResponse(
|
||||
session.getId(),
|
||||
rawPacket.requestId(),
|
||||
requestContext,
|
||||
"UNKNOWN_COMMAND",
|
||||
"This command is unknown to the server."));
|
||||
|
||||
@@ -97,8 +95,7 @@ public class SessionReader implements Runnable {
|
||||
logger.error("Unexpected RuntimeException occured", e);
|
||||
sendErrorResponse(
|
||||
new ErrorResponse(
|
||||
session.getId(),
|
||||
rawPacket.requestId(),
|
||||
requestContext,
|
||||
"INTERNAL_ERROR",
|
||||
"Unexpected internal server error occured."));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user