Skip to content
This repository has been archived by the owner on Apr 8, 2022. It is now read-only.

Commit

Permalink
event compat with Denizen 1.1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Apr 2, 2020
1 parent 90d8f0e commit 9b238d4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 30 deletions.
4 changes: 0 additions & 4 deletions pom.xml
Expand Up @@ -24,10 +24,6 @@

<!-- Repositories -->
<repositories>
<repository>
<id>bukkit-repo</id>
<url>http://repo.bukkit.org/content/groups/public/</url>
</repository>
<repository>
<id>everything</id>
<url>https://repo.citizensnpcs.co</url>
Expand Down
Expand Up @@ -28,11 +28,16 @@
public abstract class BasicRequestScriptEvent extends ScriptEvent {

public HttpExchange httpExchange;
public ElementTag contentType;
public ElementTag responseText;
public ElementTag responseCode;
public File responseFile;
public ElementTag parseFile;

public class ResponseOptions {
public ElementTag contentType;
public ElementTag responseText;
public ElementTag responseCode;
public File responseFile;
public ElementTag parseFile;
}

public ResponseOptions scriptResponse;

private String requestType = getRequestType();
private String lowerRequestType = CoreUtilities.toLowerCase(requestType);
Expand All @@ -46,11 +51,7 @@ public abstract class BasicRequestScriptEvent extends ScriptEvent {

public void fire(HttpExchange httpExchange) {
this.httpExchange = httpExchange;
this.contentType = null;
this.responseText = null;
this.responseCode = null;
this.responseFile = null;
this.parseFile = null;
scriptResponse = new ResponseOptions();

fire();

Expand All @@ -60,17 +61,17 @@ public void fire(HttpExchange httpExchange) {
reusableScriptEntry.getResidingQueue().setContextSource(this);

// Set HTTP response headers before anything else
response.setContentType(this.contentType != null ? this.contentType.asString() : "text/html");
if (this.responseCode != null) {
response.setStatus(this.responseCode.asInt());
response.setContentType(scriptResponse.contentType != null ? scriptResponse.contentType.asString() : "text/html");
if (scriptResponse.responseCode != null) {
response.setStatus(scriptResponse.responseCode.asInt());
}

if (this.responseText != null || this.responseFile != null) {
if (this.responseText != null) {
response.write(this.responseText.asString().getBytes(StandardCharsets.UTF_8));
if (scriptResponse.responseText != null || scriptResponse.responseFile != null) {
if (scriptResponse.responseText != null) {
response.write(scriptResponse.responseText.asString().getBytes(StandardCharsets.UTF_8));
}
else if (this.parseFile.asBoolean()) {
FileInputStream input = new FileInputStream(this.responseFile);
else if (scriptResponse.parseFile.asBoolean()) {
FileInputStream input = new FileInputStream(scriptResponse.responseFile);
FileChannel channel = input.getChannel();
ByteBuffer bbuf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
StringBuffer s = new StringBuffer();
Expand All @@ -91,7 +92,7 @@ else if (this.parseFile.asBoolean()) {
response.write(s.toString().getBytes(StandardCharsets.UTF_8));
}
else {
response.copyFileFrom(this.responseFile.toPath());
response.copyFileFrom(scriptResponse.responseFile.toPath());
}
}
} catch (IOException e) {
Expand Down Expand Up @@ -128,31 +129,31 @@ public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
Debug.echoError("Determination for 'code' must be a valid number.");
return false;
}
responseCode = code;
scriptResponse.responseCode = code;
}
else if (lower.startsWith("file:")) {
File file = new File(DenizenAPI.getCurrentInstance().getDataFolder(), lower.substring(5));
if (!file.exists()) {
Debug.echoError("File '" + file + "' does not exist.");
return false;
}
responseFile = file;
parseFile = new ElementTag("false");
scriptResponse.responseFile = file;
scriptResponse.parseFile = new ElementTag("false");
}
else if (lower.startsWith("parsed_file:")) {
File file = new File(DenizenAPI.getCurrentInstance().getDataFolder(), lower.substring(12));
if (!file.exists()) {
Debug.echoError("File '" + file + "' does not exist.");
return false;
}
responseFile = file;
parseFile = new ElementTag("true");
scriptResponse.responseFile = file;
scriptResponse.parseFile = new ElementTag("true");
}
else if (lower.startsWith("type:")) {
contentType = new ElementTag(lower.substring(5));
scriptResponse.contentType = new ElementTag(lower.substring(5));
}
else {
responseText = new ElementTag(determinationObj.toString());
scriptResponse.responseText = new ElementTag(determinationObj.toString());
}
return true;
}
Expand Down

0 comments on commit 9b238d4

Please sign in to comment.