Browse code

Fix: prevent creation of responses without status code

Thomas Fricker authored on 12/07/2018 11:40:04
Showing 4 changed files
... ...
@@ -85,8 +85,7 @@ public class AsyncCompletionHandler extends AsyncCompletionHandlerBase {
85 85
 				bufferedStream.end();
86 86
 			} else {
87 87
 				logger.debug("normal response  {}", response.getStatusCode());
88
-				RestResponse resp = new RestResponse();
89
-				resp.statusCode = response.getStatusCode();
88
+				RestResponse resp = new RestResponse(response.getStatusCode());
90 89
 				CaseInsensitiveMultiMap h = new CaseInsensitiveMultiMap();
91 90
 				for (Entry<String, List<String>> he : response.getHeaders().entrySet()) {
92 91
 					h.add(he.getKey(), he.getValue());
... ...
@@ -119,7 +119,6 @@ public class VertxSockJsCallHandler implements IRestCallHandler {
119 119
 		}
120 120
 
121 121
 		RestResponse response = RestResponse.ok(headers, msg.getInteger("statusCode"), body);
122
-		response.statusCode = msg.getInteger("statusCode");
123 122
 		return response;
124 123
 	}
125 124
 
... ...
@@ -23,7 +23,6 @@ import org.vertx.java.core.buffer.Buffer;
23 23
 import org.vertx.java.core.http.CaseInsensitiveMultiMap;
24 24
 import org.vertx.java.core.streams.ReadStream;
25 25
 
26
-import io.netty.buffer.Unpooled;
27 26
 import net.bluemind.core.api.fault.ErrorCode;
28 27
 import net.bluemind.core.api.fault.ServerFault;
29 28
 
... ...
@@ -33,44 +32,16 @@ public class RestResponse {
33 32
 	public ReadStream<?> responseStream = null;
34 33
 
35 34
 	public MultiMap headers = new CaseInsensitiveMultiMap();
36
-	public int statusCode;
35
+	public final int statusCode;
37 36
 
38
-	public static RestResponse ok(byte[] value) {
39
-		RestResponse ret = new RestResponse();
40
-		if (value == null) {
41
-			ret.data = null;
42
-		} else {
43
-			ret.data = new Buffer(Unpooled.wrappedBuffer(value));
44
-		}
45
-
46
-		return ret;
47
-	}
48
-
49
-	public static RestResponse ok(String value) {
50
-		RestResponse ret = new RestResponse();
51
-		if (value == null) {
52
-			ret.data = null;
53
-		} else {
54
-			ret.data = new Buffer(value);
55
-		}
56
-
57
-		return ret;
37
+	public RestResponse(int statusCode) {
38
+		this.statusCode = statusCode;
58 39
 	}
59 40
 
60 41
 	public static RestResponse fault(Throwable e) {
61 42
 		return ResponseBuilder.replyFault(500, "server error", e);
62 43
 	}
63 44
 
64
-	public static RestResponse end() {
65
-		RestResponse ret = new RestResponse();
66
-		return ret;
67
-	}
68
-
69
-	public static RestResponse notExists() {
70
-		// TODO Auto-generated method stub
71
-		return null;
72
-	}
73
-
74 45
 	public static RestResponse invalidSession(String key) {
75 46
 		return ResponseBuilder.replyFault(401, key, new ServerFault(key, ErrorCode.AUTHENTICATION_FAIL));
76 47
 	}
... ...
@@ -80,38 +51,34 @@ public class RestResponse {
80 51
 	}
81 52
 
82 53
 	public static RestResponse fault(int statusCode, String statusMessage, Buffer buffer) {
83
-		RestResponse ret = new RestResponse();
54
+		RestResponse ret = new RestResponse(statusCode);
84 55
 		ret.data = buffer;
85
-		ret.statusCode = statusCode;
86 56
 		return ret;
87 57
 	}
88 58
 
89 59
 	public static RestResponse ok(int statusCode, Buffer buffer) {
90
-		RestResponse ret = new RestResponse();
60
+		RestResponse ret = new RestResponse(statusCode);
91 61
 		ret.data = buffer;
92
-		ret.statusCode = statusCode;
93 62
 		return ret;
94 63
 	}
95 64
 
96 65
 	public static RestResponse ok(String mimeType, int statusCode, Buffer buffer) {
97
-		RestResponse ret = new RestResponse();
66
+		RestResponse ret = new RestResponse(statusCode);
98 67
 		ret.headers.add("Content-Type", mimeType);
99 68
 		ret.data = buffer;
100
-		ret.statusCode = statusCode;
101 69
 		return ret;
102 70
 	}
103 71
 
104 72
 	public static RestResponse ok(MultiMap headers, int statusCode, Buffer buffer) {
105
-		RestResponse ret = new RestResponse();
73
+		RestResponse ret = new RestResponse(statusCode);
106 74
 		ret.headers = new CaseInsensitiveMultiMap();
107 75
 		ret.headers.add(headers);
108 76
 		ret.data = buffer;
109
-		ret.statusCode = statusCode;
110 77
 		return ret;
111 78
 	}
112 79
 
113 80
 	public static RestResponse stream(ReadStream<?> stream) {
114
-		RestResponse ret = new RestResponse();
81
+		RestResponse ret = new RestResponse(200);
115 82
 		ret.responseStream = stream;
116 83
 		return ret;
117 84
 	}
... ...
@@ -67,10 +67,8 @@ public class VertxRestResponse extends JsonObject {
67 67
 	}
68 68
 
69 69
 	public RestResponse asResponse() {
70
-		RestResponse response = new RestResponse();
71
-		response.data = responseData;
70
+		RestResponse response = RestResponse.ok(statusCode, responseData);
72 71
 		response.headers = headers;
73
-		response.statusCode = statusCode;
74 72
 		return response;
75 73
 	}
76 74