Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
[![javadoc](https://javadoc.io/badge2/io.avaje/avaje-jex/javadoc.svg?color=purple)](https://javadoc.io/doc/io.avaje/avaje-jex)

# Avaje-Jex
Lightweight (~120KB) wrapper over the JDK's built-in [HTTP server](https://docs.oracle.com/en/java/javase/23/docs/api/jdk.httpserver/module-summary.html).
Lightweight (~100KB) wrapper over the JDK's built-in [HTTP server](https://docs.oracle.com/en/java/javase/23/docs/api/jdk.httpserver/module-summary.html).

Features:

Expand All @@ -30,11 +30,6 @@ var app = Jex.create()
System.out.println("after request");
})
.error(IllegalStateException.class, (ctx, exception) -> ctx.status(500).text("Handled IllegalStateException|" + exception.getMessage()))
.staticResource(
b ->
b.httpPath("/myResource")
.resource("/public")
.directoryIndex("index.html"))
.port(8080)
.start();
```
Expand Down
6 changes: 3 additions & 3 deletions avaje-jex-freemarker/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<artifactId>avaje-jex-parent</artifactId>
<groupId>io.avaje</groupId>
<version>3.0-RC7</version>
<version>3.0-RC8</version>
</parent>

<artifactId>avaje-jex-freemarker</artifactId>
Expand All @@ -18,7 +18,7 @@
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-jex</artifactId>
<version>3.0-RC7</version>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>

Expand All @@ -39,7 +39,7 @@
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-jex-test</artifactId>
<version>3.0-RC7</version>
<version>${project.version}</version>
<scope>test</scope>
</dependency>

Expand Down
4 changes: 2 additions & 2 deletions avaje-jex-htmx/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>io.avaje</groupId>
<artifactId>avaje-jex-parent</artifactId>
<version>3.0-RC7</version>
<version>3.0-RC8</version>
</parent>

<artifactId>avaje-jex-htmx</artifactId>
Expand All @@ -24,7 +24,7 @@
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-jex</artifactId>
<version>3.0-RC7</version>
<version>${project.version}</version>
</dependency>
</dependencies>

Expand Down
6 changes: 3 additions & 3 deletions avaje-jex-mustache/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<artifactId>avaje-jex-parent</artifactId>
<groupId>io.avaje</groupId>
<version>3.0-RC7</version>
<version>3.0-RC8</version>
</parent>

<artifactId>avaje-jex-mustache</artifactId>
Expand All @@ -18,7 +18,7 @@
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-jex</artifactId>
<version>3.0-RC7</version>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>

Expand All @@ -40,7 +40,7 @@
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-jex-test</artifactId>
<version>3.0-RC7</version>
<version>${project.version}</version>
<scope>test</scope>
</dependency>

Expand Down
27 changes: 27 additions & 0 deletions avaje-jex-static-content/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.avaje</groupId>
<artifactId>avaje-jex-parent</artifactId>
<version>3.0-RC8</version>
</parent>
<artifactId>avaje-jex-static-content</artifactId>
<dependencies>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-jex</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-jex-test</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.avaje.jex;
package io.avaje.jex.staticcontent;

import java.net.FileNameMap;
import java.net.URLConnection;
Expand All @@ -8,6 +8,8 @@

import com.sun.net.httpserver.HttpExchange;

import io.avaje.jex.Context;
import io.avaje.jex.ExchangeHandler;
import io.avaje.jex.http.BadRequestException;
import io.avaje.jex.http.NotFoundException;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.avaje.jex;
package io.avaje.jex.staticcontent;

import java.io.InputStream;
import java.net.URL;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.avaje.jex;
package io.avaje.jex.staticcontent;

import java.io.InputStream;
import java.net.URL;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package io.avaje.jex;
package io.avaje.jex.staticcontent;

import java.net.URL;
import java.nio.file.Paths;
import java.util.Map;
import java.util.function.Predicate;

final class StaticClassResourceHandler extends AbstractStaticHandler implements ExchangeHandler {
import io.avaje.jex.Context;

final class StaticClassResourceHandler extends AbstractStaticHandler {

private final URL indexFile;
private final URL singleFile;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,49 +1,56 @@
package io.avaje.jex;
package io.avaje.jex.staticcontent;

import java.net.URLConnection;
import java.util.function.Predicate;

import io.avaje.jex.Context;
import io.avaje.jex.Routing.HttpService;

/** Builder for a static resource exchange handler. */
public sealed interface StaticContentConfig permits StaticResourceHandlerBuilder {
public sealed interface StaticContentService extends HttpService
permits StaticResourceHandlerBuilder {

/** Create and return a new static content configuration. */
static StaticContentConfig create() {
return StaticResourceHandlerBuilder.builder();
/**
* Create and return a new static content configuration.
*
* @param resourceRoot The file to serve, or the directory the files are located in.
*/
static StaticContentService createCP(String resourceRoot) {
return StaticResourceHandlerBuilder.builder(resourceRoot);
}

/** Return a new ExchangeHandler that will serve the resources */
ExchangeHandler createHandler();

/**
* Sets the HTTP path for the static resource handler.
*
* @param path the HTTP path prefix
* @return the updated configuration
* Create and return a new static class path content configuration with the `/public` directory as
* the root.
*/
StaticContentConfig httpPath(String path);
static StaticContentService createCP() {
return StaticResourceHandlerBuilder.builder("/public/");
}

/**
* Gets the current HTTP path.
* Create and return a new static content configuration for a File.
*
* @return the current HTTP path
* @param resourceRoot The path of the file to serve, or the directory the files are located in.
*/
String httpPath();
static StaticContentService createFile(String resourceRoot) {
return StaticResourceHandlerBuilder.builder(resourceRoot).file();
}

/**
* Sets the file to serve, or the folder your files are located in. (default: "/public/")
* Sets the HTTP path for the static resource handler.
*
* @param resource the root directory
* @param path the HTTP path prefix
* @return the updated configuration
*/
StaticContentConfig resource(String resource);
StaticContentService httpPath(String path);

/**
* Sets the index file to be served when a directory is requests.
*
* @param directoryIndex the index file
* @return the updated configuration
*/
StaticContentConfig directoryIndex(String directoryIndex);
StaticContentService directoryIndex(String directoryIndex);

/**
* Sets a custom resource loader for loading class/module path resources. This is normally used
Expand All @@ -54,7 +61,7 @@ static StaticContentConfig create() {
* @param resourceLoader the custom resource loader
* @return the updated configuration
*/
StaticContentConfig resourceLoader(ClassResourceLoader resourceLoader);
StaticContentService resourceLoader(ClassResourceLoader resourceLoader);

/**
* Adds a new MIME type mapping to the configuration. (Default: uses {@link
Expand All @@ -65,7 +72,7 @@ static StaticContentConfig create() {
* "application/javascript")
* @return the updated configuration
*/
StaticContentConfig putMimeTypeMapping(String ext, String mimeType);
StaticContentService putMimeTypeMapping(String ext, String mimeType);

/**
* Adds a new response header to the configuration.
Expand All @@ -74,27 +81,13 @@ static StaticContentConfig create() {
* @param value the header value
* @return the updated configuration
*/
StaticContentConfig putResponseHeader(String key, String value);
StaticContentService putResponseHeader(String key, String value);

/**
* Sets a predicate to filter files based on the request context.
*
* @param skipFilePredicate the predicate to use
* @return the updated configuration
*/
StaticContentConfig skipFilePredicate(Predicate<Context> skipFilePredicate);

/**
* Sets the resource location (CLASSPATH or FILE).
*
* @param location the resource location
* @return the updated configuration
*/
StaticContentConfig location(ResourceLocation location);

/** Resource location */
enum ResourceLocation {
CLASS_PATH,
FILE
}
StaticContentService skipFilePredicate(Predicate<Context> skipFilePredicate);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.avaje.jex;
package io.avaje.jex.staticcontent;

import java.io.File;
import java.io.FileInputStream;
Expand All @@ -9,7 +9,9 @@

import com.sun.net.httpserver.HttpExchange;

final class StaticFileHandler extends AbstractStaticHandler implements ExchangeHandler {
import io.avaje.jex.Context;

final class StaticFileHandler extends AbstractStaticHandler {

private final File indexFile;
private final File singleFile;
Expand Down
Loading
Loading