public void init() throws ServletException {
    String appPath = getServletContext().getRealPath("/");

    String baseUrl = "";

    try {
      baseUrl = getServletContext().getContextPath();
    } catch (java.lang.NoSuchMethodError ex) {
      baseUrl = getServletContext().getServletContextName();
    }

    if (!appPath.endsWith("/")) appPath += "/";

    String configPath = appPath + "WEB-INF/";

    jeeves.init(appPath, configPath, baseUrl, this);
    initialized = true;
  }
 public void destroy() {
   jeeves.destroy();
   super.destroy();
 }
  private void execute(HttpServletRequest req, HttpServletResponse res) throws IOException {
    String ip = req.getRemoteAddr();
    // if we do have the optional x-forwarded-for request header then
    // use whatever is in it to record ip address of client
    String forwardedFor = req.getHeader("x-forwarded-for");
    if (forwardedFor != null) ip = forwardedFor;

    Log.info(Log.REQUEST, "==========================================================");
    Log.info(Log.REQUEST, "HTML Request (from " + ip + ") : " + req.getRequestURI());
    Log.debug(Log.REQUEST, "Method       : " + req.getMethod());
    Log.debug(Log.REQUEST, "Content type : " + req.getContentType());
    //		Log.debug(Log.REQUEST, "Context path : "+ req.getContextPath());
    //		Log.debug(Log.REQUEST, "Char encoding: "+ req.getCharacterEncoding());
    Log.debug(Log.REQUEST, "Accept       : " + req.getHeader("Accept"));
    //		Log.debug(Log.REQUEST, "Server name  : "+ req.getServerName());
    //		Log.debug(Log.REQUEST, "Server port  : "+ req.getServerPort());

    //		for (Enumeration e = req.getHeaderNames(); e.hasMoreElements();) {
    //			String theHeader = (String)e.nextElement();
    //			Log.debug(Log.REQUEST, "Got header: "+theHeader);
    //			Log.debug(Log.REQUEST, "With value: "+req.getHeader(theHeader));
    //		}
    HttpSession httpSession = req.getSession();
    Log.debug(Log.REQUEST, "Session id is " + httpSession.getId());
    UserSession session = (UserSession) httpSession.getAttribute("session");

    // ------------------------------------------------------------------------
    // --- create a new session if doesn't exist

    if (session == null) {
      // --- create session

      session = new UserSession();

      httpSession.setAttribute("session", session);
      Log.debug(Log.REQUEST, "Session created for client : " + ip);
    }

    // ------------------------------------------------------------------------
    // --- build service request

    ServiceRequest srvReq = null;

    // --- create request

    try {
      srvReq =
          ServiceRequestFactory.create(req, res, jeeves.getUploadDir(), jeeves.getMaxUploadSize());
    } catch (FileUploadTooBigEx e) {
      StringBuffer sb = new StringBuffer();
      sb.append("File upload too big - exceeds " + jeeves.getMaxUploadSize() + " Mb\n");
      sb.append("Error : " + e.getClass().getName() + "\n");
      res.sendError(400, sb.toString());

      // now stick the stack trace on the end and log the whole lot
      sb.append("Stack :\n");
      sb.append(Util.getStackTrace(e));
      Log.error(Log.REQUEST, sb.toString());
      return;
    } catch (Exception e) {
      StringBuffer sb = new StringBuffer();

      sb.append("Cannot build ServiceRequest\n");
      sb.append("Cause : " + e.getMessage() + "\n");
      sb.append("Error : " + e.getClass().getName() + "\n");
      res.sendError(400, sb.toString());

      // now stick the stack trace on the end and log the whole lot
      sb.append("Stack :\n");
      sb.append(Util.getStackTrace(e));
      Log.error(Log.REQUEST, sb.toString());
      return;
    }

    // --- execute request

    jeeves.dispatch(srvReq, session);
  }