|
| 1 | +package ktt.lib.httpserver; |
| 2 | + |
| 3 | +import com.sun.net.httpserver.Headers; |
| 4 | +import com.sun.net.httpserver.HttpExchange; |
| 5 | +import ktt.lib.httpserver.http.HTTPCode; |
| 6 | +import ktt.lib.httpserver.http.RequestMethod; |
| 7 | + |
| 8 | +import java.io.IOException; |
| 9 | +import java.net.InetSocketAddress; |
| 10 | +import java.net.URI; |
| 11 | +import java.util.HashMap; |
| 12 | + |
| 13 | +/** |
| 14 | + * <i>This class is a simplified implementation of {@link com.sun.net.httpserver.HttpExchange}.</i> |
| 15 | + * <br> |
| 16 | + * This class provides methods to examine requests from the client, and send a response back. Each request can only send one response. Responses are gziped by default. |
| 17 | + * <br> <br> |
| 18 | + * Get/post type requests for <code>application/x-www-form-urlencoded</code> (default) will be a simple key/value map. <br> |
| 19 | + * Get/post type requests for <code>multipart/form-data</code> will use an expanded map. <br> |
| 20 | + * Example: |
| 21 | + * <pre> |
| 22 | + * { |
| 23 | + * name : { |
| 24 | + * headers: { |
| 25 | + * Content-Disposition : { |
| 26 | + * header-name: "Content-Disposition", |
| 27 | + * header-value: "form-data", |
| 28 | + * parameters: [ |
| 29 | + * filename: "file.txt", |
| 30 | + * name: "file" |
| 31 | + * ] |
| 32 | + * }, |
| 33 | + * Content-Type: { |
| 34 | + * header-name: "Content-Type", |
| 35 | + * header-value: "text/plain", |
| 36 | + * parameters: [] |
| 37 | + * } |
| 38 | + * }, |
| 39 | + * value: "file content" |
| 40 | + * } |
| 41 | + * } |
| 42 | + * </pre> |
| 43 | + * If a file is submitted the value will be in bytes unless it is plain text. |
| 44 | + * @see HttpExchange |
| 45 | + * @since 01.00.00 |
| 46 | + * @version 01.01.01 |
| 47 | + * @author Ktt Development |
| 48 | + */ |
| 49 | +@SuppressWarnings("unused") |
| 50 | +public abstract class ExchangePacket { |
| 51 | + |
| 52 | + /** |
| 53 | + * Creates a Exchange Packet using this provider. Applications do not normally use this method. |
| 54 | + * @param exchange the native Http Exchange |
| 55 | + * @param server the server associated with the request |
| 56 | + * @param context the context associated with the request |
| 57 | + * @return an Exchange Packet instance |
| 58 | + * @see HttpExchange |
| 59 | + * @see HttpServer |
| 60 | + * @since 01.00.00 |
| 61 | + */ |
| 62 | + static ExchangePacket createExchangePacket(HttpExchange exchange, HttpServer server, String context){ |
| 63 | + return ExchangePacketProvider.createExchangePacket(exchange,server,context); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Returns the native Http Exchange. |
| 68 | + * @return the native Http Exchange |
| 69 | + * @see HttpExchange |
| 70 | + * @since 01.00.00 |
| 71 | + */ |
| 72 | + public abstract HttpExchange getRawHttpExchange(); |
| 73 | + |
| 74 | + /** |
| 75 | + * Returns the server in which the request came from. |
| 76 | + * @return the server where the request came from |
| 77 | + * @see HttpServer |
| 78 | + * @since 01.00.00 |
| 79 | + */ |
| 80 | + public abstract HttpServer getServer(); |
| 81 | + |
| 82 | + /** |
| 83 | + * Returns the context that the handler was created under. |
| 84 | + * @return the handler's context |
| 85 | + * @see #getRequestContext() () |
| 86 | + * @see #getRelativeContext() |
| 87 | + * @see #getScheme() |
| 88 | + * @since 01.00.00 |
| 89 | + */ |
| 90 | + public abstract String getHandlerContext(); |
| 91 | + |
| 92 | + /** |
| 93 | + * Returns the context that the client requested. |
| 94 | + * @return the client's context |
| 95 | + * @see #getHandlerContext() |
| 96 | + * @see #getRelativeContext() |
| 97 | + * @see #getScheme() |
| 98 | + * @since 01.00.00 |
| 99 | + */ |
| 100 | + public abstract String getRequestContext(); |
| 101 | + |
| 102 | + /** |
| 103 | + * Returns the string between the handler context and the request context. |
| 104 | + * @return the relative context |
| 105 | + * @see #getHandlerContext() |
| 106 | + * @see #getRequestContext() |
| 107 | + * @see #getScheme() |
| 108 | + * @since 01.00.00 |
| 109 | + */ |
| 110 | + public abstract String getRelativeContext(); |
| 111 | + |
| 112 | + /** |
| 113 | + * Returns the scheme of the context (the # in the context). |
| 114 | + * @return the context scheme |
| 115 | + * @see #getHandlerContext() |
| 116 | + * @see #getRequestContext() |
| 117 | + * @see #getRelativeContext() |
| 118 | + * @since 01.00.00 |
| 119 | + */ |
| 120 | + public abstract String getScheme(); |
| 121 | + |
| 122 | + /** |
| 123 | + * Returns the client's address. |
| 124 | + * @return the client's address |
| 125 | + * @see #getPublicAddress() |
| 126 | + * @see #getLocalAddress() |
| 127 | + * @deprecated use {@link #getPublicAddress()} instead |
| 128 | + * @since 01.00.00 |
| 129 | + */ |
| 130 | + @Deprecated |
| 131 | + public abstract InetSocketAddress getRequestAddress(); |
| 132 | + |
| 133 | + /** |
| 134 | + * Returns the client's public address. |
| 135 | + * @return the client's address |
| 136 | + * @see #getLocalAddress() |
| 137 | + * @since 01.01.00 |
| 138 | + */ |
| 139 | + public abstract InetSocketAddress getPublicAddress(); |
| 140 | + |
| 141 | + /** |
| 142 | + * returns the client's local address on the server. |
| 143 | + * @return the client's address |
| 144 | + * @see #getPublicAddress() |
| 145 | + * @since 01.01.00 |
| 146 | + */ |
| 147 | + public abstract InetSocketAddress getLocalAddress(); |
| 148 | + |
| 149 | + /** |
| 150 | + * Returns the request URI. |
| 151 | + * @return the request URI |
| 152 | + * @since 01.00.00 |
| 153 | + */ |
| 154 | + public abstract URI getURI(); |
| 155 | + |
| 156 | + /** |
| 157 | + * Returns the GET request as a String. |
| 158 | + * @return GET request as a String |
| 159 | + * @see #getGetMap() |
| 160 | + * @see #hasGet() |
| 161 | + * @since 01.00.00 |
| 162 | + */ |
| 163 | + public abstract String getRawGet(); |
| 164 | + |
| 165 | + /** |
| 166 | + * Returns the GET request as keys mapped to values. |
| 167 | + * @return GET request as a Map |
| 168 | + * @see #getRawGet() |
| 169 | + * @see #hasGet() |
| 170 | + * @since 01.00.00 |
| 171 | + */ |
| 172 | + public abstract HashMap<String,String> getGetMap(); |
| 173 | + |
| 174 | + /** |
| 175 | + * Returns if there is a GET request. |
| 176 | + * @return if GET request exists |
| 177 | + * @see #getRawGet() |
| 178 | + * @see #getGetMap() |
| 179 | + * @since 01.00.00 |
| 180 | + */ |
| 181 | + public abstract boolean hasGet(); |
| 182 | + |
| 183 | + /** |
| 184 | + * Returns the POST request as a String. Note that syntax of the multipart/form-data is not the same as the GET request. |
| 185 | + * @return POST request as a String |
| 186 | + * @see #getPostMap() |
| 187 | + * @see #hasPost() |
| 188 | + * @since 01.00.00 |
| 189 | + */ |
| 190 | + public abstract String getRawPost(); |
| 191 | + |
| 192 | + /** |
| 193 | + * Returns the POST request as keys mapped to values. Note that files in a multipart/form-data will return bytes. |
| 194 | + * @return POST request as a Map |
| 195 | + * @see #getRawPost() |
| 196 | + * @see #hasPost() |
| 197 | + * @since 01.00.00 |
| 198 | + */ |
| 199 | + public abstract HashMap getPostMap(); |
| 200 | + |
| 201 | + /** |
| 202 | + * Returns if there is a POST request. |
| 203 | + * @return if POST request exists |
| 204 | + * @see #getRawPost() |
| 205 | + * @see #getPostMap() |
| 206 | + * @since 01.00.00 |
| 207 | + */ |
| 208 | + public abstract boolean hasPost(); |
| 209 | + |
| 210 | + /** |
| 211 | + * Returns the request headers. |
| 212 | + * @return request headers |
| 213 | + * @since 01.00.00 |
| 214 | + */ |
| 215 | + public abstract Headers getRequestHeaders(); |
| 216 | + |
| 217 | + /** |
| 218 | + * Returns the request method. |
| 219 | + * @return request method |
| 220 | + * @see RequestMethod |
| 221 | + * @since 01.00.00 |
| 222 | + */ |
| 223 | + public abstract RequestMethod getRequestMethod(); |
| 224 | + |
| 225 | + /** |
| 226 | + * Returns response headers that will be sent to the client. |
| 227 | + * @return response headers |
| 228 | + * @since 01.00.00 |
| 229 | + */ |
| 230 | + public abstract Headers getResponseHeaders(); |
| 231 | + |
| 232 | + /** |
| 233 | + * Returns the response code that will be sent to the client. |
| 234 | + * @return response code |
| 235 | + * @see HTTPCode |
| 236 | + * @since 01.00.00 |
| 237 | + */ |
| 238 | + public abstract int getResponseCode(); |
| 239 | + |
| 240 | + /** |
| 241 | + * Sends response headers to the client. |
| 242 | + * @param code response code |
| 243 | + * @param length the size of the response in bytes |
| 244 | + * @throws IOException internal server error |
| 245 | + * @see HTTPCode |
| 246 | + * @since 01.00.00 |
| 247 | + */ |
| 248 | + public abstract void sendResponseHeaders(int code, long length) throws IOException; |
| 249 | + |
| 250 | + /** |
| 251 | + * Sends response to the client with code only. |
| 252 | + * @param code response code |
| 253 | + * @throws IOException internal server error |
| 254 | + * @see HTTPCode |
| 255 | + * @since 01.00.00 |
| 256 | + */ |
| 257 | + public abstract void send(int code) throws IOException; |
| 258 | + |
| 259 | + /** |
| 260 | + * Sends response to the client with code 200. |
| 261 | + * @param response bytes to send |
| 262 | + * @throws IOException internal server error |
| 263 | + * @since 01.00.00 |
| 264 | + */ |
| 265 | + public abstract void send(byte[] response) throws IOException; |
| 266 | + |
| 267 | + /** |
| 268 | + * Sends response to the client. |
| 269 | + * @param response bytes to send |
| 270 | + * @param code response code |
| 271 | + * @throws IOException internal server error |
| 272 | + * @see HTTPCode |
| 273 | + * @since 01.00.00 |
| 274 | + */ |
| 275 | + public abstract void send(byte[] response, int code) throws IOException; |
| 276 | + |
| 277 | + /** |
| 278 | + * Sends response to the client with code 200. |
| 279 | + * @param response String to send |
| 280 | + * @throws IOException internal server error |
| 281 | + * @since 01.00.00 |
| 282 | + */ |
| 283 | + public abstract void send(String response) throws IOException; |
| 284 | + |
| 285 | + /** |
| 286 | + * Sends response to the client. |
| 287 | + * @param response String to send |
| 288 | + * @param code response code |
| 289 | + * @throws IOException internal server error |
| 290 | + * @see HTTPCode |
| 291 | + * @since 01.00.00 |
| 292 | + */ |
| 293 | + public abstract void send(String response, int code) throws IOException; |
| 294 | + |
| 295 | + /** |
| 296 | + * Closes the request. |
| 297 | + * @since 01.00.00 |
| 298 | + */ |
| 299 | + public abstract void close(); |
| 300 | + |
| 301 | +} |
0 commit comments