String remoteAddr = req.getRemoteAddr();
log.debug("Got request from "+req.getRemoteAddr());
    public void doGet(HttpServletRequest req,  HttpServletResponse res) throws java.io.IOException, ServletException {        log.debug(">doGet()");        // Check if authorized        EjbcaWebBean ejbcawebbean= (se.anatom.ejbca.webdist.webconfiguration.EjbcaWebBean)                                   req.getSession().getAttribute("ejbcawebbean");        if ( ejbcawebbean == null ){          try {            ejbcawebbean = (se.anatom.ejbca.webdist.webconfiguration.EjbcaWebBean) java.beans.Beans.instantiate(this.getClass().getClassLoader(), "se.anatom.ejbca.webdist.webconfiguration.EjbcaWebBean");           } catch (ClassNotFoundException exc) {               throw new ServletException(exc.getMessage());           }catch (Exception exc) {               throw new ServletException (" Cannot create bean of class "+"se.anatom.ejbca.webdist.webconfiguration.EjbcaWebBean", exc);           }           req.getSession().setAttribute("ejbcawebbean", ejbcawebbean);        }        try{          ejbcawebbean.initialize(req,"/ca_functionality/basic_functions");        } catch(Exception e){           throw new java.io.IOException("Authorization Denied");        }                String issuerdn = req.getParameter(ISSUER_PROPERTY);                        String command;        // Keep this for logging.        String remoteAddr = req.getRemoteAddr();        command = req.getParameter(COMMAND_PROPERTY_NAME);        if (command == null)            command = "";        if ((command.equalsIgnoreCase(COMMAND_NSCACERT) || command.equalsIgnoreCase(COMMAND_IECACERT) || command.equalsIgnoreCase(COMMAND_CACERT)) && issuerdn != null ) {            String lev = req.getParameter(LEVEL_PROPERTY);            int level = 0;            if (lev != null)                level = Integer.parseInt(lev);            // Root CA is level 0, next below root level 1 etc etc            try {                ISignSessionLocal ss = signhome.create();                Admin admin = new Admin(((X509Certificate[]) req.getAttribute( "javax.servlet.request.X509Certificate" ))[0]);                Certificate[] chain = (Certificate[]) ss.getCertificateChain(admin, issuerdn.hashCode()).toArray(new Certificate[0]);                                                                            // chain.length-1 is last cert in chain (root CA)                if ( (chain.length-1-level) < 0 ) {                    PrintStream ps = new PrintStream(res.getOutputStream());                    ps.println("No CA certificate of level "+level+"exist.");                    log.error("No CA certificate of level "+level+"exist.");                    return;                }                X509Certificate cacert = (X509Certificate)chain[level];                byte[] enccert = cacert.getEncoded();                if (res.containsHeader("Pragma")) {                    log.debug("Removing Pragma header to avoid caching issues in IE");                    res.setHeader("Pragma",null);                }                if (res.containsHeader("Cache-Control")) {                    log.debug("Removing Cache-Control header to avoid caching issues in IE");                    res.setHeader("Cache-Control",null);                }                if (command.equalsIgnoreCase(COMMAND_NSCACERT)) {                    res.setContentType("application/x-x509-ca-cert");                    res.setContentLength(enccert.length);                    res.getOutputStream().write(enccert);                    log.debug("Sent CA cert to NS client, len="+enccert.length+".");                } else if (command.equalsIgnoreCase(COMMAND_IECACERT)) {                    res.setHeader("Content-disposition", "attachment; filename=ca.crt");                    res.setContentType("application/octet-stream");                    res.setContentLength(enccert.length);                    res.getOutputStream().write(enccert);                    log.debug("Sent CA cert to IE client, len="+enccert.length+".");                } else if (command.equalsIgnoreCase(COMMAND_CACERT)) {                    byte[] b64cert = Base64.encode(enccert);                    String out = RequestHelper.BEGIN_CERTIFICATE_WITH_NL;                                       out += new String(b64cert);                    out += RequestHelper.END_CERTIFICATE_WITH_NL;                    res.setHeader("Content-disposition", "attachment; filename=ca.pem");                    res.setContentType("application/octet-stream");                    res.setContentLength(out.length());                    res.getOutputStream().write(out.getBytes());                    log.debug("Sent CA cert to client, len="+out.length()+".");                } else {                    res.setContentType("text/plain");                    res.getOutputStream().println("Commands="+COMMAND_NSCACERT+" || "+COMMAND_IECACERT+" || "+COMMAND_CACERT);                    return;                }            } catch (Exception e) {                PrintStream ps = new PrintStream(res.getOutputStream());                e.printStackTrace(ps);                res.sendError(HttpServletResponse.SC_NOT_FOUND, "Error getting CA certificates.");                log.error("Error getting CA certificates.");                log.error(e);                return;            }        }        else {            res.setContentType("text/plain");            res.sendError(HttpServletResponse.SC_BAD_REQUEST, "Commands=lastcert | listcerts | crl | revoked");            return;        }    } // doGet