Skip to content

Commit fced7c5

Browse files
committed
Refactor code
1 parent 2836fb2 commit fced7c5

File tree

17 files changed

+117
-73
lines changed

17 files changed

+117
-73
lines changed

src/main/java/express/Express.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public Object get(String key) {
138138
*/
139139
public void setExecutor(Executor executor) throws IOException {
140140
if (httpServer != null) {
141-
throw new IOException("Cannot set the executor after the server has starderd!");
141+
throw new IOException("Cannot set executor after the server has stardet!");
142142
} else {
143143
this.executor = executor;
144144
}
@@ -371,8 +371,9 @@ public void listen(ExpressListener onStart, int port) {
371371
httpServer.start();
372372

373373
// Fire listener
374-
if (onStart != null)
374+
if (onStart != null) {
375375
onStart.action();
376+
}
376377

377378
} catch (IOException e) {
378379
e.printStackTrace();

src/main/java/express/ExpressListener.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,5 @@
55
* Listener for express actions
66
*/
77
public interface ExpressListener {
8-
98
void action();
10-
119
}

src/main/java/express/filter/FilterImpl.java

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,19 @@ public FilterImpl(String requestMethod, String context, HttpRequestHandler httpR
4040
}
4141

4242
public void setRoot(String root) {
43-
if (root == null || root.isEmpty())
43+
44+
// Ignore empty root
45+
if (root == null || root.isEmpty()) {
4446
return;
47+
}
4548

46-
if (root.charAt(0) != '/')
49+
if (root.charAt(0) != '/') {
4750
root = '/' + root;
51+
}
4852

49-
if (root.charAt(root.length() - 1) != '/')
53+
if (root.charAt(root.length() - 1) != '/') {
5054
root += '/';
55+
}
5156

5257
this.root = normalizePath(root);
5358
this.fullContext = normalizePath(this.root + context);
@@ -69,8 +74,9 @@ public void handle(Request req, Response res) {
6974

7075
// Parse params
7176
HashMap<String, String> params = matchURL(fullContext, requestPath);
72-
if (params == null)
77+
if (params == null) {
7378
return;
79+
}
7480

7581
// Save parameter to request object
7682
req.setParams(params);
@@ -79,13 +85,15 @@ public void handle(Request req, Response res) {
7985
params.forEach((s, s2) -> {
8086
HttpRequestHandler request = parameterListener.get(s);
8187

82-
if (request != null)
88+
if (request != null) {
8389
request.handle(req, res);
90+
}
8491
});
8592

8693
// Check if the response is closed
87-
if (res.isClosed())
94+
if (res.isClosed()) {
8895
return;
96+
}
8997

9098
// Handle request
9199
req.setContext(context);
@@ -110,11 +118,14 @@ private HashMap<String, String> matchURL(String filter, String url) {
110118
val.setLength(0);
111119

112120
fi++;
113-
while (fi < fc.length && fc[fi] != '/')
121+
122+
while (fi < fc.length && fc[fi] != '/') {
114123
key.append(fc[fi++]);
124+
}
115125

116-
while (ui < uc.length && uc[ui] != '/')
126+
while (ui < uc.length && uc[ui] != '/') {
117127
val.append(uc[ui++]);
128+
}
118129

119130
try {
120131
String decVal = URLDecoder.decode(val.toString(), "UTF8");
@@ -149,8 +160,9 @@ private String normalizePath(String context) {
149160

150161
sb.append(chars[0]);
151162
for (int i = 1; i < chars.length; i++) {
152-
if ((chars[i] == '/' && chars[i - 1] != '/') || chars[i] != '/')
163+
if ((chars[i] == '/' && chars[i - 1] != '/') || chars[i] != '/') {
153164
sb.append(chars[i]);
165+
}
154166
}
155167

156168
return sb.toString();

src/main/java/express/filter/FilterLayerHandler.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ public FilterLayerHandler(int layers) {
2222

2323
// Create & initialize layers
2424
this.layers = new FilterLayer[layers];
25-
for (int i = 0; i < this.layers.length; i++)
25+
for (int i = 0; i < this.layers.length; i++) {
2626
this.layers[i] = new FilterLayer<>();
27+
}
2728
}
2829

2930
public void handle(HttpExchange httpExchange, Express express) {
@@ -34,8 +35,9 @@ public void handle(HttpExchange httpExchange, Express express) {
3435
for (FilterLayer chain : layers) {
3536
chain.filter(request, response);
3637

37-
if (response.isClosed())
38+
if (response.isClosed()) {
3839
return;
40+
}
3941
}
4042
}
4143

@@ -48,10 +50,13 @@ public void handle(HttpExchange httpExchange, Express express) {
4850
@SuppressWarnings("unchecked")
4951
public void add(int level, HttpRequestHandler handler) {
5052

51-
if (level >= layers.length)
53+
if (level >= layers.length) {
5254
throw new IndexOutOfBoundsException("Out of bounds: " + level + " > " + layers.length);
53-
if (level < 0)
55+
}
56+
57+
if (level < 0) {
5458
throw new IndexOutOfBoundsException("Cannot be under zero: " + level + " < 0");
59+
}
5560

5661
layers[level].add(handler);
5762
}
@@ -66,11 +71,13 @@ public void combine(FilterLayerHandler filterLayerHandler) {
6671
if (filterLayerHandler != null) {
6772
FilterLayer[] chains = filterLayerHandler.getLayers();
6873

69-
if (chains.length != layers.length)
74+
if (chains.length != layers.length) {
7075
throw new ExpressException("Cannot add an filterLayerHandler with different layers sizes: " + chains.length + " != " + layers.length);
76+
}
7177

72-
for (int i = 0; i < chains.length; i++)
78+
for (int i = 0; i < chains.length; i++) {
7379
layers[i].addAll(chains[i].getFilter());
80+
}
7481
}
7582
}
7683

@@ -80,11 +87,13 @@ public void combine(FilterLayerHandler filterLayerHandler) {
8087
* @param layerConsumer An consumer for the layers
8188
*/
8289
public void forEach(Consumer<FilterLayer> layerConsumer) {
83-
if (layerConsumer == null)
90+
if (layerConsumer == null) {
8491
return;
92+
}
8593

86-
for (FilterLayer layer : layers)
94+
for (FilterLayer layer : layers) {
8795
layerConsumer.accept(layer);
96+
}
8897
}
8998

9099
private FilterLayer[] getLayers() {

src/main/java/express/http/Cookie.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ public class Cookie {
2727
public Cookie(String name, String value) {
2828
name = name.trim();
2929

30-
if (name.isEmpty() || name.charAt(0) == '$')
30+
if (name.isEmpty() || name.charAt(0) == '$') {
3131
throw new IllegalArgumentException("Illegal cookie name");
32+
}
3233

3334
this.name = name;
3435
this.value = value;
@@ -203,6 +204,7 @@ public boolean equals(Object obj) {
203204
if (!other.getExpire().equals(this.getExpire())) return false;
204205
if (other.getMaxAge() != this.getMaxAge()) return false;
205206
if (!other.getSameSite().equals(this.getSameSite())) return false;
207+
206208
return other.getPath().equals(this.getPath());
207209
}
208210
return super.equals(obj);

src/main/java/express/http/CookieFactory.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,16 @@ public static Cookie fromString(String cookieString) {
5656
public static HashMap<String, Cookie> fromStrings(String[] stringCookies) {
5757
HashMap<String, Cookie> cookies = new HashMap<>();
5858

59-
if (stringCookies == null || stringCookies.length == 0)
59+
if (stringCookies == null || stringCookies.length == 0) {
6060
return cookies;
61+
}
6162

6263
for (String s : stringCookies) {
6364
Cookie cookie = fromString(s);
64-
if (cookie != null)
65+
66+
if (cookie != null) {
6567
cookies.put(cookie.getName(), cookie);
68+
}
6669
}
6770

6871
return cookies;
@@ -109,8 +112,9 @@ private static Cookie addField(Cookie cookie, String key, String value) {
109112
private static boolean isInteger(String str) {
110113
char[] chars = str.toCharArray();
111114

112-
for (char c : chars)
115+
for (char c : chars) {
113116
if (c < 48 || c > 57) return false;
117+
}
114118

115119
return true;
116120
}

src/main/java/express/http/SameSite.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
* @author Simon Reinisch
55
*/
66
public enum SameSite {
7-
87
STRICT,
98
LAX
10-
119
}

src/main/java/express/http/request/Authorization.java

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,38 +29,19 @@ public Authorization(String authHeader) {
2929
this.data = parts[1];
3030
}
3131

32-
/**
33-
* @return The Authorization type
34-
*/
35-
public String getType() {
36-
return type;
37-
}
38-
39-
/**
40-
* @return The Authorization data
41-
*/
42-
public String getData() {
43-
return data;
44-
}
45-
46-
/**
47-
* @return The Authorization data base64 decoded
48-
*/
49-
public String getDataBase64Decoded() {
50-
return new String(Base64.getDecoder().decode(data));
51-
}
52-
5332
/**
5433
* @return A list of authorization options that are contained in the given request.
5534
* Authorization options can be separated by a comma in the Authorization header.
5635
*/
5736
public static List<Authorization> get(Request req) {
5837
List<String> headerVals = req.getHeader(HEADER_NAME);
38+
5939
if (!headerVals.isEmpty()) {
6040
String authHeader = headerVals.get(0);
6141
return Collections.unmodifiableList(Stream.of(authHeader.split(","))
6242
.map(Authorization::new).collect(Collectors.toList()));
6343
}
44+
6445
return Collections.emptyList();
6546
}
6647

@@ -73,7 +54,9 @@ public static List<Authorization> get(Request req) {
7354
public static boolean validate(Request req, Predicate<Authorization>... validators) {
7455
for (Authorization auth : get(req)) {
7556
for (Predicate<Authorization> validator : validators) {
76-
if (validator.test(auth)) return true;
57+
if (validator.test(auth)) {
58+
return true;
59+
}
7760
}
7861
}
7962
return false;
@@ -88,4 +71,25 @@ public static boolean validate(Request req, Predicate<Authorization>... validato
8871
public static Predicate<Authorization> validator(String type, String data) {
8972
return (auth -> auth.getType().equals(type) && auth.getData().equals(data));
9073
}
74+
75+
/**
76+
* @return The Authorization type
77+
*/
78+
public String getType() {
79+
return type;
80+
}
81+
82+
/**
83+
* @return The Authorization data
84+
*/
85+
public String getData() {
86+
return data;
87+
}
88+
89+
/**
90+
* @return The Authorization data base64 decoded
91+
*/
92+
public String getDataBase64Decoded() {
93+
return new String(Base64.getDecoder().decode(data));
94+
}
9195
}

src/main/java/express/http/request/Request.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public void pipe(Path f, int bufferSize) throws IOException {
126126
}
127127

128128
/**
129-
* Get an request cookie by name.
129+
* Get a request cookie by name.
130130
*
131131
* @param name The cookie name.
132132
* @return The cookie, null if there is no cookie with this name.
@@ -145,7 +145,7 @@ public HashMap<String, Cookie> getCookies() {
145145
}
146146

147147
/**
148-
* Add an the content from an middleware
148+
* Add a the content from a middleware
149149
*
150150
* @param middleware The middleware
151151
* @param middlewareData The data from the middleware

src/main/java/express/http/request/RequestUtils.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,10 @@ static HashMap<String, Cookie> parseCookies(Headers headers) {
6565
static HashMap<String, String> parseRawQuery(String rawQuery) {
6666
HashMap<String, String> querys = new HashMap<>();
6767

68-
if (rawQuery == null)
68+
// Return empty map on null
69+
if (rawQuery == null) {
6970
return querys;
71+
}
7072

7173
StringBuilder key = new StringBuilder();
7274
StringBuilder val = new StringBuilder();
@@ -96,8 +98,9 @@ static HashMap<String, String> parseRawQuery(String rawQuery) {
9698
}
9799
}
98100

99-
if (c != '=' && c != '&')
101+
if (c != '=' && c != '&') {
100102
querys.put(key.toString(), val.toString());
103+
}
101104

102105
return querys;
103106
}

0 commit comments

Comments
 (0)