📄 webappservlet.java
字号:
if (req instanceof Acme.Serve.Serve.ServeConnection) {
if (((Serve.ServeConnection)req).getSocket() instanceof SSLSocket) {
SSLSocket ssocket = (SSLSocket) ((Serve.ServeConnection)req).getSocket();
SSLSession ssess = ssocket.getSession();
String cipherSuite= ssess.getCipherSuite();
req.setAttribute("javax.servlet.request.cipher_suite", cipherSuite);
int cipherBits = 0;
// TODO cache in session
if (cipherSuite.indexOf("128") > 0)
cipherBits = 128;
else if (cipherSuite.indexOf("40") > 0)
cipherBits = 40;
else if (cipherSuite.indexOf("3DES") > 0)
cipherBits = 168;
else if (cipherSuite.indexOf("IDEA") > 0)
cipherBits = 128;
else if (cipherSuite.indexOf("DES") > 0)
cipherBits = 56;
req.setAttribute("javax.servlet.request.key_size", cipherBits);
try {
req.setAttribute("javax.servlet.request.X509Certificate", ssess.getPeerCertificateChain());
} catch (SSLPeerUnverifiedException e) {
}
}
}
}
protected SimpleFilterChain buildFilterChain(String servletName, String requestPath, DispatchFilterType filterType) {
SimpleFilterChain sfc = new SimpleFilterChain();
// add path filters
if (requestPath != null)
for (FilterAccessDescriptor fad : filters)
if (fad.matchDispatcher(filterType) && fad.matchPath(requestPath) >= 0)
sfc.add(fad);
// add name filters
if (servletName != null)
for (FilterAccessDescriptor fad : filters)
if (fad.matchDispatcher(DispatchFilterType.REQUEST) && fad.matchServlet(servletName) >= 0)
sfc.add(fad);
return sfc;
}
protected void returnFileContent(String path, HttpServletRequest req, HttpServletResponse res) throws IOException,
ServletException {
File fpath = new File(path);
if (fpath.isDirectory()) {
File baseDir = fpath;
for (String indexPage : welcomeFiles) {
fpath = new File(baseDir, indexPage);
if (fpath.exists() && fpath.isFile()) {
if (indexPage.charAt(0) != '/')
indexPage = "/" + indexPage;
RequestDispatcher rd = req.getRequestDispatcher(indexPage);
if (rd != null) {
rd.forward(req, res);
return;
}
break;
}
}
}
if (fpath.exists() == false) {
res.sendError(res.SC_NOT_FOUND);
return;
}
if (fpath.isFile() == false) {
res.sendError(res.SC_FORBIDDEN);
return;
}
res.setContentType(getServletContext().getMimeType(fpath.getName()));
res.setHeader("Content-Length", Long.toString(fpath.length()));
res.setContentType(getMimeType(fpath.getName()));
long lastMod = fpath.lastModified();
res.setDateHeader("Last-modified", lastMod);
String ifModSinceStr = req.getHeader("If-Modified-Since");
long ifModSince = -1;
if (ifModSinceStr != null) {
int semi = ifModSinceStr.indexOf(';');
if (semi != -1)
ifModSinceStr = ifModSinceStr.substring(0, semi);
try {
ifModSince = DateFormat.getDateInstance().parse(ifModSinceStr).getTime();
} catch (Exception ignore) {
}
}
if (ifModSince != -1 && ifModSince >= lastMod) {
res.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
return;
}
if ("HEAD".equals(req.getMethod()))
return;
OutputStream os = null;
InputStream is = null;
try {
is = new FileInputStream(fpath);
os = res.getOutputStream();
WarRoller.copyStream(is, os);
} catch (IllegalStateException ise) {
PrintWriter pw = res.getWriter();
// TODO decide on encoding/charset used by the reader
String charSetName = res.getCharacterEncoding();
if (charSetName == null)
charSetName = Utils.ISO_8859_1;
Utils.copyStream(new InputStreamReader(is, charSetName), pw);
// consider Writer is OK to do not close
// consider underneath stream closing OK
} finally {
try {
is.close();
} catch (Exception x) {
}
try {
if (os != null)
os.close();
} catch (Exception x) {
}
}
}
protected void addJSPServlet(List<String> patterns) {
ServletAccessDescr sad = createDescriptor();
// get class name from serve
sad.initParams = new HashMap<String, String>(10);
Map<String, String> arguments = (Map<String, String>) server.arguments;
sad.className = arguments.get(Serve.ARG_JSP);
if (sad.className == null) {
sad.className = "org.gjt.jsp.JSPServlet";
sad.initParams.put("repository", new File(deployDir, "~~~").getPath());
sad.initParams.put("debug", System.getProperty(getClass().getName() + ".debug") != null ? "yes" : "no");
sad.initParams.put("classloadername", WEBAPPCLASSLOADER);
} else {
String pnpx = sad.className + '.';
int cnl = pnpx.length();
String classPath = Utils.calculateClassPath(ucl);
for (String ipn : arguments.keySet())
if (ipn.startsWith(pnpx))
sad.initParams.put(ipn.substring(cnl), arguments.get(ipn).replace("%context%", contextName)
.replace("%deploydir%", deployDir.getPath()).replace("%classloader%", WEBAPPCLASSLOADER)
.replace("%classpath%", classPath));
}
sad.descr = "JSP support servlet";
sad.label = "JSP";
sad.loadOnStart = false;
sad.name = "jsp";
String jspPat;
if (patterns == null || patterns.size() == 0)
jspPat = "/.*\\.jsp";
else {
jspPat = buildREbyPathPatt(patterns.get(0));
for (int i = 1; i < patterns.size(); i++)
jspPat += "|" + buildREbyPathPatt(patterns.get(i));
}
sad.add(new MappingEntry("/", jspPat));
servlets.add(sad);
}
protected ServletAccessDescr createDescriptor() {
return new ServletAccessDescr();
}
protected FilterAccessDescriptor createFilterDescriptor() {
return new FilterAccessDescriptor();
}
protected void makeCP(File dd) throws IOException {
deployDir = dd.getCanonicalFile();
final List<URL> urls = new ArrayList<URL>();
File classesFile = new File(deployDir, "WEB-INF/classes");
if (classesFile.exists() && classesFile.isDirectory())
try {
urls.add(classesFile.toURL());
} catch (java.net.MalformedURLException mfe) {
}
File libFile = new File(deployDir, "WEB-INF/lib");
libFile.listFiles(new FileFilter() {
public boolean accept(File file) {
String name = file.getName().toLowerCase();
if (name.endsWith(".jar") || name.endsWith(".zip"))
try {
urls.add(file.toURL());
} catch (java.net.MalformedURLException mfe) {
}
return false;
}
});
cpUrls = urls.toArray(new URL[urls.size()]);
ucl = URLClassLoader.newInstance(cpUrls, getClass().getClassLoader());
setAttribute(WEBAPPCLASSLOADER, ucl);
// System.err.println("CP "+urls+"\nLoader:"+ucl);
}
protected HttpServlet newInstance(ServletAccessDescr descr) throws ServletException {
try {
// System.err.printf("new instance %s %s%n", descr.className, Arrays.toString(ucl.getURLs()));
descr.instance = (HttpServlet) ucl.loadClass(descr.className).newInstance();
descr.instance.init(descr);
// TODO think about setting back context loader
} catch (InstantiationException ie) {
throw new ServletException("Servlet class " + descr.className + " can't instantiate. ", ie);
} catch (IllegalAccessException iae) {
throw new ServletException("Servlet class " + descr.className + " can't access. ", iae);
} catch (ClassNotFoundException cnfe) {
log("", cnfe);
throw new ServletException("Servlet class " + descr.className + " not found. ", cnfe);
} catch (Error e) {
throw new ServletException("Servlet class " + descr.className
+ " can't be instantiated or initialized due an error.", e);
} catch (Throwable t) {
if (t instanceof ThreadDeath)
throw (ThreadDeath)t;
throw new ServletException("Servlet class " + descr.className
+ " can't be instantiated or initialized due an exception.", t);
}
return descr.instance;
}
protected Filter newFilterInstance(FilterAccessDescriptor descr) throws ServletException {
try {
descr.filterInstance = (Filter) ucl.loadClass(descr.className).newInstance();
descr.filterInstance.init(descr);
} catch (InstantiationException ie) {
throw new ServletException("Filter class " + descr.className + " can't instantiate. ", ie);
} catch (IllegalAccessException iae) {
throw new ServletException("Filter class " + descr.className + " can't access. ", iae);
} catch (ClassNotFoundException cnfe) {
throw new ServletException("Filter class " + descr.className + " not found. ", cnfe);
}
return descr.filterInstance;
}
/*
* protected URL toURL(File file) throws MalformedURLException { System.err.println("file:/"+file.getAbsolutePath()+(file.isDirectory()?"/":"")); return new
* URL("file:/"+file.getAbsolutePath()+(file.isDirectory()?"/":"")); }
*/
// /////////////////////////////////////////////////////////////////////////////////
// context methods
public String getServletContextName() {
return contextName;
}
public void removeAttribute(java.lang.String name) {
Object value = attributes.remove(name);
if (listeners != null)
for (EventListener listener : listeners)
if (listener instanceof ServletContextAttributeListener)
((ServletContextAttributeListener) listener).attributeRemoved(new ServletContextAttributeEvent(
this, name, value));
}
public void setAttribute(java.lang.String name, java.lang.Object object) {
// log("Set attr:"+name+" to "+object);
if (object == null) {
removeAttribute(name);
return;
}
Object oldObj = attributes.put(name, object);
if (listeners != null)
for (EventListener listener : listeners) {
if (listener instanceof ServletContextAttributeListener)
if (oldObj == null)
((ServletContextAttributeListener) listener).attributeAdded(new ServletContextAttributeEvent(
this, name, object));
else
((ServletContextAttributeListener) listener)
.attributeReplaced(new ServletContextAttributeEvent(this, name, object));
}
}
public java.util.Enumeration getAttributeNames() {
return attributes.keys();
}
public java.lang.Object getAttribute(java.lang.String name) {
// log("context: "+this+" return attr:"+name+" as "+attributes.get(name));
return attributes.get(name);
}
public java.lang.String getServerInfo() {
return "TJWS/J2EE container, Copyright © 1998-2007 Dmitriy Rogatkin";
}
public java.lang.String getRealPath(java.lang.String path) {
path = validatePath(path);
if (path == null)
return deployDir.toString();
else
return new File(deployDir, path).toString();
}
public void log(java.lang.String msg) {
server.log((contextName == null ? "" : contextName) + "> " + msg);
}
public void log(java.lang.Exception exception, java.lang.String msg) {
server.log(exception, (contextName == null ? "" : contextName) + "> " + msg);
}
public void log(java.lang.String message, java.lang.Throwable throwable) {
server.log((contextName == null ? "" : contextName) + "> " + message, throwable);
}
public java.util.Enumeration getServletNames() {
Vector<String> result = new Vector<String>();
for (ServletAccessDescr sad : servlets)
result.add(sad.name);
return result.elements();
}
public java.util.Enumeration getServlets() {
Vector<HttpServlet> result = new Vector<HttpServlet>();
for (ServletAccessDescr sad : servlets)
result.add(sad.instance);
return result.elements();
}
public Servlet getServlet(java.lang.String name) throws ServletException {
for (ServletAccessDescr sad : servlets)
if (name.equals(sad.name))
return sad.instance;
throw new ServletException("No servlet " + name);
}
public RequestDispatcher getNamedDispatcher(java.lang.String name) {
for (ServletAccessDescr sad : servlets)
if (name.equals(sad.name)) {
if (sad.instance == null && sad.loadOnStart == false)
try {
newInstance(sad);
} catch (ServletException se) {
}
if (sad.instance != null)
return new SimpleDispatcher(name, sad.instance);
else
break;
}
return null;
}
public RequestDispatcher getRequestDispatcher(java.lang.String path) {
if (_DEBUG)
System.err.printf("getRequestDispatcher(%s)%n", path);
if (path == null || path.length() == 0 || path.charAt(0) != '/')
return null; // path must start with / for call from context
// look for servlets first
int sp = path.indexOf('?');
if (sp < 0)
sp = path.indexOf('#'); // exclude possible fragment
// if (sp < 0)
// sp = path.indexOf(Serve.ServeConnection.SESSION_URL_NAME);
String clearPath = sp < 0 ? path : path.substring(0, sp);
for (ServletAccessDescr sad : servlets) {
if (_DEBUG)
System.err.printf("For dispatcher trying match %s (%s) %s = %b%n", path, clearPath, Arrays
.toString(sad.mapping), sad.matchPath(clearPath));
int patIndex;
if ((patIndex = sad.matchPath(clearPath)) >= 0) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -