Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
package base.jee;
import java.io.IOException;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import base.template.Resource;
import static base.jee.servlet.BaseServlet.getSite;
public class ResourceManager {
public static boolean loadResource(JeeBase jee, HttpServletRequest request, HttpServletResponse response) throws 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;
}
// Now we know were doing a template look up, find the site domain
String site = getSite(request);
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(site).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;
}
}