Skip to content
This repository was archived by the owner on Jul 31, 2022. It is now read-only.

Commit 2ad0342

Browse files
committed
Documentation + optimization
1 parent 0a02094 commit 2ad0342

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

src/main/java/com/kttdevelopment/simplehttpserver/handler/TemporaryHandler.java

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,72 @@
55

66
import java.io.IOException;
77

8+
/**
9+
* A temporary handler handles a single request and then removes itself from the server. This can be used for single use downloads, or media file hosting.
10+
*
11+
* @see HttpHandler
12+
* @since 03.03.00
13+
* @version 03.03.00
14+
* @author Ktt Development
15+
*/
816
public class TemporaryHandler implements HttpHandler {
917

1018
private final HttpHandler handler;
1119

1220
private boolean hasExpiry;
1321
private long initTime;
1422
private long maxTime;
23+
private long expiry;
1524

25+
/**
26+
* Creates a temporary handler that removes itself after the first connection.
27+
*
28+
* @param handler handler to use
29+
*
30+
* @since 03.03.00
31+
* @author Ktt Development
32+
*/
1633
public TemporaryHandler(final HttpHandler handler){
1734
this.handler = handler;
1835
}
1936

37+
/**
38+
* Creates a temporary handler that removes itself after the first connection, or after the time expires.
39+
*
40+
* @param handler handler to use
41+
* @param maxTime how long the handler may exists for in milliseconds
42+
*
43+
* @since 03.03.00
44+
* @author Ktt Development
45+
*/
2046
public TemporaryHandler(final HttpHandler handler, final long maxTime){
2147
this.handler = handler;
2248

2349
hasExpiry = true;
2450
initTime = System.currentTimeMillis();
2551
this.maxTime = maxTime;
52+
expiry = initTime + maxTime;
2653
}
2754

2855
@Override
2956
public final void handle(final HttpExchange exchange) throws IOException{
30-
if(!hasExpiry || maxTime + initTime < System.currentTimeMillis())
57+
if(!hasExpiry || expiry < System.currentTimeMillis())
3158
handler.handle(exchange);
3259
exchange.getHttpContext().getServer().removeContext(exchange.getHttpContext());
3360
}
3461

62+
@Override
63+
public String toString(){
64+
final StringBuilder OUT = new StringBuilder();
65+
OUT.append("TemporaryHandler") .append('{');
66+
OUT.append("handler") .append('=') .append(handler.toString()) .append(',');
67+
if(hasExpiry){
68+
OUT.append("initTime") .append('=') .append(initTime) .append(',');
69+
OUT.append("maxTime") .append('=') .append(maxTime) .append(',');
70+
OUT.append("expiry") .append('=') .append(expiry);
71+
}
72+
OUT.append('}');
73+
return OUT.toString();
74+
}
75+
3576
}

0 commit comments

Comments
 (0)