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

Commit be8d868

Browse files
authored
Optimize MODLOAD (removed WATCHLOAD) and Add CACHELOAD. (#107)
* start work on watchload removal * Make file handler more efficient * remove whitespace * Fixed test failures * Fixed test failure * Implement cache #80 * Added test cases Feature is now ready for final review * code cleanup * README fixes * README fixes (again) * Add additional check * Update readme * Optimize when cache is updated * made update thread safe
1 parent 4788a57 commit be8d868

File tree

13 files changed

+503
-267
lines changed

13 files changed

+503
-267
lines changed

.gitignore

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,5 @@
22
.idea
33
out
44
*.iml
5-
# Project ignore
6-
.docs
7-
src/LICENSE.txt
8-
/src/main/java/_ignore/
9-
# Gradle
10-
.gradle
115
# Maven
12-
target
13-
/test/
6+
/target

README.md

Lines changed: 96 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,104 @@
1-
# SimpleHttpServer
2-
[![Deploy](https://github.com/Ktt-Development/simplehttpserver/workflows/Deploy/badge.svg)](https://github.com/orgs/Ktt-Development/packages?repo_name=simplehttpserver)
1+
<p align="center">
2+
<a href="https://github.com/Ktt-Development/simplehttpserver">
3+
<img src="https://raw.githubusercontent.com/Ktt-Development/simplehttpserver/main/branding/Logo.png" alt="Logo" width="100" height="100">
4+
</a>
5+
<h3 align="center">SimpleHttpServer</h3>
6+
<p align="center">
7+
A simplified implementation of the <a href="https://docs.oracle.com/en/java/javase/11/docs/api/jdk.httpserver/com/sun/net/httpserver/package-summary.html">sun http server</a> for JDK11.
8+
<br />
9+
This library simplifies complex operations for both the server, exchange, and handlers.
10+
<br />
11+
<a href="https://docs.kttdevelopment.com/simplehttpserver/">Docs</a>
12+
13+
<a href="https://wiki.kttdevelopment.com/simplehttpserver/">Wiki</a>
14+
15+
<a href="https://github.com/Ktt-Development/simplehttpserver/issues">Issues</a>
16+
</p>
17+
</p>
18+
19+
[![Deploy](https://github.com/Ktt-Development/simplehttpserver/workflows/Deploy/badge.svg)](https://github.com/Ktt-Development/simplehttpserver/actions?query=workflow%3ADeploy)
320
[![Java CI](https://github.com/Ktt-Development/simplehttpserver/workflows/Java%20CI/badge.svg)](https://github.com/Ktt-Development/simplehttpserver/actions?query=workflow%3A%22Java+CI%22)
421
[![Maven Central](https://img.shields.io/maven-central/v/com.kttdevelopment/simplehttpserver)](https://mvnrepository.com/artifact/com.kttdevelopment/simplehttpserver)
522
[![version](https://img.shields.io/github/v/release/ktt-development/simplehttpserver?include_prereleases)](https://github.com/Ktt-Development/simplehttpserver/releases)
623
[![license](https://img.shields.io/github/license/Ktt-Development/simplehttpserver)](https://github.com/Ktt-Development/simplehttpserver/blob/main/LICENSE)
24+
---
25+
26+
27+
# Setup
28+
Compiled binaries can be found on Maven Central.
29+
[![Maven Central](https://img.shields.io/maven-central/v/com.kttdevelopment/simplehttpserver)](https://mvnrepository.com/artifact/com.kttdevelopment/simplehttpserver)
30+
31+
For projects built locally, compiled binaries can also be found in releases.
32+
[![releases](https://img.shields.io/github/v/release/ktt-development/simplehttpserver?include_prereleases")](https://github.com/Ktt-Development/simplehttpserver/releases)
33+
34+
# Features
35+
36+
## 📋 Complicated tasks made easy
37+
38+
Simplified exchange methods for:
39+
- Parsing HTTP `GET`/`POST` with `multipart/form-data` support.
40+
- Output stream writing with `#send`.
41+
- Sending gzip compressed responses.
42+
- Sending files
43+
44+
```java
45+
SimpleHttpHandler handler = new SimpleHttpHandler(){
46+
47+
@Override
48+
public void handle(SimpleHttpExchange exchange){
49+
Map POST = exchange.getPostMap();
50+
51+
exchange.send(new File("OK.png"), HttpCode.HTTP_OK, true);
52+
}
53+
54+
};
55+
```
56+
57+
## ⭐ Extended Features
58+
59+
Out of the box support for:
60+
- HTTP Cookies
61+
- HTTP Sessions
62+
- Multithreaded Servers
63+
64+
```java
65+
SimpleHttpServer server = new SimpleHttpServer(8080);
66+
server.setHttpSessionHandler(new HttpSessionHandler());
67+
68+
HttpHandler handler = new HttpHandler(){
69+
70+
@Override
71+
public void handle(HttpExchange exchange){
72+
HttpSession session = server.getHttpSession(exchange);
73+
String session_id = session.getSessionID();
74+
75+
Map<String,String> cookies = exchange.getCookies();
76+
77+
exchange.close();
78+
}
79+
80+
};
81+
```
82+
83+
## 💻 Simplified Handlers
84+
85+
Easy to use handlers:
86+
- Redirect Handler
87+
- Predicate Handler
88+
- File Handler
89+
- Server-Sent-Events Handler
90+
- Temporary Handler
91+
- Throttled Handler
792

8-
SimpleHttpServer is a simplified implementation of the [sun http server](https://docs.oracle.com/en/java/javase/11/docs/api/jdk.httpserver/com/sun/net/httpserver/package-summary.html) for JDK11. This library simplifies complex operations for both the server, exchange, and handler.
93+
```java
94+
RedirectHandler redirect = new RedirectHandler("https://github.com/");
995

10-
### Setup
11-
- [Setting Up SimpleHttpServer](https://wiki.kttdevelopment.com/simplehttpserver/#setup)
12-
- [Read the Wiki](https://wiki.kttdevelopment.com/simplehttpserver)
96+
FileHandler fileHandler = new FileHandler();
97+
fileHandler.addFile(new File("index.html"));
98+
fileHandler.addDirectory(new File("/site"))
1399

14-
### Documentation
15-
- [Documentation](https://docs.kttdevelopment.com/simplehttpserver)
16-
- [Wiki](https://wiki.kttdevelopment.com/simplehttpserver)
100+
SSEHandler SSE = new SSEHandler();
101+
SSE.push("Server sent events!");
17102

18-
### Reporting Issues
19-
Issues or suggestions can be posted [here](https://github.com/Ktt-Development/simplehttpserver/issues).
103+
ThrottledHandler throttled = new ThrottledHandler(new HttpHandler(), new ServerExchangeThrottler())
104+
```

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
<organizationUrl>https://github.com/Ktt-Development</organizationUrl>
3636
</developer>
3737
</developers>
38-
38+
3939
<scm>
4040
<url>https://github.com/Ktt-Development/simplehttpserver.git</url>
4141
<connection>scm:git:git@github.com:Ktt-Development/simplehttpserver.git</connection>

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,21 @@
44
* Determines how files will be loaded in the {@link FileHandler}. <br>
55
*
66
* <code>PRELOAD</code> - read file when it is added to the handler <br>
7-
* <code>WATCHLOAD</code> - read file when it is added and anytime it is updated <br>
7+
* <code>MODLOAD</code> - read file when it is added and anytime it is updated <br>
8+
* <code>CACHELOAD</code> - load file when requested and clear from memory when maximum time expires <br>
89
* <code>LIVELOAD</code> - read file each time an exchange happens
910
*
1011
* @see FileHandler
1112
* @since 03.05.00
12-
* @version 03.05.01
13+
* @version 4.0.0
1314
* @author Ktt Development
1415
*/
1516
@SuppressWarnings("SpellCheckingInspection")
1617
public enum ByteLoadingOption {
1718

1819
PRELOAD,
19-
WATCHLOAD,
20+
MODLOAD,
21+
CACHELOAD,
2022
LIVELOAD
2123

2224
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.kttdevelopment.simplehttpserver.handler;
2+
3+
import java.util.concurrent.TimeUnit;
4+
import java.util.concurrent.atomic.AtomicLong;
5+
6+
/**
7+
* This class caches file bytes when adding to the {@link FileHandler}.
8+
*
9+
* @see FileHandlerAdapter
10+
* @see FileHandler
11+
* @since 4.0.0
12+
* @version 4.0.0
13+
* @author Ktt Development
14+
*/
15+
public class CacheFileAdapter implements FileHandlerAdapter {
16+
17+
private final long cacheTimeMillis;
18+
private final AtomicLong closestExpiry = new AtomicLong(0);
19+
20+
/**
21+
* Creates a CacheFileAdapter where files will expire after set milliseconds.
22+
*
23+
* @param cacheTimeMillis how long a file should exist for
24+
*
25+
* @since 4.0.0
26+
* @author Ktt Development
27+
*/
28+
public CacheFileAdapter(final long cacheTimeMillis){
29+
this.cacheTimeMillis = cacheTimeMillis;
30+
}
31+
32+
/**
33+
* Creates a CacheFileAdapter where files will expire after a set time.
34+
*
35+
* @param cacheTime how long a file should exist for
36+
* @param timeUnit the time unit
37+
*
38+
* @see TimeUnit
39+
* @since 4.0.0
40+
* @author Ktt Development
41+
*/
42+
public CacheFileAdapter(final long cacheTime, final TimeUnit timeUnit){
43+
cacheTimeMillis = timeUnit.toMillis(cacheTime);
44+
}
45+
46+
/**
47+
* Returns how long files should be cached for.
48+
*
49+
* @return file cache time
50+
*
51+
* @since 4.0.0
52+
* @author Ktt Development
53+
*/
54+
final long getCacheTimeMillis(){
55+
return cacheTimeMillis;
56+
}
57+
58+
/**
59+
* Returns the closest expiry.
60+
*
61+
* @return closest expiry
62+
*
63+
* @see #updateClosestExpiry(long)
64+
* @since 4.0.0
65+
* @author Ktt Development
66+
*/
67+
final long getClosestExpiry(){
68+
return closestExpiry.get();
69+
}
70+
71+
/**
72+
* Sets the closest expiry if it is less than the current expiry
73+
*
74+
* @param expiry newest expiry
75+
*
76+
* @see #getClosestExpiry()
77+
* @since 4.0.0
78+
* @author Ktt Development
79+
*/
80+
synchronized final void updateClosestExpiry(final long expiry){
81+
final long was = closestExpiry.get();
82+
if(expiry < was || was < System.currentTimeMillis()) // update expiry if new is lower or if expiry has lapsed
83+
closestExpiry.set(expiry);
84+
}
85+
86+
@Override
87+
public String toString(){
88+
return
89+
"CacheFileAdapter" + '{' +
90+
"cacheTimeMillis" + '=' + cacheTimeMillis +
91+
'}';
92+
}
93+
94+
}

0 commit comments

Comments
 (0)