1 22 package org.jboss.netboot; 23 24 import java.io.IOException ; 25 import java.io.PrintWriter ; 26 import java.text.SimpleDateFormat ; 27 import java.util.Date ; 28 import java.util.Iterator ; 29 import java.util.Set ; 30 import java.util.TimeZone ; 31 import javax.servlet.Filter ; 32 import javax.servlet.FilterChain ; 33 import javax.servlet.FilterConfig ; 34 import javax.servlet.ServletContext ; 35 import javax.servlet.ServletException ; 36 import javax.servlet.ServletRequest ; 37 import javax.servlet.ServletResponse ; 38 import javax.servlet.http.HttpServletRequest ; 39 import javax.servlet.http.HttpServletResponse ; 40 41 45 public class TrivialDavFilter implements Filter { 46 private ServletContext context; 47 private SimpleDateFormat sharedFormat; 48 49 public void init(FilterConfig filterConfig) throws ServletException { 50 context = filterConfig.getServletContext(); 51 sharedFormat = new SimpleDateFormat ("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 request, ServletResponse response, FilterChain chain) throws IOException , ServletException { 56 if (request instanceof HttpServletRequest == false) { 57 chain.doFilter(request, response); 58 return; 59 } 60 61 62 HttpServletRequest hrequest = (HttpServletRequest ) request; 63 String method = hrequest.getMethod(); 64 if ("PROPFIND".equals(method)) { 65 doPropfind(hrequest, (HttpServletResponse ) 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 request, HttpServletResponse response) throws IOException { 77 String contextPath = request.getContextPath(); 78 79 String path = request.getRequestURI().substring(contextPath.length()); 82 83 if (path.startsWith("/WEB-INF") || context.getResource(path) == null) { 86 response.sendError(HttpServletResponse.SC_NOT_FOUND); 87 return; 88 } 89 90 Set resourcePaths = context.getResourcePaths(path); 91 if (resourcePaths == null) { 92 response.sendError(HttpServletResponse.SC_NOT_FOUND); 95 return; 96 } 97 98 SimpleDateFormat dateFormat = (SimpleDateFormat ) sharedFormat.clone(); 100 101 response.setStatus(207); PrintWriter 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 writeResponse(out, contextPath, path, dateFormat); 109 } else { 110 for (Iterator i = resourcePaths.iterator(); i.hasNext();) { 111 String s = (String ) 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 out, String contextPath, String path, SimpleDateFormat dateFormat) throws IOException { 121 Date lastModified = new Date (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 |