package com.loomis.jsf.servlet;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.loomis.jsf.component.resources.Resources;
public class ResourceServlet extends HttpServlet {
public static final String RESOURCE_REQUEST_KEY = "resourcePath";
private static final String ASSUMED_DIR_PREFIX = Resources.RESOURCE_DIR;
private static Map<String, String> resourceMap;
public static String generateResourceURL(String resourcePath) {
return "/resources?" + RESOURCE_REQUEST_KEY + "=" + resourcePath;
}
private synchronized void loadResource(String resourcePath) {
InputStream is = getClass().getResourceAsStream(ASSUMED_DIR_PREFIX + resourcePath);
if (is == null) {
return;
}
BufferedReader br = new BufferedReader(new InputStreamReader(is));
try {
String line = null;
StringBuffer resourceBuf = new StringBuffer();
while ((line = br.readLine()) != null) {
resourceBuf.append(line + "\n");
}
getResourceMap().put(resourcePath, resourceBuf.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void doWork(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String resourcePath = request.getParameter(RESOURCE_REQUEST_KEY);
if (!getResourceMap().containsKey(resourcePath)) {
loadResource(resourcePath);
}
String resource = getResourceMap().get(resourcePath);
if (resource == null) {
return;
}
PrintWriter out = null;
response.setContentType("text/plain;charset=UTF-8");
out = response.getWriter();
out.print(resource);
if (out != null) {
out.close();
}
}
private static Map<String, String> getResourceMap() {
if (resourceMap == null) {
resourceMap = new HashMap<String, String>();
}
return resourceMap;
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doWork(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doWork(request, response);
}
}