Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package base.jee;
 
 import java.util.Date;
 
 
public class ResourceManager {
	public static boolean loadResource(JeeBase jeeHttpServletRequest requestHttpServletResponse responsethrows IOException {
		String requestUrl = request.getRequestURI();
		String resourceName = requestUrl.substring(requestUrl.lastIndexOf("/")+1);
		if(!resourceName.equals("favicon.ico") && !resourceName.equals("robots.txt") && !requestUrl.startsWith("/r/")) {
			return false;
		}
		if(resourceName.endsWith(".js")) {
			response.setContentType("text/javascript");
else if(resourceName.endsWith(".css")) {
			response.setContentType("text/css");
else if(resourceName.endsWith(".gif")) {
			response.setContentType("image/gif");
else if(resourceName.endsWith(".png")) {
			response.setContentType("image/png");
else if(resourceName.endsWith(".eot")) {
			response.setContentType("application/octet-stream");
else if(resourceName.endsWith(".ttf")) {
			response.setContentType("application/octet-stream");
else if(resourceName.endsWith(".woff")) {
			response.setContentType("application/font-woff");
else if(resourceName.endsWith(".ico")) {
			response.setContentType("image/vnd.microsoft.icon");
else if(resourceName.endsWith(".txt")) {
			response.setContentType("text/plain");
else {
			response.setContentType("text/html");
		}
		if(resourceName.startsWith("/r/")) {
			resourceName = resourceName.substring(2);
		}
		Resource resource = jee.getTemplateManager().getCurrentTemplate().getResource(resourceName);
		if(resource == null) {
			response.sendError(404);
			response.getOutputStream().write("Not found.".getBytes());
			return true;
		}
		// Set appropriate cache control settings.
		if(resourceName.endsWith(".eot") || resourceName.endsWith(".ttf") || resourceName.endsWith(".woff")) {
			// font files don't need cookies. Cache for a month
			response.setHeader("Cache-Control""max-age=2592000");
			response.setDateHeader("Expires"new Date().getTime()+(1000*2592000));
else if(resourceName.endsWith(".css") || resourceName.endsWith(".js") || resourceName.endsWith(".png") || resourceName.endsWith(".gif") || resourceName.endsWith(".jpg") || resourceName.endsWith(".ico") || resourceName.endsWith(".pdf")) {
			// css, js, png, gif, and js files don't get cookies
			response.setHeader("Cache-Control""max-age=3600");
			response.setDateHeader("Expires"new Date().getTime()+(1000*3600));
else {
			response.setHeader("Cache-Control""max-age=3600");
			response.setDateHeader("Expires"new Date().getTime()+(1000*3600));
		}
		response.getOutputStream().write(resource.getData());
		return true;
	}
New to GrepCode? Check out our FAQ X