Skip to content

Commit 40e32c2

Browse files
committed
Created the ResponseModificationData to return modification responses.
Added Documentation in the README.md for the ResponseModificationData. Added an Example Route ("/redirect") to Example.java to show the usage of the ResponseModificationData. Updated to Version 1.3.2
1 parent 00cd563 commit 40e32c2

6 files changed

Lines changed: 86 additions & 2 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ You can also download the newest version from the [releases](https://github.com/
1515
<dependency>
1616
<groupId>de.joshicodes</groupId>
1717
<artifactId>webapi</artifactId>
18-
<version>1.3.1b</version>
18+
<version>1.3.2</version>
1919
</dependency>
2020
```
2121

@@ -106,6 +106,8 @@ You can create a new ResponseData object using the `ResponseData.Builder` class.
106106
```
107107
You can also use `ResponseData#from(int code, String body)` to create a new Builder object.
108108

109+
To redirect the client, return a `ResponseModificationData` Object, which you can create by using `ResponseData#redirect(String)`. This will redirect the client to the specified path.
110+
109111
<br><br>
110112

111113
## Error Handling

examples/Example.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ public ResponseData handle(RequestData request) {
4949
}
5050
});
5151

52+
// Redirect to another url
53+
builder.addRoute("/redirect", request -> ResponseData.redirect("https://google.com"));
54+
5255
Webserver server = builder.build(); // build the server
5356
server.start(); // start the server
5457

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>de.joshicodes</groupId>
88
<artifactId>webapi</artifactId>
9-
<version>1.3.1d</version>
9+
<version>1.3.2</version>
1010

1111
<build>
1212
<plugins>

src/main/java/de/joshicodes/webapi/Webserver.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,23 @@ private void write(ResponseData response, HttpExchange exchange) throws IOExcept
253253
exchange.getResponseBody().write(body.getBytes());
254254
exchange.getResponseBody().close();
255255
} else {
256+
257+
if(response instanceof ResponseModificationData modificationData) {
258+
259+
if(modificationData.getRedirectLocation() != null) {
260+
261+
exchange.getResponseHeaders().add("Location", modificationData.getRedirectLocation());
262+
exchange.sendResponseHeaders(302, 0);
263+
exchange.sendResponseHeaders(302, 0);
264+
exchange.getResponseBody().close();
265+
266+
} else {
267+
// Future implementations here
268+
}
269+
270+
return;
271+
}
272+
256273
exchange.sendResponseHeaders(response.getStatusCode(), 0);
257274
exchange.getResponseBody().close();
258275
}

src/main/java/de/joshicodes/webapi/request/ResponseData.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@ public abstract class ResponseData {
1313
abstract public String getHeader(String key);
1414
abstract public HashMap<String, String> getHeaders();
1515

16+
/**
17+
* Creates a new {@link ResponseModificationData} object that redirects to the given URL
18+
* @param url The URL to redirect to. Has to be a valid URL
19+
* @return The ResponseModificationData object
20+
*/
21+
public static ResponseModificationData redirect(String url) {
22+
return new ResponseModificationData() {
23+
@Override
24+
public String getRedirectLocation() {
25+
return url;
26+
}
27+
};
28+
}
1629

1730
/**
1831
* Creates a new ResponseData object with the given body<br>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package de.joshicodes.webapi.request;
2+
3+
import org.jetbrains.annotations.Nullable;
4+
5+
import java.util.HashMap;
6+
7+
/**
8+
* This class is used when the Response returned by the server should end up being modified.
9+
* This is used for example when the server should redirect to another page.
10+
* @see ResponseData
11+
*/
12+
public abstract class ResponseModificationData extends ResponseData {
13+
14+
/**
15+
* Returns the location to redirect to or null if the response should not be redirected
16+
* Has to be a valid URL
17+
* @return The location to redirect to
18+
*/
19+
abstract @Nullable public String getRedirectLocation();
20+
21+
22+
// Override all methods from ResponseData to return null or -1
23+
24+
@Override
25+
public String getBody() {
26+
return null;
27+
}
28+
29+
@Override
30+
public String getHeader(String key) {
31+
return null;
32+
}
33+
34+
@Override
35+
public HashMap<String, String> getHeaders() {
36+
return null;
37+
}
38+
39+
@Override
40+
public int getStatusCode() {
41+
return -1;
42+
}
43+
44+
@Override
45+
public String getContentType() {
46+
return null;
47+
}
48+
49+
}

0 commit comments

Comments
 (0)