Skip to content

Commit

Permalink
Re-added .asResponse() to all tags
Browse files Browse the repository at this point in the history
  • Loading branch information
LatvianModder committed Oct 15, 2023
1 parent 84b1dd7 commit 6eb9214
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 26 deletions.
19 changes: 0 additions & 19 deletions src/main/java/dev/latvian/apps/webutils/html/ResponseTag.java

This file was deleted.

9 changes: 5 additions & 4 deletions src/main/java/dev/latvian/apps/webutils/html/RootTag.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package dev.latvian.apps.webutils.html;

import dev.latvian.apps.webutils.net.MimeType;
import dev.latvian.apps.webutils.net.Response;
import io.javalin.http.HttpStatus;

import java.util.List;

public abstract class RootTag extends PairedTag implements ResponseTag {
public abstract class RootTag extends PairedTag {
public final String path;
public final String title;
public final String description;
Expand Down Expand Up @@ -90,8 +91,8 @@ public void append(StringBuilder builder, boolean header) {
}

@Override
public String getMimeType() {
return MimeType.HTML;
public Response asResponse() {
return asResponse(HttpStatus.OK, true);
}

public abstract String getSiteName();
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/dev/latvian/apps/webutils/html/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

import dev.latvian.apps.webutils.ansi.Ansi;
import dev.latvian.apps.webutils.ansi.AnsiComponent;
import dev.latvian.apps.webutils.net.FileResponse;
import dev.latvian.apps.webutils.net.MimeType;
import dev.latvian.apps.webutils.net.Response;
import io.javalin.http.HttpStatus;
import org.jetbrains.annotations.Nullable;

import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.function.BiConsumer;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -126,6 +131,14 @@ default boolean isEmptyRecursively() {
default void replace(Pattern pattern, BiConsumer<Tag, Matcher> replace) {
}

default Response asResponse() {
return asResponse(HttpStatus.OK, false);
}

default Response asResponse(HttpStatus status, boolean header) {
return FileResponse.of(status, MimeType.HTML, toTagString(header).getBytes(StandardCharsets.UTF_8));
}

default Tag string(Object string) {
var str = String.valueOf(string);
return str.isEmpty() ? this : add(new StringTag(str));
Expand Down
16 changes: 13 additions & 3 deletions src/main/java/dev/latvian/apps/webutils/html/XMLTag.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package dev.latvian.apps.webutils.html;

import dev.latvian.apps.webutils.net.FileResponse;
import dev.latvian.apps.webutils.net.MimeType;
import dev.latvian.apps.webutils.net.Response;
import io.javalin.http.HttpStatus;

public class XMLTag extends PairedTag implements ResponseTag {
import java.nio.charset.StandardCharsets;

public class XMLTag extends PairedTag {
public XMLTag(String tag) {
super(tag);
}
Expand All @@ -22,7 +27,12 @@ protected XMLTag copy0() {
}

@Override
public String getMimeType() {
return MimeType.XML_TEXT;
public Response asResponse() {
return asResponse(HttpStatus.OK, true);
}

@Override
public Response asResponse(HttpStatus status, boolean header) {
return FileResponse.of(status, MimeType.XML_TEXT, toTagString(header).getBytes(StandardCharsets.UTF_8));
}
}
1 change: 1 addition & 0 deletions src/main/java/dev/latvian/apps/webutils/net/MimeType.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ public interface MimeType {
String XML = "application/xml; charset=utf-8";
String XML_TEXT = "text/xml; charset=utf-8";
String SVG = "image/svg+xml; charset=utf-8";
String CSS = "text/css; charset=utf-8";
}

0 comments on commit 6eb9214

Please sign in to comment.