Skip to content
This repository was archived by the owner on Jul 31, 2022. It is now read-only.

Commit 361a4d9

Browse files
authored
Replaced RequestMethod enums with HttpRequestMethod static strings. (#121)
* move requestmethod single class packages don't make any sense. * Replace RequestMethod with static strings This change is necessary as there is no fixed list of request methods. * cleanup * Update module-info.java
1 parent b0fd553 commit 361a4d9

File tree

9 files changed

+37
-58
lines changed

9 files changed

+37
-58
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.kttdevelopment.simplehttpserver;
2+
3+
/**
4+
* A list of expected HTTP method requests.
5+
* See <a href="https://tools.ietf.org/html/rfc7231#section-4.3" target="_blank">IANA RFC7324 Section 4.3</a>
6+
* @since 01.00.00
7+
* @version 4.3.0
8+
* @author Ktt Development
9+
*/
10+
@SuppressWarnings("unused")
11+
public abstract class HttpRequestMethod {
12+
13+
public static final String GET = "GET";
14+
public static final String HEAD = "HEAD";
15+
public static final String POST = "POST";
16+
public static final String PUT = "PUT";
17+
public static final String DELETE = "DELETE";
18+
public static final String CONNECT = "CONNECT";
19+
public static final String OPTIONS = "OPTIONS";
20+
public static final String TRACE = "TRACE";
21+
public static final String PATCH = "PATCH";
22+
public static final String UNSUPPORTED = "UNSUPPORTED";
23+
24+
}

src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpExchange.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.kttdevelopment.simplehttpserver;
22

3-
import com.kttdevelopment.simplehttpserver.var.RequestMethod;
43
import com.sun.net.httpserver.*;
54

65
import java.io.*;
@@ -161,11 +160,11 @@ static SimpleHttpExchange create(final HttpExchange exchange){
161160
*
162161
* @return request method
163162
*
164-
* @see RequestMethod
163+
* @see HttpRequestMethod
165164
* @since 02.00.00
166165
* @author Ktt Development
167166
*/
168-
public abstract RequestMethod getRequestMethod();
167+
public abstract String getRequestMethod();
169168

170169
//
171170

src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpExchangeImpl.java

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.kttdevelopment.simplehttpserver;
22

33
import com.sun.net.httpserver.*;
4-
import com.kttdevelopment.simplehttpserver.var.*;
54

65
import java.io.*;
76
import java.net.*;
@@ -35,7 +34,7 @@ final class SimpleHttpExchangeImpl extends SimpleHttpExchange {
3534
private final String protocol;
3635

3736
private final Headers requestHeaders;
38-
private final RequestMethod requestMethod;
37+
private final String requestMethod;
3938

4039
private final String rawGet;
4140
private final Map<String,String> getMap;
@@ -96,28 +95,7 @@ static SimpleHttpExchange create(final HttpExchange exchange){
9695
protocol = httpExchange.getProtocol();
9796
//
9897
requestHeaders = httpExchange.getRequestHeaders();
99-
switch(exchange.getRequestMethod()){
100-
case "GET":
101-
requestMethod = RequestMethod.GET; break;
102-
case "HEAD":
103-
requestMethod = RequestMethod.HEAD; break;
104-
case "POST":
105-
requestMethod = RequestMethod.POST; break;
106-
case "PUT":
107-
requestMethod = RequestMethod.PUT; break;
108-
case "DELETE":
109-
requestMethod = RequestMethod.DELETE; break;
110-
case "CONNECT":
111-
requestMethod = RequestMethod.CONNECT; break;
112-
case "OPTIONS":
113-
requestMethod = RequestMethod.OPTIONS; break;
114-
case "TRACE":
115-
requestMethod = RequestMethod.TRACE; break;
116-
case "PATCH":
117-
requestMethod = RequestMethod.PATCH; break;
118-
default:
119-
requestMethod = RequestMethod.UNSUPPORTED; break;
120-
}
98+
requestMethod = exchange.getRequestMethod().toUpperCase();
12199
//
122100
hasGet = (rawGet = URI.getRawQuery()) != null;
123101
getMap = hasGet ? Collections.unmodifiableMap(parseWwwFormEnc.apply(rawGet)) : new HashMap<>();
@@ -267,7 +245,7 @@ public final Headers getRequestHeaders(){
267245
}
268246

269247
@Override
270-
public final RequestMethod getRequestMethod(){
248+
public final String getRequestMethod(){
271249
return requestMethod;
272250
}
273251

src/main/java/com/kttdevelopment/simplehttpserver/handler/SSEHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import com.kttdevelopment.simplehttpserver.SimpleHttpExchange;
44
import com.kttdevelopment.simplehttpserver.SimpleHttpHandler;
5-
import com.kttdevelopment.simplehttpserver.var.RequestMethod;
5+
import com.kttdevelopment.simplehttpserver.HttpRequestMethod;
66
import com.sun.net.httpserver.HttpExchange;
77

88
import java.io.IOException;
@@ -41,7 +41,7 @@ public final void handle(final SimpleHttpExchange exchange) throws IOException{
4141
exchange.getResponseHeaders().add("Access-Control-Max-Age", String.valueOf(TimeUnit.HOURS.toSeconds(1)));
4242
exchange.getResponseHeaders().add("Cache-Control","no-cache");
4343

44-
if(exchange.getRequestMethod() == RequestMethod.OPTIONS){
44+
if(exchange.getRequestMethod().equals(HttpRequestMethod.OPTIONS)){
4545
exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, 0);
4646
return;
4747
}

src/main/java/com/kttdevelopment/simplehttpserver/var/RequestMethod.java

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/main/java/module-info.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
requires jdk.httpserver;
44
requires java.net.http; // test requirement
55
exports com.kttdevelopment.simplehttpserver.handler;
6-
exports com.kttdevelopment.simplehttpserver.var;
76
exports com.kttdevelopment.simplehttpserver;
87

98
}

src/test/java/com/kttdevelopment/simplehttpserver/simplehttpexchange/io/SimpleHttpExchangeGetTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.kttdevelopment.simplehttpserver.simplehttpexchange.io;
22

33
import com.kttdevelopment.simplehttpserver.*;
4-
import com.kttdevelopment.simplehttpserver.var.RequestMethod;
4+
import com.kttdevelopment.simplehttpserver.HttpRequestMethod;
55
import com.sun.net.httpserver.HttpContext;
66
import org.junit.jupiter.api.Assertions;
77
import org.junit.jupiter.api.Test;
@@ -49,7 +49,7 @@ public final void get() throws IOException, ExecutionException, InterruptedExcep
4949
// exchange
5050
final SimpleHttpExchange exchange = exchangeRef.get();
5151

52-
Assertions.assertEquals(RequestMethod.GET, exchange.getRequestMethod(), "Client request method did not match exchange request method (GET)");
52+
Assertions.assertEquals(HttpRequestMethod.GET, exchange.getRequestMethod(), "Client request method did not match exchange request method (GET)");
5353
Assertions.assertTrue(exchange.hasGet(), "Exchange was missing client GET map");
5454
Assertions.assertEquals(queryValue, exchange.getGetMap().get(queryKey), "Exchange GET did not match client GET");
5555
Assertions.assertEquals(altValueRaw, exchange.getGetMap().get(altKey), "Exchange GET did not match client GET");

src/test/java/com/kttdevelopment/simplehttpserver/simplehttpexchange/io/SimpleHttpExchangeMultipartFormTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.kttdevelopment.simplehttpserver.simplehttpexchange.io;
22

33
import com.kttdevelopment.simplehttpserver.*;
4-
import com.kttdevelopment.simplehttpserver.var.RequestMethod;
4+
import com.kttdevelopment.simplehttpserver.HttpRequestMethod;
55
import org.junit.jupiter.api.Assertions;
66
import org.junit.jupiter.api.Test;
77

@@ -62,7 +62,7 @@ public final void postMultipartFormData() throws IOException, ExecutionException
6262
// exchange
6363
final SimpleHttpExchange exchange = exchangeRef.get();
6464

65-
Assertions.assertEquals(RequestMethod.POST, exchange.getRequestMethod(), "Client request method did not match exchange request method (POST)");
65+
Assertions.assertEquals(HttpRequestMethod.POST, exchange.getRequestMethod(), "Client request method did not match exchange request method (POST)");
6666
Assertions.assertTrue(exchange.hasPost(), "Exchange was missing client POST map");
6767

6868
Assertions.assertEquals(value, ((Map) exchange.getPostMap().get(key)).get("value"), "Client form value did not match server value");

src/test/java/com/kttdevelopment/simplehttpserver/simplehttpexchange/io/SimpleHttpExchangePostTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.kttdevelopment.simplehttpserver.simplehttpexchange.io;
22

33
import com.kttdevelopment.simplehttpserver.*;
4-
import com.kttdevelopment.simplehttpserver.var.RequestMethod;
4+
import com.kttdevelopment.simplehttpserver.HttpRequestMethod;
55
import com.sun.net.httpserver.HttpContext;
66
import org.junit.jupiter.api.Assertions;
77
import org.junit.jupiter.api.Test;
@@ -46,7 +46,7 @@ public final void postSimple() throws IOException, ExecutionException, Interrupt
4646
// exchange
4747
final SimpleHttpExchange exchange = exchangeRef.get();
4848

49-
Assertions.assertEquals(RequestMethod.POST, exchange.getRequestMethod(), "Client request method did not match exchange request method (POST)");
49+
Assertions.assertEquals(HttpRequestMethod.POST, exchange.getRequestMethod(), "Client request method did not match exchange request method (POST)");
5050
Assertions.assertTrue(exchange.hasPost(), "Exchange was missing client POST map");
5151
Assertions.assertEquals(queryValue, exchange.getPostMap().get(queryKey), "Exchange POST did not match client POST");
5252

0 commit comments

Comments
 (0)