Skip to content

Commit eaba2f7

Browse files
committed
examples SourceViewer show java sources and files from resources/static
1 parent 285cae1 commit eaba2f7

File tree

1 file changed

+29
-47
lines changed

1 file changed

+29
-47
lines changed

examples/src/main/java/org/apache/click/examples/page/SourceViewer.java

Lines changed: 29 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
import java.io.Serial;
1616
import java.nio.charset.StandardCharsets;
1717

18-
import static org.apache.click.util.ClickUtils.trim;
19-
2018
/**
2119
* Provides a Java source code, HTML and XML examples rendering page.
2220
*
@@ -56,89 +54,73 @@ public class SourceViewer extends BorderPage {
5654
"#macro(", "#foreach", "#foreach(", "##", "#*", "*#", "#" };
5755

5856
private boolean isJava = false;
59-
6057
private boolean isXml = false;
61-
6258
private boolean isHtml = false;
6359

64-
/**
65-
* @see Page#onGet()
66-
*/
67-
@Override public void onGet (){
60+
/** @see Page#onGet() */
61+
@Override
62+
public void onGet (){
6863
HttpServletRequest request = getContext().getRequest();
6964

7065
String filename = request.getParameter("filename");
7166

72-
if (filename != null) {
73-
loadFilename(filename.replace('\\','/'));
74-
75-
getModel().put("title", "Source Viewer : " + filename); // ?
67+
if (StringUtils.isNotBlank(filename)){
68+
loadFilename(filename.trim().replace('\\','/'));
69+
getModel().put("title", "Source Viewer : "+ filename); // ?
7670
} else {
7771
addModel("error", "filename not defined");
7872
}
7973
}
8074

81-
private void loadFilename (String filename){
75+
private void loadFilename (String fileName){
8276
ServletContext context = getContext().getServletContext();
8377

84-
// Orion server requires '/' prefix to find resources
85-
String resourceFilename = filename.charAt(0) != '/' ? "/" + filename : filename;
78+
if (fileName.charAt(0) != '/'){
79+
fileName = '/'+ fileName;// Orion server requires '/' prefix to find resources
80+
}
8681

8782
InputStream in = null;
8883
try {
89-
//1. web root
90-
in = context.getResourceAsStream(resourceFilename);
84+
//1. web root /
85+
in = context.getResourceAsStream(fileName);
9186

9287
//2. web root, but .htm→.jsp?
93-
if (in == null && filename.endsWith(".htm")){
94-
resourceFilename = resourceFilename.substring(0, resourceFilename.length()-4) +".jsp";
95-
96-
in = context.getResourceAsStream(resourceFilename);
88+
if (in == null && fileName.endsWith(".htm")){
89+
in = context.getResourceAsStream(fileName.substring(0, fileName.length()-4) +".jsp");
9790
}
91+
9892
//3. in classpath?
9993
if (in == null){// && filename.endsWith(".java")
100-
if (filename.startsWith("/")){ filename = filename.substring(1);}// without / in ClassPath
101-
in = ClickUtils.getResourceAsStream(filename, getClass());
94+
fileName = fileName.substring(1);// cut leading / for ClassPath
95+
in = ClickUtils.getResourceAsStream(fileName, getClass());
10296
}
10397

10498
if (in == null){// WEB-INF/classes/net/sf/click/examples/page/SourceViewer.java
105-
if (filename.startsWith("WEB-INF/classes/")){
106-
filename = filename.substring(16);
107-
} else if (filename.startsWith("/WEB-INF/classes/")){
108-
filename = filename.substring(17);
99+
//4. in class-path without WEB-INF/classes prefix?
100+
if (fileName.startsWith("WEB-INF/classes/")){
101+
fileName = fileName.substring(16);
102+
in = ClickUtils.getResourceAsStream(fileName, getClass());
109103
}
110-
//4. in class-path without WEB-INF/classes prefix?
111-
in = ClickUtils.getResourceAsStream(filename, getClass());
104+
if (in == null){
105+
in = ClickUtils.getResourceAsStream("static/"+fileName, getClass());// resources/static are served by Spring from ClassPath, not by Tomcat
106+
}
112107

113-
//5. in subproject - in file system?
108+
//6. in subproject - in file system?
114109
if (in == null){// ok. There are no sources... Maybe we are still under gradle?
115110
try {
116-
in = new FileInputStream("./src/main/java/"+filename);
111+
in = new FileInputStream("./src/main/java/"+fileName);
117112
} catch (IOException ignore){}// just best-effort last attempt..
118-
119-
//6. in root project - in file system?
120-
if (in == null){// gradle + project root? ~ catalina.home = C:\opt\github\click\examples\build\tmp\tomcatRunWar
121-
String catalina = trim(System.getProperty("catalina.base")).replace('\\','/');
122-
int i = catalina.indexOf("/build/");
123-
if (i>0){
124-
try {
125-
in = new FileInputStream(catalina.substring(0,i)+"/src/main/java/"+filename);
126-
} catch (IOException ignore){}// just best-effort last attempt..
127-
}
128-
}
129113
}
130114
}
131115

132116
if (in != null){
133-
loadResource(in, filename);
134-
117+
loadResource(in, fileName);
135118
} else {
136-
addModel("error", "File " + filename + " not found");
119+
addModel("error", "File "+ fileName +" not found");
137120
}
138121

139122
} catch (IOException e){
140-
addModel("error", "Could not read "+ filename);
141-
123+
addModel("error", "Could not read "+ fileName+" with error "+e);
142124
} finally {
143125
ClickUtils.close(in);
144126
}

0 commit comments

Comments
 (0)