1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.waarp.http.protocol.servlet;
22
23 import org.waarp.common.logging.WaarpLogger;
24 import org.waarp.common.logging.WaarpLoggerFactory;
25 import org.waarp.http.protocol.WaarpStartup;
26 import org.waarp.openr66.protocol.utils.ChannelUtils;
27
28 import javax.servlet.ServletConfig;
29 import javax.servlet.ServletException;
30 import javax.servlet.http.HttpServlet;
31 import java.io.File;
32 import java.net.URL;
33
34 public class AbstractServlet extends HttpServlet {
35 private static final long serialVersionUID = 2001L;
36 public static final String R_66_CONFIG = "r66config";
37 public static final String AUTHENT_CLASSNAME = "authentClassName";
38 public static final String RULENAME = "rulename";
39 public static final String COMMENT = "comment";
40 protected static final String INVALID_REQUEST_PARAMS =
41 "Invalid request params.";
42 private static final WaarpLogger logger =
43 WaarpLoggerFactory.getLogger(AbstractServlet.class);
44 protected Class<HttpAuthent> authentClass;
45
46 @Override
47 public final void destroy() {
48 ChannelUtils.exit();
49 super.destroy();
50 }
51
52 @Override
53 public final void init(final ServletConfig config) throws ServletException {
54 final String r66Config = config.getInitParameter(R_66_CONFIG);
55 logger.warn("Parameter Init: {}", r66Config);
56 File file = new File(r66Config);
57 logger.debug("Parameter Init: {} {}?", file.getAbsolutePath(),
58 file.exists());
59 if (!file.exists()) {
60 final ClassLoader classLoader = AbstractServlet.class.getClassLoader();
61 final URL url = classLoader.getResource(r66Config);
62 if (url != null) {
63 file = new File(url.getFile());
64 }
65 logger.debug("Parameter Init: {} {}?", file.getAbsolutePath(),
66 file.exists());
67 }
68 final String sauthent = config.getInitParameter(AUTHENT_CLASSNAME);
69 logger.warn("Parameter Init: {}", sauthent);
70 try {
71 authentClass = (Class<HttpAuthent>) Class.forName(sauthent);
72 } catch (final ClassNotFoundException e) {
73 logger.error("Cannot find authent class {}", sauthent);
74 throw new ServletException("Cannot find authent class");
75 } catch (final ClassCastException e) {
76 logger.error(
77 "Cannot cast authent class {} to mandatory inherited HttpAuthent",
78 sauthent);
79 throw new ServletException(
80 "Cannot cast authent class to mandatory inherited HttpAuthent");
81 }
82 WaarpStartup.startupWaarp(file);
83 logger.info("{}: {} {}", config.getServletName(),
84 config.getServletContext().getContextPath(),
85 config.getServletContext().getServletContextName());
86 }
87 }