|
1 | | -package com.ithit.webdav.samples.fsstorageservlet; |
2 | | - |
3 | | -import com.ithit.webdav.server.Folder; |
4 | | -import com.ithit.webdav.server.HierarchyItem; |
5 | | -import com.ithit.webdav.server.MethodHandler; |
6 | | -import com.ithit.webdav.server.exceptions.DavException; |
7 | | -import com.ithit.webdav.server.http.DavRequest; |
8 | | -import com.ithit.webdav.server.http.DavResponse; |
9 | | -import org.apache.commons.io.FileUtils; |
10 | | - |
11 | | -import java.io.IOException; |
12 | | -import java.io.PrintStream; |
13 | | -import java.nio.charset.Charset; |
14 | | -import java.nio.charset.StandardCharsets; |
15 | | -import java.nio.file.Files; |
16 | | -import java.nio.file.Path; |
17 | | -import java.nio.file.Paths; |
18 | | - |
19 | | -/** |
20 | | - * This handler processes GET requests to folders returning custom HTML page. |
21 | | - */ |
22 | | -public class CustomFolderGetHandler implements MethodHandler { |
23 | | - |
24 | | - private MethodHandler previousHandler; |
25 | | - private String charset; |
26 | | - private String version; |
27 | | - private String pathToHTML = "WEB-INF/MyCustomHandlerPage.html"; |
28 | | - private String pathToErrorHTML = "WEB-INF/attributesErrorPage.html"; |
29 | | - |
30 | | - public CustomFolderGetHandler(String charset, String version) { |
31 | | - this.charset = charset; |
32 | | - this.version = version; |
33 | | - } |
34 | | - |
35 | | - @Override |
36 | | - public void processRequest(DavRequest request, DavResponse response, HierarchyItem item) |
37 | | - throws DavException, IOException { |
38 | | - if (item instanceof Folder) { |
39 | | - PrintStream stream = new PrintStream(response.getOutputStream(), true, charset); |
40 | | - response.setCharacterEncoding(charset); |
41 | | - response.setContentType("text/html"); |
42 | | - if (!WebDavServlet.isSupportsUserDefinedAttributes()) { |
43 | | - Path path = Paths.get(WebDavServlet.getRealPath(), pathToErrorHTML); |
44 | | - String lines = FileUtils.readFileToString(path.toFile(), Charset.defaultCharset()); |
45 | | - stream.println(lines); |
46 | | - } else { |
47 | | - Path path = Paths.get(WebDavServlet.getRealPath(), pathToHTML); |
48 | | - String context = WebDavServlet.getContext() + "/"; |
49 | | - String wsContext = context.replaceFirst("/", ""); |
50 | | - int ind = wsContext.lastIndexOf("/"); |
51 | | - if (ind >= 0) { |
52 | | - wsContext = new StringBuilder(wsContext).replace(ind, ind + 1, "\\/").toString(); |
53 | | - } |
54 | | - for (String line : Files.readAllLines(path, StandardCharsets.UTF_8)) { |
55 | | - String contextRootString = "<%context root%>"; |
56 | | - if (line.contains(contextRootString)) { |
57 | | - line = line.replace(contextRootString, context); |
58 | | - } |
59 | | - String versionNumber = "<%version%>"; |
60 | | - if (line.contains(versionNumber)) { |
61 | | - line = line.replace(versionNumber, version); |
62 | | - } |
63 | | - String ws = "<%ws root%>"; |
64 | | - if (line.contains(ws)) { |
65 | | - line = line.replace(ws, wsContext); |
66 | | - } |
67 | | - String version = "<%startTime%>"; |
68 | | - if (line.contains(version)) { |
69 | | - line = line.replace(version, WebDavServlet.START_TIME); |
70 | | - } |
71 | | - stream.println(line); |
72 | | - } |
73 | | - } |
74 | | - stream.flush(); |
75 | | - } else { |
76 | | - previousHandler.processRequest(request, response, item); |
77 | | - } |
78 | | - } |
79 | | - |
80 | | - /** |
81 | | - * Determines whether request body shall be logged. |
82 | | - * |
83 | | - * @return {@code true} if request body shall be logged. |
84 | | - */ |
85 | | - public boolean getLogInput() { |
86 | | - return false; |
87 | | - } |
88 | | - |
89 | | - /** |
90 | | - * Determines whether response body shall be logged. |
91 | | - * |
92 | | - * @return {@code true} if response body shall be logged. |
93 | | - */ |
94 | | - public boolean getLogOutput() { |
95 | | - return false; |
96 | | - } |
97 | | - |
98 | | - /** |
99 | | - * Determines whether response content length shall be calculated by engine. |
100 | | - * |
101 | | - * @return {@code true} if content length shall be calculated by engine. |
102 | | - */ |
103 | | - public boolean getCalculateContentLength() { |
104 | | - return false; |
105 | | - } |
106 | | - |
107 | | - /** |
108 | | - * Set previous handler fo GET operation. |
109 | | - * |
110 | | - * @param methodHandler previous handler. |
111 | | - */ |
112 | | - void setPreviousHandler(MethodHandler methodHandler) { |
113 | | - previousHandler = methodHandler; |
114 | | - } |
115 | | -} |
| 1 | +package com.ithit.webdav.samples.fsstorageservlet; |
| 2 | + |
| 3 | +import com.ithit.webdav.server.Folder; |
| 4 | +import com.ithit.webdav.server.HierarchyItem; |
| 5 | +import com.ithit.webdav.server.MethodHandler; |
| 6 | +import com.ithit.webdav.server.exceptions.DavException; |
| 7 | +import com.ithit.webdav.server.http.DavRequest; |
| 8 | +import com.ithit.webdav.server.http.DavResponse; |
| 9 | +import org.apache.commons.io.FileUtils; |
| 10 | + |
| 11 | +import java.io.IOException; |
| 12 | +import java.io.PrintStream; |
| 13 | +import java.nio.charset.Charset; |
| 14 | +import java.nio.charset.StandardCharsets; |
| 15 | +import java.nio.file.Files; |
| 16 | +import java.nio.file.Path; |
| 17 | +import java.nio.file.Paths; |
| 18 | + |
| 19 | +/** |
| 20 | + * This handler processes GET requests to folders returning custom HTML page. |
| 21 | + */ |
| 22 | +public class CustomFolderGetHandler implements MethodHandler { |
| 23 | + |
| 24 | + private MethodHandler previousHandler; |
| 25 | + private String charset; |
| 26 | + private String version; |
| 27 | + private String pathToHTML = "WEB-INF/MyCustomHandlerPage.html"; |
| 28 | + private String pathToErrorHTML = "WEB-INF/attributesErrorPage.html"; |
| 29 | + |
| 30 | + public CustomFolderGetHandler(String charset, String version) { |
| 31 | + this.charset = charset; |
| 32 | + this.version = version; |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public void processRequest(DavRequest request, DavResponse response, HierarchyItem item) |
| 37 | + throws DavException, IOException { |
| 38 | + if (item instanceof Folder) { |
| 39 | + PrintStream stream = new PrintStream(response.getOutputStream(), true, charset); |
| 40 | + response.setCharacterEncoding(charset); |
| 41 | + response.setContentType("text/html"); |
| 42 | + if (!WebDavServlet.isSupportsUserDefinedAttributes()) { |
| 43 | + Path path = Paths.get(WebDavServlet.getRealPath(), pathToErrorHTML); |
| 44 | + String lines = FileUtils.readFileToString(path.toFile(), Charset.defaultCharset()); |
| 45 | + stream.println(lines); |
| 46 | + } else { |
| 47 | + Path path = Paths.get(WebDavServlet.getRealPath(), pathToHTML); |
| 48 | + String context = WebDavServlet.getContext() + "/"; |
| 49 | + String wsContext = context.replaceFirst("/", ""); |
| 50 | + int ind = wsContext.lastIndexOf("/"); |
| 51 | + if (ind >= 0) { |
| 52 | + wsContext = new StringBuilder(wsContext).replace(ind, ind + 1, "\\/").toString(); |
| 53 | + } |
| 54 | + for (String line : Files.readAllLines(path, StandardCharsets.UTF_8)) { |
| 55 | + String contextRootString = "<%context root%>"; |
| 56 | + if (line.contains(contextRootString)) { |
| 57 | + line = line.replace(contextRootString, context); |
| 58 | + } |
| 59 | + String versionNumber = "<%version%>"; |
| 60 | + if (line.contains(versionNumber)) { |
| 61 | + line = line.replace(versionNumber, version); |
| 62 | + } |
| 63 | + String ws = "<%ws root%>"; |
| 64 | + if (line.contains(ws)) { |
| 65 | + line = line.replace(ws, wsContext); |
| 66 | + } |
| 67 | + String version = "<%startTime%>"; |
| 68 | + if (line.contains(version)) { |
| 69 | + line = line.replace(version, WebDavServlet.START_TIME); |
| 70 | + } |
| 71 | + stream.println(line); |
| 72 | + } |
| 73 | + } |
| 74 | + stream.flush(); |
| 75 | + } else { |
| 76 | + previousHandler.processRequest(request, response, item); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Determines whether request body shall be logged. |
| 82 | + * |
| 83 | + * @return {@code true} if request body shall be logged. |
| 84 | + */ |
| 85 | + public boolean getLogInput() { |
| 86 | + return false; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Determines whether response body shall be logged. |
| 91 | + * |
| 92 | + * @return {@code true} if response body shall be logged. |
| 93 | + */ |
| 94 | + public boolean getLogOutput() { |
| 95 | + return false; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Determines whether response content length shall be calculated by engine. |
| 100 | + * |
| 101 | + * @return {@code true} if content length shall be calculated by engine. |
| 102 | + */ |
| 103 | + public boolean getCalculateContentLength() { |
| 104 | + return false; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Set previous handler fo GET operation. |
| 109 | + * |
| 110 | + * @param methodHandler previous handler. |
| 111 | + */ |
| 112 | + void setPreviousHandler(MethodHandler methodHandler) { |
| 113 | + previousHandler = methodHandler; |
| 114 | + } |
| 115 | +} |
0 commit comments