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

Commit 38b6abf

Browse files
authored
Fix #69 optional, fix remove context issue (#70)
Co-authored-by: Katsute <[email protected]>
1 parent c1cf2b5 commit 38b6abf

File tree

2 files changed

+52
-8
lines changed

2 files changed

+52
-8
lines changed

src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpServerImpl.java

+11-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
* @see SimpleHttpServer
1515
* @since 02.00.00
16-
* @version 03.05.00
16+
* @version 03.05.02
1717
* @author Ktt Development
1818
*/
1919
@SuppressWarnings("SpellCheckingInspection")
@@ -168,11 +168,16 @@ public synchronized final HttpContext createContext(final String path, final Htt
168168

169169
@Override
170170
public synchronized final void removeContext(final String path){
171-
server.removeContext(getContext(path));
172-
for(final HttpContext context : contexts.keySet()){
173-
if(context.getPath().equalsIgnoreCase(getContext(path))){
174-
contexts.remove(context);
175-
break;
171+
try{
172+
server.removeContext(getContext(path));
173+
}catch(final IllegalArgumentException e){
174+
throw e;
175+
}finally{
176+
for(final HttpContext context : contexts.keySet()){
177+
if(context.getPath().equalsIgnoreCase(getContext(path))){
178+
contexts.remove(context);
179+
break;
180+
}
176181
}
177182
}
178183
}

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

+41-2
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@
88

99
/**
1010
* 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.
11+
* For time based expiry the handler will only remove itself if a user connects to the handler after the time expires.
1112
*
1213
* @see HttpHandler
1314
* @see SimpleHttpServer#getRandomContext()
1415
* @see SimpleHttpServer#getRandomContext(String)
1516
* @since 03.03.00
16-
* @version 03.05.00
17+
* @version 03.05.02
1718
* @author Ktt Development
1819
*/
1920
public class TemporaryHandler implements HttpHandler {
2021

22+
private SimpleHttpServer server = null;
2123
private final HttpHandler handler;
2224

2325
private boolean hasExpiry;
@@ -30,23 +32,57 @@ public class TemporaryHandler implements HttpHandler {
3032
*
3133
* @param handler handler to use
3234
*
35+
* @see HttpHandler
3336
* @since 03.03.00
3437
* @author Ktt Development
3538
*/
3639
public TemporaryHandler(final HttpHandler handler){
3740
this.handler = handler;
3841
}
3942

43+
/**
44+
* Creates a temporary handler that removes itself after the first connection.
45+
*
46+
* @param server simple http server; required if you want {@link SimpleHttpServer#getContexts()} to work properly
47+
* @param handler handler to use
48+
*
49+
* @see SimpleHttpServer
50+
* @see HttpHandler
51+
* @since 03.05.02
52+
*/
53+
public TemporaryHandler(final SimpleHttpServer server, final HttpHandler handler){
54+
this.server = server;
55+
this.handler = handler;
56+
}
57+
4058
/**
4159
* Creates a temporary handler that removes itself after the first connection, or after the time expires.
4260
*
4361
* @param handler handler to use
4462
* @param maxTime how long the handler may exists for in milliseconds
4563
*
64+
* @see HttpHandler
4665
* @since 03.03.00
4766
* @author Ktt Development
4867
*/
4968
public TemporaryHandler(final HttpHandler handler, final long maxTime){
69+
this(null,handler,maxTime);
70+
}
71+
72+
/**
73+
* Creates a temporary handler that removes itself after the first connection, or after the time expires.
74+
*
75+
* @param server simple http server; required if you want {@link SimpleHttpServer#getContexts()} to work properly
76+
* @param handler handler to use
77+
* @param maxTime how long the handler may exists for in milliseconds
78+
*
79+
* @see SimpleHttpServer
80+
* @see HttpHandler
81+
* @since 03.05.02
82+
* @author Ktt Development
83+
*/
84+
public TemporaryHandler(final SimpleHttpServer server, final HttpHandler handler, final long maxTime){
85+
this.server = server;
5086
this.handler = handler;
5187

5288
hasExpiry = true;
@@ -59,7 +95,10 @@ public TemporaryHandler(final HttpHandler handler, final long maxTime){
5995
public final void handle(final HttpExchange exchange) throws IOException{
6096
if(!hasExpiry || System.currentTimeMillis() < expiry)
6197
handler.handle(exchange);
62-
exchange.getHttpContext().getServer().removeContext(exchange.getHttpContext());
98+
if(server == null)
99+
exchange.getHttpContext().getServer().removeContext(exchange.getHttpContext());
100+
else
101+
server.removeContext(exchange.getHttpContext());
63102
exchange.close();
64103
}
65104

0 commit comments

Comments
 (0)