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 33
 	public ReadStream<?> responseStream = null;
34 34
 
35 35
 	public MultiMap headers = new CaseInsensitiveMultiMap();
36
-	public int statusCode;
36
+	public final int statusCode;
37 37
 
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;
38
+	public RestResponse(int statusCode) {
39
+		this.statusCode = statusCode;
58 40
 	}
59 41
 
60 42
 	public static RestResponse fault(Throwable e) {
61 43
 		return ResponseBuilder.replyFault(500, "server error", e);
62 44
 	}
63 45
 
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 46
 	public static RestResponse invalidSession(String key) {
75 47
 		return ResponseBuilder.replyFault(401, key, new ServerFault(key, ErrorCode.AUTHENTICATION_FAIL));
76 48
 	}
... ...
@@ -80,38 +51,34 @@ public class RestResponse {
80 80
 	}
81 81
 
82 82
 	public static RestResponse fault(int statusCode, String statusMessage, Buffer buffer) {
83
-		RestResponse ret = new RestResponse();
83
+		RestResponse ret = new RestResponse(statusCode);
84 84
 		ret.data = buffer;
85
-		ret.statusCode = statusCode;
86 85
 		return ret;
87 86
 	}
88 87
 
89 88
 	public static RestResponse ok(int statusCode, Buffer buffer) {
90
-		RestResponse ret = new RestResponse();
89
+		RestResponse ret = new RestResponse(statusCode);
91 90
 		ret.data = buffer;
92
-		ret.statusCode = statusCode;
93 91
 		return ret;
94 92
 	}
95 93
 
96 94
 	public static RestResponse ok(String mimeType, int statusCode, Buffer buffer) {
97
-		RestResponse ret = new RestResponse();
95
+		RestResponse ret = new RestResponse(statusCode);
98 96
 		ret.headers.add("Content-Type", mimeType);
99 97
 		ret.data = buffer;
100
-		ret.statusCode = statusCode;
101 98
 		return ret;
102 99
 	}
103 100
 
104 101
 	public static RestResponse ok(MultiMap headers, int statusCode, Buffer buffer) {
105
-		RestResponse ret = new RestResponse();
102
+		RestResponse ret = new RestResponse(statusCode);
106 103
 		ret.headers = new CaseInsensitiveMultiMap();
107 104
 		ret.headers.add(headers);
108 105
 		ret.data = buffer;
109
-		ret.statusCode = statusCode;
110 106
 		return ret;
111 107
 	}
112 108
 
113 109
 	public static RestResponse stream(ReadStream<?> stream) {
114
-		RestResponse ret = new RestResponse();
110
+		RestResponse ret = new RestResponse(200);
115 111
 		ret.responseStream = stream;
116 112
 		return ret;
117 113
 	}
... ...
@@ -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