1 24 package org.riotfamily.common.web.resource; 25 26 import java.io.IOException ; 27 import java.io.InputStreamReader ; 28 import java.io.Reader ; 29 import java.io.Writer ; 30 import java.net.SocketException ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 34 import javax.activation.FileTypeMap ; 35 import javax.servlet.http.HttpServletRequest ; 36 import javax.servlet.http.HttpServletResponse ; 37 38 import org.apache.commons.logging.Log; 39 import org.apache.commons.logging.LogFactory; 40 import org.riotfamily.common.util.FormatUtils; 41 import org.springframework.core.io.Resource; 42 import org.springframework.util.FileCopyUtils; 43 import org.springframework.web.context.support.WebApplicationObjectSupport; 44 import org.springframework.web.servlet.ModelAndView; 45 import org.springframework.web.servlet.mvc.Controller; 46 import org.springframework.web.servlet.mvc.LastModified; 47 48 56 public class ResourceController extends WebApplicationObjectSupport 57 implements Controller, LastModified { 58 59 private static final String HEADER_EXPIRES = "Expires"; 60 61 private Log log = LogFactory.getLog(ResourceController.class); 62 63 private FileTypeMap fileTypeMap = FileTypeMap.getDefaultFileTypeMap(); 64 65 private List mappings; 66 67 private List filters; 68 69 private long expiresAfter = 1000 * 60 * 60 * 24; 70 71 private long lastModified = System.currentTimeMillis(); 72 73 private String pathAttribute; 74 75 public void setExpiresAfter(String s) { 76 this.expiresAfter = FormatUtils.parseMillis(s); 77 } 78 79 82 public void setPathAttribute(String pathAttribute) { 83 this.pathAttribute = pathAttribute; 84 } 85 86 public void setFileTypeMap(FileTypeMap fileTypeMap) { 87 this.fileTypeMap = fileTypeMap; 88 } 89 90 public void setMappings(List resourceMappings) { 91 this.mappings = resourceMappings; 92 } 93 94 public void setFilters(List filters) { 95 this.filters = filters; 96 } 97 98 public long getLastModified(HttpServletRequest request) { 99 return lastModified; 100 } 101 102 public ModelAndView handleRequest(HttpServletRequest request, 103 HttpServletResponse response) throws IOException { 104 105 String path; 106 if (pathAttribute != null) { 107 path = "/" + request.getAttribute(pathAttribute); 108 } 109 else { 110 path = request.getPathInfo(); 111 } 112 log.debug("Looking up resource " + path); 113 Iterator it = mappings.iterator(); 114 while (it.hasNext()) { 115 ResourceMapping mapping = (ResourceMapping) it.next(); 116 Resource res = mapping.getResource(path); 117 if (res != null) { 118 response.addDateHeader(HEADER_EXPIRES, 119 System.currentTimeMillis() + expiresAfter); 120 121 String contentType = fileTypeMap.getContentType( 122 res.getFilename()); 123 124 response.setContentType(contentType); 125 if (contentType.startsWith("text/")) { 126 serveText(path, res, request, response); 127 } 128 else { 129 try { 130 FileCopyUtils.copy(res.getInputStream(), 131 response.getOutputStream()); 132 } 133 catch (IOException e) { 134 if (!SocketException .class.isInstance(e.getCause())) { 135 throw e; 136 } 137 } 138 } 139 return null; 140 } 141 } 142 response.sendError(HttpServletResponse.SC_NOT_FOUND); 143 return null; 144 } 145 146 protected void serveText(String path, Resource res, 147 HttpServletRequest request, HttpServletResponse response) 148 throws IOException { 149 150 log.debug("Serving text resource: " + path); 151 Reader in = new InputStreamReader (res.getInputStream()); 152 Writer out = response.getWriter(); 153 154 if (filters != null) { 155 Iterator it = filters.iterator(); 156 while (it.hasNext()) { 157 ResourceFilter filter = (ResourceFilter) it.next(); 158 if (filter.matches(path)) { 159 log.debug("Filter " + filter + " matches."); 160 in = filter.createFilterReader(in, request); 161 break; 162 } 163 log.debug("Filter " + filter + " does not match."); 164 } 165 } 166 FileCopyUtils.copy(in, out); 167 } 168 169 } 170 | Popular Tags |