|
15 | 15 | import java.io.Serial; |
16 | 16 | import java.nio.charset.StandardCharsets; |
17 | 17 |
|
18 | | -import static org.apache.click.util.ClickUtils.trim; |
19 | | - |
20 | 18 | /** |
21 | 19 | * Provides a Java source code, HTML and XML examples rendering page. |
22 | 20 | * |
@@ -56,89 +54,73 @@ public class SourceViewer extends BorderPage { |
56 | 54 | "#macro(", "#foreach", "#foreach(", "##", "#*", "*#", "#" }; |
57 | 55 |
|
58 | 56 | private boolean isJava = false; |
59 | | - |
60 | 57 | private boolean isXml = false; |
61 | | - |
62 | 58 | private boolean isHtml = false; |
63 | 59 |
|
64 | | - /** |
65 | | - * @see Page#onGet() |
66 | | - */ |
67 | | - @Override public void onGet (){ |
| 60 | + /** @see Page#onGet() */ |
| 61 | + @Override |
| 62 | + public void onGet (){ |
68 | 63 | HttpServletRequest request = getContext().getRequest(); |
69 | 64 |
|
70 | 65 | String filename = request.getParameter("filename"); |
71 | 66 |
|
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); // ? |
76 | 70 | } else { |
77 | 71 | addModel("error", "filename not defined"); |
78 | 72 | } |
79 | 73 | } |
80 | 74 |
|
81 | | - private void loadFilename (String filename){ |
| 75 | + private void loadFilename (String fileName){ |
82 | 76 | ServletContext context = getContext().getServletContext(); |
83 | 77 |
|
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 | + } |
86 | 81 |
|
87 | 82 | InputStream in = null; |
88 | 83 | try { |
89 | | - //1. web root |
90 | | - in = context.getResourceAsStream(resourceFilename); |
| 84 | + //1. web root / |
| 85 | + in = context.getResourceAsStream(fileName); |
91 | 86 |
|
92 | 87 | //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"); |
97 | 90 | } |
| 91 | + |
98 | 92 | //3. in classpath? |
99 | 93 | 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()); |
102 | 96 | } |
103 | 97 |
|
104 | 98 | 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()); |
109 | 103 | } |
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 | + } |
112 | 107 |
|
113 | | - //5. in subproject - in file system? |
| 108 | + //6. in subproject - in file system? |
114 | 109 | if (in == null){// ok. There are no sources... Maybe we are still under gradle? |
115 | 110 | try { |
116 | | - in = new FileInputStream("./src/main/java/"+filename); |
| 111 | + in = new FileInputStream("./src/main/java/"+fileName); |
117 | 112 | } 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 | | - } |
129 | 113 | } |
130 | 114 | } |
131 | 115 |
|
132 | 116 | if (in != null){ |
133 | | - loadResource(in, filename); |
134 | | - |
| 117 | + loadResource(in, fileName); |
135 | 118 | } else { |
136 | | - addModel("error", "File " + filename + " not found"); |
| 119 | + addModel("error", "File "+ fileName +" not found"); |
137 | 120 | } |
138 | 121 |
|
139 | 122 | } catch (IOException e){ |
140 | | - addModel("error", "Could not read "+ filename); |
141 | | - |
| 123 | + addModel("error", "Could not read "+ fileName+" with error "+e); |
142 | 124 | } finally { |
143 | 125 | ClickUtils.close(in); |
144 | 126 | } |
|
0 commit comments