Skip to content

Commit

Permalink
f serverhelper findfiles, addroutes
Browse files Browse the repository at this point in the history
  • Loading branch information
xabolcs committed Feb 14, 2025
1 parent 78d8dc6 commit 77e663a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import javax.annotation.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import play.ClasspathResource;
import play.Play;
import play.template2.GTFileResolver;
import play.template2.GTTemplateLocationReal;
import play.templates.TemplateLoader;

public class GTFileResolver1xImpl implements GTFileResolver.Resolver {
private static final Logger logger = LoggerFactory.getLogger(GTFileResolver1xImpl.class);
private final List<File> templateFolders;

public GTFileResolver1xImpl(List<File> templatesPaths) {
Expand Down Expand Up @@ -87,9 +91,11 @@ public GTTemplateLocationReal getTemplateLocationFromRelativePath(String relativ
URL url = null;
if (vf == null) {
try {
final String classloadedPrefix = "/" + Play.tmpDir.getName() + "/" + TemplateLoader.CLASSPATH_LOADED_TEMPLATE_TMP_PATH_PREFIX;
final String classloadedPrefix = "/" + (Play.tmpDir == null ? "" : Play.tmpDir.getName() + "/") + TemplateLoader.CLASSPATH_LOADED_TEMPLATE_TMP_PATH_PREFIX;
if (relativePath.startsWith(classloadedPrefix)) {
relativePath = relativePath.split(classloadedPrefix, 2) [1];
} else {
logger.error("appRoot: {}, relpath: {}, prefix: {}", Play.appRoot.getName(), relativePath, classloadedPrefix);
}
ClasspathResource cf = null;
// do the search as the TemplateLoader.loadTemplateFromClasspath()'s usage:
Expand All @@ -101,6 +107,7 @@ public GTTemplateLocationReal getTemplateLocationFromRelativePath(String relativ
}
url = cf.url();
} catch (Exception e) {
logger.trace("Ignoring exception while resolving {}", relativePath, e);
return null;
}
}
Expand Down
3 changes: 2 additions & 1 deletion framework/src/play/mvc/Router.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -71,7 +72,7 @@ private static void loadRoutesFromFile() {
// This automatically added route is needed to serve the static files extracted from classpath.
// The "public" postfix (!) on the end is to prevent unwanted leakage of classpath resource files!
// TODO: automatically expose routes (e.g. with "staticDir:" parsing or with application.conf settings from dependencies instead of hardcoded "public"
addRoute("GET", "/public/", "staticDir:" + Play.tmpDir.getName() + "/" + SERVER_HELPER_FIND_FILE_TMP_PATH_PREFIX + "public");
addRoute("GET", "/public/", "staticDir:" + Optional.ofNullable(Play.tmpDir).orElse(new File("")).getName() + "/" + SERVER_HELPER_FIND_FILE_TMP_PATH_PREFIX + "public");
lastLoading = System.currentTimeMillis();
}

Expand Down
4 changes: 2 additions & 2 deletions framework/src/play/server/ServerHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;
import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;
Expand Down Expand Up @@ -125,8 +126,7 @@ public static File findFile(String resource) {
}
}
if (file == null) {
final String pr = SERVER_HELPER_FIND_FILE_TMP_PATH_PREFIX + resource;
file = new File(Play.tmpDir, SERVER_HELPER_FIND_FILE_TMP_PATH_PREFIX + resource);
file = new File(Optional.ofNullable(Play.tmpDir).orElse(Play.appRoot), SERVER_HELPER_FIND_FILE_TMP_PATH_PREFIX + resource);
if (file.exists())
return file;
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
application.app.included=app.conf included!
%test.play.tmp=none
play.tmp=none

0 comments on commit 77e663a

Please sign in to comment.