Skip to content
This repository was archived by the owner on May 10, 2024. It is now read-only.

Commit 3c48989

Browse files
committed
basic documentation, nonnull checks, fixes
1 parent ee4d294 commit 3c48989

30 files changed

+1658
-154
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@
246246
<excludes>
247247
<exclude/>
248248
</excludes>
249+
<reuseForks>false</reuseForks> <!-- required to prevent server binding issues -->
249250
<trimStackTrace>false</trimStackTrace>
250251
</configuration>
251252
</plugin>

src/main/java/dev/katsute/simplehttpserver/FileRecord.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@
2121
import java.nio.charset.StandardCharsets;
2222
import java.util.*;
2323

24+
/**
25+
* Represents a file in a multipart/form-data request.
26+
*
27+
* @see MultipartFormData
28+
* @see Record
29+
*
30+
* @since 5.0.0
31+
* @version 5.0.0
32+
* @author Katsute
33+
*/
2434
public class FileRecord extends Record {
2535

2636
final String fileName, contentType;
@@ -34,14 +44,35 @@ public class FileRecord extends Record {
3444
bytes = getValue().getBytes(StandardCharsets.UTF_8);
3545
}
3646

47+
/**
48+
* Returns the file name.
49+
*
50+
* @return file name
51+
*
52+
* @since 5.0.0
53+
*/
3754
public final String getFileName(){
3855
return fileName;
3956
}
4057

58+
/**
59+
* Returns the file content type.
60+
*
61+
* @return content type
62+
*
63+
* @since 5.0.0
64+
*/
4165
public final String getContentType(){
4266
return contentType;
4367
}
4468

69+
/**
70+
* Returns the file content as a byte array
71+
*
72+
* @return file in bytes
73+
*
74+
* @since 5.0.0
75+
*/
4576
public final byte[] getBytes(){
4677
return Arrays.copyOf(bytes, bytes.length); // dereference
4778
}

src/main/java/dev/katsute/simplehttpserver/HttpServerExtensions.java

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,34 +24,137 @@
2424
import java.net.InetSocketAddress;
2525
import java.util.Map;
2626

27+
/**
28+
* Additional extensions provided to a {@link SimpleHttpServer}.
29+
*
30+
* @see SimpleHttpServer
31+
* @see SimpleHttpsServer
32+
* @since 5.0.0
33+
* @version 5.0.0
34+
* @author Katsute
35+
*/
2736
interface HttpServerExtensions {
2837

38+
/**
39+
* Binds the server to a port.
40+
*
41+
* @param port port to bind to
42+
* @return server address
43+
* @throws IOException internal error
44+
* @throws java.net.BindException if server could not be bounded
45+
*
46+
* @see HttpServer#bind(InetSocketAddress, int)
47+
* @see #bind(int, int)
48+
* @since 5.0.0
49+
*/
2950
InetSocketAddress bind(final int port) throws IOException;
3051

52+
/**
53+
* Binds the server to a port.
54+
*
55+
* @param port port to bind to
56+
* @param backlog maximum amount of inbound connections at any given time
57+
* @return server address
58+
* @throws IOException internal error
59+
* @throws java.net.BindException if server could not be bounded
60+
*
61+
* @see HttpServer#bind(InetSocketAddress, int)
62+
* @see #bind(int, int)
63+
* @since 5.0.0
64+
*/
3165
InetSocketAddress bind(final int port, final int backlog) throws IOException;
3266

3367
//
3468

69+
/**
70+
* Sets as session handler to use for the server.
71+
*
72+
* @param sessionHandler session handler
73+
*
74+
* @see HttpSessionHandler
75+
* @see #getSessionHandler()
76+
* @since 5.0.0
77+
*/
3578
void setSessionHandler(final HttpSessionHandler sessionHandler);
3679

3780
HttpSessionHandler getSessionHandler();
3881

82+
/**
83+
* Returns the session for a given exchange.
84+
*
85+
* @param exchange http exchange
86+
* @return session associated with an exchange
87+
*
88+
* @see HttpSession
89+
* @since 5.0.0
90+
*/
3991
HttpSession getSession(final HttpExchange exchange);
4092

4193
//
4294

95+
/**
96+
* Returns the handler for a given context.
97+
*
98+
* @param context context
99+
* @return handler for context
100+
*
101+
* @see #getContextHandler(HttpContext)
102+
* @see #getContexts()
103+
* @since 5.0.0
104+
*/
43105
HttpHandler getContextHandler(final String context);
44106

107+
/**
108+
* Returns the handler for a given context.
109+
*
110+
* @param context http context
111+
* @return handler for context
112+
*
113+
* @see #getContextHandler(String)
114+
* @see #getContexts()
115+
* @since 5.0.0
116+
*/
45117
HttpHandler getContextHandler(final HttpContext context);
46118

119+
/**
120+
* Returns a map of all the contexts registered to the server.
121+
* @return map of contexts
122+
*
123+
* @see #getContextHandler(String)
124+
* @see #getContextHandler(HttpContext)
125+
* @since 5.0.0
126+
*/
47127
Map<HttpContext,HttpHandler> getContexts();
48128

129+
/**
130+
* Returns a random context that doesn't yet exist on the server.
131+
*
132+
* @return random unused context
133+
*
134+
* @see #getRandomContext(String)
135+
* @since 5.0.0
136+
*/
49137
String getRandomContext();
50138

139+
/**
140+
* Returns a random context prefixed by a set context.
141+
*
142+
* @param context context to prefix
143+
* @return random unused context with prefix
144+
*
145+
* @see #getRandomContext()
146+
* @since 5.0.0
147+
*/
51148
String getRandomContext(final String context);
52149

53150
//
54151

152+
/**
153+
* Stops the server immediately without waiting.
154+
*
155+
* @see HttpServer#stop(int)
156+
* @since 5.0.0
157+
*/
55158
void stop();
56159

57160
}

src/main/java/dev/katsute/simplehttpserver/HttpSession.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,52 @@
1818

1919
package dev.katsute.simplehttpserver;
2020

21+
/**
22+
* A session keeps track of a single client across multiple exchanges.
23+
*
24+
* @see HttpSessionHandler
25+
* @since 5.0.0
26+
* @version 5.0.0
27+
* @author Katsute
28+
*/
2129
public abstract class HttpSession {
2230

2331
HttpSession(){ }
2432

33+
/**
34+
* Returns the session ID.
35+
*
36+
* @return session ID
37+
*
38+
* @since 5.0.0
39+
*/
2540
public abstract String getSessionID();
2641

42+
/**
43+
* Returns when the session was created as milliseconds since epoch.
44+
*
45+
* @return session creation time
46+
*
47+
* @since 5.0.0
48+
*/
2749
public abstract long getCreationTime();
2850

51+
/**
52+
* Returns when the session was last accessed as milliseconds since epoch.
53+
*
54+
* @return session last accessed time
55+
*
56+
* @see #update()
57+
* @since 5.0.0
58+
*/
2959
public abstract long getLastAccessed();
3060

61+
/**
62+
* Refreshes when the session was last accessed to now.
63+
*
64+
* @see #getLastAccessed()
65+
* @since 5.0.0
66+
*/
3167
public abstract void update();
3268

3369
}

src/main/java/dev/katsute/simplehttpserver/HttpSessionHandler.java

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,48 @@
2424
import java.net.HttpCookie;
2525
import java.util.*;
2626

27+
/**
28+
* The session handler is used to assign sessions to exchanges.
29+
*
30+
* @see HttpSession
31+
* @since 5.0.0
32+
* @version 5.0.0
33+
* @author Katsute
34+
*/
2735
public class HttpSessionHandler {
2836

2937
private final Map<String,HttpSession> sessions = Collections.synchronizedMap(new HashMap<>());
3038

3139
private final String cookie;
3240

41+
/**
42+
* Creates a session handler using the cookie <code>__session-id</code>.
43+
*
44+
* @since 5.0.0
45+
*/
3346
public HttpSessionHandler(){
3447
this("__session-id");
3548
}
3649

50+
/**
51+
* Creates a session handler using a specified cookie.
52+
*
53+
* @param cookie cookie to use for session ID
54+
*
55+
* @since 5.0.0
56+
*/
3757
public HttpSessionHandler(final String cookie){
38-
this.cookie = cookie;
58+
this.cookie = Objects.requireNonNull(cookie);
3959
}
4060

61+
/**
62+
* Assigns a unique session ID to an exchange.
63+
*
64+
* @param exchange http exchange
65+
* @return unique session ID
66+
*
67+
* @since 5.0.0
68+
*/
4169
public synchronized String assignSessionID(final HttpExchange exchange){
4270
String id;
4371
do id = UUID.randomUUID().toString(); // assign session ID
@@ -53,12 +81,20 @@ private String getSetSession(final Headers headers){ // get session that will be
5381
return null;
5482
}
5583

84+
/**
85+
* Returns the session associated with a particular exchange.
86+
*
87+
* @param exchange http exchange
88+
* @return session associated with exchange
89+
*
90+
* @since 5.0.0
91+
*/
5692
public final HttpSession getSession(final HttpExchange exchange){
5793
final String sessionID;
5894
final HttpSession session;
5995

6096
@SuppressWarnings("SpellCheckingInspection")
61-
final String rcookies = exchange.getRequestHeaders().getFirst("Cookie");
97+
final String rcookies = Objects.requireNonNull(exchange).getRequestHeaders().getFirst("Cookie");
6298
final Map<String,String> cookies = new HashMap<>();
6399

64100
if(rcookies != null && !rcookies.isEmpty()){

src/main/java/dev/katsute/simplehttpserver/MultipartFormData.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,18 @@
1818

1919
package dev.katsute.simplehttpserver;
2020

21-
import java.util.HashMap;
22-
import java.util.Map;
21+
import java.util.*;
2322

23+
/**
24+
* Representation of a multipart/form-data body.
25+
*
26+
* @see FileRecord
27+
* @see Record
28+
* @see SimpleHttpExchange#getMultipartFormData()
29+
* @since 5.0.0
30+
* @version 5.0.0
31+
* @author Katsute
32+
*/
2433
public class MultipartFormData {
2534

2635
private final Map<String,Record> data;
@@ -29,10 +38,31 @@ public class MultipartFormData {
2938
this.data = new HashMap<>(data);
3039
}
3140

41+
/**
42+
* Returns the record for a particular key. If the record is supposed to be a file use {@link Record#asFile()}.
43+
*
44+
* @param name record key
45+
* @return record
46+
*
47+
* @see Record
48+
* @see FileRecord
49+
* @see #getEntries()
50+
* @since 5.0.0
51+
*/
3252
public final Record getEntry(final String name){
33-
return data.get(name);
53+
return data.get(Objects.requireNonNull(name));
3454
}
3555

56+
/**
57+
* Returns all the records in the response body.
58+
*
59+
* @return map of all records
60+
*
61+
* @see Record
62+
* @see FileRecord
63+
* @see #getEntry(String)
64+
* @since 5.0.0
65+
*/
3666
public final Map<String,Record> getEntries(){
3767
return new HashMap<>(data);
3868
}

0 commit comments

Comments
 (0)