Skip to content

Commit 0412f90

Browse files
committed
Refactor if statements
1 parent 6522522 commit 0412f90

File tree

9 files changed

+86
-73
lines changed

9 files changed

+86
-73
lines changed

src/main/java/httpserver/responder/DeleteResponder.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@ public DeleteResponder(PathExaminer pathExaminer, FileOperator fileOperator) {
2323

2424
@Override
2525
public Response respond(AppConfig appConfig, Request request) throws IOException {
26-
if (handles(request.getPathString())) {
27-
Path fullPath = pathExaminer.getFullPath(appConfig.getRoot(), request.getPathString());
28-
if (pathExaminer.pathExists(fullPath)) {
29-
fileOperator.deleteFileAtPath(fullPath);
30-
return new OkResponse("".getBytes());
31-
} else {
32-
return new NotFoundResponse();
33-
}
34-
} else {
26+
if (!handles(request.getPathString())) {
3527
return new MethodNotAllowedResponse();
3628
}
29+
30+
Path fullPath = pathExaminer.getFullPath(appConfig.getRoot(), request.getPathString());
31+
if (pathExaminer.pathExists(fullPath)) {
32+
fileOperator.deleteFileAtPath(fullPath);
33+
return new OkResponse("".getBytes());
34+
}
35+
36+
return new NotFoundResponse();
3737
}
3838

3939
public boolean handles(String pathString) {

src/main/java/httpserver/responder/FormGetResponder.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import httpserver.Request;
55
import httpserver.file.FileOperator;
66
import httpserver.file.PathExaminer;
7-
import httpserver.response.NotFoundResponse;
87
import httpserver.response.OkResponse;
98
import httpserver.response.Response;
109

@@ -23,12 +22,12 @@ public FormGetResponder(PathExaminer pathExaminer, FileOperator fileOperator) {
2322
@Override
2423
public Response respond(AppConfig appConfig, Request request) throws IOException {
2524
Path fullPath = pathExaminer.getFullPath(appConfig.getRoot(), request.getPathString());
26-
if (pathExaminer.pathExists(fullPath)) {
27-
return new OkResponse(fileOperator.readContents(fullPath));
28-
} else {
25+
26+
if (!pathExaminer.pathExists(fullPath)) {
2927
fileOperator.createFileAtPath(fullPath);
30-
return new OkResponse(fileOperator.readContents(fullPath));
3128
}
29+
30+
return new OkResponse(fileOperator.readContents(fullPath));
3231
}
3332

3433
@Override

src/main/java/httpserver/responder/GetResponder.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,22 @@ public Response respond(AppConfig appConfig, Request request) throws IOException
3737
String requestPathString = request.getPathString();
3838

3939
if (specialCaseRouteMap.hasRoute(requestPathString)) {
40-
return specialCaseRouteMap.getResponderForRoute(requestPathString).respond(appConfig, request);
40+
Responder responderForRoute = specialCaseRouteMap.getResponderForRoute(requestPathString);
41+
return responderForRoute.respond(appConfig, request);
4142
}
4243

4344
Path root = appConfig.getRoot();
44-
Path path = pathExaminer.getFullPath(root, requestPathString);
45-
46-
if (pathExaminer.pathExists(path)) {
47-
if (pathExaminer.isFile(path)) {
48-
return responseForFile(path, request);
49-
} else {
50-
return responseForDir(root, path);
51-
}
45+
Path fullPath = pathExaminer.getFullPath(root, requestPathString);
46+
47+
if (!pathExaminer.pathExists(fullPath)) {
48+
return new NotFoundResponse();
49+
}
50+
51+
if (!pathExaminer.isFile(fullPath)) {
52+
return responseForDir(root, fullPath);
5253
}
5354

54-
return new NotFoundResponse();
55+
return responseForFile(fullPath, request);
5556
}
5657

5758
private Response responseForDir(Path root, Path path) {
@@ -61,17 +62,18 @@ private Response responseForDir(Path root, Path path) {
6162
}
6263

6364
private String htmlLinksForContents(Path root, Path[] paths) {
64-
String result = "";
65+
StringBuilder stringBuilder = new StringBuilder();
6566
for (Path subPath: paths) {
66-
result = result + html.linkString(root, subPath);
67+
stringBuilder.append(html.linkString(root, subPath));
6768
}
68-
return result;
69+
return stringBuilder.toString();
6970
}
7071

7172
private Response responseForFile(Path path, Request request) {
7273
byte[] payload = pathExaminer.fileContents(path);
7374

74-
Response response = null;
75+
Response response;
76+
7577
if(request.hasHeader("Range")) {
7678
String rangeHeaderValue = request.getHeaderValue("Range");
7779
Range range = rangeHeaderValueParser.parse(rangeHeaderValue, payload.length);

src/main/java/httpserver/responder/OptionsResponder.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import httpserver.response.OkResponse;
99
import httpserver.response.Response;
1010

11-
import java.io.IOException;
1211
import java.util.StringJoiner;
1312

1413
public class OptionsResponder implements Responder {
@@ -19,15 +18,17 @@ public OptionsResponder(ResponderSupplier responderSupplier) {
1918
}
2019

2120
@Override
22-
public Response respond(AppConfig appConfig, Request request) throws IOException {
21+
public Response respond(AppConfig appConfig, Request request) {
2322
Method[] methods = Method.values();
2423
StringJoiner joiner = new StringJoiner(",");
24+
2525
for (Method method: methods) {
2626
Responder responder = responderSupplier.responderForMethodString(method.toString());
2727
if (responder.handles(request.getPathString())) {
2828
joiner.add(method.toString());
2929
}
3030
}
31+
3132
Response response = new OkResponse("".getBytes());
3233
response.setHeader(new Header("Allow", joiner.toString()));
3334
return response;

src/main/java/httpserver/responder/PatchResponder.java

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,27 @@ public PatchResponder(PathExaminer pathExaminer,
2424

2525
@Override
2626
public Response respond(AppConfig appConfig, Request request) throws IOException {
27-
if (handles(request.getPathString())) {
28-
Path fullPath = pathExaminer.getFullPath(appConfig.getRoot(), request.getPathString());
29-
if (pathExaminer.pathExists(fullPath)) {
30-
if (noIfMatchHeader(request)) {
31-
return new ConflictResponse();
32-
}
33-
if (matchingHash(fullPath, request)) {
34-
byte[] newFileContents = request.getBody().getBytes();
35-
fileOperator.replaceContents(fullPath, newFileContents);
36-
return new NoContentResponse(hasher.getHash(newFileContents));
37-
}
38-
return new ConflictResponse();
39-
} else {
40-
return new NotFoundResponse();
41-
}
27+
if (!handles(request.getPathString())) {
28+
return new MethodNotAllowedResponse();
4229
}
43-
return new MethodNotAllowedResponse();
30+
31+
Path fullPath = pathExaminer.getFullPath(appConfig.getRoot(), request.getPathString());
32+
33+
if (!pathExaminer.pathExists(fullPath)) {
34+
return new NotFoundResponse();
35+
}
36+
37+
if (noIfMatchHeader(request)) {
38+
return new ConflictResponse();
39+
}
40+
41+
if (matchingHash(fullPath, request)) {
42+
byte[] newFileContents = request.getBody().getBytes();
43+
fileOperator.replaceContents(fullPath, newFileContents);
44+
return new NoContentResponse(hasher.getHash(newFileContents));
45+
}
46+
47+
return new ConflictResponse();
4448
}
4549

4650
private boolean noIfMatchHeader(Request request) {

src/main/java/httpserver/responder/PostResponder.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@ public PostResponder(PathExaminer pathExaminer, FileOperator fileOperator) {
2121

2222
@Override
2323
public Response respond(AppConfig appConfig, Request request) throws IOException {
24-
if (handles(request.getPathString())) {
25-
Path fullPath = pathExaminer.getFullPath(appConfig.getRoot(), request.getPathString());
26-
if (pathExaminer.pathExists(fullPath)) {
27-
fileOperator.replaceContents(fullPath, request.getBody().getBytes());
28-
return new OkResponse(fileOperator.readContents(fullPath));
29-
} else {
30-
fileOperator.createFileAtPath(fullPath);
31-
fileOperator.replaceContents(fullPath, request.getBody().getBytes());
32-
return new OkResponse(fileOperator.readContents(fullPath));
33-
}
24+
if (!handles(request.getPathString())) {
25+
return new MethodNotAllowedResponse();
3426
}
35-
return new MethodNotAllowedResponse();
27+
28+
Path fullPath = pathExaminer.getFullPath(appConfig.getRoot(), request.getPathString());
29+
30+
if (!pathExaminer.pathExists(fullPath)) {
31+
fileOperator.createFileAtPath(fullPath);
32+
}
33+
34+
fileOperator.replaceContents(fullPath, request.getBody().getBytes());
35+
return new OkResponse(fileOperator.readContents(fullPath));
3636
}
3737

3838
public boolean handles(String pathString) {

src/main/java/httpserver/responder/PutResponder.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,18 @@ public PutResponder(PathExaminer pathExaminer, FileOperator fileOperator) {
2323

2424
@Override
2525
public Response respond(AppConfig appConfig, Request request) throws IOException {
26-
if (handles(request.getPathString())) {
27-
Path fullPath = pathExaminer.getFullPath(appConfig.getRoot(), request.getPathString());
28-
if (pathExaminer.pathExists(fullPath)) {
29-
fileOperator.replaceContents(fullPath, request.getBody().getBytes());
30-
return new OkResponse(fileOperator.readContents(fullPath));
31-
} else {
32-
return new NotFoundResponse();
33-
}
26+
if (!handles(request.getPathString())) {
27+
return new MethodNotAllowedResponse();
3428
}
35-
return new MethodNotAllowedResponse();
29+
30+
Path fullPath = pathExaminer.getFullPath(appConfig.getRoot(), request.getPathString());
31+
32+
if (!pathExaminer.pathExists(fullPath)) {
33+
return new NotFoundResponse();
34+
}
35+
36+
fileOperator.replaceContents(fullPath, request.getBody().getBytes());
37+
return new OkResponse(fileOperator.readContents(fullPath));
3638
}
3739

3840
public boolean handles(String pathString) {

src/main/java/httpserver/responder/special/LogsResponder.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@
1111
public class LogsResponder implements Responder {
1212
@Override
1313
public Response respond(AppConfig appConfig, Request request) {
14-
if (new Authorizer().authorize(request)) {
15-
byte[] log = appConfig.getLogger().readLog();
16-
return new OkResponse(log);
17-
} else {
14+
Authorizer authorizer = new Authorizer();
15+
16+
if (!authorizer.authorize(request)) {
1817
return new UnauthorizedResponse();
1918
}
19+
20+
byte[] log = appConfig.getLogger().readLog();
21+
return new OkResponse(log);
2022
}
2123

2224
@Override

src/main/java/httpserver/responder/special/ParametersResponder.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@ public boolean handles(String pathString) {
2020

2121
private String echoQueryString(Request request) {
2222
Parameter[] parameters = request.getParams();
23-
String payload = "";
23+
StringBuilder stringBuilder = new StringBuilder();
2424
for (Parameter parameter: parameters) {
25-
payload = payload + parameter.getKey() + " = " + parameter.getValue() + "\r\n";
25+
stringBuilder.append(parameter.getKey());
26+
stringBuilder.append(" = ");
27+
stringBuilder.append(parameter.getValue());
28+
stringBuilder.append("\r\n");
2629
}
27-
return payload;
30+
return stringBuilder.toString();
2831
}
2932

3033
}

0 commit comments

Comments
 (0)