1 19 20 package com.sslexplorer.core; 21 22 import java.io.ByteArrayInputStream ; 23 import java.io.ByteArrayOutputStream ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 27 import javax.servlet.ServletException ; 28 import javax.servlet.http.HttpServlet ; 29 import javax.servlet.http.HttpServletRequest ; 30 import javax.servlet.http.HttpServletResponse ; 31 32 import org.apache.commons.logging.Log; 33 import org.apache.commons.logging.LogFactory; 34 import org.apache.struts.action.ActionForm; 35 import org.apache.struts.action.ActionMapping; 36 37 import com.sslexplorer.boot.ContextHolder; 38 import com.sslexplorer.boot.Util; 39 import com.sslexplorer.security.SessionInfo; 40 41 50 public class MessageResourceLoaderServlet extends HttpServlet { 51 final static Log log = LogFactory.getLog(MessageResourceLoaderServlet.class); 52 53 public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 54 String name = request.getPathInfo().substring(1); 55 if (name.equals("")) { 56 log.error("No name supplied to the class loader servlet."); 57 response.sendError(500); 58 return; 59 } 60 int idx = name.lastIndexOf('/'); 61 String basename = name; 62 if(idx != -1) { 63 basename = name.substring(idx + 1); 64 } 65 if(!basename.startsWith("ApplicationResources") || ( !basename.endsWith(".properties") && !basename.endsWith(".class"))) { 66 log.debug("Attempt to load something other that a resource bundle via the class loader servlet."); 67 response.sendError(500); 68 return; 69 } 70 71 76 77 if(basename.endsWith(".class")) { 78 basename = basename.substring(0, basename.length() - 6) + ".properties"; 79 name = name.substring(0, name.length() - 6) + ".properties"; 80 } 81 82 86 ByteArrayOutputStream bout = new ByteArrayOutputStream (); 87 InputStream in = ContextHolder.getContext().getContextLoader().getResourceAsStream(name); 88 if (in == null) { 89 response.setContentType("text/plain"); 90 response.sendError(404, "Class not found"); 91 } else { 92 try { 93 Util.copy(in, bout); 94 } 95 finally { 96 bout.close(); 97 in.close(); 98 } 99 response.setContentType("text/plain"); 100 response.setContentLength(bout.size()); 101 response.setStatus(200); 102 ByteArrayInputStream bin = new ByteArrayInputStream (bout.toByteArray()); 103 sendFile(bin, bout.size(), response); 104 } 105 } 106 107 private void sendFile(InputStream in, long length, HttpServletResponse response) throws IOException { 108 response.setHeader("Content-Type", "text/plain"); 109 response.setHeader("Content-Length", String.valueOf(length)); 110 response.setContentLength((int) length); 111 Util.noCache(response); 112 try { 113 Util.copy(in, response.getOutputStream()); 114 response.getOutputStream().flush(); 115 } catch (IOException ex) { 116 } finally { 117 Util.closeStream(in); 118 Util.closeStream(response.getOutputStream()); 119 } 120 121 } 122 123 public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { 124 return SessionInfo.ALL_CONTEXTS; 125 } 126 127 }
| Popular Tags
|