Skip to content

Commit 7be07e9

Browse files
author
Joshua
committed
Implemented ResponseData#getContentType in Webserver#write, as it was not yet implemented
Added ResponseData#getHeaders and implemented it in Webserver#write Added Documentation to ResponseData.Builder Updated the Version in pom.xml and README.md
1 parent de86ec7 commit 7be07e9

4 files changed

Lines changed: 39 additions & 3 deletions

File tree

README.md

Lines changed: 1 addition & 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</version>
18+
<version>1.3.1</version>
1919
</dependency>
2020
```
2121

pom.xml

Lines changed: 2 additions & 2 deletions
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</version>
9+
<version>1.3.1</version>
1010

1111
<build>
1212
<plugins>
@@ -74,7 +74,7 @@
7474
<dependency>
7575
<groupId>org.jetbrains</groupId>
7676
<artifactId>annotations</artifactId>
77-
<version>23.0.0</version>
77+
<version>23.1.0</version>
7878
<scope>compile</scope>
7979
</dependency>
8080
<dependency>

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,13 @@ private void write(ResponseData response, HttpExchange exchange) throws IOExcept
241241
String body = response.getBody();
242242
if(body != null) {
243243
exchange.sendResponseHeaders(response.getStatusCode(), body.length());
244+
245+
if(response.getHeaders() != null)
246+
response.getHeaders().forEach((key, value) -> exchange.getResponseHeaders().add(key, value));
247+
248+
if(response.getContentType() != null)
249+
exchange.getResponseHeaders().add("Content-Type", response.getContentType());
250+
244251
exchange.getResponseBody().write(body.getBytes());
245252
exchange.getResponseBody().close();
246253
} else {

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public abstract class ResponseData {
1111
abstract public int getStatusCode();
1212
abstract public String getContentType();
1313
abstract public String getHeader(String key);
14+
abstract public HashMap<String, String> getHeaders();
1415

1516

1617
/**
@@ -81,21 +82,43 @@ public Builder() {
8182
this.headers = new HashMap<>();
8283
}
8384

85+
/**
86+
* Sets the body of the response
87+
* @param body The body of the response
88+
* @return This builder object
89+
*/
8490
public Builder setBody(String body) {
8591
this.body = body;
8692
return this;
8793
}
8894

95+
/**
96+
* Sets the status code of the response
97+
* @param statusCode The status code of the response
98+
* @return This builder object
99+
*/
89100
public Builder setStatusCode(int statusCode) {
90101
this.statusCode = statusCode;
91102
return this;
92103
}
93104

105+
/**
106+
* Sets the content type of the response<br>
107+
* This overrides the "Content-Type" header if it is set using {@link Builder#setHeader(String, String)}
108+
* @param contentType The content type of the response
109+
* @return This builder object
110+
*/
94111
public Builder setContentType(String contentType) {
95112
this.contentType = contentType;
96113
return this;
97114
}
98115

116+
/**
117+
* Sets a header of the response
118+
* @param key The key of the header
119+
* @param value The value of the header
120+
* @return This builder object
121+
*/
99122
public Builder setHeader(String key, String value) {
100123
this.headers.put(key, value);
101124
return this;
@@ -122,6 +145,12 @@ public String getContentType() {
122145
public String getHeader(String key) {
123146
return headers.get(key);
124147
}
148+
149+
@Override
150+
public HashMap<String, String> getHeaders() {
151+
return headers;
152+
}
153+
125154
};
126155
}
127156

0 commit comments

Comments
 (0)