Skip to content

Commit

Permalink
COMMANDBOX-1629
Browse files Browse the repository at this point in the history
  • Loading branch information
bdw429s committed Jul 14, 2024
1 parent 06bab10 commit ba383f4
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 23 deletions.
2 changes: 1 addition & 1 deletion gradle/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.0.3-34ef669222d1c38da6ad8791b8a79d66ba05a79c-96ea216b7cfb6883a6af15c191a94953a0596b9c
5.0.4-06bab10df910f6e01ed290108762a532f44f3f7e-96ea216b7cfb6883a6af15c191a94953a0596b9c
27 changes: 22 additions & 5 deletions src/main/java/runwar/undertow/predicate/IsDirectoryPredicate.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@

package runwar.undertow.predicate;

import io.undertow.UndertowLogger;
import io.undertow.attribute.ExchangeAttribute;
import io.undertow.attribute.ExchangeAttributes;
import io.undertow.predicate.Predicate;
import io.undertow.predicate.PredicateBuilder;
import io.undertow.server.HttpServerExchange;
import io.undertow.server.handlers.resource.Resource;
import io.undertow.server.handlers.resource.ResourceManager;
import io.undertow.UndertowLogger;

import java.io.IOException;
import java.util.Collections;
Expand All @@ -37,12 +39,19 @@

/**
* Predicate that returns true if the given location corresponds to a directory.
* This is a copy of the servlet version, but uses a resource manager attached to the exchange
* This is a copy of the servlet version, but uses a resource manager attached
* to the exchange
*
* @author Stuart Douglas, Brad Wood
*/
public class IsDirectoryPredicate implements Predicate {

private static final boolean traceEnabled;

static {
traceEnabled = UndertowLogger.PREDICATE_LOGGER.isTraceEnabled();
}

private final ExchangeAttribute location;

public IsDirectoryPredicate(final ExchangeAttribute location) {
Expand All @@ -54,15 +63,23 @@ public boolean resolve(final HttpServerExchange exchange) {
String location = this.location.readAttribute(exchange);

SiteDeployment deployment = exchange.getAttachment(SiteDeploymentManager.SITE_DEPLOYMENT_KEY);
if(deployment == null) {
throw new RuntimeException( "is-directory predicate could not access the site deployment on this exchange" );
if (deployment == null) {
throw new RuntimeException("is-directory predicate could not access the site deployment on this exchange");
}
ResourceManager manager = deployment.getResourceManager();
try {
Resource resource = manager.getResource(location);
if(resource == null) {
if (resource == null) {
if (traceEnabled) {
UndertowLogger.PREDICATE_LOGGER.tracef(
"is-directory check of [%s] returned [null], so directory does not exist.", location);
}
return false;
}
if (traceEnabled) {
UndertowLogger.PREDICATE_LOGGER.tracef("is-directory check of [%s] returned %s.", location,
resource.isDirectory());
}
return resource.isDirectory();
} catch (IOException e) {
throw new RuntimeException(e);
Expand Down Expand Up @@ -101,7 +118,7 @@ public String defaultParameter() {
@Override
public Predicate build(final Map<String, Object> config) {
ExchangeAttribute value = (ExchangeAttribute) config.get("value");
if(value == null) {
if (value == null) {
value = ExchangeAttributes.relativePath();
}
return new IsDirectoryPredicate(value);
Expand Down
83 changes: 66 additions & 17 deletions src/main/java/runwar/undertow/predicate/IsFilePredicate.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package runwar.undertow.predicate;

import io.undertow.UndertowLogger;
import io.undertow.attribute.ExchangeAttribute;
import io.undertow.attribute.ExchangeAttributes;
import io.undertow.predicate.Predicate;
Expand All @@ -37,12 +38,19 @@
import runwar.undertow.SiteDeploymentManager;

/**
* Predicate that returns true if the given location corresponds to a regular file.
* Predicate that returns true if the given location corresponds to a regular
* file.
*
* @author Stuart Douglas
*/
public class IsFilePredicate implements Predicate {

private static final boolean traceEnabled;

static {
traceEnabled = UndertowLogger.PREDICATE_LOGGER.isTraceEnabled();
}

private final ExchangeAttribute location;
private final boolean requireContent;

Expand All @@ -60,28 +68,69 @@ public boolean resolve(final HttpServerExchange exchange) {
String location = this.location.readAttribute(exchange);

SiteDeployment deployment = exchange.getAttachment(SiteDeploymentManager.SITE_DEPLOYMENT_KEY);
if(deployment == null) {
throw new RuntimeException( "is-file predicate could not access the site deployment on this exchange" );
if (deployment == null) {
throw new RuntimeException("is-file predicate could not access the site deployment on this exchange");
}
ResourceManager manager = deployment.getResourceManager();
try {
Resource resource = manager.getResource(location);
if(resource == null) {
return false;
}
if(resource.isDirectory()) {
return false;
}
if(requireContent){
return resource.getContentLength() != null && resource.getContentLength() > 0;
} else {
return true;
}
return resolveInternal(exchange, manager, location);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private void logTrace(String format, Object... args) {
if (traceEnabled) {
UndertowLogger.PREDICATE_LOGGER.tracef(format, args);
}
}

/**
* internal recursive method to resolve the file. If not found, we strip path
* segments until we find a file or run out of path
* This allows us to ignore path info which the servlet will strip off such as
* foo/index.cfm/bar/baz
*
* @param exchange The exchange
* @param manager The resource manager
* @param location The location
* @return true if the location is a file
* @throws IOException
*/
private boolean resolveInternal(HttpServerExchange exchange, ResourceManager manager, String location)
throws IOException {

logTrace("is-file checking path [%s] ...", location);
Resource resource = manager.getResource(location);
if (resource == null) {
// /a/b is min for 2 segments
if (location.length() > 3) {
// remove any trailing slash
if (location.endsWith("/")) {
location = location.substring(0, location.length() - 1);
}
int lastSlashIndex = location.lastIndexOf('/');
if (lastSlashIndex > 0) { // More than one character in the path, not counting a leading slash
return resolveInternal(exchange, manager, location.substring(0, lastSlashIndex));
}
}
logTrace("is-file check of [%s] returned [null], so file does not exist.", location);
return false;
}
if (resource.isDirectory()) {
logTrace("is-file check of [%s] returned false because path is a directory.", location);
return false;
}
if (requireContent) {
boolean result = resource.getContentLength() != null && resource.getContentLength() > 0;
logTrace("is-file check of [%s] and content length > 0 check returned %s.", location, result);
return result;
} else {
logTrace("is-file check of [%s] returned true.", location);
return true;
}
}

@Override
public String toString() {
return "is-file( " + location.toString() + " )";
Expand Down Expand Up @@ -115,8 +164,8 @@ public String defaultParameter() {
@Override
public Predicate build(final Map<String, Object> config) {
ExchangeAttribute value = (ExchangeAttribute) config.get("value");
Boolean requireContent = (Boolean)config.get("require-content");
if(value == null) {
Boolean requireContent = (Boolean) config.get("require-content");
if (value == null) {
value = ExchangeAttributes.relativePath();
}
return new IsFilePredicate(value, requireContent == null ? false : requireContent);
Expand Down

0 comments on commit ba383f4

Please sign in to comment.