KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > netboot > TrivialDavFilter


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.netboot;
23
24 import java.io.IOException JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import java.text.SimpleDateFormat JavaDoc;
27 import java.util.Date JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.TimeZone JavaDoc;
31 import javax.servlet.Filter JavaDoc;
32 import javax.servlet.FilterChain JavaDoc;
33 import javax.servlet.FilterConfig JavaDoc;
34 import javax.servlet.ServletContext JavaDoc;
35 import javax.servlet.ServletException JavaDoc;
36 import javax.servlet.ServletRequest JavaDoc;
37 import javax.servlet.ServletResponse JavaDoc;
38 import javax.servlet.http.HttpServletRequest JavaDoc;
39 import javax.servlet.http.HttpServletResponse JavaDoc;
40
41 /**
42  * <em>Very</em> simple filter to support WebDAV access to resources.
43  * Only intended to support the DavURLLister.
44  */

45 public class TrivialDavFilter implements Filter JavaDoc {
46    private ServletContext JavaDoc context;
47    private SimpleDateFormat JavaDoc sharedFormat;
48
49    public void init(FilterConfig JavaDoc filterConfig) throws ServletException JavaDoc {
50       context = filterConfig.getServletContext();
51       sharedFormat = new SimpleDateFormat JavaDoc("EEE, d MMM yyyy HH:mm:ss z", java.util.Locale.US);
52       sharedFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
53    }
54
55    public void doFilter(ServletRequest JavaDoc request, ServletResponse JavaDoc response, FilterChain JavaDoc chain) throws IOException JavaDoc, ServletException JavaDoc {
56       if (request instanceof HttpServletRequest JavaDoc == false) {
57          chain.doFilter(request, response);
58          return;
59       }
60
61
62       HttpServletRequest JavaDoc hrequest = (HttpServletRequest JavaDoc) request;
63       String JavaDoc method = hrequest.getMethod();
64       if ("PROPFIND".equals(method)) {
65          doPropfind(hrequest, (HttpServletResponse JavaDoc) response);
66       } else {
67          chain.doFilter(request, response);
68       }
69    }
70
71    public void destroy() {
72       sharedFormat = null;
73       context = null;
74    }
75
76    private void doPropfind(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc {
77       String JavaDoc contextPath = request.getContextPath();
78
79       // get the path from the request URI - Jetty does not return a valid
80
// servlet path if this filter is mapped to '/'
81
String JavaDoc path = request.getRequestURI().substring(contextPath.length());
82
83       // keep WEB-INF hidden
84
// also check for missing resources (Jetty does not report this below)
85
if (path.startsWith("/WEB-INF") || context.getResource(path) == null) {
86          response.sendError(HttpServletResponse.SC_NOT_FOUND);
87          return;
88       }
89
90       Set JavaDoc resourcePaths = context.getResourcePaths(path);
91       if (resourcePaths == null) {
92          // not found -- should not happen due to check above
93
// not all containers seem to return null here
94
response.sendError(HttpServletResponse.SC_NOT_FOUND);
95          return;
96       }
97
98       // SimpleDateFormat is not thread-safe, so clone here.
99
SimpleDateFormat JavaDoc dateFormat = (SimpleDateFormat JavaDoc) sharedFormat.clone();
100
101       response.setStatus(207); // DAV MultiStatus Response
102
PrintWriter JavaDoc out = response.getWriter();
103       out.println("<?xml version='1.0' encoding='utf-8' ?>");
104       out.println("<D:multistatus xmlns:D='DAV:'>");
105
106       if (resourcePaths.isEmpty()) {
107          // not a collection - do the resource itself
108
writeResponse(out, contextPath, path, dateFormat);
109       } else {
110          for (Iterator JavaDoc i = resourcePaths.iterator(); i.hasNext();) {
111             String JavaDoc s = (String JavaDoc) i.next();
112             writeResponse(out, contextPath, s, dateFormat);
113          }
114       }
115       out.println("</D:multistatus>");
116       out.flush();
117       response.flushBuffer();
118    }
119
120    private void writeResponse(PrintWriter JavaDoc out, String JavaDoc contextPath, String JavaDoc path, SimpleDateFormat JavaDoc dateFormat) throws IOException JavaDoc {
121       Date JavaDoc lastModified = new Date JavaDoc(context.getResource(path).openConnection().getLastModified());
122       out.println("<D:response>");
123       out.println("<D:href>" + contextPath + path + "</D:href>");
124       out.println("<D:propstat>");
125       out.println("<D:prop>");
126       if (path.endsWith("/")) {
127          out.println("<D:resourcetype><D:collection/></D:resourcetype>");
128       } else {
129          out.println("<D:resourcetype/>");
130       }
131       out.println("<D:getlastmodified>" + dateFormat.format(lastModified) + "</D:getlastmodified>");
132       out.println("</D:prop>");
133       out.println("<D:status>HTTP/1.1 200 OK</D:status>");
134       out.println("</D:propstat>");
135       out.println("</D:response>");
136    }
137 }
138
Popular Tags