|
1 | 1 | package javajs.http; |
2 | 2 |
|
| 3 | +import java.io.ByteArrayInputStream; |
3 | 4 | import java.io.Closeable; |
4 | 5 | import java.io.File; |
5 | 6 | import java.io.IOException; |
@@ -88,7 +89,10 @@ public interface HttpRequest { |
88 | 89 | * @param fileName server-side fileName to associate with this file |
89 | 90 | * @return |
90 | 91 | */ |
91 | | - public HttpRequest addFilePart(String name, String data, String contentType, String fileName); |
| 92 | + public default HttpRequest addFilePart(String name, String data, String contentType, String fileName) { |
| 93 | + var stream = new ByteArrayInputStream(data.getBytes()); |
| 94 | + return addFilePart(name, stream, contentType, fileName); |
| 95 | + } |
92 | 96 |
|
93 | 97 | /** |
94 | 98 | * Add a file-type multipart/form-data derived from a File object, using |
@@ -179,6 +183,64 @@ public interface HttpResponse extends Closeable { |
179 | 183 | */ |
180 | 184 | public int getStatusCode(); |
181 | 185 |
|
| 186 | + /** |
| 187 | + * Get the reason phrase for the response code. |
| 188 | + * Implementations should use the reason returned by the server. |
| 189 | + * Default implementation uses phrases defined by RFC 2616 and RFC 4918 |
| 190 | + * |
| 191 | + * @return status line reason phrase |
| 192 | + */ |
| 193 | + public default String getReasonPhrase() { |
| 194 | + switch (getStatusCode()) { |
| 195 | + case 100: return "Continue"; |
| 196 | + case 101: return "Switching Protocols"; |
| 197 | + case 200: return "OK"; |
| 198 | + case 201: return "Created"; |
| 199 | + case 202: return "Accepted"; |
| 200 | + case 203: return "Non-Authoritative Information"; |
| 201 | + case 204: return "No Content"; |
| 202 | + case 205: return "Reset Content"; |
| 203 | + case 206: return "Partial Content"; |
| 204 | + case 207: return "Multi-status"; |
| 205 | + case 300: return "Multiple Choices"; |
| 206 | + case 301: return "Moved Permanently"; |
| 207 | + case 302: return "Found"; |
| 208 | + case 303: return "See Other"; |
| 209 | + case 304: return "Not Modified"; |
| 210 | + case 305: return "Use Proxy"; |
| 211 | + case 307: return "Temporary Redirect"; |
| 212 | + case 400: return "Bad Request"; |
| 213 | + case 401: return "Unauthorized"; |
| 214 | + case 402: return "Payment Required"; |
| 215 | + case 403: return "Forbidden"; |
| 216 | + case 404: return "Not Found"; |
| 217 | + case 405: return "Method Not Allowed"; |
| 218 | + case 406: return "Not Acceptable"; |
| 219 | + case 407: return "Proxy Authentication Required"; |
| 220 | + case 408: return "Request Timeout"; |
| 221 | + case 409: return "Conflict"; |
| 222 | + case 410: return "Gone"; |
| 223 | + case 411: return "Length Required"; |
| 224 | + case 412: return "Precondition Failed"; |
| 225 | + case 413: return "Request Entity Too Large"; |
| 226 | + case 414: return "Request-URI Too Long"; |
| 227 | + case 415: return "Unsupported Media Type"; |
| 228 | + case 416: return "Requested Range Not Satisfiable"; |
| 229 | + case 417: return "Expectation Failed"; |
| 230 | + case 422: return "Unprocessable Entity"; |
| 231 | + case 423: return "Locked"; |
| 232 | + case 424: return "Failed Dependency"; |
| 233 | + case 500: return "Internal Server Error"; |
| 234 | + case 501: return "Not Implemented"; |
| 235 | + case 502: return "Bad Gateway"; |
| 236 | + case 503: return "Service Unavailable"; |
| 237 | + case 504: return "Gateway Timeout"; |
| 238 | + case 505: return "HTTP Version Not Supported"; |
| 239 | + case 507: return "Insufficient Storage"; |
| 240 | + default: return ""; |
| 241 | + } |
| 242 | + } |
| 243 | + |
182 | 244 | /** |
183 | 245 | * Get the response headers, combining same-name headers with commas, preserving |
184 | 246 | * order, as per RFC 2616 |
|
0 commit comments