View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
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  }