1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| package org.apache.xindice.webadmin.webdav; |
19 |
| |
20 |
| import org.apache.commons.logging.Log; |
21 |
| import org.apache.commons.logging.LogFactory; |
22 |
| |
23 |
| import org.w3c.dom.Document; |
24 |
| import org.xml.sax.InputSource; |
25 |
| |
26 |
| import javax.servlet.ServletInputStream; |
27 |
| import javax.servlet.http.HttpServletRequest; |
28 |
| import javax.xml.parsers.DocumentBuilder; |
29 |
| import javax.xml.parsers.DocumentBuilderFactory; |
30 |
| |
31 |
| import java.io.IOException; |
32 |
| import java.io.UnsupportedEncodingException; |
33 |
| import java.net.URLDecoder; |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| public class DAVRequest { |
41 |
| public static final int DEPTH_0 = 0; |
42 |
| public static final int DEPTH_1 = 1; |
43 |
| public static final int DEPTH_INFINITY = -1; |
44 |
| |
45 |
| private static final Log log = LogFactory.getLog(DAVRequest.class); |
46 |
| |
47 |
| private HttpServletRequest httpRequest; |
48 |
| private String contextPath; |
49 |
| private Document requestDoc; |
50 |
| private boolean documentInit; |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| |
57 |
36
| public DAVRequest(HttpServletRequest request) {
|
58 |
36
| httpRequest = request;
|
59 |
36
| contextPath = makeContextPath();
|
60 |
| } |
61 |
| |
62 |
| |
63 |
| |
64 |
| |
65 |
36
| private String makeContextPath() {
|
66 |
36
| StringBuffer path = new StringBuffer();
|
67 |
36
| path.append(httpRequest.getContextPath());
|
68 |
| |
69 |
36
| if (httpRequest.getPathInfo() != null) {
|
70 |
0
| path.append(httpRequest.getServletPath());
|
71 |
| } |
72 |
36
| if (path.length() > 0 && path.charAt(path.length() - 1) == '/') {
|
73 |
0
| path.deleteCharAt(path.length() - 1);
|
74 |
| } |
75 |
| |
76 |
36
| return path.toString();
|
77 |
| } |
78 |
| |
79 |
| |
80 |
| |
81 |
| |
82 |
| |
83 |
| |
84 |
| |
85 |
| |
86 |
7
| public Document getRequestDoc() throws IOException {
|
87 |
7
| if (!documentInit) {
|
88 |
7
| documentInit = true;
|
89 |
7
| requestDoc = parseRequestContent();
|
90 |
| } |
91 |
| |
92 |
7
| return requestDoc;
|
93 |
| } |
94 |
| |
95 |
| |
96 |
| |
97 |
| |
98 |
| |
99 |
| |
100 |
| |
101 |
| |
102 |
| |
103 |
| |
104 |
| |
105 |
| |
106 |
| |
107 |
19
| public String getPath() {
|
108 |
19
| String path = httpRequest.getPathInfo();
|
109 |
19
| if (path == null) {
|
110 |
9
| path = httpRequest.getServletPath();
|
111 |
| } |
112 |
19
| return path;
|
113 |
| } |
114 |
| |
115 |
| |
116 |
| |
117 |
| |
118 |
| |
119 |
| |
120 |
0
| public String getHeader(String name) {
|
121 |
0
| return httpRequest.getHeader(name);
|
122 |
| } |
123 |
| |
124 |
| |
125 |
| |
126 |
| |
127 |
| |
128 |
| |
129 |
| |
130 |
11
| public boolean getOverwrite() {
|
131 |
| |
132 |
11
| boolean overwrite = true;
|
133 |
11
| String overwriteHeader = httpRequest.getHeader("Overwrite");
|
134 |
11
| if (overwriteHeader != null) {
|
135 |
2
| overwrite = !overwriteHeader.equalsIgnoreCase("F");
|
136 |
| } |
137 |
11
| return overwrite;
|
138 |
| } |
139 |
| |
140 |
| |
141 |
| |
142 |
| |
143 |
| |
144 |
| |
145 |
| |
146 |
16
| public int getDepth() {
|
147 |
16
| String value = httpRequest.getHeader("Depth");
|
148 |
16
| int depth = -1;
|
149 |
16
| if ("0".equals(value)) {
|
150 |
3
| depth = 0;
|
151 |
13
| } else if ("1".equals(value)) {
|
152 |
1
| depth = 1;
|
153 |
12
| } else if (value == null || "infinity".equals(value)) {
|
154 |
12
| depth = -1;
|
155 |
| } |
156 |
| |
157 |
16
| return depth;
|
158 |
| } |
159 |
| |
160 |
| |
161 |
| |
162 |
| |
163 |
| |
164 |
| |
165 |
| |
166 |
| |
167 |
24
| public String getDestinationPath() {
|
168 |
24
| String destinationPath = httpRequest.getHeader("Destination");
|
169 |
24
| if (destinationPath == null) {
|
170 |
4
| return null;
|
171 |
| } |
172 |
| |
173 |
20
| int protocolIndex = destinationPath.indexOf("://");
|
174 |
20
| if (protocolIndex >= 0) {
|
175 |
| |
176 |
| |
177 |
0
| int firstSeparator = destinationPath.indexOf("/", protocolIndex + 4);
|
178 |
0
| if (firstSeparator < 0) {
|
179 |
0
| destinationPath = "/";
|
180 |
| } else { |
181 |
0
| destinationPath = destinationPath.substring(firstSeparator);
|
182 |
| } |
183 |
| } else { |
184 |
20
| String hostName = httpRequest.getServerName();
|
185 |
20
| if ((hostName != null) && (destinationPath.startsWith(hostName))) {
|
186 |
0
| destinationPath = destinationPath.substring(hostName.length());
|
187 |
| } |
188 |
| |
189 |
20
| int portIndex = destinationPath.indexOf(":");
|
190 |
20
| if (portIndex >= 0) {
|
191 |
0
| destinationPath = destinationPath.substring(portIndex);
|
192 |
| } |
193 |
| |
194 |
20
| if (destinationPath.startsWith(":")) {
|
195 |
0
| int firstSeparator = destinationPath.indexOf("/");
|
196 |
0
| if (firstSeparator < 0) {
|
197 |
0
| destinationPath = "/";
|
198 |
| } else { |
199 |
0
| destinationPath = destinationPath.substring(firstSeparator);
|
200 |
| } |
201 |
| } |
202 |
| } |
203 |
| |
204 |
20
| String contextPath = httpRequest.getContextPath();
|
205 |
20
| if ((contextPath != null) && (destinationPath.startsWith(contextPath))) {
|
206 |
20
| destinationPath = destinationPath.substring(contextPath.length());
|
207 |
| } |
208 |
| |
209 |
20
| String pathInfo = httpRequest.getPathInfo();
|
210 |
20
| if (pathInfo != null) {
|
211 |
7
| String servletPath = httpRequest.getServletPath();
|
212 |
7
| if ((servletPath != null) && (destinationPath.startsWith(servletPath))) {
|
213 |
7
| destinationPath = destinationPath.substring(servletPath.length());
|
214 |
| } |
215 |
| } |
216 |
| |
217 |
20
| try {
|
218 |
20
| destinationPath = URLDecoder.decode(destinationPath, "utf-8");
|
219 |
| } catch (UnsupportedEncodingException e) { |
220 |
| |
221 |
0
| log.error(e);
|
222 |
0
| destinationPath = null;
|
223 |
| } |
224 |
| |
225 |
20
| return destinationPath;
|
226 |
| } |
227 |
| |
228 |
| |
229 |
| |
230 |
| |
231 |
| |
232 |
5
| public String getContextPath() {
|
233 |
5
| return contextPath;
|
234 |
| } |
235 |
| |
236 |
7
| private Document parseRequestContent() throws IOException {
|
237 |
7
| if (httpRequest.getContentLength() != 0) {
|
238 |
5
| DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
239 |
5
| dbf.setNamespaceAware(true);
|
240 |
5
| try {
|
241 |
5
| DocumentBuilder documentBuilder = dbf.newDocumentBuilder();
|
242 |
5
| return documentBuilder.parse(new InputSource(httpRequest.getInputStream()));
|
243 |
| } catch (IOException e) { |
244 |
0
| throw e;
|
245 |
| } catch (Exception e) { |
246 |
0
| if (log.isWarnEnabled()) {
|
247 |
0
| log.warn("Cannot parse request document", e);
|
248 |
| } |
249 |
| } |
250 |
| } |
251 |
| |
252 |
2
| return null;
|
253 |
| } |
254 |
| |
255 |
| |
256 |
| |
257 |
| |
258 |
| |
259 |
4
| public ServletInputStream getInputStream() throws IOException {
|
260 |
4
| return httpRequest.getInputStream();
|
261 |
| } |
262 |
| |
263 |
| |
264 |
| |
265 |
| |
266 |
| |
267 |
| |
268 |
| |
269 |
0
| public String getParameter(String name) {
|
270 |
0
| return httpRequest.getParameter(name);
|
271 |
| } |
272 |
| |
273 |
| |
274 |
| |
275 |
| |
276 |
| |
277 |
| |
278 |
5
| public String getProtocol() {
|
279 |
5
| return httpRequest.getProtocol();
|
280 |
| } |
281 |
| } |